// #define DEBUG

using System;

public class Program
{
  const int NMAX = 505;
  const int CMAX = 32;
  const int INF = (int)1e9;
  private static int[,] dat, D;
  private static int N,C;
  private static int[] dp0, dp1, dp2;

  private static int ans = INF;
  static int colorMod(int i, int c0, int c1, int c2){
    switch(i % 3){
      case 0:
      return c0;
      case 1:
      return c1;
      case 2:
      return c2;
      default:
      Console.WriteLine("error: Wrong Modular");
      return -1;
    }
  }
  static int calcDiff(int c0, int c1, int c2){
    if(dp0[c0] != -1 && dp1[c1] != -1 && dp2[c2] != -1)
      return dp0[c0] + dp1[c1] + dp2[c2];
    int ret = 0;
    dp0[c0] = dp1[c1] = dp2[c2] = 0;
    for(int i = 1; i <= N; ++i){
      for(int j = 1; j <= N; ++j)
        {
          // Console.WriteLine("dat[{0}, {1}] => {2}", i, j, dat[i, j]);
          // Console.WriteLine("colorMod[{0}, {1}, {2}, {3}] => {4}",
           // i + j, c0, c1, c2, colorMod(i + j, c0, c1, c2));
          int mod = colorMod(i + j, c0, c1, c2);
          ret += D[dat[i, j], mod];
          switch((i + j) % 3){
            case 0:
            dp0[c0] += D[dat[i ,j], mod];
            break;
            case 1:
            dp1[c1] += D[dat[i ,j], mod];
            break;
            case 2:
            dp2[c2] += D[dat[i ,j], mod];
            break;
          }
        }
    }
    #if DEBUG
        Console.WriteLine("calcDiff({0}, {1}, {2}) => {3}",
          c0, c1, c2, ret);
    #endif
    return ret;
  }

  static void solve(){
    // Console.WriteLine("solve() begin N: {0}", N);
    dp0 = new int[C + 1];
    dp1 = new int[C + 1];
    dp2 = new int[C + 1];
    for(int i = 0; i <= C; ++i) dp0[i] = dp1[i] = dp2[i] = -1;

    for(int i = 1; i <= C; ++i){
      for(int j = i + 1; j <= C; ++j){
        for(int k = j + 1; k <= C; ++k)
        {
          ans = Math.Min(ans, calcDiff(i, j, k));
          ans = Math.Min(ans, calcDiff(i, k, j));
          ans = Math.Min(ans, calcDiff(j, i, k));
          ans = Math.Min(ans, calcDiff(j, k, i));
          ans = Math.Min(ans, calcDiff(k, i, j));
          ans = Math.Min(ans, calcDiff(k, j, i));
        }
      }
    }
  }
  static void debug(){
    Console.WriteLine("D: ");
    for(int i = 0; i < C; ++i){
      for(int j = 0; j < C; ++j){
        Console.Write("{0} ", D[i+1, j+1]);
      }
      Console.Write("\n");
    }
    Console.WriteLine("dat: ");
    for(int i = 0; i <= N + 1; ++i){
      for(int j = 0; j <= N + 1; ++j){
        Console.Write("{0} ", dat[i, j]);
      }
      Console.Write("\n");
    }
    Console.WriteLine("ans: ");
  }

  static public void Main ()
  {
    String[] NC = Console.ReadLine().Split(' ');
    N = int.Parse(NC[0]);
    C = int.Parse(NC[1]);

    D = new int[C + 1, C + 1];
    for(int i  = 0; i < C; ++i){
      String[] Di = Console.ReadLine().Split(' ');
      for(int j = 0; j < C; ++j){
        D[i + 1, j + 1] = int.Parse(Di[j]);
      }
    }

    dat = new int[N + 2, N + 2];
    for(int i  = 0; i < N; ++i){
      String[] ci = Console.ReadLine().Split(' ');
      for(int j = 0; j < N; ++j){
        dat[i + 1, j + 1] = int.Parse(ci[j]);
      }
    }
    solve();
    #if DEBUG
      debug();
    #endif
    Console.WriteLine (ans);
  }
}

Measured memory_bytes: 18376