#include #include #include using namespace std; int main() { int N, M; cin >> N >> M; vector > > players(M); 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]; } card[j] = row; } players[i] = card; } vector > > bingos(M); for (int i = 0; i < M; i++) { // left to right for (int j = 0; j < N; j++) { vector lr = players[i][j]; bingos[i].push_back(lr); } // top to down for (int j = 0; j < N; j++) { vector td(N); for (int k = 0; k < N; k++) { td[k] = players[i][k][j]; } bingos[i].push_back(td); } // upper left to lower right vector ullr(N); for (int j = 0; j < N; j++) { ullr[j] = players[i][j][j]; } bingos[i].push_back(ullr); // upper right to lower left vector urll(N); for (int j = 0; j < N; j++) { urll[j] = players[i][j][N-j-1]; } bingos[i].push_back(urll); for (auto& bingo : bingos[i]) { sort(bingo.begin(), bingo.end()); } } int answer = 2*N; for (int i = 0; i < M; i++) { for (auto& x : bingos[i]) { for (int j = 0; j < i; j++) { for (auto& y : bingos[j]) { vector z; set_union(x.begin(), x.end(), y.begin(), y.end(), back_inserter(z)); answer = min(answer, (int)z.size()-1); } } } } cout << answer << endl; }