#include using namespace std; int main() { int N, M; cin >> N >> M; int B[M][N][N]; int L[M][N*2+2][N]; int cnt = 0; for (int m = 0; m < M; ++m) { for (int y = 0; y < N; ++y) { for (int x = 0; x < N; ++x) { cin >> B[m][x][y]; } } } for (int m = 0; m < M; ++m) { int c = 0; for (int x = 0; x < N; ++x, ++c) { for (int i = 0; i < N; ++i) { L[m][c][i] = B[m][x][i]; } } for (int y = 0; y < N; ++y, ++c) { for (int i = 0; i < N; ++i) { L[m][c][i] = B[m][i][y]; } } for (int i = 0; i < N; ++i) { L[m][c][i] = B[m][i][i]; L[m][c+1][i] = B[m][i][N-i-1]; } } unordered_set s{}; for (int m = 0; m < M; ++m) { for (int a = 0; a < N*2+2; ++a) { for (int i = 0; i < M; ++i) { if (i == m) { continue; } for (int j = 0, c = 0; j < N*2+2; ++j, c = 0) { s.clear(); for (int k = 0; k < N; ++k) { s.emplace(L[m][a][k]); s.emplace(L[i][j][k]); } cnt = max(cnt, N*2-(int)s.size()); } } } } cout << N+N-cnt-1 << endl; return 0; }