/*
ID: divad151
PROG: pname
LANG: C++
*/
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <queue>
#include <cassert>

using namespace std;

//------------------------------------------------------------------------------------------------------------------------
// CONSTANTS
//------------------------------------------------------------------------------------------------------------------------

#define PI          3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803   /* all from memory! */
#define PHI         1.6180339887498948482045868343656   /* = (sqrt(5)+1)/2  */
#define MAXINT      0x7fffffff                          /* for a signed integer */
#define EPSILON     0.00000001                          /* for floating-point value comparison */
#define MAXDOUBLE   1.79769313486231570e+308

#define NORTH       0
#define EAST        1
#define SOUTH       2
#define WEST        3

#define MS(a,b)     memset(a, b, sizeof(a))
#define FOR(a,b,c)  for (a = (b); a < (c); ++a)

const int fact[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600 }; // up to 12!
const int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  // lengths of each month


//------------------------------------------------------------------------------------------------------------------------
// GLOBAL VARIABLES
//------------------------------------------------------------------------------------------------------------------------

int dc[] = {  0,  1,  0, -1 };
int dr[] = { -1,  0,  1,  0 };
int cmax = -MAXINT;
int cmin = MAXINT;


//------------------------------------------------------------------------------------------------------------------------
// FUNCTIONS/CLASS DEFINITIONS
//------------------------------------------------------------------------------------------------------------------------





string grid[10];
/*
struct node
{
    int label, dist;
    node(l,d)
    {
        label=l;
        dist=d;
    }
    node(){}
};
*/
int matrix[300][300];

//------------------------------------------------------------------------------------------------------------------------
// MAIN PROGRAM EXECUTION
//------------------------------------------------------------------------------------------------------------------------
int main()
{
    ifstream fin("data4.txt");
    ofstream fout("out4.txt");
    int i = 0, j = 0, k = 0, a, b;
    int num, n1, n2, n3;
    string str;
    int counter = 47;

//    vector<node>adjList[300];
    int r[300];
    int c[300];


    for (i=0;i<5; ++i)
    {
        cmin = MAXINT;
        counter = 47;
        for(j=0;j<10;++j)
        {
            fin >> grid[j];
            for (k=0;k<10;++k)
            {
                if(grid[j][k] == '#')
                {
                    grid[j][k] = counter;
                    r[grid[i][k]] = i;
                    c[grid[i][k]] = j;
                    counter++;
                }
            }
        }
        if (counter == 47 || counter == 48)
        {
            fout << 0 <<endl; continue;
        }

        for (j=0;j<10;++j)
        {
            for (k=0;k<10;++k)
            {
                if (grid[j][k] == '.') continue;
                for(a=0;a<10;++a)
                {
                    for(b=0;b<10;++b)
                    {
                        if (a== j && b == k) continue;
                        if (grid[a][b] != '.')
                        {
                            //adjList[grid[a][b]].push_back(node(grid[a][b],abs(a-j)+abs(b-k)));
                            matrix[grid[j][k]][grid[a][b]] = abs(a-j)+abs(b-k);
                            //cout << (int)grid[j][k] << " " << (int)grid[a][b] << " " << a << " " << j << " " << b << " " << k; cin.get();
                        }
                    }
                }
            }
        }
        if (counter-47<=9)
        {
        vector<int>perm;
        for(j=47;j<counter;++j)
        {
            perm.push_back(j);
        }
        vector<int>::iterator it;
        int sum;
        do
        {

            sum =0;
            for (it= perm.begin(), it++;it!=perm.end();++it)
            {
                sum += matrix[*(it-1)][*it];
                //cout << "dist:"<< matrix[*(it-1)][*it]; cin.get();
            }

            sum += matrix[*(it-1)][*(perm.begin())];
            //cout << "got";
            cmin = min(sum, cmin);
        }while(next_permutation(perm.begin(),perm.end()));
        fout << cmin<<endl; //cin.get();
        }
        else
        {
        int sum = 0;
        vector<pair<int,int> > edges;
        vector<pair<int,int> >::iterator del;
        edges.push_back(pair<int,int>(47,48));
        edges.push_back(pair<int,int>(47,48));
        sum += 2*matrix[47][48];
        for (j=49;j<counter;++j)
        {
            int best = 99999999;
            int one, two;
            for (vector<pair<int,int> >::iterator pit = edges.begin(); pit != edges.end(); ++pit)
            {
                if ( best > sum - matrix[pit->first][pit->second]+matrix[j][pit->first]+matrix[j][pit->second])
                {
                    best = sum - matrix[pit->first][pit->second]+matrix[j][pit->first]+matrix[j][pit->second];
                    one = pit->first;
                    two = pit->second;
                    del = pit;
                    //cout << sum << " - " <<matrix[pit->first][pit->second]<<"+"<<matrix[j][pit->first]<<"+"<<matrix[j][pit->second] << endl;
                }
            }
            //cout << best << " ";
            edges.push_back(pair<int,int>(j,two));
            edges.push_back(pair<int,int>(j,one));
            edges.erase(del);
            sum = best;
        }
        fout << sum << endl;
        }
    }












    return 0;
}




