#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <sstream>
#include <fstream>
#include <set>
#include <iostream>

using namespace std;

struct pt {
    int x, y;
    pt(int a, int b) : x(a), y(b) { }
    pt() { }
    bool operator==(const pt &b) const {
        return x == b.x && y == b.y;
    }
    bool operator!=(const pt &b) const {
        return ! (*this == b);
    }
};

enum {
    UP, LEFT, DOWN, RIGHT
};

int maze[10][10];
pt poi[6];

pt move(pt pos, int dir, int &dist) {
    int x = 0, y = 0;
    switch(dir) {
        case UP: y = -1; break;
        case LEFT: x = -1; break;
        case DOWN: y = 1; break;
        case RIGHT: x = 1; break;
    }
    dist = 0;
    while (pos.y >= 0 && pos.y < 10 && pos.x >= 0 && pos.x < 10 && maze[pos.y][pos.x] != '#') {
        pos.y += y;
        pos.x += x;
        dist++;
    }
    pos.y -= y;
    pos.x -= x;
    dist--;
    return pos;
}

class node {
    public:
    pt pos;
    int cost;
    bool operator<(const node& b) const {
        return cost < b.cost;
    }
    node(pt p, int c) : pos(p), cost(c) { }
};

int main() {
    ifstream in("DATA5.txt");
    ofstream out("OUT5.txt");
    
    for (int i = 0; i < 10; i++) {
        string s;
        in >> s;
        for (int j = 0; j < 10; j++) {
            maze[i][j] = s[j];
            if (s[j] >= 'A' && s[j] <= 'F') {
                poi[s[j] - 'A'] = pt(j, i);
            }
        }
    }
    for (int i = 0; i < 5; i++) {
        multiset<node> paths;
        paths.insert(node(poi[i], 0));
        while (paths.size()) {
            multiset<node>::iterator p = paths.begin();
            node f = *p;
            if (f.pos == poi[i+1]) {
                out << f.cost << endl;
                cout << endl;
                break;
            }
            paths.erase(p);
            int cdown, cup, cleft, cright;
            pt pdown = move(f.pos, DOWN, cdown);
            pt pup = move(f.pos, UP, cup);
            pt pleft = move(f.pos, LEFT, cleft);
            pt pright = move(f.pos, RIGHT, cright);
            if (cdown) paths.insert(node(pdown, cdown + f.cost));
            if (cup) {
                paths.insert(node(pup, cup + f.cost));
            }
            if (cleft) {
                paths.insert(node(pleft, cleft + f.cost));
            }
            if (cright) {
                paths.insert(node(pright, cright + f.cost));
            }
        }
    }

    return 0;
}

