#include<bits/stdc++.h>
#define ll long long
#define lp(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define lps(i, n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
const ll MOD = (ll)1e9+7;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
using namespace std;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}

struct dice{
  string f,r,t,d,l,b;
  dice(){};
  dice(string v1,string v2,string v3,string v4,string v5,string v6){
    f=v1;r=v2;t=v3;d=v4;l=v5;b=v6;
  }
  int same(dice d2){
    int cnt=6;
    if(f==d2.f)cnt--;
    if(r==d2.r)cnt--;
    if(t==d2.t)cnt--;
    if(d==d2.d)cnt--;
    if(l==d2.l)cnt--;
    if(b==d2.b)cnt--;
    return cnt;
  }
  void roll(char c){
    if(c=='s'){
      string x=f;
      f=r;
      r=b;
      b=l;
      l=x;
      return;
    }
    else{
      string x=f;
      f=t;
      t=b;
      b=d;
      d=x;
    }
  }
};

string task="sssstsssstsssstsssststssssttssss";

vector<dice> v;

int acnt(){
  vector<string> x;
  int cnt=0;
  x.clear();
  lp(i,v.size()){
    x.push_back(v[i].f);
  }
  sort(x.begin(),x.end());
  if(v.size()==3){
    if(x[0]==x[1]&&x[1]==x[2])cnt+=0;
    else if(x[0]==x[1]||x[1]==x[2])cnt+=1;
    else{
      cnt+=2;
    }
  }
  else{
    if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;
    else if(x[0]==x[1]&&x[1]==x[2]){
      cnt+=1;
    }
    else if(x[1]==x[2]&&x[2]==x[3]){
      cnt++;
    }
    else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;
    else cnt+=3;
  }

  x.clear();
  lp(i,v.size()){
    x.push_back(v[i].r);
  }
  sort(x.begin(),x.end());
  if(v.size()==3){
    if(x[0]==x[1]&&x[1]==x[2])cnt+=0;
    else if(x[0]==x[1]||x[1]==x[2])cnt+=1;
    else{
      cnt+=2;
    }
  }
  else{
    if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;
    else if(x[0]==x[1]&&x[1]==x[2]){
      cnt+=1;
    }
    else if(x[1]==x[2]&&x[2]==x[3]){
      cnt++;
    }
    else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;
    else cnt+=3;
  }
  
  x.clear();
  lp(i,v.size()){
    x.push_back(v[i].t);
  }
  sort(x.begin(),x.end());
  if(v.size()==3){
    if(x[0]==x[1]&&x[1]==x[2])cnt+=0;
    else if(x[0]==x[1]||x[1]==x[2])cnt+=1;
    else{
      cnt+=2;
    }
  }
  else{
    if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;
    else if(x[0]==x[1]&&x[1]==x[2]){
      cnt+=1;
    }
    else if(x[1]==x[2]&&x[2]==x[3]){
      cnt++;
    }
    else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;
    else cnt+=3;
  }
x.clear();
  lp(i,v.size()){
    x.push_back(v[i].d);
  }
  sort(x.begin(),x.end());
  if(v.size()==3){
    if(x[0]==x[1]&&x[1]==x[2])cnt+=0;
    else if(x[0]==x[1]||x[1]==x[2])cnt+=1;
    else{
      cnt+=2;
    }
  }
  else{
    if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;
    else if(x[0]==x[1]&&x[1]==x[2]){
      cnt+=1;
    }
    else if(x[1]==x[2]&&x[2]==x[3]){
      cnt++;
    }
    else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;
    else cnt+=3;
  }
x.clear();
  lp(i,v.size()){
    x.push_back(v[i].l);
  }
  sort(x.begin(),x.end());
  if(v.size()==3){
    if(x[0]==x[1]&&x[1]==x[2])cnt+=0;
    else if(x[0]==x[1]||x[1]==x[2])cnt+=1;
    else{
      cnt+=2;
    }
  }
  else{
    if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;
    else if(x[0]==x[1]&&x[1]==x[2]){
      cnt+=1;
    }
    else if(x[1]==x[2]&&x[2]==x[3]){
      cnt++;
    }
    else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;
    else cnt+=3;
  }
x.clear();
  lp(i,v.size()){
    x.push_back(v[i].b);
  }
  sort(x.begin(),x.end());
  if(v.size()==3){
    if(x[0]==x[1]&&x[1]==x[2])cnt+=0;
    else if(x[0]==x[1]||x[1]==x[2])cnt+=1;
    else{
      cnt+=2;
    }
  }
  else{
    if(x[0]==x[1]&&x[1]==x[2]&&x[2]==x[3])cnt+=0;
    else if(x[0]==x[1]&&x[1]==x[2]){
      cnt+=1;
    }
    else if(x[1]==x[2]&&x[2]==x[3]){
      cnt++;
    }
    else if(x[0]==x[1]||x[2]==x[3]||x[1]==x[2])cnt+=2;
    else cnt+=3;
  }
  return cnt;
}

int solve(){
  if(v.size()==1)return 0;
  if(v.size()==2){
    int ans=INF;
    lp(i,task.size()){
      v[0].roll(task[i]);
      ans=min(ans,v[0].same(v[1]));
    }
    return ans;
  }
  if(v.size()==3){
    int ans=INF;
    lp(i,task.size()){
      v[0].roll(task[i]);
      lp(j,task.size()){
	v[1].roll(task[j]);
	ans=min(ans,acnt());
      }
    }
    return ans;
  }
  else{
    int ans=INF;
    lp(i,task.size()){
      v[0].roll(task[i]);
      lp(j,task.size()){
	v[1].roll(task[j]);
	lp(k,task.size()){
	  v[2].roll(task[k]);
	  ans=min(ans,acnt());
	}
      }
    }
    return ans;
  }
  return -1;
}

int main(){
  while(1){
    int n;
    cin>>n;
    if(n==0)break;
      v.clear();
    lp(i,n){
      string a,b,c,d,e,f;
      cin>>a>>b>>c>>d>>e>>f;
      dice k(a,b,c,d,e,f);
      v.push_back(k);
    }
    int ans=INF;
    cout<<solve()<<endl;
  }
  return 0;
}





Measured memory_bytes: 3164