### constant

#     (+x,+y,+z,-z,-y,-x)
DICE = [0, 1, 2, 3, 4, 5]

INF = 1 << 30

### subroutines

def rot_x(dc0, n = 1)
  d = dc0.clone
  n.times do
    t = d[1]; d[1] = d[3]; d[3] = d[4]; d[4] = d[2]; d[2] = t
  end
  d
end

def rot_y(dc0, n = 1)
  d = dc0.clone
  n.times do
    t = d[2]; d[2] = d[5]; d[5] = d[3]; d[3] = d[0]; d[0] = t
  end
  d
end

def rot_z(dc0, n = 1)
  d = dc0.clone
  n.times do
    t = d[0]; d[0] = d[4]; d[4] = d[5]; d[5] = d[1]; d[1] = t
  end
  d
end

def calc_min_repaint(dcs)
  count = 0
  
  for i in (0...6)
    cs = [0] * $nc
    (0...$n).each{|k| cs[dcs[k][i]] +=1}
    count += $n - cs.max
  end

  count
end

def min_r_rec(k, dcs)
  if k >= $n
    return calc_min_repaint(dcs)
  end

  min_r = INF

  $rots.each do |rot|
    dc = (0...6).map{|i| $dices[k][rot[i]]}
    r = min_r_rec(k + 1, dcs + [dc])
    min_r = r if min_r > r
  end

  min_r
end

def min_repaint
  min_r_rec(1, [$dices.first])
end

### main

$rots = []

for zr in (0...4)
  for yr in (0...4)
    for xr in (0...4)
      $rots << rot_z(rot_y(rot_x(DICE, xr), yr), zr)
    end
  end
end
$rots.sort!.uniq!
$nrots = $rots.length
#p $rots; exit

loop do
  $n = gets.to_i
  break if $n == 0

  cnmhash = {}
  $nc = 0
  $dices = []
  
  $n.times do
    cnms = gets.strip.split
    $dices << cnms.map do |cnm|
      if cnmhash[cnm].nil?
        cnmhash[cnm] = $nc
        $nc += 1
      end
      cnmhash[cnm]
    end
  end
  #p $nc
  #p $dices

  p min_repaint
end