stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



int N, R;



int main () {

    cin >> N >>R;

    if (N >= 10) {

         cout << R << endl;

    } else {

         cout << R + 100*(10 - N) << endl;

    }

} 

// Faster C++ solution for the same problem (measured speedup: 5.269144289744394x):
#include <stdio.h>



int main(){

	int N,R;

	scanf("%d%d",&N,&R);

	if (N < 10){

		printf("%d",R+(100*(10-N)));

	}

	else if(N >= 10){

		printf("%d",R);

	}

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



int N, R;



int main() {

    cin>>N>>R;

    if(N>=10) {

        cout<<R<<endl;

    }

    else{

        cout<<R+100*(10-N)<<endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.269144289744394x):
#include <stdio.h>



int main(){

	int N,R;

	scanf("%d%d",&N,&R);

	if (N < 10){

		printf("%d",R+(100*(10-N)));

	}

	else if(N >= 10){

		printf("%d",R);

	}

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

using namespace std;





int main()

{

	int n,r;

	scanf("%d%d",&n,&r);

	int ans;

	if(n<10)

	{

		ans=r+(10-n)*100;

	}

	else ans=r;

	printf("%d",ans);

}

// Faster C++ solution for the same problem (measured speedup: 5.200647531987203x):
#include <stdio.h>



int main(){

	int N,R;

	scanf("%d%d",&N,&R);

	if (N < 10){

		printf("%d",R+(100*(10-N)));

	}

	else if(N >= 10){

		printf("%d",R);

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;

int main ()

{

	int n,r;

	while(cin>>n>>r)

	{

		int sum;

		if(n<10)

		{

			sum=r+100*(10-n);

			cout<<sum<<endl;

		}

		else

		{

			cout<<r<<endl;

		}

	}

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 5.262815184328819x):
#include <stdio.h>



int main(){

	int N, R;

	scanf("%d %d", &N, &R);

	

	if (N >= 10){

		printf("%d \n", R);

	}

	else{

		printf("%d \n", R + 100 * (10-N));

	} 

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int main()

{

	int a,b;

	cin>>a>>b;

	if(a<=10)

	{

		cout<<(b+100*(10-a))<<endl;

	}

	else cout<<b<<endl;

	return 0;

 } 

// Faster C++ solution for the same problem (measured speedup: 5.259891581778903x):
#include <stdio.h>



int main(){

	int N, R;

	scanf("%d %d", &N, &R);

	

	if (N >= 10){

		printf("%d \n", R);

	}

	else{

		printf("%d \n", R + 100 * (10-N));

	} 

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



int N, R;



int main () {

    cin >> N >>R;

    if (N >= 10) {

         cout << R << endl;

    } else {

         cout << R + 100*(10 - N) << endl;

    }

} 

// Faster C++ solution for the same problem (measured speedup: 5.259670484308486x):
#include <stdio.h>



int main(){

	int N, R;

	scanf("%d %d", &N, &R);

	

	if (N >= 10){

		printf("%d \n", R);

	}

	else{

		printf("%d \n", R + 100 * (10-N));

	} 

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



int N, R;



int main() {

    cin>>N>>R;

    if(N>=10) {

        cout<<R<<endl;

    }

    else{

        cout<<R+100*(10-N)<<endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.259670484308486x):
#include <stdio.h>



int main(){

	int N, R;

	scanf("%d %d", &N, &R);

	

	if (N >= 10){

		printf("%d \n", R);

	}

	else{

		printf("%d \n", R + 100 * (10-N));

	} 

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

using namespace std;





int main()

{

	int n,r;

	scanf("%d%d",&n,&r);

	int ans;

	if(n<10)

	{

		ans=r+(10-n)*100;

	}

	else ans=r;

	printf("%d",ans);

}

// Faster C++ solution for the same problem (measured speedup: 5.191296882213827x):
#include <stdio.h>



int main(){

	int N, R;

	scanf("%d %d", &N, &R);

	

	if (N >= 10){

		printf("%d \n", R);

	}

	else{

		printf("%d \n", R + 100 * (10-N));

	} 

	return 0;

}
// Slower C++ solution:
#include <iostream>

using namespace std;

int main ()

{

	int n,r;

	while(cin>>n>>r)

	{

		int sum;

		if(n<10)

		{

			sum=r+100*(10-n);

			cout<<sum<<endl;

		}

		else

		{

			cout<<r<<endl;

		}

	}

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 5.271731446149595x):
#include <stdio.h>



int main(){

	int K, R;

	scanf("%d %d", &K, &R);

	if(K<=10){

		printf("%d", (100*(10-K)+R));

	}else if(K>10){

		printf("%d", R);

	}

	

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int main()

{

	int a,b;

	cin>>a>>b;

	if(a<=10)

	{

		cout<<(b+100*(10-a))<<endl;

	}

	else cout<<b<<endl;

	return 0;

 } 

// Faster C++ solution for the same problem (measured speedup: 5.268802890432053x):
#include <stdio.h>



int main(){

	int K, R;

	scanf("%d %d", &K, &R);

	if(K<=10){

		printf("%d", (100*(10-K)+R));

	}else if(K>10){

		printf("%d", R);

	}

	

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



int N, R;



int main () {

    cin >> N >>R;

    if (N >= 10) {

         cout << R << endl;

    } else {

         cout << R + 100*(10 - N) << endl;

    }

} 

// Faster C++ solution for the same problem (measured speedup: 5.268581418378288x):
#include <stdio.h>



int main(){

	int K, R;

	scanf("%d %d", &K, &R);

	if(K<=10){

		printf("%d", (100*(10-K)+R));

	}else if(K>10){

		printf("%d", R);

	}

	

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



int N, R;



int main() {

    cin>>N>>R;

    if(N>=10) {

        cout<<R<<endl;

    }

    else{

        cout<<R+100*(10-N)<<endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.268581418378288x):
#include <stdio.h>



int main(){

	int K, R;

	scanf("%d %d", &K, &R);

	if(K<=10){

		printf("%d", (100*(10-K)+R));

	}else if(K>10){

		printf("%d", R);

	}

	

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

using namespace std;





int main()

{

	int n,r;

	scanf("%d%d",&n,&r);

	int ans;

	if(n<10)

	{

		ans=r+(10-n)*100;

	}

	else ans=r;

	printf("%d",ans);

}

// Faster C++ solution for the same problem (measured speedup: 5.200091977722602x):
#include <stdio.h>



int main(){

	int K, R;

	scanf("%d %d", &K, &R);

	if(K<=10){

		printf("%d", (100*(10-K)+R));

	}else if(K>10){

		printf("%d", R);

	}

	

	return 0;

}
// Slower C++ solution:
// Input N dan R.

// Jika N>=10, output R

// Jika tidak, output R - 100*(10-N)



#include <bits/stdc++.h>

using namespace std;



int N, R;



int main () {

    cin >> N >> R;

    if (N >= 10) {

        cout << R << endl;

    } else {

        cout << R + 100*(10-N) << endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.260145559150195x):
#include<stdio.h>

int main(){

	int n, r, t;

	scanf("%d %d", &n, &r);

	t = 100 * (10 - n);

	if(n<10){

		printf("%d\n", r+t);

	}

	else{

		printf("%d\n", r);

	}

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<cmath>

#include<iostream>

#include<algorithm>

using namespace std;

long long n,r;

int main()

{

	cin>>n>>r;

	if (n>=10)

	{

		cout<<r<<"\n";

	}

	else

	{

		long long a=100*(10-n);

		a+=r;

		cout<<a<<"\n";

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.260288179973092x):
#include<stdio.h>

int main(){

	int n, r, t;

	scanf("%d %d", &n, &r);

	t = 100 * (10 - n);

	if(n<10){

		printf("%d\n", r+t);

	}

	else{

		printf("%d\n", r);

	}

	return 0;

}
// Slower C++ solution:
// Input N dan R.

// Jika N>=10, output R

// Jika tidak, output R - 100*(10-N)



#include <bits/stdc++.h>

using namespace std;



int N, R;



int main () {

    cin >> N >> R;

    if (N >= 10) {

        cout << R << endl;

    } else {

        cout << R + 100*(10-N) << endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.268581418378288x):
#include <stdio.h>



int main() {

	int a, b, c, d;

	scanf("%d %d", &a, &b);

	

	if(a < 10) {

		c = 100 * (10 - a);

		d = b + c;

		printf("%d", d);

	} else {

		printf("%d", b);

	}

	

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<cmath>

#include<iostream>

#include<algorithm>

using namespace std;

long long n,r;

int main()

{

	cin>>n>>r;

	if (n>=10)

	{

		cout<<r<<"\n";

	}

	else

	{

		long long a=100*(10-n);

		a+=r;

		cout<<a<<"\n";

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.268724267926639x):
#include <stdio.h>



int main() {

	int a, b, c, d;

	scanf("%d %d", &a, &b);

	

	if(a < 10) {

		c = 100 * (10 - a);

		d = b + c;

		printf("%d", d);

	} else {

		printf("%d", b);

	}

	

	return 0;

}
// Slower C++ solution:
// Input N dan R.

// Jika N>=10, output R

// Jika tidak, output R - 100*(10-N)



#include <bits/stdc++.h>

using namespace std;



int N, R;



int main () {

    cin >> N >> R;

    if (N >= 10) {

        cout << R << endl;

    } else {

        cout << R + 100*(10-N) << endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.268581418378288x):
#include<stdio.h>



int main(){

	int n,r,sum;

	scanf("%d %d",&n,&r);

	sum=100*(10-n);

	if (10>=n){

		printf("%d",sum+r);

	}

	else if (n>10){

		printf("%d",r);

	}	

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<cmath>

#include<iostream>

#include<algorithm>

using namespace std;

long long n,r;

int main()

{

	cin>>n>>r;

	if (n>=10)

	{

		cout<<r<<"\n";

	}

	else

	{

		long long a=100*(10-n);

		a+=r;

		cout<<a<<"\n";

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.268724267926639x):
#include<stdio.h>



int main(){

	int n,r,sum;

	scanf("%d %d",&n,&r);

	sum=100*(10-n);

	if (10>=n){

		printf("%d",sum+r);

	}

	else if (n>10){

		printf("%d",r);

	}	

}
// Slower C++ solution:
// Input N dan R.

// Jika N>=10, output R

// Jika tidak, output R - 100*(10-N)



#include <bits/stdc++.h>

using namespace std;



int N, R;



int main () {

    cin >> N >> R;

    if (N >= 10) {

        cout << R << endl;

    } else {

        cout << R + 100*(10-N) << endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.268800031096668x):
#include <stdio.h>



int main()

{

	int c, dr; //c = contest, ir = inner rating, dr = displayed rating

	scanf("%d %d", &c, &dr);

	

	if(c >= 10)

{

	printf("%d", dr);

}	

	else

{

	printf("%d", dr+100*(10-c));

}

	return 0;	

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<cmath>

#include<iostream>

#include<algorithm>

using namespace std;

long long n,r;

int main()

{

	cin>>n>>r;

	if (n>=10)

	{

		cout<<r<<"\n";

	}

	else

	{

		long long a=100*(10-n);

		a+=r;

		cout<<a<<"\n";

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.268942886572369x):
#include <stdio.h>



int main()

{

	int c, dr; //c = contest, ir = inner rating, dr = displayed rating

	scanf("%d %d", &c, &dr);

	

	if(c >= 10)

{

	printf("%d", dr);

}	

	else

{

	printf("%d", dr+100*(10-c));

}

	return 0;	

}
// Slower C++ solution:
// Input N dan R.

// Jika N>=10, output R

// Jika tidak, output R - 100*(10-N)



#include <bits/stdc++.h>

using namespace std;



int N, R;



int main () {

    cin >> N >> R;

    if (N >= 10) {

        cout << R << endl;

    } else {

        cout << R + 100*(10-N) << endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.268581418378288x):
#include<stdio.h>



int main()

{

	int a,b,c;

	scanf("%d %d", &a, &b);

	if(a<10){

		c = 100 * (10-a);

		printf("%d", c+b);

	}

	else{

		printf("%d", b);

	}

	return 0;	

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<cmath>

#include<iostream>

#include<algorithm>

using namespace std;

long long n,r;

int main()

{

	cin>>n>>r;

	if (n>=10)

	{

		cout<<r<<"\n";

	}

	else

	{

		long long a=100*(10-n);

		a+=r;

		cout<<a<<"\n";

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.268724267926639x):
#include<stdio.h>



int main()

{

	int a,b,c;

	scanf("%d %d", &a, &b);

	if(a<10){

		c = 100 * (10-a);

		printf("%d", c+b);

	}

	else{

		printf("%d", b);

	}

	return 0;	

}
// Slower C++ solution:
// Input N dan R.

// Jika N>=10, output R

// Jika tidak, output R - 100*(10-N)



#include <bits/stdc++.h>

using namespace std;



int N, R;



int main () {

    cin >> N >> R;

    if (N >= 10) {

        cout << R << endl;

    } else {

        cout << R + 100*(10-N) << endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.26888900297218x):
#include <stdio.h>

int main()

{

	int N, R, v, z, x;

	scanf("%d %d", &N, &R);

	v = R + 100*(10-N);

	if ( N >= 10 ){

		printf("%d", R);

	}

	else {

		printf("%d", v);

	}

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<cmath>

#include<iostream>

#include<algorithm>

using namespace std;

long long n,r;

int main()

{

	cin>>n>>r;

	if (n>=10)

	{

		cout<<r<<"\n";

	}

	else

	{

		long long a=100*(10-n);

		a+=r;

		cout<<a<<"\n";

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.269031860860219x):
#include <stdio.h>

int main()

{

	int N, R, v, z, x;

	scanf("%d %d", &N, &R);

	v = R + 100*(10-N);

	if ( N >= 10 ){

		printf("%d", R);

	}

	else {

		printf("%d", v);

	}

	return 0;

}
// Slower C++ solution:
// Input N dan R.

// Jika N>=10, output R

// Jika tidak, output R - 100*(10-N)



#include <bits/stdc++.h>

using namespace std;



int N, R;



int main () {

    cin >> N >> R;

    if (N >= 10) {

        cout << R << endl;

    } else {

        cout << R + 100*(10-N) << endl;

    }

}

// Faster C++ solution for the same problem (measured speedup: 5.266521481047087x):
#include <stdio.h>



int main()

{

    int N,R,disp;

    scanf("%d%d", &N, &R);



    if (N >= 10)

    {

        disp = R;

    }

    else 

    {

        disp = R + (100 * (10-N));

    }

    printf("%d", disp);

    return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<cmath>

#include<iostream>

#include<algorithm>

using namespace std;

long long n,r;

int main()

{

	cin>>n>>r;

	if (n>=10)

	{

		cout<<r<<"\n";

	}

	else

	{

		long long a=100*(10-n);

		a+=r;

		cout<<a<<"\n";

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.266664274743379x):
#include <stdio.h>



int main()

{

    int N,R,disp;

    scanf("%d%d", &N, &R);



    if (N >= 10)

    {

        disp = R;

    }

    else 

    {

        disp = R + (100 * (10-N));

    }

    printf("%d", disp);

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<algorithm>

#include<cmath>

#include<map>

#include<cstring>

#include<climits>

#include<queue>

using namespace std;

#define MAXC 30

#define MAXN 500

int n,c,ans;

int D[MAXC+5][MAXC+5],C[MAXN+5][MAXN+5];

int cont[4][MAXC+5];

int main()

{

    ans=INT_MAX;

    scanf("%d%d",&n,&c);

    for(int i=1;i<=c;i++)

        for(int j=1;j<=c;j++)

        scanf("%d",&D[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=n;j++)

        scanf("%d",&C[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=n;j++)

        cont[(i+j)%3][C[i][j]]++;

    for(int i=1;i<=c;i++)

        for(int j=1;j<=c;j++)

        if(i!=j)

            for(int k=1;k<=c;k++)

            if(k!=i&&k!=j)

            {

                int res=0;

                for(int p=1;p<=c;p++)res+=cont[0][p]*D[p][i];

                for(int p=1;p<=c;p++)res+=cont[1][p]*D[p][j];

                for(int p=1;p<=c;p++)res+=cont[2][p]*D[p][k];

                ans=min(ans,res);

            }

    printf("%d",ans);

}


// Faster C++ solution for the same problem (measured speedup: 5.1050911883045496x):
#include<cstdio>

#include<algorithm>

#include<cstring>

#include<climits>

#define MAXN 500

int n,c,x,y,z,ans;

int d[MAXN+5][MAXN+5],C[MAXN+5][MAXN+5],t[4][MAXN+5];

inline void BaoSo()

{

	for(int i=1;i<=c;i++)

	{ 

        x=0; 

        for(int j=1;j<=c;j++)

		{

            x+=t[0][j]*d[j][i];

        }

        

        for(int j=1;j<=c;j++)

		{   

            if(j==i) continue;    

            y=0;

            for(int k=1;k<=c;k++)

			{

                y+=t[1][k]*d[k][j];

            }

            

            for(int k=1;k<=c;k++)

			{  

                if(k==j || k==i) continue;   

                z=0;

                for(int l=1;l<=c;l++)

				{

                    z+=t[2][l]*d[l][k];

                }

                if(ans>x+y+z)

				{   

                    ans=x+y+z;

                }

            }

        }

    } 

    printf("%d",ans);

} 

inline void Init()

{

	scanf("%d %d",&n,&c);

	

	for(int i=1;i<=c;i++)

		for(int j=1;j<=c;j++)

		{

			scanf("%d",&d[i][j]);

		}

		

	for(int i=1;i<=n;i++)

		for(int j=1;j<=n;j++)

		{

			scanf("%d",&C[i][j]);

			t[(i+j)%3][C[i][j]]++;

		}

	BaoSo();			

}

int main()

{

	ans=INT_MAX;

	Init();

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<algorithm>

#include<cmath>

#include<map>

#include<cstring>

#include<climits>

#include<queue>

using namespace std;

#define MAXC 30

#define MAXN 500

int n,c,ans;

int D[MAXC+5][MAXC+5],C[MAXN+5][MAXN+5];

int cont[4][MAXC+5];

int main()

{

    ans=INT_MAX;

    scanf("%d%d",&n,&c);

    for(int i=1;i<=c;i++)

        for(int j=1;j<=c;j++)

        scanf("%d",&D[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=n;j++)

        scanf("%d",&C[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=n;j++)

        cont[(i+j)%3][C[i][j]]++;

    for(int i=1;i<=c;i++)

        for(int j=1;j<=c;j++)

        if(i!=j)

            for(int k=1;k<=c;k++)

            if(k!=i&&k!=j)

            {

                int res=0;

                for(int p=1;p<=c;p++)res+=cont[0][p]*D[p][i];

                for(int p=1;p<=c;p++)res+=cont[1][p]*D[p][j];

                for(int p=1;p<=c;p++)res+=cont[2][p]*D[p][k];

                ans=min(ans,res);

            }

    printf("%d",ans);

}


// Faster C++ solution for the same problem (measured speedup: 5.093733624658164x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

const int MAXCOL=30;

int n_col,siz,spd[MAXCOL+5][MAXCOL+5];

int tot[3][MAXCOL+5];

int main()

{

	scanf("%d%d",&siz,&n_col);

	for(int i=0;i<n_col;i++)

		for(int j=0;j<n_col;j++)

			scanf("%d",&spd[i][j]);

	for(int i=0;i<siz;i++)

		for(int j=0;j<siz;j++)

		{

			int col;

			scanf("%d",&col);

			tot[(i+j+2)%3][col-1]++;

		}

	int ans=1e9;

	for(int i=0;i<n_col;i++)

	{

		int A=0;

		for(int col=0;col<n_col;col++)

			A+=tot[0][col]*spd[col][i];

		for(int j=0;j<n_col;j++)

			if(i!=j)

			{

				int B=0;

				for(int col=0;col<n_col;col++)

					B+=tot[1][col]*spd[col][j];

				for(int k=0;k<n_col;k++)

					if(i!=k && j!=k)

					{

						int C=0;

						for(int col=0;col<n_col;col++)

							C+=tot[2][col]*spd[col][k];

						ans=min(ans,A+B+C);

					}

			}

	}

	printf("%d\n",ans);

	return 0;

}
// Slower C++ solution:
#include<set> 

#include<map> 

#include<stack> 

#include<cmath> 

#include<cstdio> 

#include<queue> 

#include<vector> 

#include<climits> 

#include<cstring> 

#include<iostream> 

#include<algorithm> 

using namespace std; 

#define MAXC 30

#define MAXN 500

#define LL long long 

#define INF INT_MAX 

int read(){ 

    int f=1,x=0; 

    char c=getchar(); 

    while(c<'0'||'9'<c){if(c=='-')f=-1;c=getchar();} 

    while('0'<=c&&c<='9'){x=x*10+c-'0';c=getchar();} 

    return f*x; 

}

int c,d[MAXC+5][MAXC+5],cnt[3][MAXC+5],S[3][MAXC+5];

int change(int t,int x){

	int ret=0;

	for(int i=1;i<=c;i++)

		ret+=cnt[t][i]*d[i][x];

	return ret;

}

int main(){

	int n=read(),ans=INF;

	c=read();

	for(int i=1;i<=c;i++)

		for(int j=1;j<=c;j++)

			d[i][j]=read();

	for(int i=1;i<=n;i++)

		for(int j=1;j<=n;j++)

			cnt[(i+j)%3][read()]++;

	for(int t=0;t<=2;t++)

		for(int i=1;i<=c;i++) 

			S[t][i]=change(t,i);

	for(int i=1;i<=c;i++)

		for(int j=1;j<=c;j++)

			for(int k=1;k<=c;k++)

				if(i!=j&&j!=k&&i!=k){

					int tmp=S[0][i]+S[1][j]+S[2][k];

					if(tmp<ans) ans=tmp;

				}

	printf("%d\n",ans);

	return 0;

} 


// Faster C++ solution for the same problem (measured speedup: 5.068046001505342x):
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

const int MAXCOL=30;

int n_col,siz,spd[MAXCOL+5][MAXCOL+5];

int tot[3][MAXCOL+5];

int main()

{

	scanf("%d%d",&siz,&n_col);

	for(int i=0;i<n_col;i++)

		for(int j=0;j<n_col;j++)

			scanf("%d",&spd[i][j]);

	for(int i=0;i<siz;i++)

		for(int j=0;j<siz;j++)

		{

			int col;

			scanf("%d",&col);

			tot[(i+j+2)%3][col-1]++;

		}

	int ans=1e9;

	for(int i=0;i<n_col;i++)

	{

		int A=0;

		for(int col=0;col<n_col;col++)

			A+=tot[0][col]*spd[col][i];

		for(int j=0;j<n_col;j++)

			if(i!=j)

			{

				int B=0;

				for(int col=0;col<n_col;col++)

					B+=tot[1][col]*spd[col][j];

				for(int k=0;k<n_col;k++)

					if(i!=k && j!=k)

					{

						int C=0;

						for(int col=0;col<n_col;col++)

							C+=tot[2][col]*spd[col][k];

						ans=min(ans,A+B+C);

					}

			}

	}

	printf("%d\n",ans);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<algorithm>

#include<cmath>

#include<map>

#include<cstring>

#include<climits>

#include<queue>

using namespace std;

#define MAXC 30

#define MAXN 500

int n,c,ans;

int D[MAXC+5][MAXC+5],C[MAXN+5][MAXN+5];

int cont[4][MAXC+5];

int main()

{

    ans=INT_MAX;

    scanf("%d%d",&n,&c);

    for(int i=1;i<=c;i++)

        for(int j=1;j<=c;j++)

        scanf("%d",&D[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=n;j++)

        scanf("%d",&C[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=n;j++)

        cont[(i+j)%3][C[i][j]]++;

    for(int i=1;i<=c;i++)

        for(int j=1;j<=c;j++)

        if(i!=j)

            for(int k=1;k<=c;k++)

            if(k!=i&&k!=j)

            {

                int res=0;

                for(int p=1;p<=c;p++)res+=cont[0][p]*D[p][i];

                for(int p=1;p<=c;p++)res+=cont[1][p]*D[p][j];

                for(int p=1;p<=c;p++)res+=cont[2][p]*D[p][k];

                ans=min(ans,res);

            }

    printf("%d",ans);

}


// Faster C++ solution for the same problem (measured speedup: 5.109806317278964x):
#include<cstdio>

#include<algorithm>

using namespace std;

int d[300+5][300+5];

int t[5][300+5],s[5][300+5];//3的余数为0,1,2——>枚举所有情况求最优值

int x,y,z,ans=(int)1E9;

int n,c;

int solve(int x,int y)

{

	int ret=0;

	for(int i=1;i<=c;i++)

		ret+=t[x][i]*d[i][y];

	return ret;

}

int main()

{

	scanf("%d %d",&n,&c);

	for(int i=1;i<=c;i++)

    	for(int j=1;j<=c;j++)

			scanf("%d",&d[i][j]);//颜色i变成颜色j的代价

	for(int i=1;i<=n;i++)

		for(int j=1;j<=n;j++)

		{

			scanf("%d",&x);

			t[(i+j)%3][x]++;//余数为i,颜色为j的方格个数+1

		}

	for(int i=0;i<=2;i++)

		for(int j=1;j<=c;j++)

			s[i][j]=solve(i,j);

	for(int i=1;i<=c;i++)

		for(int j=1;j<=c;j++)

			for(int k=1;k<=c;k++)

			{

				if(i!=j&&i!=k&&j!=k)

					ans=min(ans,(s[0][i]+s[1][j]+s[2][k]));

			}

	printf("%d\n",ans);

	return 0;

}