#include<iostream>
#include<array>
#include<utility>
#include<set>
#include<algorithm>

using namespace std;

typedef array<char,9> P;

void cw(P &p){
  P n;
  for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
      n[j*3+2-i]=p[i*3+j];
    }
  }
  p=n;
}

void ccw(P &p){
  cw(p);
  cw(p);
  cw(p);
}

P lr(P p){
  P n=p;
  for(int i=0;i<3;i++){
    swap(n[0+i*3],n[2+i*3]);
  }
  return n;
}

P inv(P p){
  ccw(p);
  ccw(p);
  return p;
}

P nccw(P p){
  ccw(p);
  return p;
}

struct Dice{
  P p[6];
  Dice(){
    for(int i=0;i<6;i++){
      for(int j=0;j<9;j++){
	cin>>p[i][j];
      }
    }
    // auto d=Dice(*this).l();
    // cout<<"=---"<<endl;
    // for(auto e:d.p){
    //   for(int i=0;i<3;i++){
    // 	for(int j=0;j<3;j++){
    // 	  cout<<e[i*3+j];
    // 	}
    // 	cout<<endl;
    //   }
    // }
    // cout<<"=---"<<endl;
  }
  bool operator<(Dice d)const{
    return lexicographical_compare(begin(p),end(p),begin(d.p),end(d.p));
  }
  Dice &u(){
    auto t=p[4];
    p[4]=p[0];
    p[0]=p[5];
    p[5]=inv(p[2]);
    p[2]=inv(t);
    cw(p[1]);
    ccw(p[3]);
    return *this;
  }
  Dice &d(){
    u();
    u();
    u();
    return *this;
  }
  Dice &l(){
    auto t=p[4];
    p[4]=nccw(p[1]);
    p[1]=nccw(p[5]);
    p[5]=nccw(p[3]);
    p[3]=lr(nccw(t));
    cw(p[2]);
    ccw(p[0]);
    return *this;
  }
  Dice &r(){
    l();
    l();
    l();
    return *this;
  }
};

int main(){
  for(;;){
    Dice d;
    if(!cin)break;
    set<pair<int,Dice> >s{{0,d}};
    for(auto e:s){
      auto cd=e.second;
      if(count(begin(cd.p[0])+6,begin(cd.p[0])+9,'*')&&count(begin(cd.p[2])+6,begin(cd.p[2])+9,'*')){
	cout<<e.first<<endl;
	goto next;
      }
      s.insert({e.first+1,Dice(cd).u()});
      s.insert({e.first+1,Dice(cd).r()});
      s.insert({e.first+1,Dice(cd).d()});
      s.insert({e.first+1,Dice(cd).l()});
    }
  next:
    ;
  }
}
### Prompt

Develop a solution in Cpp to the problem described below:

You are playing a popular video game which is famous for its depthful story and interesting puzzles. In the game you were locked in a mysterious house alone and there is no way to call for help, so you have to escape on yours own. However, almost every room in the house has some kind of puzzles and you cannot move to neighboring room without solving them.

One of the puzzles you encountered in the house is following. In a room, there was a device which looked just like a dice and laid on a table in the center of the room. Direction was written on the wall. It read:

"This cube is a remote controller and you can manipulate a remote room, Dice Room, by it. The room has also a cubic shape whose surfaces are made up of 3x3 unit squares and some squares have a hole on them large enough for you to go though it. You can rotate this cube so that in the middle of rotation at least one edge always touch the table, that is, to 4 directions. Rotating this cube affects the remote room in the same way and positions of holes on the room change. To get through the room, you should have holes on at least one of lower three squares on the front and back side of the room."

You can see current positions of holes by a monitor. Before going to Dice Room, you should rotate the cube so that you can go though the room. But you know rotating a room takes some time and you don’t have much time, so you should minimize the number of rotation. How many rotations do you need to make it possible to get though Dice Room?



Input

