#include using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } int main() { fast_io(); int N, M; cin >> N >> M; vector n(N * M), m(N * M); for (int i = 0; i < N * M; i++) { cin >> n[i] >> m[i]; } int ans = 1e9; for (int j = 1; j <= M; j++) { for (int st = 1; st <= N - 3; st++) { set need_n; if (st < N - 3) { for (int k = st; k < st + 5; k++) { need_n.insert(k); } } else { for (int k = N - 3; k <= N; k++) { need_n.insert(k); } need_n.insert(1); } int cur = 0; for (int i = 0; i < 5; i++) { if (m[i] == j && need_n.count(n[i]) > 0) { cur++; } } int op = 0; int i = 5; while (cur < 5) { int op_cnt = 5 - cur; op++; for (int k = 0; k < op_cnt; k++) { if (m[i] == j && need_n.count(n[i]) > 0) { cur++; } i++; } } ans = min(ans, op); } } cout << ans << endl; }