#include #include #include typedef long long signed int LL; typedef long long unsigned int LU; #define incID(i, l, r) for(int i = (l) ; i < (r); i++) #define incII(i, l, r) for(int i = (l) ; i <= (r); i++) #define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--) #define decII(i, l, r) for(int i = (r) ; i >= (l); i--) #define inc( i, n) incID(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec( i, n) decID(i, 0, n) #define dec1(i, n) decII(i, 1, n) #define inII(v, l, r) ((l) <= (v) && (v) <= (r)) #define inID(v, l, r) ((l) <= (v) && (v) < (r)) template void swap(T & x, T & y) { T t = x; x = y; y = t; return; } template T abs(T x) { return (0 <= x ? x : -x); } template T max(T a, T b) { return (b <= a ? a : b); } template T min(T a, T b) { return (a <= b ? a : b); } template bool setmin(T & a, T b) { if(a <= b) { return false; } else { a = b; return true; } } template bool setmax(T & a, T b) { if(b <= a) { return false; } else { a = b; return true; } } template T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); } template T lcm(T a, T b) { return a / gcd(a, b) * b; } // ---- ---- int n, m, c[200][100][100]; int d[200][202][100]; int main() { scanf("%d%d", &n, &m); inc(k, m) { inc(i, n) { inc(j, n) { scanf("%d", &c[k][i][j]); } } } inc(k, m) { inc(i, n) { inc(j, n) { d[k][i][j] = c[k][i][j]; } } inc(j, n) { inc(i, n) { d[k][n + j][i] = c[k][i][j]; } } inc(i, n) { d[k][2 * n][i] = c[k][i][i]; } inc(i, n) { d[k][2 * n + 1][i] = c[k][i][n - 1 - i]; } } inc(k, m) { inc(i, 2 * n + 2) { std::sort(d[k][i], d[k][i] + n); } } int ans = 2 * n; inc(k, m) { inc(l, k) { inc(kk, 2 * n + 2) { inc(ll, 2 * n + 2) { int e = 0, y = 0; inc(x, n) { while(y < n && d[k][kk][x] > d[l][ll][y]) { y++; } if(y < n && d[k][kk][x] == d[l][ll][y]) { e++; } } setmin(ans, 2 * n - e); } } } } printf("%d\n", ans - 1); return 0; }