Problem: Training is indispensable for achieving good results at ICPC. Rabbit wants to win at ICPC, so he decided to practice today as well.

Today's training is to increase creativity by drawing pictures. Let's draw a pattern well using a square stamp.

I want to use stamps of various sizes to complete the picture of the red, green, and blue streets specified on the 4 x 4 squared paper. The stamp is rectangular and is used to fit the squares. The height and width of the stamp cannot be swapped.

The paper is initially uncolored. When you stamp on paper, the stamped part changes to the color of the stamp, and the color hidden underneath becomes completely invisible. Since the color of the stamp is determined by the ink to be applied, it is possible to choose the color of any stamp. The stamp can be stamped with a part protruding from the paper, and the protruding part is ignored.

It is possible to use one stamp multiple times. You may use the same stamp for different colors. Stamping is a rather nerve-wracking task, so I want to reduce the number of stamps as much as possible.



Input


N
H1 W1
...
HN WN
C1,1C1,2C1,3C1,4
C2,1C2,2C2,3C2,4
C3,1C3,2C3,3C3,4
C4,1C4,2C4,3C4,4


N is the number of stamps, and Hi and Wi (1 ≤ i ≤ N) are integers representing the vertical and horizontal lengths of the i-th stamp, respectively. Ci, j (1 ≤ i ≤ 4, 1 ≤ j ≤ 4) is a character that represents the color of the picture specified for the cells in the i-th row from the top and the j-th column from the left. Red is represented by `R`, green is represented by` G`, and blue is represented by `B`.

Satisfy 1 ≤ N ≤ 16, 1 ≤ Hi ≤ 4, 1 ≤ Wi ≤ 4. The same set as (Hi, Wi) does not appear multiple times.

Output

Print the minimum number of stamps that must be stamped to complete the picture on a single line.

Examples

Input

2
4 4
1 1
RRRR
RRGR
RBRR
RRRR


Output

3


Input

1
2 3
RRGG
BRGG
BRRR
BRRR


Output

5

Solution (in CPP):

Solution:
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cassert>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <fstream>
#include <cmath>
using namespace std;

#define REP2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for(ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n) & 1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

int dist[1 << 16];
int N;
int W[16], H[16];
char goal[4][4];


int main(){

  cin >> N;
  REP(i, N) cin >> H[i] >> W[i];
  REP(i, 4)REP(j, 4){
    cin >> goal[i][j];
    if(goal[i][j] == 'R') goal[i][j] = 0;
    if(goal[i][j] == 'G') goal[i][j] = 1;
    if(goal[i][j] == 'B') goal[i][j] = 2;
  }

  
  priority_queue<P, vector<P>, greater<P> > que;
  que.push(P(0, 0));
  fill(dist, dist + (1 << 16), INF);
  dist[0] = 0;
  
  while(!que.empty()){
    int cost = que.top().first;
    int mask = que.top().second;
    que.pop();
    if(cost > dist[mask]) continue;

    if(mask == 0xFFFF){
      cout << cost << endl;
      return 0;
    }
    
    
    REP(i, N){
      for(int h1 = -H[i] + 1; h1 < 4; h1++){
        for(int w1 = -W[i] + 1; w1 < 4; w1++){
          int h2 = h1 + H[i];
          int w2 = w1 + W[i];
          int nmaskc[3] = {0,0,0};
          
          int nmaskn = 0;
          
          REP(r, 4)REP(c, 4){
            if(h1 <= r && r < h2 && w1 <= c && c < w2){
              nmaskc[goal[r][c]] |= 1 << (r * 4 + c);
            }else{
              nmaskn |= mask & (1 << (r * 4 + c));
            }
          }

          REP(color, 3){
            int nmask = nmaskc[color] | nmaskn;
            if(dist[nmask] > cost + 1){
              dist[nmask] = cost + 1;
              que.push(P(cost + 1, nmask));
            }
          }
        }
      }
    }
  }
  assert(false);
  return 0;
}