using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;
using TextReader = System.IO.TextReader;
using System.Text;

namespace AtCoderProject
{
    public class Program
    {
        public object Calc()
        {
            var N = consoleReader.Int;
            var C = consoleReader.Int;

            var diffs = consoleReader.Repeat(C).Select(cr => cr.Split.Int).ToArray();
            var colors = consoleReader.Repeat(N).Select(cr => cr.Split.Int.Select(i => i - 1).ToArray()).ToArray();

            var grouped = new[] { new List<int>(), new List<int>(), new List<int>() };
            for (int i = 0; i < N; i++)
                for (int j = 0; j < N; j++)
                {
                    grouped[(i + j) % 3].Add(colors[i][j]);
                }

            var costs = new int[][] { new int[C], new int[C], new int[C] };
            for (int c = 0; c < C; c++)
                for (int i = 0; i < 3; i++)
                    foreach (var g in grouped[i])
                        costs[i][c] += diffs[g][c];
            var result = costs.Select(g => g.Select((v, i) => new KeyValuePair<int, int>(i, v)).OrderBy(p => p.Value).Take(3).ToArray()).ToArray();
            var min = int.MaxValue;
            foreach (var item0 in result[0])
                foreach (var item1 in result[1])
                    foreach (var item2 in result[2])
                        if (item0.Key != item1.Key && item0.Key != item2.Key && item1.Key != item2.Key)
                        {
                            min = Math.Min(item0.Value + item1.Value + item2.Value, min);
                        }
            return min;
        }

        #region いつもの
        private readonly ConsoleReader consoleReader;
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program(new ConsoleReader(Console.In)).Calc()); static string AllLines<T>(IEnumerable<T> source) => string.Join("\n", source);
        #endregion
    }

#pragma warning disable
    public class ConsoleReader
    {
        private string[] line = Array.Empty<string>();
        private int linePosition;
        private TextReader textReader;
        public ConsoleReader(TextReader tr) { textReader = tr; }
        public int Int => int.Parse(String);
        public long Long => long.Parse(String);
        public double Double => double.Parse(String);
        public string String
        {
            get
            {
                if (linePosition >= line.Length)
                {
                    linePosition = 0;
                    line = textReader.ReadLine().Split();
                }
                return line[linePosition++];
            }
        }
        public class SplitLine
        {
            private string[] splited;
            public SplitLine(ConsoleReader cr) { splited = cr.textReader.ReadLine().Split(); }
            public int[] Int => String.Select(x => int.Parse(x)).ToArray();
            public long[] Long => String.Select(x => long.Parse(x)).ToArray();
            public double[] Double => String.Select(x => double.Parse(x)).ToArray();
            public string[] String => splited;
        }
        public SplitLine Split => new SplitLine(this);
        public class RepeatReader
        {
            ConsoleReader cr; int count;
            public RepeatReader(ConsoleReader cr, int count) { this.cr = cr; this.count = count; }
            public IEnumerable<T> Select<T>(Func<ConsoleReader, T> func) { for (int i = 0; i < count; i++) yield return func(cr); }
            public IEnumerable<string> String { get { for (int i = 0; i < count; i++) yield return cr.String; } }
            public IEnumerable<int> Int { get { for (int i = 0; i < count; i++) yield return cr.Int; } }
            public IEnumerable<double> Double { get { for (int i = 0; i < count; i++) yield return cr.Double; } }
        }
        public RepeatReader Repeat(int count) => new RepeatReader(this, count);
    }
}

Measured memory_bytes: 19916