/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mypkg;

import java.io.*;

/**
 *
 * @author 073795502
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    static int[][] field = new int[10][10];
    static int[][] dest = new int[6][2];
    static final int max = (int) (Math.pow(2, 31) - 1);

    static final int[][] coord={{0,1,0,-1},{1,0,-1,0}};

    public static void main(String[] args) throws IOException {
        // TODO code application logic here

        BufferedReader fi = new BufferedReader(new FileReader("DATA5.txt"));
        PrintWriter fo = new PrintWriter(new FileWriter("OUT5.txt"));

        String temp;

        for (int i = 0; i < 10; i++) {

            temp = fi.readLine();

            for (int j = 0; j < 10; j++) {
                if (temp.charAt(j) == '#') {
                    field[i][j] = -1;
                }
                else {
                    if (temp.charAt(j) - 'A' >= 0 && temp.charAt(j) - 'A' <= 5) {
                        dest[temp.charAt(j) - 'A'][0] = j;
                        dest[temp.charAt(j) - 'A'][1] = i;
                    }

                    field[i][j]=max;
                }
            }
        }

        Rec(dest[0][0],dest[0][1],0,dest[1][0],dest[1][1]);

        fo.println(field[dest[1][1]][dest[1][0]]);



        for(int i=1; i<5; i++) {
            Initialize();
            Rec(dest[i][0],dest[i][1],0,dest[i+1][0],dest[i+1][1]);
            fo.print(field[dest[i+1][1]][dest[i+1][0]]);

            if(i!=4)
                fo.println();
        }

        fi.close();
        fo.close();
    }


    public static void Initialize()
    {
        for(int i=0; i<10; i++){
            for(int j=0; j<10; j++){
                if(field[i][j]!=-1)
                    field[i][j]=max;
            }
        }
    }


    public static void Rec(int cx, int cy, int dist, int tx, int ty) {
        if (field[cy][cx] > dist) {
            field[cy][cx] = dist;

            if (!(cx == tx && cy == ty)) {
                int mx, my, counter;

                for (int i = 0; i < 4; i++) {
                    mx = cx;
                    my = cy;
                    counter = 0;

                    while (0 <= mx + coord[0][i] && mx + coord[0][i] < 10 && 0 <= my + coord[1][i] && my + coord[1][i] < 10 && field[my + coord[1][i]][mx + coord[0][i]] != -1) {
                        mx += coord[0][i];
                        my += coord[1][i];
                        counter++;
                    }

                    if (counter > 0) {
                        Rec(mx, my, dist + counter, tx, ty);
                    }
                }
            }
        }
    }
}

