//AOJ0121

#include <iostream>
#include <queue>
using namespace std;

const int INF = 100000000;

class Point{
public:
  int x;
  int y;
};

bool is_over(){

}

int main(){
  char paz[3][5]; //???????????????
  int xz, yz; //0?????\??£???????????´???

  for(int y=0; y<=3; y++){
    for(int x=0; x<=5; x++){
      paz[y][x] = '#';
    }
  }

  for(int y=1; y<2; y++){
    for(int x=1; x<4; x++){
      cin >> paz[y][x]; //???????????????????????????
      if(paz[y][x] == 0){ //????????????????????????????????°???0??????
        xz == x;  //0?????´???????¨????
        yz == y;
      }
    }
  }

  int count[2][4];  //?????°
  for(int y=0; y<2; y++){ //?????°?????§?????????
    for(int x=0; x<4; x++){
      count[y][x] = INF;
    }
  }

queue<Point> que;

Point p;
p.x = xz; p.y = yz;
que.push(p);  //0?????????????????\???????????\
count[p.y][p.x] = 0;  //?????°???0

while(!que.empty()){  //?????\???????????§???????????°?????????
  p = que.front();  que.pop();  //?????\?????????????????????

  if(paz[1][1]<paz[1][2] && paz[1][2]<paz[1][3] && paz[1][3]<paz[1][4] && paz[1][4]<paz[2][1] && paz[2][1]<paz[2][2] && paz[2][2]<paz[2][3] && paz[2][3]<paz[2][4]){
    cout << count[p.y][p.x] << endl;
    break;  //??????????????£???????????????????????????????°??????°?????????????????????
  }

  if(paz[p.y-1][p.x]!='#' && count[p.y-1][p.x]==INF){ //???
    Point q;
    q.x = p.x;  q.y = p.y-1;
    que.push(q);
    count[q.y][q.x] = count[q.y][q.x]+1;
  }
  if(paz[p.y+1][p.x]!='#' && count[p.y+1][p.x]==INF){ //???
    Point q;
    q.x = p.x;  q.y = p.y+1;
    que.push(q);
    count[q.y][q.x] = count[q.y][q.x]+1;
  }
  if(paz[p.y][p.x-1]!='#' && count[p.y][p.x-1]==INF){ //???
    Point q;
    q.x = p.x-1;  q.y = p.y;
    que.push(q);
    count[q.y][q.x] = count[q.y][q.x]+1;
  }
  if(paz[p.y][p.x+1]!='#' && count[p.y][p.x+1]==INF){ //???
    Point q;
    q.x = p.x+1;  q.y = p.y;
    que.push(q);
    count[q.y][q.x] = count[q.y][q.x]+1;
  }
}

return 0;
}