#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
#include <string>
#include <strings.h>
#include <vector>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <stdio.h>

#define fori(I) for (int i = 0; i < I; i++)
#define forj(J) for (int j = 0; j < J; j++)
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)

#define inb(a,b) (a >= 0 && a < nR && b >= 0 && b < nC)
#define isl(a) (a >= 'A' && a <= 'L')

using namespace std;

string cMaze[10];
int iMaze[10][19];
int nR = 10, nC = 19;

int minN(int r, int c) {
    int mn = 9999;
    int pairs[4][2] = {
        {r-1,c},
        {r+1,c},
        {r,c-1},
        {r,c+1}
    };
    fori(4) {
        int newr = pairs[i][0];
        int newc = pairs[i][1];
        if (inb(newr,newc) && iMaze[newr][newc]) {
            if (iMaze[newr][newc] < mn)
                mn = iMaze[newr][newc];
        }
    }
    if (mn == 9999) return 0;
    else return mn;
}

int getDist(char s, char e) {
    if (s == e) return 1;
    int eC, eR;
    for (int r = 0; r < nR; r++) {
        for (int c = 0; c < nC; c++) {
            iMaze[r][c] = 0;
            if (cMaze[r][c] == s) iMaze[r][c] = 1;
            else if (cMaze[r][c] == e) {
                eR = r;
                eC = c;
            }
        }
    }
    
    while(1) {
        bool done = false;
        bool changed = false;
        int ans = 0;
        int r;
        for (r = 0; r < nR; r++) {
            int c;
            for (c = 0; c < nC; c++) {
                //cout << (char)(iMaze[r][c]+'A');
                if (iMaze[r][c]) continue;
                if (cMaze[r][c] == '#') continue;
                
                int mn = minN(r,c);
                if (mn) {
                    changed = true;
                    if (r == eR && c == eC) {
/*
                       for (int r = 0; r < nR; r++) {
                            for (int c = 0; c < nC; c++) {
                                if (!iMaze[r][c]) cout << '.';
                                else cout << (char)(iMaze[r][c]+'A');
                            }
                            cout << endl;
                        }
*/
                        return mn + 1;
                    }
                    iMaze[r][c] = mn + 1;
                }
            }
            //cout << endl;
            //cout << r << "," << c << " from "<< s << " to "<<e<<endl;
        }
        if (!changed) break;
        /*
        for (int r = 0; r < nR; r++) {
            for (int c = 0; c < nC; c++) {
                cout << cMaze[r][c];
            }
            cout << endl;
        }
        getchar();*/
    }
    return -1;
}

int main() {



    ifstream in ("DATA3.txt");  //DID YOU FIX THIS?
    ofstream out ("OUT3.txt");  //DID YOU FIX THIS?
    
    fori(nR) {
        getline(in,cMaze[i]);
    }
/*
    string inp = "ABCDEFGHIJKL";
    /*for (int i = 0; i < inp.length()-1; i++) {
        cout << inp[i] << " to " << inp[i+1] << ": " << getDist(inp[i],inp[i+1])-1 << endl;
    }
    for (char s = 'A'; s <= 'L'; s++) {
        for (char e = 'A'; e <= 'L'; e++) {
            cout << s << " to " << e << ": " << getDist(s,e)-1 << endl;
        }
    }
*/

    for (int Z = 0; Z < 5; Z++) {
        string inp;
        getline(in,inp);
        int totdist = 0;
        for (int i = 0; i < inp.length()-1; i++) {
            if (!(isl(inp[i])) || !(isl(inp[i+1]))) continue;
            totdist += getDist(inp[i],inp[i+1])-1;
        }
        out << totdist << endl;
    }


    system("pause");            //REMOVE THIS BEFORE SUBMITTING
    return 0;
}
