#include #include #include #include #include #include #include #include using namespace std; #define GET_ARG(a,b,c,F,...) F #define REP3(i,s,e) for (i = s; i <= e; i++) #define REP2(i,n) REP3 (i,0,(int)(n)-1) #define REP(...) GET_ARG (__VA_ARGS__,REP3,REP2) (__VA_ARGS__) #define RREP3(i,s,e) for (i = s; i >= e; i--) #define RREP2(i,n) RREP3 (i,(int)(n)-1,0) #define RREP(...) GET_ARG (__VA_ARGS__,RREP3,RREP2) (__VA_ARGS__) #define DEBUG(x) cerr << #x ": " << x << endl int c[200][100][100]; vector> nums[200]; int main(void) { int i, j, k, n, m; scanf("%d%d",&n,&m); REP (k,m) REP (i,n) REP (j,n) scanf("%d",&c[k][i][j]); REP (k,m) { REP (i,n) { set st; REP (j,n) st.insert(c[k][i][j]); nums[k].push_back(st); } REP (j,n) { set st; REP (i,n) st.insert(c[k][i][j]); nums[k].push_back(st); } { set st; REP (i,n) st.insert(c[k][i][i]); nums[k].push_back(st); } { set st; REP (i,n) st.insert(c[k][i][n-i-1]); nums[k].push_back(st); } } int a, b, ans = 1e8; REP (a,m) REP (b,m) if (a != b) { REP (i,n) { set st; REP (j,n) st.insert(c[b][i][j]); for (auto s: nums[a]) { int same = 0; for (auto x: s) same += st.count(x); ans = min(ans,2*n-same-1); } } REP (j,n) { set st; REP (i,n) st.insert(c[b][i][j]); for (auto s: nums[a]) { int same = 0; for (auto x: s) same += st.count(x); ans = min(ans,2*n-same-1); } } { set st; REP (i,n) st.insert(c[b][i][i]); for (auto s: nums[a]) { int same = 0; for (auto x: s) same += st.count(x); ans = min(ans,2*n-same-1); } } { set st; REP (i,n) st.insert(c[b][i][n-i-1]); for (auto s: nums[a]) { int same = 0; for (auto x: s) same += st.count(x); ans = min(ans,2*n-same-1); } } } ans = max(ans,n); printf("%d\n",ans); return 0; }