#include using namespace std; int main(){ int n, m; cin >> n >> m; vector> ind(n, vector(m)); vector x(n*m), y(n*m); for (int i=0; i> x[i] >> y[i]; x[i]--; y[i]--; ind[x[i]][y[i]] = i; } int ans = 998244353; for (int stx=0; stx seen(5); vector pivx(5); for (int i=0; i<5; i++){ pivx[i] = (stx+i)%n; } int piv = 0; for (int i=0; i<5; i++){ for (int j=0; j<5; j++){ if (x[i] == pivx[j] && y[i] == sty){ seen[j] = true; piv++; } } } int zan = 0; for (int i=0; i<5; i++){ if (!seen[i]){ zan++; } } int tmp = 0; while(zan != 0){ int gomin = 998244353; for (int i=0; i<5; i++){ if (!seen[i]){ gomin = min(gomin, ind[pivx[i]][sty]); } } int tobu = (gomin - piv) / zan; piv += tobu * zan; tmp += tobu; int hod = 0; for (int i=0; i= n*m) continue; if (x[i+piv] == pivx[j] && y[i+piv] == sty){ seen[j] = true; hod++; } } } piv += hod; zan = 0; for (int i=0; i<5; i++){ if (!seen[i]){ zan++; } } } ans = min(ans, tmp); } } cout << ans << '\n'; }