/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <iomanip>
#include <limits>//setprecision
//#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
//#include <unordered_set> //unordered_set
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
#define endl "\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
const int MOD = 1000000007;
typedef pair<long long,long long> P;

void chmax(long long &a,long long b){if(a < b) a = b;}
void chmin(long long &a,long long b){if(a > b) a = b;}

//Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
    if (M == 0) return 1;
    long long res = mod_pow((N * N) % MOD, M / 2);
    //When end-of-a bit is 1, times simple N.
    if (M & 1) res = (res * N) % MOD;
    return res %= MOD;
}
long long gcd(long long a, long long b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b ;
}



int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int N;cin >> N;
    int C;cin >> C;
    vector<vector<int>> wrongness(C,vector<int>(C));
    vector<vector<int>> ban(N,vector<int>(N));
    for(int i = 0;i<C;i++)for(int j = 0;j<C;j++) cin >> wrongness.at(i).at(j);
    for(int i = 0;i<N;i++)for(int j = 0;j<N;j++) cin >> ban.at(i).at(j),ban.at(i).at(j)--;

    vector<int> wrongnessSum_0(31);
    vector<int> wrongnessSum_1(31);
    vector<int> wrongnessSum_2(31);
    vector<vector<int>> GoodGrid(N,vector<int>(N,-1));
    //Good Gridを構築する
    for(int i = 0;i<N;i++){
        for(int j = 0;j<N;j++){
            if(i == 0 && j == 0){
                GoodGrid.at(0).at(0) = 2;
                continue;
            }
            if(j == 0){
                GoodGrid.at(i).at(j) = (GoodGrid.at(i-1).at(j)+1)%3;
            }
            else{
                GoodGrid.at(i).at(j) = (GoodGrid.at(i).at(j-1)+1)%3;
            }
        }
    }

    //余りが0,1,2のそれぞれでどの色に合わせたかによる違和感の総和を求める
    for(int remain = 0;remain<3;remain++){
        for(int c = 0;c<C;c++){
            int wrongnessSum = 0;
            for(int i = 0;i<N;i++){
                for(int j = 0;j<N;j++){
                    if(GoodGrid.at(i).at(j) != remain) continue;
                    wrongnessSum += wrongness.at(ban.at(i).at(j)).at(c);
                }
            }
            if(remain == 0){
                wrongnessSum_0.at(c) = wrongnessSum;
            }
            if(remain == 1){
                wrongnessSum_1.at(c) = wrongnessSum;
            }
            if(remain == 2){
                wrongnessSum_2.at(c) = wrongnessSum;
            }
        }
    }

    //違う色を選んで違和感の最小値を求める
    int ans = inf;
    for(int i = 0;i<C;i++){
        for(int j = 0;j<C;j++){
            for(int k = 0;k<C;k++){
                if(i == j || j == k || k == i) continue;
                ans = min(ans,wrongnessSum_0.at(i)+wrongnessSum_1.at(j)+wrongnessSum_2.at(k));
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 5288