#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef vector vi; const int MAXM = 222; const int MAXN = 111; int C[MAXM][MAXN][MAXN]; bool done[MAXN][MAXN]; int N, M; int cnt() { int ret = N; for (int i = 0; i < N; i++) { int tmp = 0; for (int j = 0; j < N; j++) { if (done[i][j]) ++tmp; } ret = min(ret, N-tmp); } for (int j = 0; j < N; j++) { int tmp = 0; for (int i = 0; i < N; i++) { if (done[i][j]) ++tmp; } ret = min(ret, N-tmp); } int tmp = 0; for (int i = 0; i < N; i++) { if (done[i][i]) ++tmp; } ret = min(ret, N-tmp); tmp = 0; for (int i = 0; i < N; i++) { if (done[i][N-1-i]) ++tmp; } ret = min(ret, N-tmp); return ret; } int calc(int a, int b) { int ans = N-1; for (int i = 0; i < N; i++) { set S; for (int j = 0; j < N; j++) { S.insert(C[a][i][j]); } memset(done, false, sizeof(done)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (S.find(C[b][i][j]) != S.end()) done[i][j] = true; } } ans = min(ans, cnt()-1); } return ans+N; } int main() { cin >> N >> M; for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { cin >> C[i][j][k]; } } } int ans = 2*N-1; for (int i = 0; i < M; i++) { for (int j = i+1; j < M; j++) { ans = min(ans, calc(i, j)); } } cout << ans << endl; return 0; }