#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
#define INF 1e18
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;
int D[40][40];
int C[510][510];
signed main()
{
    int n, c;
    cin >> n >> c;
    vector<int> mp0(c+1,0), mp1(c+1,0), mp2(c+1,0);
    for (int i = 1; i <= c; i++)
    {
        for (int j = 1; 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];
           
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            
            if ((i + j) % 3 == 0)
            {
                mp0[C[i][j]]++;
            }
            if ((i + j) % 3 == 1)
            {
                mp1[C[i][j]]++;
            }
            if ((i + j) % 3 == 2)
            {
                mp2[C[i][j]]++;
            }
        }
    }
    vector<pair<int, int>> v0, v1, v2;
   // show(mp1);
    for (int i = 1; i <= c; i++)
    {
        int cnt = 0;
        for (int j = 1; j <=c; j++)
        {
           
            cnt += mp0[j]*D[j][i];
        }
        v0.push_back({cnt, i});
    }
    for (int i = 1; i <= c; i++)
    {
        int cnt = 0;
        for (int j = 1; j <=c; j++)
        {
            
            cnt += mp1[j]*D[j][i];
        }
        v1.push_back({cnt, i});
    }
    for (int i = 1; i <= c; i++)
    {
        int cnt = 0;
        for (int j = 1; j <=c; j++)
        {
            if (mp2[j] == 0)
            {
                continue;
            }
            cnt += mp2[j]*D[j][i];
        }
        v2.push_back({cnt, i});
    }
    sort(ALL(v0));
    sort(ALL(v1));
    sort(ALL(v2));
   // reverse(ALL(v0));
   // reverse(ALL(v1));
    //reverse(ALL(v2));
  /*  for (int i = 0; i < v0.size(); i++)
    {
        cout << v0[i].first << " " << v0[i].second << endl;
    }
    for (int i = 0; i < v1.size(); i++)
    {
        cout << v1[i].first << " " << v1[i].second << endl;
    }
    for (int i = 0; i < v2.size(); i++)
    {
        cout << v2[i].first << " " << v2[i].second << endl;
    }*/
    int ans = INF;
    for (int i = 0; i < 3; i++)
    {
        
        for (int j = 0; j < 3; j++)
        {
            for (int k = 0; k < 3; k++)
            {
                int cnt = 0;
                if (v0[i].second != v1[j].second && v1[j].second != v2[k].second && v0[i].second != v2[k].second)
                {
                    //cerr << v0[i].second << " " << v1[j].second << " " << v2[k].second << endl;
                   
                    cnt = v0[i].first + v1[j].first + v2[k].first;
                   // cerr << cnt << endl;
                    chmin(ans, cnt);
                }
            }
            
        }
        
    }
    cout << ans << endl;
    
    return 0;
}

Measured memory_bytes: 2304