output_description: Print the first three characters of the label of the N-th round of AtCoder
  Beginner Contest.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
attempt: # import time
  N, C = map(int, input().split())  # 同上。map関数を使うとこのように書ける
  
  D = [[0 for i in range(C)] for j in range(C)]
  c = [[0 for i in range(N)] for j in range(N)]
  # iwakan = [[[0 for i in range(N)] for j in range(N)] for k in range(C)]
  
  for i in range(C):
      D[i] = list(map(int, input().split()))
  
  for i in range(N):
      c[i] = list(map(int, input().split()))
  
  # まず違和感リストを作る。
  # for i in range(C):
  #     for j in range(N):
  #         for k in range(N):
  #             iwakan[i][j][k] = D[c[j][k] - 1][i]
  
  """
  Gij0 : (i + j) % 3 == 0 となる場所
  Gij1 : (i + j) % 3 == 1 となる場所
  Gij2 : (i + j) % 3 == 2 となる場所
  とした時に、
  Gijnに属する場所全ての色をaに塗り替える場合の違和感の合計を
  iwakan[n][a]とする。
  iwakan[n][a]を全て求めるための計算量は、30*500*500 = 7500000
  iwakan を用いて全ての色の塗り替えの組み合わせを求めるための計算量は
  30 * 29 * 28 = 24360
  """
  
  # t1 = time.time()
  
  # Gijnに属する色の数を合計したリストを作る。
  numOfColors = [[0 for i in range(C)] for j in range(3)]
  for i in range(N):
      for j in range(N):
          if (i + j) % 3 == 0:
              numOfColors[0][c[i][j] - 1] = numOfColors[0][c[i][j] - 1] + 1
          if (i + j) % 3 == 1:
              numOfColors[1][c[i][j] - 1] = numOfColors[1][c[i][j] - 1] + 1
          if (i + j) % 3 == 2:
              numOfColors[2][c[i][j] - 1] = numOfColors[2][c[i][j] - 1] + 1
  
  iwakan = [[0 for i in range(C)] for j in range(3)]
  for i in range(3):
      for j in range(C):
          for a in range(C):
              iwakan[i][j] = iwakan[i][j] + numOfColors[i][a] * D[a][j]
  
  # t2 = time.time()
  
  sum_min = float("inf")
  for i in range(C):
      for j in range(C):
          for k in range(C):
              if (i == j) or (j == k) or (k == i):
                  pass
              else:
                  sum_tmp = iwakan[0][i] + iwakan[1][j] + iwakan[2][k]
                  if sum_min > sum_tmp:
                      sum_min = sum_tmp
  
  # t3 = time.time()
  
  print(sum_min)
  
  # elapsed_time1 = t2 - t1
  # elapsed_time2 = t3 - t2
  # print(f'time1: {elapsed_time1}')
  # print(f'time2: {elapsed_time2}')
  
problem_description: Statement
  
  Decades have passed since the beginning of AtCoder Beginner Contest.
  
  The contests are labeled as `ABC001`, `ABC002`, ... from the first round, but
  after the 999-th round `ABC999`, a problem occurred: how the future rounds
  should be labeled?
  
  In the end, the labels for the rounds from the 1000-th to the 1998-th are
  decided: `ABD001`, `ABD002`, ..., `ABD999`.
  
  You are given an integer N between 1 and 1998 (inclusive). Print the first
  three characters of the label of the N-th round of AtCoder Beginner Contest.
samples: [{"input": "999", "output": "ABC\n    \n\nThe 999-th round of AtCoder Beginner Contest is labeled as `ABC999`.\n\n* * *"}, {"input": "1000", "output": "ABD\n    \n\nThe 1000-th round of AtCoder Beginner Contest is labeled as `ABD001`.\n\n* * *"}, {"input": "1481", "output": "ABD\n    \n\nThe 1481-th round of AtCoder Beginner Contest is labeled as `ABD482`."}]