package yukicoder; import java.util.ArrayList; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Q452 { @SuppressWarnings("unchecked") public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); int m = Integer.parseInt(sc.next()); int[][][] table = new int[m][n][n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < n; ++k) { table[i][j][k] = Integer.parseInt(sc.next()); } } } ArrayList[][] list = new ArrayList[m][2 * n + 2]; for (int i = 0; i < list.length; ++i) { for (int j = 0; j < list[i].length; ++j) { list[i][j] = new ArrayList<>(); } } for (int i = 0; i < m; ++i) { int p = 0; for (int j = 0; j < n; ++j) { for (int k = 0; k < n; ++k) { list[i][p].add(table[i][j][k]); list[i][p + 1].add(table[i][k][j]); } p += 2; list[i][2 * n].add(table[i][j][j]); list[i][2 * n + 1].add(table[i][n - 1 - j][j]); } } int min = Integer.MAX_VALUE; for (int i = 0; i < m; ++i) { for (int j = i + 1; j < m; ++j) { for (ArrayList list1 : list[i]) { boolean[] vis = new boolean[n * n * m + 1]; for (int set1 : list1) { vis[set1] = true; } for (ArrayList list2 : list[j]) { int count = 0; for (int set2 : list2) { if (vis[set2]) { ++count; } } min = Math.min(2 * n - count - 1, min); } } } } System.out.println(min); } }