#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>


using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(ll i = 0; i < n; i++)




int main() {
    ll n, C;
    cin >> n >> C;
    
    vector<vector<ll>> d(C, vector<ll>(C, 0));
    REP(i, C){
        REP(j, C){
            cin >> d[i][j];
        }
    }
    
    vector<ll> a(C), b(C), c(C);
    REP(i, n){
        REP(j, n){
            ll t;
            cin >> t;
            t--;
            
            if((i + j) % 3 == 0){
                a[t]++;
            }
            else if((i + j) % 3 == 1){
                b[t]++;
            }
            else{
                c[t]++;
            }
        }
    }
    
    /*
    REP(i, C){
        cout << a[i] << ' ' << b[i] << ' ' << c[i] << endl;
    }
    */
    
    ll ans = INF;
    for(int i = 0; i < C - 2; i++){
        for(int j = i + 1; j < C - 1; j++){
            for(int k = j + 1; k < C; k++){
                ll sum = 0;
                REP(l, C){
                    if(l != i){
                        sum += a[l] * d[l][i];
                    }
                    if(l != j){
                        sum += b[l] * d[l][j];
                    }
                    if(l != k){
                        sum += c[l] * d[l][k];
                    }
                }
                // cout << sum << endl;
                ans = min(ans, sum);
                
                sum = 0;
                REP(l, C){
                    if(l != i){
                        sum += a[l] * d[l][i];
                    }
                    if(l != k){
                        sum += b[l] * d[l][k];
                    }
                    if(l != j){
                        sum += c[l] * d[l][j];
                    }
                }
                // cout << sum << endl;
                ans = min(ans, sum);
                
                sum = 0;
                REP(l, C){
                    if(l != j){
                        sum += a[l] * d[l][j];
                    }
                    if(l != i){
                        sum += b[l] * d[l][i];
                    }
                    if(l != k){
                        sum += c[l] * d[l][k];
                    }
                }
                // cout << sum << endl;
                ans = min(ans, sum);
                
                sum = 0;
                REP(l, C){
                    if(l != j){
                        sum += a[l] * d[l][j];
                    }
                    if(l != k){
                        sum += b[l] * d[l][k];
                    }
                    if(l != i){
                        sum += c[l] * d[l][i];
                    }
                }
                // cout << sum << endl;
                ans = min(ans, sum);
                
                sum = 0;
                REP(l, C){
                    if(l != k){
                        sum += a[l] * d[l][k];
                    }
                    if(l != i){
                        sum += b[l] * d[l][i];
                    }
                    if(l != j){
                        sum += c[l] * d[l][j];
                    }
                }
                // cout << sum << endl;
                ans = min(ans, sum);
                
                sum = 0;
                REP(l, C){
                    if(l != k){
                        sum += a[l] * d[l][k];
                    }
                    if(l != j){
                        sum += b[l] * d[l][j];
                    }
                    if(l != i){
                        sum += c[l] * d[l][i];
                    }
                }
                // cout << sum << endl;
                ans = min(ans, sum);
            }
        }
    }
    
    cout << ans << endl;
}

Measured memory_bytes: 256