The input consists of several datasets. Each dataset contains 6 tables of 3x3 characters which describe the initial position of holes on each side. Each character is either '*' or '.'. A hole is indicated by '*'. The order which six sides appears in is: front, right, back, left, top, bottom. The order and orientation of them are described by this development view:

<image>

Figure 4: Dice Room

<image>

Figure 5: available dice rotations

<image>

There is a blank line after each dataset. The end of the input is indicated by a single '#'.

Output

Print the minimum number of rotations needed in a line for each dataset. You may assume all datasets have a solution.

Examples

Input

...
...
.*.
...
...
.*.
...
...
...
...
...
.*.
...
...
.*.
...
...
...

...
.*.
...
*..
...
..*
*.*
*.*
*.*
*.*
.*.
*.*
*..
.*.
..*
*.*
...
*.*

...
.*.
.*.
...
.**
*..
...
...
.*.
.*.
...
*..
..*
...
.**
...
*..
...

#


Output

3
1
0


Input

...
...
.*.
...
...
.*.
...
...
...
...
...
.*.
...
...
.*.
...
...
...

...
.*.
...
*..
...
..*
*.*
*.*
*.*
*.*
.*.
*.*
*..
.*.
..*
*.*
...
*.*

...
.*.
.*.
...
.**
*..
...
...
.*.
.*.
...
*..
..*
...
.**
...
*..
...


Output

3
1
0

### Response

```cpp
#include<iostream>
#include<array>
#include<utility>
#include<set>
#include<algorithm>

using namespace std;

typedef array<char,9> P;

void cw(P &p){
  P n;
  for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
      n[j*3+2-i]=p[i*3+j];
    }
  }
  p=n;
}

void ccw(P &p){
  cw(p);
  cw(p);
  cw(p);
}

P lr(P p){
  P n=p;
  for(int i=0;i<3;i++){
    swap(n[0+i*3],n[2+i*3]);
  }
  return n;
}

P inv(P p){
  ccw(p);
  ccw(p);
  return p;
}

P nccw(P p){
  ccw(p);
  return p;
}

struct Dice{
  P p[6];
  Dice(){
    for(int i=0;i<6;i++){
      for(int j=0;j<9;j++){
	cin>>p[i][j];
      }
    }
    // auto d=Dice(*this).l();
    // cout<<"=---"<<endl;
    // for(auto e:d.p){
    //   for(int i=0;i<3;i++){
    // 	for(int j=0;j<3;j++){
    // 	  cout<<e[i*3+j];
    // 	}
    // 	cout<<endl;
    //   }
    // }
    // cout<<"=---"<<endl;
  }
  bool operator<(Dice d)const{
    return lexicographical_compare(begin(p),end(p),begin(d.p),end(d.p));
  }
  Dice &u(){
    auto t=p[4];
    p[4]=p[0];
    p[0]=p[5];
    p[5]=inv(p[2]);
    p[2]=inv(t);
    cw(p[1]);
    ccw(p[3]);
    return *this;
  }
  Dice &d(){
    u();
    u();
    u();
    return *this;
  }
  Dice &l(){
    auto t=p[4];
    p[4]=nccw(p[1]);
    p[1]=nccw(p[5]);
    p[5]=nccw(p[3]);
    p[3]=lr(nccw(t));
    cw(p[2]);
    ccw(p[0]);
    return *this;
  }
  Dice &r(){
    l();
    l();
    l();
    return *this;
  }
};

int main(){
  for(;;){
    Dice d;
    if(!cin)break;
    set<pair<int,Dice> >s{{0,d}};
    for(auto e:s){
      auto cd=e.second;
      if(count(begin(cd.p[0])+6,begin(cd.p[0])+9,'*')&&count(begin(cd.p[2])+6,begin(cd.p[2])+9,'*')){
	cout<<e.first<<endl;
	goto next;
      }
      s.insert({e.first+1,Dice(cd).u()});
      s.insert({e.first+1,Dice(cd).r()});
      s.insert({e.first+1,Dice(cd).d()});
      s.insert({e.first+1,Dice(cd).l()});
    }
  next:
    ;
  }
}
```