using System.Collections;
using System.Text;
using System.Threading.Tasks;
using static System.Math;
using static System.Console;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Numerics;
using System.IO.Pipes;
using System.Threading;
using System.Net;
using System.Runtime.Serialization;
using System.Globalization;
using System.Collections.Immutable;
using System.Data.SqlTypes;
using System.Security.Cryptography;
using System.Net.WebSockets;

class Program
{
    #region Reader

    static string ReadStr => Console.ReadLine();
    static string[] ReadStrs => Console.ReadLine().Split(' ');
    static int ReadInt => Convert.ToInt32(Console.ReadLine());
    static int[] ReadInts => Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray();
    static long ReadLong => Convert.ToInt64(Console.ReadLine());
    static long[] ReadLongs => Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray();
    static long[,] ReLongMatrix(int r, int c)
    {
        var mat = new long[r, c];
        for (int i = 0; i < r; ++i)
        {
            var x = ReadLongs;
            for (int j = 0; j < c; ++j)
            {
                mat[i, j] = x[j];
            }
        }
        return mat;
    }
    static Tuple<string, string>[] ReTupleStr(int n)
    {
        List<Tuple<string, string>> list = new List<Tuple<string, string>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadStrs;
            list.Add(new Tuple<string, string>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Tuple<long, long>[] ReTupleLong(int n)
    {
        List<Tuple<long, long>> list = new List<Tuple<long, long>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadLongs;
            list.Add(new Tuple<long, long>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Tuple<int, int>[] ReadTupleInt(int n)
    {
        List<Tuple<int, int>> list = new List<Tuple<int, int>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadInts;
            list.Add(new Tuple<int, int>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Tuple<double, double>[] ReTupleDouble(int n)
    {
        List<Tuple<double, double>> list = new List<Tuple<double, double>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadLongs;
            list.Add(new Tuple<double, double>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Dictionary<int, HashSet<int>> ReAdjacencyList(int N, int M)
    {
        Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
        for (int i = 0; i <= N; ++i)
        {
            dict.Add(i, new HashSet<int>());
        }
        for (int i = 0; i < M; ++i)
        {
            var ab = ReadInts;
            dict[ab[0]].Add(ab[1]);
            dict[ab[1]].Add(ab[0]);
        }
        return dict;
    }
    #endregion
    static void Swap<T>(ref T a, ref T b)
    {
        T temp = a;
        a = b;
        b = temp;
    }
    static int mod = (int)Pow(10, 9) + 7;

 
    static void Main()
    {
        int[] NC = ReadInts;
        int N = NC[0];
        int C = NC[1];


        int[][] D_map = new int[C][];
        Dictionary<int,int>[] C_map = new Dictionary<int, int>[3] {new Dictionary<int, int>(),new Dictionary<int, int>(),new Dictionary<int, int>() };



        for (int i = 0; i < C; i++)
        {
            D_map[i] = ReadInts;
        }
        for (int i = 0; i < N; i++)
        {
            var line = ReadInts;

            for (int l = 0; l < line.Length; l++)
            {
                line[l]--;
            }

            for (int j = 0; j < N; j++)
            {
                var d = C_map[(i + j) % 3];

                if(d.ContainsKey(line[j]))
                {
                    d[line[j]]++;
                }
                else
                {
                    d.Add(line[j], 1);
                }
               
            }
        }

        long ans = 10000000000;
        for (int i = 0; i < C; i++)
        {
            for (int j = 0; j < C; j++)
            {
                if (i == j) continue;
                for (int k = 0; k < C; k++)
                {
                    if (i == k || j == k) continue;


                    long cost = 0;
                    int[] resultColor = { i, j, k };

                    for (int r = 0; r < 3; r++)
                    {
                        Dictionary<int, int> item = C_map[r];
                        foreach (var pair in item)
                        {
                            cost += D_map[pair.Key][resultColor[r]] * pair.Value;
                        }
                    }

                    ans = Min(ans, cost);

                }
            }
        }


        WriteLine(ans);
    }

    

}


Measured memory_bytes: 41784