#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>

using namespace std;

int n;
int H;
int broj[41][41];

void input() { 
   memset( broj, -1, sizeof broj );
   
   n = 4;

   for( int y = -H; y <= H; ++y ) {

      int x1 = (y <= 0) ? -H : y-H;
      int x2 = (y <= 0) ? y+H : H;

      for( int x = x1; x <= x2; ++x ) {
         char znak;
         scanf( " %c", &znak );
         
         int br;
         if( znak == 'A' ) br = 0;
         else if( znak == 'B' ) br = 1;
         else if( znak == 'C' ) br = 2;
         else if( znak == 'D' ) br = 3;
         else br = n++;
         
         broj[y+H][x+H] = br;
      }
   }
}

char g[1200][1200];
vector<int> adj[1200];

int dist[1200][1200];

int dx[6] = { 0, 1, 1, 0, -1, -1 };
int dy[6] = { -1, 0, 1, 1, 0, -1 };

void make_graph() {
   memset( g, 0, sizeof g );

   for( int y = -H; y <= H; ++y ) {
      for( int x = -H; x <= H; ++x ) {
         int a = broj[y+H][x+H];
         if( a == -1 ) continue;         

         for( int d = 0; d < 6; ++d ) {
            int yy = y + dy[d];
            int xx = x + dx[d];

            if( yy < -H || yy > H ) continue;
            if( xx < -H || xx > H ) continue;

            int b = broj[yy+H][xx+H];

            if( b == -1 ) continue;
            
            if( g[a][b] ) continue;
            g[a][b] = 1;
            adj[a].push_back( b );
         }
         
      }
   }
}

void bfs( int start ) {
   for( int i = 0; i < n; ++i )
      dist[start][i] = 1000000;
   
   dist[start][start] = 0;

   queue<int> Q;
   for( Q.push( start ); !Q.empty(); Q.pop() ) {
      int u = Q.front();
      
      for( vector<int>::iterator it = adj[u].begin(); it != adj[u].end(); ++it ) {
         if( dist[start][u] + 1 >= dist[start][*it] ) continue;
         dist[start][*it] = 1 + dist[start][u];
         Q.push( *it );
      }
   }

   for( int i = 0; i < n; ++i )
      if( i != start )
         dist[start][i] -= 1;   
}

int solve( int a, int b, int c, int d, int e, int f ) {
   int dodaje = (e >= 4);
   int dodajf = (f >= 4) && (f != e);

   return dist[a][e] + dist[b][e] + dist[c][f] + dist[d][f] + dist[e][f] + dodaje + dodajf;
}

int solve( int e, int f ) {
   int ret = 1000000;

   ret <?= solve( 0, 1, 2, 3, e, f );
   ret <?= solve( 0, 2, 1, 3, e, f );
   ret <?= solve( 0, 3, 1, 2, e, f );
   ret <?= solve( 1, 2, 0, 3, e, f );
   ret <?= solve( 1, 3, 0, 2, e, f );
   ret <?= solve( 2, 3, 0, 1, e, f );

   return ret;
}

int main( void ) {
   while( scanf( "%d", &H ) == 1 ) {
      if( H == 0 ) break;

      --H;

      input();

      make_graph();      

      for( int i = 0; i < n; ++i ) 
         bfs( i );
    
      int ret = 1000000;
      for( int e = 0; e < n; ++e )
         for( int f = 0; f < n; ++f )
            ret <?= solve( e, f );
      
      printf( "You have to buy %d parcels.\n", ret );

      for( int i = 0; i < n; ++i ) adj[i].clear();
   }
   return 0;
}
