#include "bits/stdc++.h" using namespace std; #define int long long #define FOR(i, a, b) for(int i=(a);i<(b);i++) #define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--) #define REP(i, n) for(int i=0; i<(n); i++) #define RREP(i, n) for(int i=(n-1); i>=0; i--) #define ALL(a) (a).begin(),(a).end() #define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end()); #define CONTAIN(a, b) find(ALL(a), (b)) != (a).end() #define array2(type, x, y) array, x> #define vector2(type) vector > #define out(...) printf(__VA_ARGS__) int dxy[] = {0, 1, 0, -1, 0}; /*================================*/ int N,M,L; int C[222][222][222]; void set_line(set *line, int m, int l) { if (linsert(C[m][l][i]); } else if (l<2*N) { // 縦 REP(i,N) line->insert(C[m][i][l-N]); } else if (l==2*N) { // 斜め REP(i,N) line->insert(C[m][i][i]); } else { // 斜め(逆) REP(i,N) line->insert(C[m][i][N-1-i]); } } signed main() { #if DEBUG std::ifstream in("input.txt"); std::cin.rdbuf(in.rdbuf()); #endif cin>>N>>M; REP(m,M) { REP(i,N)REP(j,N)cin>>C[m][i][j]; } int ma = 0; // 最大マッチ数 REP(m,M-1) { REP(l,2*N+2) { // 各列について set line1; set_line(&line1,m,l); FOR(o,m+1,M) { REP(r,2*N+2) { // 他人の各列と比較 set line2; set_line(&line2,o,r); set result; set_intersection(line1.begin(), line1.end(), line2.begin(), line2.end(), std::inserter(result, result.end())); ma = max(ma, (int)result.size()); } } } } cout << 2*N-1-ma << endl; return 0; }