#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;




struct Info{
	char buf[25];
	int count;
};

class Dice{
public:

	void setPos(int num){

		for(int i = 0; i < 6; i++)work[i] = index[i];

		switch(num){
		case 0:
			setindex(work[4],work[0],work[2],work[3],work[5],work[1]);
			break;
		case 1:
			setindex(work[5],work[4],work[2],work[3],work[1],work[0]);
			break;
		case 2:
			setindex(work[1],work[5],work[2],work[3],work[0],work[4]);
			break;
		case 3:
			setindex(work[0],work[1],work[2],work[3],work[4],work[5]);
			break;
		case 4:
			setindex(work[2],work[1],work[5],work[0],work[4],work[3]);
			break;
		case 5:
			setindex(work[4],work[2],work[5],work[0],work[3],work[1]);
			break;
		case 6:
			setindex(work[3],work[4],work[5],work[0],work[1],work[2]);
			break;
		case 7:
			setindex(work[1],work[3],work[5],work[0],work[2],work[4]);
			break;
		case 8:
			setindex(work[5],work[3],work[4],work[1],work[2],work[0]);
			break;
		case 9:
			setindex(work[2],work[5],work[4],work[1],work[0],work[3]);
			break;
		case 10:
			setindex(work[0],work[2],work[4],work[1],work[3],work[5]);
			break;
		case 11:
			setindex(work[3],work[0],work[4],work[1],work[5],work[2]);
			break;
		case 12:
			setindex(work[1],work[0],work[3],work[2],work[5],work[4]);
			break;
		case 13:
			setindex(work[5],work[1],work[3],work[2],work[4],work[0]);
			break;
		case 14:
			setindex(work[4],work[5],work[3],work[2],work[0],work[1]);
			break;
		case 15:
			setindex(work[0],work[4],work[3],work[2],work[1],work[5]);
			break;
		case 16:
			setindex(work[2],work[4],work[0],work[5],work[1],work[3]);
			break;
		case 17:
			setindex(work[1],work[2],work[0],work[5],work[3],work[4]);
			break;
		case 18:
			setindex(work[3],work[1],work[0],work[5],work[4],work[2]);
			break;
		case 19:
			setindex(work[4],work[3],work[0],work[5],work[2],work[1]);
			break;
		case 20:
			setindex(work[0],work[3],work[1],work[4],work[2],work[5]);
			break;
		case 21:
			setindex(work[2],work[0],work[1],work[4],work[5],work[3]);
			break;
		case 22:
			setindex(work[5],work[2],work[1],work[4],work[3],work[0]);
			break;
		case 23:
			setindex(work[3],work[5],work[1],work[4],work[0],work[2]);
			break;
		case 24:
			//Do nothing
			break;
		}
	}

	void setindex(int w0,int w1,int w2,int w3,int w4,int w5){
		index[0] = w0;
		index[1] = w1;
		index[2] = w2;
		index[3] = w3;
		index[4] = w4;
		index[5] = w5;
	}

	void init(){
		index[0] = 2;
		index[1] = 5;
		index[2] = 1;
		index[3] = 4;
		index[4] = 0;
		index[5] = 3;
	}

	int index[6];
	int work[6];
};

int N,info_index;
Dice dice[4];
char color[4][6][25];
Info info[24];

bool strCmp(char* base, char* comp){
	int length1,length2;
	for(length1=0;base[length1] != '\0';length1++);
	for(length2=0;comp[length2] != '\0';length2++);
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

void strcpy(char* to,char* str){
	for(int i=0;str[i] != '\0';i++){
		to[i] = str[i];
		to[i+1] = '\0';
	}
}


void func(){

	info_index = 0;
	bool FLG;

	for(int i = 0; i < N; i++){
		dice[i].init();
		for(int k = 0; k < 6; k++){
			scanf("%s",color[i][k]);

			FLG = false;
			for(int a = 0;a < info_index; a++){
				if(strCmp(info[a].buf,color[i][k])){
					FLG = true;
					break;
				}
			}
			if(!FLG){
				strcpy(info[info_index].buf,color[i][k]);
				info_index++;
			}
		}
	}

	if(N == 1){
		printf("0\n");
		return;
	}

	int minimum,tmp,tmp_sum,maximum;

	if(N == 2){

		minimum = BIG_NUM;

		for(int i = 0; i <= 24; i++){
			dice[1].init();
			dice[1].setPos(i);

			tmp = 0;

			for(int k = 0; k < 6; k++){
				if(strCmp(color[0][dice[0].index[k]],color[1][dice[1].index[k]]) == false){
					tmp++;
				}
			}

			minimum = min(minimum,tmp);
		}

		printf("%d\n",minimum);

	}else if(N == 3){

		minimum = BIG_NUM;

		for(int a = 0; a <= 24; a++){
			dice[1].init();
			dice[1].setPos(a);
			for(int b = 0; b <= 24; b++){
				dice[2].init();
				dice[2].setPos(b);

				tmp_sum = 0;
				for(int i = 0; i < 6; i++){
					for(int k = 0; k < info_index; k++)info[k].count = 0;

					for(int p = 0; p < 3; p++){
						for(int k = 0; k < info_index; k++){
							if(strCmp(info[k].buf,color[p][dice[p].index[i]])){
								info[k].count++;
								break;
							}
						}
					}

					maximum = 1;
					for(int k = 0; k < info_index; k++)maximum = max(maximum,info[k].count);

					tmp_sum += N-maximum;
				}
				minimum = min(minimum,tmp_sum);
			}
		}

		printf("%d\n",minimum);

	}else{ //N == 4

		minimum = BIG_NUM;

		for(int a = 0; a <= 24; a++){
			dice[1].init();
			dice[1].setPos(a);
			for(int b = 0; b <= 24; b++){
				dice[2].init();
				dice[2].setPos(b);

				for(int c = 0; c <= 24; c++){
					dice[3].init();
					dice[3].setPos(c);

					tmp_sum = 0;
					for(int i = 0; i < 6; i++){
						for(int k = 0; k < info_index; k++)info[k].count = 0;

						for(int p = 0; p < 4; p++){
							for(int k = 0; k < info_index; k++){
								if(strCmp(info[k].buf,color[p][dice[p].index[i]])){
									info[k].count++;
									break;
								}
							}
						}

						maximum = 1;
						for(int k = 0; k < info_index; k++)maximum = max(maximum,info[k].count);

						tmp_sum += N-maximum;
					}
					minimum = min(minimum,tmp_sum);
				}
			}
		}
		printf("%d\n",minimum);
	}
}

int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}
	return 0;
}

Measured memory_bytes: 3192