#include <bits/stdc++.h>  // ver2.2.5
#define int long long
#define endl "\n"
#define ALL(v) (v).begin(),(v).end()
#define COUNT(a,i) upper_bound(ALL(a),i)-lower_bound(ALL(a),i)
#define Vi vector<int>
#define VVi vector<Vi>
#define Vs vector<string>
#define Pii pair<int,int>
#define VPii vector<Pii>
#define Tiii tuple<int,int,int>
#define PQi priority_queue<int>
#define PQir priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define itos to_string
#define stoi stoll
#define FI first
#define SE second
#define cYES cout<<"YES"<<endl
#define cNO cout<<"NO"<<endl
#define cYes cout<<"Yes"<<endl
#define cNo cout<<"No"<<endl
#define _ <<' '<<
#define sortr(v) sort(v,greater<>())
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repeq(i,a,b) for(int i=a;i<=b;i++)
#define repreq(i,a,b) for(int i=a;i>=b;i--)
#define leng(n) (int)(log10(n)+1)
#define dem(a,b) ((a+b-1)/(b))
#define Vin(a) rep(iI,0,a.size())cin>>a[iI]
#define Vout(a) rep(lZ,0,a.size()-1)cout<<a[lZ]<<' ';cout<<a.back()<<endl
#define VVout(a) rep(lY,0,a.size()){Vout(a[lY]);}
#define VPout(a) rep(lX,0,a.size())cout<<a[lX].FI<<' '<<a[lX].SE<<endl
#define Verr(a) rep(iZ,0,a.size()-1)cerr<<a[iZ]<<' ';cerr<<a.back()<<endl
#define VVerr(a) rep(iY,0,a.size()){Verr(a[iY]);}
#define VPerr(a) rep(iX,0,a.size())cerr<<a[iX].FI<<' '<<a[iX].SE<<endl
#define INF 3000000000000000000  //  3.0*10^18(MAXの1/3くらい)
#define MAX LLONG_MAX
#define MOD 1000000007  //  10^9 + 7
using namespace std;

int gcd(int a,int b){return b?gcd(b,a%b):a;}  //  最大公約数gcd
int lcm(int a,int b){return a/gcd(a,b)*b;}  //  最小公倍数lcm
void press(auto &v){v.erase(unique(ALL(v)),v.end());}  //  圧縮
int mypow(int x, int n, int m){  //  累乗x^n(mod m)  O(log(n))
if(n==0)return 1;if(n%2==0)return mypow(x*x%m,n/2,m);else return x*mypow(x,n-1,m)%m;}



signed main() {
  cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(12);
  int n,c;
  cin >> n >> c;
  VVi va(c,Vi(c));
  rep(i,0,c){
    rep(j,0,c){
      cin >> va[i][j];
    }
  }
  VVi grid(n,Vi(n));
  rep(i,0,n){
    rep(j,0,n){
      cin >> grid[i][j];
      grid[i][j]--;
    }
  }
  
  Vi s1(c),s2(c),s3(c);
  
  rep(i,0,n){
    rep(j,0,n){
      if((i+j)%3==0){
        s1[grid[i][j]]++;
      }
      if((i+j)%3==1){
        s2[grid[i][j]]++;
      }
      if((i+j)%3==2){
        s3[grid[i][j]]++;
      }
    }
  }
  
  Vi co1(c),co2(c),co3(c);
  
  int memo=0;
  rep(i,0,c){
    memo=0;
    rep(j,0,c){
      memo += va[j][i]*s1[j];
    }
    co1[i] = memo;
  }
  rep(i,0,c){
    memo=0;
    rep(j,0,c){
      memo += va[j][i]*s2[j];
    }
    co2[i] = memo;
  }rep(i,0,c){
    memo=0;
    rep(j,0,c){
      memo += va[j][i]*s3[j];
    }
    co3[i] = memo;
  }
  VPii p1(c),p2(c),p3(c);
  
  rep(i,0,c){
    p1[i] = mp(co1[i],i);
    p2[i] = mp(co2[i],i);
    p3[i] = mp(co3[i],i);
  }
  sort(ALL(p1));
  sort(ALL(p2));
  sort(ALL(p3));
  
  Vi Q(3),W(3),E(3),A(3),S(3),D(3);
  rep(i,0,3){
    Q[i] = p1[i].FI;
    W[i] = p2[i].FI;
    E[i] = p3[i].FI;
    A[i] = p1[i].SE;
    S[i] = p2[i].SE;
    D[i] = p3[i].SE;
  }
  
  int qw,er,ty,now,ans = INF;
  rep(i,0,27){
    now = i;
    qw = now%3;
    now /= 3;
    er = now%3;
    now /= 3;
    ty = now%3;
    
    cerr << Q[qw]+W[er]+E[ty] << endl;
    
    if(A[qw]==S[er] || A[qw]==D[ty] || S[er]==D[ty]) continue;
    else {
      
      ans = min(ans,Q[qw]+W[er]+E[ty]);
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 2304