package yukicoder; import java.util.ArrayList; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Q452 { 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]; for (int i = 0; i < m; ++i) list[i] = new ArrayList<>(); for (int i = 0; i < m; ++i) { ArrayList set3 = new ArrayList<>(); ArrayList set4 = new ArrayList<>(); for (int j = 0; j < n; ++j) { ArrayList set1 = new ArrayList<>(); ArrayList set2 = new ArrayList<>(); for (int k = 0; k < n; ++k) { set1.add(table[i][j][k]); set2.add(table[i][k][j]); } set3.add(table[i][j][j]); set4.add(table[i][n - 1 - j][j]); list[i].add(set1); list[i].add(set2); } list[i].add(set3); list[i].add(set4); } int min = Integer.MAX_VALUE; for (int i = 0; i < m; ++i) { for (int j = i + 1; j < m; ++j) { for (ArrayList set1 : list[i]) { for (ArrayList set2 : list[j]) { Set sum = new HashSet<>(); sum.addAll(set1); sum.addAll(set2); min = Math.min(sum.size(), min); } } } } System.out.println(min - 1); } }