#include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>
 
using namespace std;

//   swap(d[], d[]);
void rot1(vector <int> &d) {
  swap(d[1], d[2]);
  swap(d[2], d[4]);
  swap(d[4], d[3]);
}
void rot2(vector <int> &d) {
  swap(d[1], d[0]);
  swap(d[0], d[4]);
  swap(d[4], d[5]);
}
void rot3(vector <int> &d) {
  swap(d[0], d[3]);
  swap(d[3], d[5]);
  swap(d[5], d[2]);
}
void rev1(vector <int> &d) {
  swap(d[1], d[3]);
  swap(d[3], d[4]);
  swap(d[4], d[2]);
}
void rev2(vector <int> &d) {
  swap(d[1], d[5]);
  swap(d[5], d[4]);
  swap(d[4], d[0]);
}
void rev3(vector <int> &d) {
  swap(d[0], d[2]);
  swap(d[2], d[5]);
  swap(d[5], d[3]);
}

int main() {
  int ttt = 0;
  while (true) {
    int n;
    cin >> n;
    if (n == 0) break;
    int cnt = 0;
    map <string, int> memo;
    vector <int> d[n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 6; j++){
        string s;
        cin >> s;
        if (memo.find(s) == memo.end()) {
          d[i].push_back(cnt);
          memo[s] = cnt;
          cnt++;
        }else {
          d[i].push_back(memo[s]);
        }
      }
    }
    if (n == 1) {
      cout << 0 << endl;
      continue;
    }

    set<vector<vector<int> > > cut;
    stack <vector<vector<int> > > st;
    int ans = INT_MAX;
    bool same = true;
    for (int i = 0; i < 6; i++) {
      for (int j = 1; j < n; j++) {
        if (d[j][i] != d[j-1][i]) {
          same = false;
          break;
        }
      }
      if (!same) break;
    }
    if (same) {
      ans = 0;
    }
    vector <vector <int> > t;
    for (int i = 0; i < n; i++) {
      t.push_back(d[i]);
    }
    st.push(t);
    cut.insert(t);
    while (!st.empty()) {
      vector <vector<int> > np = st.top(); st.pop();

      for (int i = 1; i < n; i++) {
        for (int ch = 0; ch < 6; ch++) {
          vector <vector<int> > tnp = np;
          if (ch == 0) {
            rot1(tnp[i]);
          }else if (ch == 1) {
            rot2(tnp[i]);
          }else if (ch == 2){
            rot3(tnp[i]);
          }else if (ch == 3) {
            rev1(tnp[i]);
          }else if (ch == 4) {
            rev2(tnp[i]);
          }else if (ch == 5) {
            rev3(tnp[i]);
          }
          if (cut.find(tnp) != cut.end()) {
            continue;
          }
          st.push(tnp);
          cut.insert(tnp);
          int tsum = 0;
          for (int j = 0; j < 6; j++) {
            int tcnt[cnt];
            memset(tcnt, 0, sizeof(tcnt));
            int maxp = 0;
            for (int k = 0; k < n; k++) {
              tcnt[tnp[k][j]]++;
              if (tcnt[tnp[k][j]] > tcnt[maxp]) {
                maxp = tnp[k][j];
              }
            }
            tsum += n-tcnt[maxp];
          }
          ans = min(ans, tsum);
        }
      }
    }
    int tsum = 0;
    for (int j = 0; j < 6; j++) {
      int tcnt[cnt];
      memset(tcnt, 0, sizeof(tcnt));
      int maxp = 0;
      for (int k = 0; k < n; k++) {
        tcnt[d[k][j]]++;
        if (tcnt[d[k][j]] > tcnt[maxp]) {
          maxp = d[k][j];
        }
      }
      tsum += n-tcnt[maxp];
    }

    cout << min(ans, tsum) << endl;
  }
}

Measured memory_bytes: 8140