Problem:
Score : 400 points

Problem Statement

There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.

These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.

We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:

If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.

If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.

Here, X \% Y represents X modulo Y.

We will repaint zero or more squares so that the grid will be a good grid.

For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.

Find the minimum possible sum of the wrongness of all the squares.

Constraints

1 \leq N \leq 500

3 \leq C \leq 30

1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j)

1 \leq c_{i,j} \leq C

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N C
D_{1,1} ... D_{1,C}
:
D_{C,1} ... D_{C,C}
c_{1,1} ... c_{1,N}
:
c_{N,1} ... c_{N,N}

Output

If the minimum possible sum of the wrongness of all the squares is x, print x.

Sample Input 1

2 3
0 1 1
1 0 1
1 4 0
1 2
3 3

Sample Output 1

3

Repaint (1,1) to Color 2. The wrongness of (1,1) becomes D_{1,2}=1.

Repaint (1,2) to Color 3. The wrongness of (1,2) becomes D_{2,3}=1.

Repaint (2,2) to Color 1. The wrongness of (2,2) becomes D_{3,1}=1.

In this case, the sum of the wrongness of all the squares is 3.

Note that D_{i,j} \neq D_{j,i} is possible.

Sample Input 2

4 3
0 12 71
81 0 53
14 92 0
1 1 2 1
2 1 1 2
2 2 1 3
1 1 2 2

Sample Output 2

428

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int c = in.nextInt();
		int[][] d = new int[c][c];
		for(int i=0;i<c;i++) {
			for(int j=0;j<c;j++) {
				d[i][j] = in.nextInt();
			}
		}
		List<Integer> list0 = new ArrayList<>();
		List<Integer> list1 = new ArrayList<>();
		List<Integer> list2 = new ArrayList<>();
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				if((i+j)%3==0) {
					list0.add(in.nextInt()-1);
				}else if((i+j)%3==1) {
					list1.add(in.nextInt()-1);
				}else {
					list2.add(in.nextInt()-1);
				}
			}
		}
		
		int[] cost0 = new int[3];
		cost0[0] = cost0[1] = cost0[2] = Integer.MAX_VALUE;
		int[] idx0 = new int[3];
		for(int i=0;i<c;i++) {
			int sum = 0;
			for(int idx=0;idx<list0.size();idx++) {
				sum += d[list0.get(idx)][i];
			}
			if(sum<cost0[0]) {
				cost0[2] = cost0[1];
				idx0[2] = idx0[1];
				cost0[1] = cost0[0];
				idx0[1] = idx0[0];
				cost0[0] = sum;
				idx0[0] = i;
			}else if(sum<cost0[1]) {
				cost0[2] = cost0[1];
				idx0[2] = idx0[1];
				cost0[1] = sum;
				idx0[1] = i;
			}else if(sum<cost0[2]) {
				cost0[2] = sum;
				idx0[2] = i;
			}
		}
		
		int[] cost1 = new int[3];
		cost1[0] = cost1[1] = cost1[2] = Integer.MAX_VALUE;
		int[] idx1 = new int[3];
		for(int i=0;i<c;i++) {
			int sum = 0;
			for(int idx=0;idx<list1.size();idx++) {
				sum += d[list1.get(idx)][i];
			}
			if(sum<cost1[0]) {
				cost1[2] = cost1[1];
				idx1[2] = idx1[1];
				cost1[1] = cost1[0];
				idx1[1] = idx1[0];
				cost1[0] = sum;
				idx1[0] = i;
			}else if(sum<cost1[1]) {
				cost1[2] = cost1[1];
				idx1[2] = idx1[1];
				cost1[1] = sum;
				idx1[1] = i;
			}else if(sum<cost1[2]) {
				cost1[2] = sum;
				idx1[2] = i;
			}
		}
		
		int[] cost2 = new int[3];
		cost2[0] = cost2[1] = cost2[2] = Integer.MAX_VALUE;
		int[] idx2 = new int[3];
		for(int i=0;i<c;i++) {
			int sum = 0;
			for(int idx=0;idx<list2.size();idx++) {
				sum += d[list2.get(idx)][i];
			}
			if(sum<cost2[0]) {
				cost2[2] = cost2[1];
				idx2[2] = idx2[1];
				cost2[1] = cost2[0];
				idx2[1] = idx2[0];
				cost2[0] = sum;
				idx2[0] = i;
			}else if(sum<cost2[1]) {
				cost2[2] = cost2[1];
				idx2[2] = idx2[1];
				cost2[1] = sum;
				idx2[1] = i;
			}else if(sum<cost2[2]) {
				cost2[2] = sum;
				idx2[2] = i;
			}
		}
		
		List<Integer> kouho = new ArrayList<>();
		
		for(int x=0;x<3;x++) {
			for(int y=0;y<3;y++) {
				for(int z=0;z<3;z++) {
					if(idx0[x]!=idx1[y] && idx1[y]!=idx2[z] && idx2[z]!=idx0[x]) {
						kouho.add(cost0[x]+cost1[y]+cost2[z]);
					}
				}
			}
		}
		
		int min = Integer.MAX_VALUE;
		for(int i:kouho) {
			if(i<min) min=i;
		}
		System.out.println(min);

	}

}