#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
#define INF 1000000000
const int mov[24][6]={
  {0,1,2,3,4,5},{0,2,4,1,3,5},{0,4,3,2,1,5},{0,3,1,4,2,5},
  {3,1,0,5,4,2},{3,0,4,1,5,2},{3,4,5,0,1,2},{3,5,1,4,0,2},
  {5,1,3,2,4,0},{5,3,4,1,2,0},{5,4,2,3,1,0},{5,2,1,4,3,0},
  {2,1,5,0,4,3},{2,5,4,1,0,3},{2,4,0,5,1,3},{2,0,1,4,5,3},
  {4,0,2,3,5,1},{4,2,5,0,3,1},{4,5,3,2,0,1},{4,3,0,5,2,1},
  {1,0,3,2,5,4},{1,3,5,0,2,4},{1,5,2,3,0,4},{1,2,0,5,3,4}
};
int n,res;
struct CUBE{
  int num[6];
}cube[4],buf[4];
map<string,int> idx;
int top;
void dfs(int dep){
  int i,j;
  if(dep==n){
    int tmp=0;
    for(i=0;i<6;i++){
      int cnt[24]={0};
      for(j=0;j<n;j++) cnt[buf[j].num[i]]++;
      int mx=0;
      for(j=0;j<top;j++) mx=max(mx,cnt[j]);
      tmp+=(n-mx);
    }
    res=min(res,tmp);
    return;
  }
  for(i=0;i<24;i++){
    for(j=0;j<6;j++)
      buf[dep].num[j]=cube[dep].num[mov[i][j]];
    dfs(dep+1);
  }
}
int main(){
  int i,j;
  while(scanf("%d",&n),n){
    idx.clear(); top=0;
    for(i=0;i<n;i++)
      for(j=0;j<6;j++){
        string str;
        cin>>str;
        if(idx.find(str)==idx.end())
          idx[str]=top++;
        cube[i].num[j]=idx[str];
      }
    if(n==1){
      printf("0\n");
      continue;
    }
    res=INF;
    buf[0]=cube[0];
    dfs(1);
    printf("%d\n",res);
  }
  return 0;
}