#include #include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) #define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) using namespace std; template void setmin(T & a, T const & b) { if (b < a) a = b; } template auto vectors(X x, T a) { return vector(x, a); } template auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector(x, cont); } int main() { int n, m; cin >> n >> m; auto c = vectors(m, n, n, int()); repeat (i,m) repeat (y,n) repeat (x,n) cin >> c[i][y][x]; vector > > bingo(m); repeat (i,m) { repeat (y,n) { vector row = c[i][y]; bingo[i].push_back(row); } repeat (x,n) { vector col(n); repeat (y,n) col[y] = c[i][y][x]; bingo[i].push_back(col); } repeat (p,2) { vector diag(n); repeat (z,n) diag[z] = c[i][z][p ? z : n-z-1]; bingo[i].push_back(diag); } for (auto & it : bingo[i]) { whole(sort, it); } } int ans = 2*n; repeat (i,m) for (auto & x : bingo[i]) { repeat (j,i) for (auto & y : bingo[j]) { vector z; set_union(x.begin(), x.end(), y.begin(), y.end(), back_inserter(z)); setmin(ans, z.size()-1); } } cout << ans << endl; return 0; }