#include #include #include #include #include #include #include #define REP(i,x) for(int i=0 ; i<(int)(x) ; i++) #define LL long long #define ALL(x) (x).begin(),(x).end() using namespace std; struct Stamp{ int h,w; Stamp(){} }; int N; vector stamp; vector field; int memo[1<<16]; int ctoi(char c){ if(c=='R')return 0; if(c=='G')return 1; if(c=='B')return 2; return 3; } bool isValid(int x,int y){ return x>=0 && y>=0 && x<4 && y<4; } int rec(int bit){ char tmp[4][4]; if(bit==((1<<16)-1))return 0; if(memo[bit]!=-1)return memo[bit]; //cout << bit << endl; REP(y,4){ REP(x,4){ if((bit>>(y*4+x))&1)tmp[y][x] = 'W'; else tmp[y][x] = field[y][x]; //cout << tmp[y][x]; } //cout << endl; } //cout << endl; int res = 1<<30; REP(i,N){ int h = stamp[i].h; int w = stamp[i].w; for(int y=1-h ; y<4 ; y++){ for(int x=1-w ; x<4 ; x++){ int check[4] = {0}; int nbit = bit; for(int cy=y ; cy>N){ stamp.clear(); field.clear(); memset(memo,-1,sizeof(memo)); REP(i,N){ Stamp s; cin>>s.h>>s.w; stamp.push_back(s); } REP(y,4){ string s; cin>>s; field.push_back(s); } int bit = 0; cout << rec(bit) << endl; } return 0; }