#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>

using namespace std;

int dists[20][20];
int iTable[10][10];
int pos[20][2];
int n;

int absin(int in) {
    return (in > 0)?in:-in;
}

int dist(int n1, int n2) {
    return (absin(pos[n1][0]-pos[n2][0]) + absin(pos[n1][1]-pos[n2][1]));
}

int main() {
    ifstream DATA ("DATA4.txt");
    ofstream OUT ("OUT4.txt");

    for (int Z = 0; Z < 5; Z++)
    {
        n = 0;
        for (int y = 0; y < 10; y++) {
            string line;
            getline(DATA,line);
            for (int x = 0; x < 10; x++) {
                if (line[x] == '#') {
                    pos[n][0] = x;
                    pos[n][1] = y;
                    //cout << "Found at " << x << "," << y << endl;
                    n++;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                int ans = dist(i,j);
                dists[i][j] = ans;
                dists[j][i] = ans;
                //cout << i << " to " << j << ": " << ans << endl;
            }
        }
            
        int* order = new int[n];
        for (int i = 0; i < n; i++) {
            order[i] = i;
        }
        int best = 10000000;
        do {
            int sum = 0;
            for (int i = 0; i < n; i++) {
                //cout << order[i] << " ";
            }
            
            for (int i = 0; i < n; i++) {
                sum += dists[order[i]][order[(i+1)%n]];
            }
            
            //cout << ":" << sum << endl;
            best = min(best,sum);
        } while (next_permutation(order,order+n));
        OUT << best << endl;
    }    
    return 0;
}

