#include #include #include using namespace std; int main() { int N, M; cin >> N >> M; vector > > players(M); int maximum = 0; for (int i = 0; i < M; i++) { vector > card(N); for (int j = 0; j < N; j++) { vector row(N); for (int k = 0; k < N; k++) { cin >> row[k]; maximum = max(maximum, row[k]); } card[j] = row; } players[i] = card; } vector > > bingos(M); for (int i = 0; i < M; i++) { vector > lines; // left to right for (int j = 0; j < N; j++) { vector lr(maximum); for (int k = 0; k < N; k++) { lr[players[i][j][k]-1] = 1; } lines.push_back(lr); } // top to down for (int j = 0; j < N; j++) { vector td(maximum); for (int k = 0; k < N; k++) { td[players[i][k][j]-1] = 1; } lines.push_back(td); } // upper left to lower right vector ullr(maximum); for (int j = 0; j < N; j++) { ullr[players[i][j][j]-1] = 1; } lines.push_back(ullr); // upper right to lower left vector urll(maximum); for (int j = 0; j < N; j++) { urll[players[i][j][N-j-1]-1] = 1; } lines.push_back(urll); bingos[i] = lines; } int min_unmatched = 100000000; for (int i = 0; i < M; i++) { for (int j = i+1; j < M; j++) { for (int x = 0; x < 2*N+2; x++) { for (int y = 0; y < 2*N+2; y++) { int matched = 0; for (int s = 0; s < maximum; s++) { if (bingos[i][x][s] == 1 && bingos[j][y][s] == 1) { matched++; } } if (matched != 0) { min_unmatched = min(min_unmatched, N-matched); } } } } } min_unmatched = min(N,min_unmatched); cout << N+min_unmatched-1 << endl; }