#include <vector> 
#include <list> 
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <complex>
#include <assert.h>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;

typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(t,c) for(typeof((c).begin()) t=(c).begin(); t!=(c).end(); ++t)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(t,a,b) for(int t=(a);t<(b);++t)
#define REP(t,n)  FOR(t,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << '=' << (x) << endl;
#define debug(x) cerr << #x << '=' << (x) << '('<<'L' << __LINE__ << ')' << ' ' << __FILE__ << endl;



const LL mod = 1000000007;

LL gcd(LL a, LL b) { if (a%b == 0) { return b; } else return gcd(b, a%b); }
LL lcm(LL a, LL b) { return a / gcd(a, b)*b; }

#define Yes_ cout<<"Yes"<<endl; return 0;
#define No_ cout<<"No"<<endl; return 0;


#define YES_ cout<<"YES"<<endl; return 0;
#define NO_ cout<<"NO"<<endl; return 0;

#define POSSIBLE cout<<"POSSIBLE"<<endl; return 0;
#define IMPOSSIBLE cout<<"IMPOSSIBLE"<<endl; return 0;


class matrix : public vector< vector<int> > {
private:
	int tate_, yoko_;
public:
	matrix(const int tate__, const int yoko__) {
		assert(tate__ > 0); assert(yoko__ > 0);
		resize(tate__);
		for (size_t i = 0; i < size(); i++) { (*this)[i].resize(yoko__); }
		tate_ = tate__; yoko_ = yoko__;
	}
	matrix() {};
	int tate()const { return tate_; }
	int yoko()const { return yoko_; }
	void resizematrix(const int tate__, const int yoko__) {
		assert(tate__ > 0); assert(yoko__ > 0);

		resize(tate__);
		for (size_t i = 0; i < size(); i++) {
			(*this)[i].resize(yoko__);
		}
		tate_ = tate__; yoko_ = yoko__;
	}
	void setone() {
		for (int i = 0; i<tate(); i++)  for (int j = 0; j < yoko(); j++) { (*this)[i][j] = 1; }
	}
	void setzero() {
		for (int i = 0; i<tate(); i++) for (int j = 0; j < yoko(); j++) { (*this)[i][j] = 0; }
	}
	void setchar(char a) {
		for (int i = 0; i<tate(); i++) for (int j = 0; j < yoko(); j++) { (*this)[i][j] = a; }
	}
};
inline std::ostream & operator<<(std::ostream & os, const matrix & m)
{
	for (int j = 0; j < m.tate(); j++) {
		os << "[";
		for (int i = 0; i < m.yoko(); i++) {
			//os.std::setw(2);

			os <<m[j][i];
		}
		os << "]" << endl;
	}
	return os;
}
int main() {
	
	int N, C_;
	matrix D;
	matrix C;
	cin >> N >> C_;
	D.resizematrix(C_, C_);
	C.resizematrix(N, N);
	vector<PII > samec[3];
	int num[3][31] = {0};
	REP(i, C_)REP(j, C_) {

		cin >> D[i][j];
	}
	REP(i, N)REP(j, N) {
		cin >> C[i][j];
		C[i][j]--;
		samec[(i + j) % 3].push_back({ i,j });
		//num[(i+j)%3][ ]
	}
	LL min_iwakan = 100000000000;

	REP(i, 3) {
		REP(c, C_) {
			for (auto a : samec[i]) {
				num[i][c] += D[C[a.first][a.second]][c];
			}
		}
	}

	//vector<int> aa;
	/*REP(i, C_) {
		aa.push_back(i);
	}*/

	FOR(i,0,C_)FOR(j, 0, C_)FOR(k,0, C_) {
		if (i == j || j == k||k==i) { continue; }
		LL iwakan = 0;
		iwakan += num[0][i];
		iwakan += num[1][j];
		iwakan += num[2][k];
		
		min_iwakan = min(min_iwakan, iwakan);


	} 
	//while (next_permutation(ALL(aa)));
	cout << min_iwakan << endl;
}	


Measured memory_bytes: 3700