#include<bits/stdc++.h>
using namespace std;

// ??????????????¢??£
enum FACE { FRONT,RIGHT,BACK,LEFT,TOP,BOTTOM };


struct state{
  char fie[3][3];
  bool operator!=(const state& a) const {
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
        if( fie[j][i] != a.fie[j][i] ) return true;
    return false; 
  }
  bool operator==(const state& a) const {
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
        if( fie[j][i] != a.fie[j][i] ) return false;
    return true; 
  }
  bool operator<(const state& a) const {
    for(int i=0;i<3;i++){
      for(int j=0;j<3;j++){
        if( fie[j][i] > a.fie[j][i] ) return false;
        if( fie[j][i] < a.fie[j][i] ) return true;
      }
    }
    return false; 
  }

  void rev(){
    char bf[3][3];
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
        bf[j][i] = fie[2-j][2-i];
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
        fie[j][i] = bf[j][i];
  }

  void rot(){
    char bf[3][3];
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
        bf[j][i] = fie[2-i][j];
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
        fie[j][i] = bf[j][i];    
  }

  void ro2(){
    char bf[3][3];
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
        bf[j][i] = fie[i][2-j];
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
        fie[j][i] = bf[j][i];    
  }

  void view(){
    for(int i=0;i<3;i++){
      for(int j=0;j<3;j++)
        cout << fie[j][i];
      cout << endl;
    }
    cout << endl;
  }
};

struct dice{
public:
  dice(){
    id[0] = FRONT;
    id[1] = RIGHT;
    id[2] = BACK;
    id[3] = LEFT;
    id[4] = TOP;
    id[5] = BOTTOM;
  }
  
  state& operator[] (FACE f){return var[id[f]];}
  const state& operator[] (FACE f)const {return var[id[f]];}

  bool operator==(const dice &a)const {
    const dice &b = *this;
    for(int i=0;i<6;i++){
      if(a[(FACE)i] != b[(FACE)i]) return false;
    }
    return true;
  }
  bool operator<(const dice &a) const {
    const dice &b = *this;
    for(int i=0;i<6;i++){
      if( b[(FACE)i] < a[(FACE)i] ) return true;
      if( b[(FACE)i] == a[(FACE)i] ) continue;
      return false;
    }
    return false;
  }

  void roll_x(){
    roll(TOP,BACK,BOTTOM,FRONT);
    var[TOP].rev(); var[BACK].rev();
    var[LEFT].ro2(); var[RIGHT].rot();
  } // X?????????????????¢ (FRONT -> BOTTOM)
  void roll_y(){
    roll(TOP,LEFT,BOTTOM,RIGHT);
    var[FRONT].ro2(); var[TOP].ro2();
    var[BACK].rot(); var[BOTTOM].ro2();
    var[LEFT].ro2(); var[RIGHT].ro2();
  } // Y?????????????????¢ (TOP -> RIGHT)
  void roll_z(){roll(FRONT,RIGHT,BACK,LEFT);} // Z?????????????????¢ (FRONT -> LEFT)

  state var[6]; // ????????????????¨???¶
  int id[6]; // ?????¢????¨???¶
  void roll(FACE a,FACE b,FACE c,FACE d){//a b c d ???b c d a
    state tmc = var[a];
    var[a] = var[b];
    var[b] = var[c];
    var[c] = var[d];
    var[d] = tmc;
  }

  void view(){

    /*    cout << endl;
    var[FRONT].view();
    var[RIGHT].view();
    var[BACK].view();
    var[LEFT].view();
    var[TOP].view();
    var[BOTTOM].view();
    */
    for(int i=0;i<6;i++){
      var[id[i]].view();
      cout << endl;
    }
  }
};

dice d;
map<dice,int> M;
int solve(){
  queue<dice> q;
  M.clear();
  q.push( d );
  M[d] = 0;
  while(!q.empty()){
    dice p = q.front(); q.pop();
    //    p.view();
    //cout << M[p] << endl;
    bool f1 = false,f2 = false;
    for(int i=0;i<3;i++){
      //  cout << p[FRONT].fie[i][2] << " "<< p[BACK].fie[i][2] << endl;
      if( p[FRONT].fie[i][2] == '*' ) f1 = true;
      if( p[BACK].fie[i][2] == '*' ) f2 = true;
    }
    // cout << f1 << " "<< f2 << " "<< M[p] << endl;
    if( f1 && f2 ){
      //      p.view();
      return M[p];
    }
    dice tmp = p;
    tmp.roll_x();
    if( M.count(tmp) == 0 ) {
      q.push( tmp );
      //  tmp.view();
      //  cout << M[p]+1 << endl;
      M[tmp] = M[p] + 1;
    }
    tmp.roll_x();tmp.roll_x();
    if( M.count(tmp) == 0 ) {
      q.push( tmp );
      M[tmp] = M[p] + 1;
    }
    
    tmp = p;
    tmp.roll_y();
    if( M.count(tmp) == 0 ) {
      q.push( tmp );
      M[tmp] = M[p] + 1;
    }
    tmp.roll_y();    tmp.roll_y();
    if( M.count(tmp) == 0 ) {
      q.push( tmp );
      M[tmp] = M[p] + 1;
    }
  }
  return -1;
}


int main(){
  try {
    while(1){
      d = dice();
      for(int i=0;i<6;i++){
        for(int j=0;j<3;j++){
          for(int k=0;k<3;k++){
          cin >> d.var[i].fie[k][j];
          if( d.var[i].fie[k][j] == '#' ) throw 0;
          }
        }      
      }    
      cout << solve() << endl;
    }
    
  } catch(...){};
}

Measured memory_bytes: 1260