#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

/*typedef struct {
	
}graph;
*/

typedef struct {
	int x, y;
}coor;

int edges[100][100];

int main() {

	ifstream fin("data5.txt");
	ofstream fout("out5.txt");
	
	char map[10][10];
	int places[6];
	
	for (int i=0; i<10; i++)
		for (int j=0; j<10; j++) {
			fin >> map[i][j];
			//fout << map[i][j];
			for (int k=0; k<6; k++) {
				if (map[i][j] == k + 'A') places[k] = i*10+j;
			}
		}
	
	for (int i=0; i<100; i++)
		for (int j=0; j<100; j++) edges[i][j] = 999999;
			
	
	for (int i=0; i<10; i++) {
		for (int j=0; j<10; j++) {
            if (map[i][j] == '#') continue;
			//find out where you can go
			int x = j, y = i;
			if ( i > 0 ) {		//upwards
				while ( --y >=0 && map[y][j]!= '#' );
				y++;
				edges[i*10+j][y*10+j] = i - y;
			}
			x = j; y = i;
			if ( i < 9 ) {		//downwards
				while ( ++y <10 && map[y][j]!= '#' );
				y--;
				edges[i*10+j][y*10+j] = y - i;
			}
			x = j; y = i;
			if ( j > 0 ) {		//leftwards
				while ( --x >=0 && map[i][x]!= '#' );
				x++;
				edges[i*10+j][i*10+x] = j - x;
			}
			x = j; y = i;
			if ( j < 9 ) {		//rightwards
				while ( ++x <10 && map[i][x]!= '#' );
				x--;
				edges[i*10+j][i*10+x] = x - j;
			}
		}
	}
	for (int k=0; k<100; k++)
		for (int i=0; i<100; i++)
			for (int j=0; j<100; j++)
                edges[i][j] = min(edges[i][j], edges[i][k] + edges[k][j]);
    
    for (int i=0; i<5; i++)
        fout << edges[places[i]][places[i+1]] << endl;
        
	//ystem("pause");
	
	return 0;
}

