/*
  テ」ツつオテ」ツつ、テ」ツつウテ」ツδュテ」ツつ津ィツサツ「テ」ツ?古」ツ?療」ツ?ヲBFSテ」ツ?づ・ツ閉湘ゥツ。ツ古」ツ?ッテ」ツつエテ」ツδシテ」ツδォテ・ツ按、テ・ツョツ堙」ツ??
  テ」ツつオテ」ツつ、テ」ツつウテ」ツδュテ」ツ?ョテ・ツ青妥」ツ?催」ツ?ッテ・ツ?ィテゥツδィテ」ツ?ァ6*4=24テゥツ?堙」ツつ甘」ツ?療」ツ?凝」ツ?ェテ」ツ??」ツ?ョテ」ツ?ァ,
  テ」ツつオテ」ツつ、テ」ツつウテ」ツδュテ」ツ?ョテ・ツ青?ァツ環カテヲツ?凝」ツ?ォテ」ツ?甘」ツ??」ツ?ヲ,テァツゥツエテ」ツ?古」ツ?づ」ツつ凝」ツ?凝」ツつ津ィツェツソテ」ツ?ケテ」ツつ凝・ツソツ?ィツヲツ?」ツ?古」ツ?づ」ツつ?
  テ・ツ?・テ・ツ環崚」ツ?ョテ」ツつーテ」ツδェテ」ツδε」ツδ嘉、ツクツ甘」ツ?ョテ・ツコツァテヲツィツ凖」ツつ津・ツ?ィテゥツδィテ」ツつステ」ツδシテ」ツつケテ・ツ??」ツ?ォテァツ崢エテヲツ可禿」ツ?。テ」ツ??
 */

#include <iostream>
#include <algorithm>
using namespace std;
 
struct Dice {
  int id[6], cnt, label[6];
  const static int f[3][3];
 
  Dice() { init(); }
 
  void init() {
    for(int i = 0; i < 6; ++i) {
      id[i] = i;
    }
    cnt = 0;
    label[0] = 1;
    label[1] = 2;
    label[2] = 5;
    label[3] = 6;
    label[4] = 4;
    label[5] = 3;
  }
 
  void rotate(int type, int dir = 1) {
    const int *v = f[type];
    int a,b,c,t; a=v[0]; b=v[1]; c=v[2]; t=id[type];
    if(dir < 0) swap(a,c);
    id[type]=id[a]; id[a]=id[b]; id[b]=id[c]; id[c]=t;
  }
 
  bool next() {
    ++cnt;
    if(cnt == 24) {
      rotate(0,-1);
      rotate(1,-1);
      cnt = 0;
      return false;
    }
    if(cnt == 4*3) {
      rotate(2,-1);
      rotate(0,-1);
    } else if(cnt%4 == 0) {
      rotate(2);
    } else {
      rotate(0);
    }
    return true;
  }
 
  void roll(int dir) {
    switch(dir) {
    case 0: rotate(1,-1); break;
    case 1: rotate(2, 1); break;
    case 2: rotate(1, 1); break;
    case 3: rotate(2,-1); break;
    }
  }
 
  bool operator < (const Dice &d) const {
    for(int i = 0; i < 3; ++i) {
      if(id[i] != d.id[i]) return id[i] < d.id[i];
    }
    return false;
  }
 
  void show() {
    cout << label[id[4]] << " / \\ "<< label[id[5]] << endl;
    cout << " /   \\" << endl;
    cout << "|\\ " << label[id[2]] << " /|" << endl;
    cout << "|" << label[id[0]] << "\\ /" << label[id[1]] << "|" << endl;
    cout << " \\ | /" << endl;
    cout << "  \\|/" << endl;
    cout << "   " << label[id[3]] << endl;
  }
 
  int getLabel(int faceNum) {
    if(faceNum == 0) {
      return label[id[0]];
    } else if(faceNum == 1) {
      return label[id[5]];
    }
    return -1;
  }
};
const int Dice::f[3][3] = {{4,5,1},{3,4,2},{5,3,0}};
 
