import java.util.*;
import java.io.*;


public class D4 {
  static int result = 9999;
  static Node start;
  public static void main (String[] args)throws IOException {
    Scanner fIn = new Scanner (new FileReader("DATA4.txt"));
    PrintWriter PW = new PrintWriter (new FileWriter("OUT4.txt"));
    for(int i = 0 ; i < 5 ; i ++) {
      result = 9999;
      ArrayList<Node> list = new ArrayList<Node>();
      for(int j = 0 ; j < 10 ; j ++) {
        String s = fIn.nextLine();
        for(int k = 0 ; k < 10 ; k ++) {
          if(s.charAt(k) == '#') {
            Node n = new Node(j , k);
            list.add(n);
          }
        }
      }
      start = new Node(list.get(0).x , list.get(0).y);
      find(0 , list.get(0) , list , 0);
      PW.println(result);
    }
    
    PW.close();
  }
  static void find(int curI , Node current , ArrayList<Node> nodes , int distance) {
    if(nodes.size() == 1) {
      distance += dist(current , start); 
      if(distance < result) result = distance;
      return;
    }
    
    for(int i = 0 ; i < nodes.size() - 1 ; i ++) {
      ArrayList<Node> l = new ArrayList<Node>();
      copy(nodes,l);
      l.remove(curI);
      find(i , l.get(i) , l , distance + dist(current , l.get(i)));
    }
  }
  
  static int dist(Node a , Node b) {
    return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
  }
  static void copy(ArrayList<Node> l1 , ArrayList<Node> l2) {
    for(int i = 0 ; i < l1.size() ; i ++) {
      Node temp = new Node(l1.get(i).x , l1.get(i).y);
      l2.add(temp);
    }
  }
}

class Node {
  public int x;
  public int y;
  public Node(int xIn , int yIn) {
    x = xIn;
    y = yIn;
  }
}
