#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";}
typedef long long ll;
#define INF 1e9
int d[40][40];
int c[510][510];
signed main(){
    int N,C;
    cin>>N>>C;
    for (int i = 0; i < C; i++)
    {
        for (int j = 0; j < C; j++)
        {
            cin>>d[i][j];
        }
    }
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            cin>>c[i][j];
            c[i][j]--;
        }
    }
    //3でわりきれるところをx,1余るところをy,2余るところをzに変える
    //それぞれ行う
  
    vector<int>v={0,1,2};
    ll ans=INF;
    
    do{
       // show(v);
          ll cnt_x=INF,cnt_y=INF,cnt_z=INF;
        int line_x,line_y;
    for (int x = 0; x < C; x++)
    {   
       ll cnt=0;
       for (int i = 0; i < N; i++)
       {
           for (int j = 0; j < N; j++)
           {
               if((i+j)%3==v[0]){
                   cnt+=d[c[i][j]][x];
               }
    
           }
       }
       if(cnt<cnt_x) {
           cnt_x=cnt;
           line_x=x;
       }
    }
        
      for (int y = 0; y < C; y++)
    {   if(y==line_x) continue;
        ll cnt=0;
       for (int i = 0; i < N; i++)
       {
           for (int j = 0; j < N; j++)
           {
               if((i+j)%3==v[1]){
                   cnt+=d[c[i][j]][y];
               }
           }
       }
        if(cnt<cnt_y) {
           cnt_y=cnt;
           line_y=y;
       }
    } 
    
      for (int z = 0; z < C; z++)
    {   if(z==line_x || z==line_y) continue;
       ll cnt=0;
       for (int i = 0; i < N; i++)
       {
           for (int j = 0; j < N; j++)
           {    
               if((i+j)%3==v[2]){
                   cnt+=d[c[i][j]][z];
               }
            
           }
       }
       if(cnt<cnt_z) cnt_z=cnt;
    }
    ans=min(ans,cnt_x+cnt_y+cnt_z);
   // cerr<<cnt_x<<cnt_y<<cnt_z<<endl;
   // cerr<<line_x<<" "<<line_y<<endl;
    } while(next_permutation(ALL(v)));
    cout<<ans<<endl;
   
    

    return 0;
}

Measured memory_bytes: 2304