#include <vector>
#include <map>
#include <queue>
 
const int V = 24;
const int INF = 1<<28;
vector<int> E[V];
vector<pair<int, int> > ps;
vector<pair<int, int> > A[4];
vector<int> cost;
map<Dice, int> id;
map<int, Dice> rid;
 
char G[6][3][3];
 
void bfs(int v) {
  cost = vector<int>(V, INF);
  cost[v] = 0;
  queue<int> que;
  que.push(v);
  while(!que.empty()) {
    v = que.front();
    que.pop();
    for(int i = 0; i < E[v].size(); ++i) {
      int to = E[v][i];
      if(cost[to] != INF) continue;
      cost[to] = cost[v] + 1;
      que.push(to);
    }
  }
}
 
void init() {
  Dice d;
  do {
    id[d] = id.size()-1;
    rid[id[d]] = d;
  } while(d.next());
 
  do {
    for(int i = 0; i < 4; ++i) {
      Dice t = d;
      t.roll(i);
      E[id[d]].push_back(id[t]);
    }
  } while(d.next());
 
  A[0].push_back(make_pair(2,0));
  A[0].push_back(make_pair(2,1));
  A[0].push_back(make_pair(2,2));
 
  A[1].push_back(make_pair(0,0));
  A[1].push_back(make_pair(1,0));
  A[1].push_back(make_pair(2,0));
 
  A[2].push_back(make_pair(0,0));
  A[2].push_back(make_pair(0,1));
  A[2].push_back(make_pair(0,2));
 
  A[3].push_back(make_pair(0,2));
  A[3].push_back(make_pair(1,2));
  A[3].push_back(make_pair(2,2));
 
  ps.push_back(make_pair(0,0));
  ps.push_back(make_pair(0,0));
  ps.push_back(make_pair(0,0));
  ps.push_back(make_pair(0,0));
 
  ps.push_back(make_pair(3,3));
  ps.push_back(make_pair(3,1));
  ps.push_back(make_pair(3,3));
  ps.push_back(make_pair(1,3));
 
  ps.push_back(make_pair(1,3));
  ps.push_back(make_pair(2,0));
  ps.push_back(make_pair(3,1));
  ps.push_back(make_pair(0,2));
 
  ps.push_back(make_pair(2,2));
  ps.push_back(make_pair(2,2));
  ps.push_back(make_pair(2,2));
  ps.push_back(make_pair(2,2));
 
  ps.push_back(make_pair(2,0));
  ps.push_back(make_pair(1,3));
  ps.push_back(make_pair(0,2));
  ps.push_back(make_pair(3,1));
 
  ps.push_back(make_pair(3,1));
  ps.push_back(make_pair(1,1));
  ps.push_back(make_pair(1,3));
  ps.push_back(make_pair(1,1));
 
  bfs(0);
}
 
bool isValid(int d) {
  {
    int f = rid[d].getLabel(0) - 1;
    vector<pair<int,int> > &a = A[ps[d].first];
    int cnt = 0;
    for(int i = 0; i < a.size(); ++i) {
      cnt += G[f][a[i].first][a[i].second] == '*';
    }
    if(cnt == 0) return false;
  }
  {
    int f = rid[d].getLabel(1) - 1;
    vector<pair<int,int> > &a = A[ps[d].second];
    int cnt = 0;
    for(int i = 0; i < a.size(); ++i) {
      cnt += G[f][a[i].first][a[i].second] == '*';
    }
    if(cnt == 0) return false;
  }
  return true;
}
 
int main() {
  init();
  while(1) {
    for(int k = 0; k < 6; ++k) {
      for(int i = 0; i < 3; ++i) {
        for(int j = 0; j < 3; ++j) {
          cin >> G[k][i][j];
          if(G[k][i][j] == '#') return 0;
        }
      }
    }
 
    int ans = INF;
    for(int d = 0; d < V; ++d) {
      if(isValid(d)) {
        ans = min(ans, cost[d]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Measured memory_bytes: 1212