#define rep(i,n) for (int i=0;i<(n);i++)
#define repr(i,n) for (int i=(n)-1;i>=0;i--)
#define rep2(i,s,n) for (int i=(s);i<(n);i++)
#define rep2r(i,s,n) for (int i=(n)-1;i>=(s);i--)

#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pcc pair<char,char>
#define pic pair<int,char>
#define pci pair<char,int>
#define pllll pair<ll,ll>
#define vs vector<string>
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define vd vector<double>
#define vc vector<char>
#define cd complex<double>
#define vcd vector<cd>

#define all(a) (a).begin(),(a).end()
#define range(a,i) (a).begin(),(a).begin()+i
#define repv(itr,v) for(auto itr=(v).begin();itr!=(v).end();itr++)

#define sint(a) int a; scanf("%d",&a);
#define sint2(a,b) int a,b; scanf("%d %d",&a,&b);
#define sint3(a,b,c) int a,b,c; scanf("%d %d %d",&a,&b,&c);
#define sint4(a,b,c,d) int a,b,c,d; scanf("%d %d %d %d",&a,&b,&c,&d);
#define sint5(a,b,c,d,e) int a,b,c,d,e; scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
#define sll(a) ll a; scanf("%lld",&a);
#define sll2(a,b) ll a,b; scanf("%lld %lld",&a,&b);
#define sll3(a,b,c) ll a,b,c; scanf("%lld %lld %lld",&a,&b,&c);
#define sll4(a,b,c,d) ll a,b,c,d; scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
#define sll5(a,b,c,d,e) ll a,b,c,d,e; scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e);
#define svi(v,n) vector<int> v(n); rep(i,n) scanf("%d", &v[i]);
#define svll(v,n) vector<ll> v(n); rep(i,n) scanf("%lld", &v[i]);
#define sdouble(x) double x; scanf("%lf",&x);
#define svd(v,n) vector<double> v(n); rep(i,n) scanf("%lf", &v[i]);
#define schar(x) char x; scanf(" %c", &x);
#define sstring(x) string x; cin >> x;

#define pint(a) printf("%d\n",a);
#define pint2(a,b) printf("%d %d\n",a,b);
#define pint3(a,b,c) printf("%d %d %d\n",a,b,c);

#define pll(a) printf("%lld\n",a);
#define pll2(a,b) printf("%lld %lld\n",a,b);
#define pll3(a,b,c) printf("%lld %lld %lld\n",a,b,c);

#define printvi(v) printf("[ ");repv(itr,v) printf("%d ",*itr);printf("]\n");
#define printvll(v) printf("[ ");repv(itr,v) printf("%lld ",*itr);printf("]\n");

#define ceili(x,y) (x)%(y)?(x)/(y)+1:(x)/(y)

#define MOD 1000000007

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#else
#pragma GCC diagnostic ignored "-Wunused-result"
#endif

#ifdef _MSC_VER
#define DEBUG
#endif

#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
#include <climits>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <complex>

using namespace std;



int main(int argc, char** argv) {
	sint2(N, C);
	vvi D = vvi(C);
	rep(i, C) {
		svi(v, C);
		D[i] = v;
	}
	vvi c = vvi(N);
	rep(i, N) {
		svi(v, N);
		c[i] = v;
	}
	vector<vector<pllll>> d = vector<vector<pllll>>(3);
	d[0] = vector<pllll>(C);
	d[1] = vector<pllll>(C);
	d[2] = vector<pllll>(C);
	rep(i, C) {
		d[0][i] = pllll(0, i);
		d[1][i] = pllll(0, i);
		d[2][i] = pllll(0, i);
		rep(x, N) {
			rep(y, N) {
				d[(x + y) % 3][i].first += D[c[x][y] - 1][i];
			}
		}
	}
	sort(d[0].begin(), d[0].end(), [](const pii& x, const pii& y) {return x.first < y.first; });
	sort(d[1].begin(), d[1].end(), [](const pii& x, const pii& y) {return x.first < y.first; });
	sort(d[2].begin(), d[2].end(), [](const pii& x, const pii& y) {return x.first < y.first; });

	ll	m = -1;
	rep(x, 3) {
		rep(y, 3) {
			rep(z, 3) {
				if (d[0][x].second == d[1][y].second) {
					continue;
				}
				if (d[1][y].second == d[2][z].second) {
					continue;
				}
				if (d[2][z].second == d[0][x].second) {
					continue;
				}
				ll q = d[0][x].first + d[1][y].first + d[2][z].first;
				if (m == -1 || m > q) {
					m = q;
				}
			}
		}
	}
	pll(m);
}


Measured memory_bytes: 1280