using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
//using System.Text;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
//using System.Numerics;
//using static System.Math;
using pair = Pair<long, int>;

class Program
{
    static void Main()
    {
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    void chmax<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? b : a; }
    void chmin<T>(ref T a, T b) where T : IComparable<T> { a = a.CompareTo(b) < 0 ? a : b; }

    int N, C;
    int[,] D;
    int[,] A;

    void solve()
    {
        N = cin.nextint;
        C = cin.nextint;
        D = new int[C, C];
        for (int i = 0; i < C; i++)
        {
            for (int j = 0; j < C; j++)
            {
                D[i, j] = cin.nextint;
            }
        }

        var X = new List<int>[3];
        for (int i = 0; i < 3; i++)
        {
            X[i] = new List<int>();
        }

        A = new int[N, N];
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                A[i, j] = cin.nextint - 1;
                X[(i + j) % 3].Add(A[i, j]);
            }
        }

        var K = new pair[3][];
        for (int i = 0; i < 3; i++)
        {
            K[i] = new pair[C];
        }
        for (int i = 0; i < C; i++)
        {
            long tmp = 0;
            foreach (var item in X[0])
            {
                tmp += D[item, i];
            }
            K[0][i] = new pair(tmp, i);
        }

        for (int j = 0; j < C; j++)
        {
            long tm = 0;
            foreach (var item in X[1])
            {
                tm += D[item, j];
            }
            K[1][j] = new pair(tm, j);
        }

        for (int k = 0; k < C; k++)
        {
            long t = 0;
            foreach (var item in X[2])
            {
                t += D[item, k];
            }
            K[2][k] = new pair(t, k);
        }
        for (int i = 0; i < 3; i++)
        {
            Array.Sort(K[i]);
        }

        long ans = long.MaxValue;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    if (K[0][i].s == K[1][j].s || K[1][j].s == K[2][k].s || K[2][k].s == K[0][i].s) continue;
                    chmin(ref ans, K[0][i].f + K[1][j].f + K[2][k].f);
                }
            }
        }
        WriteLine(ans);
        


    }

}


class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U>
{
    public T f;
    public U s;
    public Pair(T f, U s)
    {
        this.f = f;
        this.s = s;
    }
    public int CompareTo(Pair<T, U> a)
    {
        return f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s);
    }
    public override string ToString()
    {
        return f + " " + s;
    }
}
class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan { get { return ReadLine().Split(); } }
    public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } }
    public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } }
    public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } }
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint { get { return int.Parse(next); } }
    public long nextlong { get { return long.Parse(next); } }
    public double nextdouble { get { return double.Parse(next); } }
}

Measured memory_bytes: 19412