#include <bits/stdc++.h> 

using namespace std;

#define rep(i,n) for(int (i)=0;(i)<n;(i)++)
#define rep1(i,n) for(int (i)=1;(i)<=n;(i)++)
#define repU(i,bottom,ceiling) for(int (i)=(bottom);(i)<=(ceiling);(i)++)
#define repD(i,ceiling,bottom) for(int (i)=(ceiling);(i)>=(bottom);(i)--)
#define pub push_back
#define pob pop_back
#define prf printf
#define scf scanf
#define vci(v) for(int i=0;i<v.size();i++) cin>>v[i]
#define vco(v) for(int i=0;i<v.size();i++){ cout<<v[i]<<' '; } cout<<endl
#define mkp make_pair
#define fir first
#define sec second
#define clr(a) memset((a),0,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<double> vdb;
typedef vector<vector<double>> vvdb;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
typedef vector<pair<int,int>> vpii;
typedef pair<int,int> pii;

const string Alphabet("abcdefghijklmnopqrstuvwxyz");
const vector<pair<int,int>> Dir={{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,1},{-1,-1},{1,-1}};
const long double Pi=3.141592653589793238462643383279502884197;
const long double Napier=2.71828182845904523536;
const int Inf=(int)2e9+1e8;
const int Mod=(int)1e9+7;

template<typename T,typename U>                                                   
inline pair<T,U> operator+(const pair<T,U> & l,const pair<T,U> & r) {   
    return {l.fir+r.fir,l.sec+r.sec};                                    
} 

template<typename T,typename U> 
inline pair<T,U> operator-(const pair<T,U> & l,const pair<T,U> & r) {   
    return {l.fir-r.fir,l.sec-r.sec};                                    
} 

template<typename T,typename U>                                                   
inline pair<T,U> operator*(const pair<T,U> & l,const pair<T,U> & r) {   
    return {l.fir*r.fir,l.sec*r.sec};                                    
}

template<typename T,typename U>                                                   
inline pair<T,U> operator/(const pair<T,U> & l,const pair<T,U> & r) {   
    return {l.fir/r.fir,l.sec/r.sec};                                    
}

template<typename T> inline T gcd(T a,T b){ if(a%b){ return gcd(b,a%b); }else{ return b; } }
template<typename T> inline T lcm(T a,T b){ return a/gcd(a,b)*b; }

template<typename T> inline T Mpow(T n,T m,T mod){
	long long res=1,pow=n;
	while(m){
		if(m&1) res=(res*pow)%mod;
		pow=(pow*pow)%mod;
		m>>=1;
	}
	return res;
}

pair<int,int> Thre;
vector<int> v;

template<typename T> inline bool judge(T n){ return (Thre.first<=v[n]&&Thre.sec>=v[n]); }

template<typename T> inline T BiS(T bottom,T ceiling,bool thretype){
	T upper=ceiling+thretype;
	T lower=bottom-(!thretype);
	while(upper-lower>1){
		T mid=(upper+lower)/2;
		if(judge(mid)) (thretype?lower:upper)=mid;
		else (thretype?upper:lower)=mid;
	}
	return (thretype?lower:upper);
}

template<class T,class U> inline auto LB(T& v,U in){ return lower_bound(v.begin(),v.end(),in); }
template<class T,class U> inline auto UB(T& v,U in){ return upper_bound(v.begin(),v.end(),in); }

int N,C;
vector<int> D[30];
vi c[500];
int S[3][30]={{}};

inline void solve(){
	rep(r,3){
		rep(i,C){
			rep(j,N)rep(k,N) if((k+j)%3==r) S[r][i]+=D[c[j][k]-1][i];
		}
	}

	int ans=Inf;
	rep(i,C){
		rep(j,C){
			if(j==i) continue;
			rep(k,C){
				if(i==k||j==k) continue;
				ans=min(ans,S[0][i]+S[1][j]+S[2][k]);
			}
		}
	}
	cout<<ans<<endl;
	return;
}

inline void input(){
	cin>>N>>C;
	rep(i,C)rep(j,C){int d; cin>>d; D[i].pub(d); }
	rep(i,N)rep(j,N){int n; cin>>n; c[i].pub(n); }
	return;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	input();
	solve();
	return 0;
}



 

Measured memory_bytes: 1280