#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;

template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;

class MinimumCostFlow {
    struct Edge {
        const int to;         // 行き先のノードid
        int flow;             // 流量
        const int cap;        // 容量
        const int cost;       // コスト
        const int rev;        // 逆辺のノードid
        const bool is_rev;    // 逆辺かどうか
        Edge(int to, int flow, int cap, int cost, int rev, bool is_rev) : to(to), flow(flow), cost(cost), cap(cap), rev(rev), is_rev(is_rev) {
            assert(this->cap >= 0);
        }
    };

    const int num_node;                   // 頂点数
    std::vector<std::vector<Edge>> graph; // グラフの隣接リスト表現

public:
    MinimumCostFlow(int num_node) : num_node(num_node) {
        graph.resize(num_node);
    }

    // fromからtoへ向かう容量cap、コストcostの辺をグラフに追加する
    void add_edge(int from, int to, int cap, int cost) {
        graph.at(from).emplace_back(Edge(to, 0, cap, cost, graph.at(to).size(), false));
        graph.at(to).emplace_back(Edge(from, cap, cap, -cost, graph.at(from).size() - 1, true));
    }

    // sからtへの流量fの最小費用流を求める
    // 流せない場合は-1を返す
    int min_cost_flow(int source, int sink, int f) {
        int res = 0;

        std::vector<int> prev_v(num_node, 0), prev_e(num_node, 0); // 直前の頂点と辺のidx
        std::vector<int> potential(num_node, 0);                   // ポテンシャル

        // 負の辺がある場合、ポテンシャルをベルマンフォードで求めておく
        // 負の辺がない場合はこの処理をskipしていい
        if (0 < f) {
            potential.assign(num_node, INT_MAX);
            potential.at(source) = 0;
            while (true) {
                bool updated = false;
                for (int v = 0; v < num_node; ++v) {
                    for (auto &e : graph.at(v)) {
                        if (e.cap - e.flow > 0) {
                            if (potential.at(v) == INT_MAX) {
                                continue;
                            }
                            if (potential.at(e.to) > potential.at(v) + e.cost) {
                                potential.at(e.to) = potential.at(v) + e.cost;
                                updated = true;
                            }
                        }
                    }
                }
                if (not updated) {
                    break;
                }
            }
        }

        while (f > 0) {
            // ダイクストラ法を用いてポテンシャルを更新する
            std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> que;
            std::vector<int> distance(num_node, INT_MAX);      // 最短距離

            distance.at(source) = 0;
            que.push(std::make_pair(0, source));
            while (not que.empty()) {
                std::pair<int, int> p = que.top(); // firstは最短距離、secondは頂点の番号
                que.pop();
                int v = p.second;
                if (distance.at(v) < p.first) {
                    continue;
                }
                for (int i = 0; i < graph.at(v).size(); ++i) {
                    Edge &e = graph.at(v).at(i);
                    if (e.cap - e.flow > 0 and distance.at(e.to) > distance.at(v) + e.cost + potential.at(v) - potential.at(e.to)) {
                        distance.at(e.to) = distance.at(v) + e.cost + potential.at(v) - potential.at(e.to);
                        prev_v.at(e.to) = v;
                        prev_e.at(e.to) = i;
                        que.push(std::make_pair(distance.at(e.to), e.to));
                    }
                }
            }

            // これ以上流せない
            if (distance[sink] == INT_MAX) {
                return -1;
            }
            for (int v = 0; v < num_node; ++v) {
                potential.at(v) += distance.at(v);
            }

            // source-sink間で流せる量を求める
            int d = f;
            for (int v = sink; v != source; v = prev_v.at(v)) {
                auto &e = graph.at(prev_v.at(v)).at(prev_e.at(v));
                d = std::min(d, e.cap - e.flow);
            }

            f -= d;
            res += d * potential.at(sink);
            for (int v = sink; v != source; v = prev_v.at(v)) {
                Edge &e = graph.at(prev_v.at(v)).at(prev_e.at(v));
                e.flow += d;
                graph.at(v).at(e.rev).flow -= d;
            }
        }

        return res;
    }

    // 水の流れたエッジを取得する
    std::set<std::tuple<int, int, int>> get_used_edges() {
        std::set<std::tuple<int, int, int>> used_edges;
        for (int from = 0; from < graph.size(); ++from) {
            for (Edge edge : graph.at(from)) {
                bool is_rev = edge.is_rev;
                int to = edge.to;

                if (not is_rev and edge.flow > 0) {
                    used_edges.insert(std::make_tuple(from , to, edge.flow));
                }
            }
        }
        return used_edges;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N, C;
    cin >> N >> C;
    vector<vector<int>> D(C, vector<int>(C));
    vector<vector<int>> c(N, vector<int>(N));
    FOR(i, 0, C) {
        FOR(j, 0, C) {
            cin >> D[i][j];
        }
    }
    FOR(i, 0, N) {
        FOR(j, 0, N) {
            cin >> c[i][j];
        }
    }

    MinimumCostFlow mcf(3 + C + 2);
    int source = 3 + C;
    int sink = source + 1;

    FOR(a, 0, C) {
        vector<int> d(3);
        FOR(i, 0, N) {
            FOR(j, 0, N) {
                d[(i + j) % 3] += D[c[i][j] - 1][a];
            }
        }
        FOR(i, 0, 3) {
            mcf.add_edge(i, a + 3, 1, d[i]);
        }
    }

    FOR(i, 0, 3) {
        mcf.add_edge(source, i, 1, 0);
    }
    FOR(i, 0, C) {
        mcf.add_edge(i + 3, sink, 1, 0);
    }

    print(mcf.min_cost_flow(source, sink, 3));
    return 0;
}

Measured memory_bytes: 1280