#include using namespace std; const int MAXN = 100; const int MAXM = 200; int N, M; int C[MAXM][MAXN][MAXN]; vector syugo[MAXM][2 * MAXN + 2]; signed main() { ios::sync_with_stdio(false); 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]; } } } for (int i = 0; i < M; ++i) { for (int j = 0; j < N; ++j) { for (int k = 0; k < N; ++k) { syugo[i][j].emplace_back(C[i][j][k]); syugo[i][N + k].emplace_back(C[i][j][k]); if (j == k) syugo[i][N << 1].emplace_back(C[i][j][k]); if (N - 1 - j == k) syugo[i][N << 1 | 1].emplace_back(C[i][N - 1 - j][k]); } } for (int j = 0; j < N + 1 << 1; ++j) { sort(syugo[i][j].begin(), syugo[i][j].end()); } } int ans = 2 * N; for (int i = 0; i < M; ++i) { for (int j = 0; j < M; ++j) { int pt1 = 0, pt2 = 0, cnt = 0; for (int k = 0; k < N + 1 << 1; ++k) { for (int l = 0; l < N + 1 << 1; ++l) { while (pt1 < N && pt2 < N) { if (syugo[i][k][pt1] < syugo[j][l][pt2]) ++pt1; else if (syugo[i][k][pt1] > syugo[j][l][pt2]) ++pt2; else ++pt1, ++pt2, ++cnt; } } } ans = min(ans, 2 * N - cnt); } } cout << ans << endl; return 0; }