//use itertools::Itertools;
use std::cmp;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::io::Read;
use std::usize::MAX;

macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() };input_inner!{next, $($r)*}};($($r:tt)*) => {let stdin = std::io::stdin();let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));let mut next = move || -> String{bytes.by_ref().map(|r|r.unwrap() as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect()};input_inner!{next, $($r)*}};}
macro_rules! input_inner {($next:expr) => {};($next:expr, ) => {};($next:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($next, $t);input_inner!{$next $($r)*}};}
macro_rules! read_value {($next:expr, ( $($t:tt),* )) => {( $(read_value!($next, $t)),* )};($next:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()};($next:expr, chars) => {read_value!($next, String).chars().collect::<Vec<char>>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, [ $t:tt ]) => {{let len = read_value!($next, usize);(0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()}};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};}

fn solve() {
    input! {
        n: usize,
        c: usize,
        disconfort: [[usize;c];c],
        grid: [[usize;n];n]
    }

    let mut ans = 0;

    // (MAX-コスト,コスト,色)
    let mut mod0Colors: Vec<usize> = Vec::new();
    let mut mod1Colors: Vec<usize> = Vec::new();
    let mut mod2Colors: Vec<usize> = Vec::new();

    for k in 0..c {
        let mut cost0 = 0;
        let mut cost1 = 0;
        let mut cost2 = 0;
        for i in 0..n {
            for j in 0..n {
                if (i + j + 2) % 3 == 0 {
                    cost0 += disconfort[grid[i][j] - 1][k];
                } else if (i + j + 2) % 3 == 1 {
                    cost1 += disconfort[grid[i][j] - 1][k];
                } else {
                    cost2 += disconfort[grid[i][j] - 1][k];
                }
            }
        }
        mod0Colors.push(cost0);
        mod1Colors.push(cost1);
        mod2Colors.push(cost2);
    }

    //println!("{:?} {:?} {:?}", mod0Colors, mod1Colors, mod2Colors);

    let mut min_cost = MAX;
    for i in 0..c {
        for j in i + 1..c {
            for k in j + 1..c {
                min_cost = cmp::min(min_cost, mod0Colors[i] + mod1Colors[j] + mod2Colors[k]);
                min_cost = cmp::min(min_cost, mod0Colors[i] + mod2Colors[j] + mod1Colors[k]);
                min_cost = cmp::min(min_cost, mod1Colors[i] + mod0Colors[j] + mod2Colors[k]);
                min_cost = cmp::min(min_cost, mod1Colors[i] + mod2Colors[j] + mod0Colors[k]);
                min_cost = cmp::min(min_cost, mod2Colors[i] + mod0Colors[j] + mod1Colors[k]);
                min_cost = cmp::min(min_cost, mod2Colors[i] + mod1Colors[j] + mod0Colors[k]);
            }
        }
    }

    println!("{}", min_cost);
}

fn main() {
    let thd = std::thread::Builder::new().stack_size(104_857_600);
    thd.spawn(|| solve()).unwrap().join().unwrap();

    /*
    // 入力を一括で読み込む場合
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut input = buf.split_whitespace();

    // inputに対しnext()で読み込んでいく
    let q: usize = input.next().unwrap().parse().unwrap();
    */
}

const LARGE_PRIME: u64 = 1_000_000_007;

// @〜でsnippet


Measured memory_bytes: 10620