#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cassert>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;

string Message[] = {"Front","Right","Back","Left","Top","Bottom"};
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};

class Box
{
public:
  vector<vector<string> > box;
  int used;  

  Box(){
    used = 0;
    box.resize(6);
    for(int i=0;i<6;i++)box[i].resize(3);
  }

  vector<string> rotate90(vector<string> &x)
  {
    vector<string> ret = x;
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	ret[j][2-i] = x[i][j];

    return ret;
  }

  vector<string> rotate270(vector<string> &x)
  {
    vector<string> ret = x;
    for(int i=0;i<3;i++)
      ret = rotate90(ret);
    return ret;
  }

  vector<vector<string> > R1(vector<vector<string> > &x)
  {
    vector<vector<string> > r = x;
    string pre[3];
    for(int i=0;i<3;i++)pre[i] = r[2][i];

    //2 <- 4
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[2][2-i][2-j] = r[4][i][j];

    //4 <- 0
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[4][i][j] = r[0][i][j];

    //0 <- 5
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[0][i][j] = r[5][i][j];

    //5 <- 2
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[5][2-i][2-j] = pre[i][j];

    r[1] = rotate90(r[1]);
    r[3] = rotate270(r[3]);

    return r;
  }

  vector<vector<string> > R2(vector<vector<string> > &x)
  {
    vector<vector<string> > r = x;
    for(int i=0;i<3;i++)
      r = R1(r);
    return r;
  }

  vector<vector<string> > R3(vector<vector<string> > &x)
  {
    vector<vector<string> > r = x;
    string pre[3];
    for(int i=0;i<3;i++)pre[i] = r[4][i];

    //4 <- 1
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[4][2-j][i] = r[1][i][j];

    //1 <- 5
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[1][2-j][i] = r[5][i][j];


    //5 <- 3
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[5][2-j][i] = r[3][i][j];


    //3 <- 4
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[3][2-j][i] = pre[i][j];


    r[0] = rotate270(r[0]);
    r[2] = rotate90(r[2]);
    return r;
  }

  vector<vector<string> > R4(vector<vector<string> > &x)
  {
    vector<vector<string> > ret = x;
    for(int i=0;i<3;i++)
      ret = R3(ret);
    return ret;
  }


  void print()
  {
    cout << "print--------------" << endl;
    for(int k=0;k<6;k++)
      {
	cout << Message[k] << "*************" << endl;
	for(int i=0;i<3;i++)
	  {
	    for(int j=0;j<3;j++)
	      {
		cout << box[k][i][j];
	      }
	    cout << endl;
	  }
      }
    cout << endl;
  }

  bool isOK(int x)
  {
    queue<pair<int,int> > que;
    bool used[3][3];
    for(int i=0;i<3;i++)for(int j=0;j<3;j++)used[i][j] = false;
    if(box[x][0][0] != '*')
      que.push(pair<int,int>(0,0)),used[0][0] = true;
    if(box[x][0][1] != '*')
      que.push(pair<int,int>(1,0)),used[0][1] = true;
    if(box[x][0][2] != '*')
      que.push(pair<int,int>(2,0)),used[0][2] = true;

    while(!que.empty())
      {
	pair<int,int> p = que.front(); que.pop();
	if(p.second == 2)return true;
	for(int i=0;i<4;i++)
	  {
	    int nx = p.first + dx[i];
	    int ny = p.second + dy[i];
	    if(!(0 <= nx && nx < 3 && 0 <= ny && ny < 3))continue;
	    if(used[ny][nx])continue;
	    used[ny][nx] = true;
	    que.push(pair<int,int>(nx,ny));
	  }

      }
    return false;
  }

  bool isFin()
  {
    bool p1 = false;
    bool p2 = false;
    if(box[0][2][0] == '*')p1 = true;
    if(box[0][2][1] == '*')p1 = true;
    if(box[0][2][2] == '*')p1 = true;
    if(box[2][2][0] == '*')p2 = true;
    if(box[2][2][1] == '*')p2 = true;
    if(box[2][2][2] == '*')p2 = true;

    return p1 & p2;
  }

};

typedef pair<Box,int> P;

int main()
{
  while(true)
    {
      Box box;
      rep(k,6)
	{
	  rep(i,3)
	    {
	      string line;
	      cin >> line;	
	      box.box[k][i] = line;
	      if(line == "#")
		{
		  assert(i == 0 && k == 0);
		  goto F;		
		}
	    }
	}

      bool found = false;
	  queue<P> que;
	  que.push(P(box,0));
	  while(!que.empty())
	    {
	      P p = que.front(); que.pop();
	      if(p.first.isFin())
		{
		  found = true;
		  cout << p.second << endl;
		  break;
		}

	      for(int i=0;i<4;i++)
		{
		  if((p.first.used >> i) & 1)continue;
		  Box bx = p.first;
		  if(i == 0)
		    bx.box = bx.R1(bx.box),p.first.used <<= (1<<i);
		  else if(i == 1)
		    bx.box = bx.R2(bx.box),p.first.used <<= (1<<i);
		  else if(i == 2)
		    bx.box = bx.R3(bx.box),p.first.used <<= (1<<i);
		  else if(i == 3)
		    bx.box = bx.R4(bx.box),p.first.used <<= (1<<i);
		  que.push(P(bx,p.second+1));
		}
	    }
	  assert(found);

    }
 F:;
  return 0;
}

Measured memory_bytes: 1352