#include <iostream>
#include <assert.h>
#include <algorithm>
#include <vector>
#include <map>

using orig_color_type = std::vector<std::vector<int32_t>>;
using diff_score_type = std::vector<std::vector<int32_t>>;
using target_color_type = std::vector<std::vector<std::pair<int32_t, int32_t>>>;
int32_t calc_score(int32_t c0, int32_t c1, int32_t c2, orig_color_type& col, diff_score_type& d){
    int32_t sum = 0;
    for(auto i : col[0]){
        sum += d[i-1][c0];
    }
    for(auto i : col[1]){
        sum += d[i-1][c1];
    }
    for(auto i : col[2]){
        sum += d[i-1][c2];
    }
    return sum;
}

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

    int32_t N, C;
    std::cin >> N >> C;
    orig_color_type orig_color(3);
    diff_score_type d(C, std::vector<int32_t>(C));

    for(int32_t i = 0; i < C; i++){
        for(int32_t j = 0; j < C; j++){
            std::cin >> d[i][j];
        }
    }

    for(int32_t i = 0; i < N; i++){
        for(int32_t j = 0; j < N; j++){
            int32_t orig;
            std::cin >> orig;
            orig_color[(i+j+2)%3].push_back(orig);
        }
    }

    constexpr int32_t INF = 1 << 30;
    target_color_type target_color(3, std::vector<std::pair<int32_t, int32_t>>(3));

    for(int32_t i = 0; i < 3; i++){
        int32_t first = INF, second = INF, third = INF;
        int32_t first_c, second_c, third_c;
        for(int32_t target_c = 0; target_c < C; target_c++){
            int32_t sum = 0;
            for(auto orig : orig_color[i]){
                sum += d[orig-1][target_c];
            }
            if(sum <= first){
                third = second;
                third_c = second_c;
                second = first;
                second_c = first_c;
                first = sum;
                first_c = target_c;
            }
            else if(sum <= second){
                third = second;
                third_c = second_c;
                second = sum;
                second_c = target_c;
            }
            else if(sum <= third){
                third = sum;
                third_c = target_c;
            }
        }
        target_color[i][0] = std::pair<int32_t, int32_t>(first_c, first);
        target_color[i][1] = std::pair<int32_t, int32_t>(second_c, second);
        target_color[i][2] = std::pair<int32_t, int32_t>(third_c, third);
    }

    int32_t min_score = INF;
    int32_t score = INF;
    for(int32_t c0 = 0; c0 < 3; c0++){
        for(int32_t c1 = 0; c1 < 3; c1++){
            if(target_color[0][c0].first == target_color[1][c1].first) continue;
            for(int32_t c2 = 0; c2 < 3; c2++){
                if(target_color[0][c0].first == target_color[2][c2].first) continue;
                if(target_color[1][c1].first == target_color[2][c2].first) continue;
                score = target_color[0][c0].second + target_color[1][c1].second + target_color[2][c2].second;
                min_score = std::min(score, min_score);
            }
        }
    }
    
    
    //for(int32_t c0 = 0; c0 < C; c0++){
    //    for(int32_t c1 = 0; c1 < C; c1++){
    //        if(c1 == c0) continue;
    //        for(int32_t c2 = 0; c2 < C; c2++){
    //            if(c2 == c1 || c2 == c0) continue;
    //            score = calc_score(c0, c1, c2, orig_color, d);
    //            min_score = std::min(score, min_score);
    //        }
    //    }
    //}

    std::cout << min_score << std::endl;
    return 0;

}


Measured memory_bytes: 1516