/*************************************************************************
  > File Name: c.cpp
  > Author: X__X
  > Mail: Kinderlas@gmail.com 
  > Created Time: 2012/8/12 14:35:54
 ************************************************************************/

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<utility>
using namespace std;

#define MP make_pair
#define PB push_back
#define IT iterator
#define B begin() 
#define E end()
#define X first
#define Y second
#define foreach(it, container) \
    for(typeof((container).begin()) it = (container).begin();it!=(container).end();++it)
#define CLR(a, x) memset(a, x, sizeof (a));
typedef vector<int> VI;
typedef pair<int,int> PII;
VI::IT it;
void op(int n){cout << n << ' ';}

const int inf = 0x3f3f3f3f;
const int maxn = 1e8+10;
int ELFhash(char *key, long N)
{
    unsigned long h = 0;
    unsigned long x = 0;
    while(*key)
    {
        h = (h << 4) + (*key++);
        if( (x = h & 0xF0000000L) != 0)
        {
            h ^= (x >> 24);
            h &= ~x;
        }
    }
    return h % N;
}

int cube[4][10];
int tcube[4][10];
int ttcube[4][10];
int _rotate[6][6] = 
{
    {1, 2, 3, 4, 5, 6}, {5, 1, 3, 4, 6, 2},
    {4, 2, 1, 6, 5, 3}, {2, 6, 3, 4, 1, 5},
    {6, 5, 3, 4, 2, 1}, {3, 2, 6, 1, 5, 4}
};
int _Rotate[40][6];


char s[100];

int cnts[10];
int tt[10][10];
int n, ans;
void dfs(int k)
{ 
    if(n == k)
    {
        int tans = 0, cnt = 0;
        for(int i = 1; i <= 6; i++)
        {
            for(int j = 0; j < n; j++)
                tt[i][j] = tcube[j][i];
        }
        for(int i = 1; i <= 6; i++)
        {
            cnt = 0;
            for(int j = 0; j < n; j++)
                cnt = max(cnt, (int)count(tt[i], tt[i]+n, tt[i][j]));
            tans += cnt;
        }
        ans = min(ans, n*6-tans);
        return ;
    }
    for(int i = 0; i < 36; i++)
    {
        if(_Rotate[i][0] != -1)
        {
            for(int j = 1; j <= 6; j++)
            {
                tcube[k][j] = cube[k][_Rotate[i][j-1]];
                /*for(int ii = 0; ii < 6; ii++)
                  {
                  for(int jj = 1; jj <= 6; jj++)
                  ttcube[k][jj] = tcube[k][_rotate[ii][jj-1]];
                  dfs(k+1);
                  }*/
            }
            dfs(k+1);
        }
    }
}

void solve()
{
    for(int i = 0; i < n; i++)
    {
        for(int j = 1; j <= 6; j++)
        {
            scanf("%s", s);
            cube[i][j] = ELFhash(s, 10000007);
        }
    }
    ans = inf;
    dfs(0);
    printf("%d\n", ans);
}

bool check(int ii, int jj)
{
    for(int i = 0; i < 6; i++)
        if(_Rotate[ii][i] != _Rotate[jj][i])
            return false;
    return true;
}

int main()
{
    for(int i = 0, cnt = 0; i < 6; i++)
    {
        for(int j = 0; j < 6; j++)
        {
            for(int k = 0; k < 6; k++)
                _Rotate[cnt][k] = _rotate[i][_rotate[j][k]-1];
            cnt++;
        }
    }
    for(int i = 0; i < 36; i++)
        for(int j = 0; j < 36; j++)
            if(i != j && check(i, j))
                for(int k = 0; k < 6; k++)
                    _Rotate[i][k] = -1;
    while(scanf("%d", &n), n)
        solve();
}

Measured memory_bytes: 1192