#include #include #include #include using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(n); i++) int main(){ int N,M; cin >> N >> M; int ans = 4; if(M == 0) ans += N % 2; else if(M == 1) ans = 5; else{ set> uv; rep(i,M){ int u,v; cin >> u >> v; u--; v--; uv.insert({ u,v }); } while(uv.size()){ set u_v; for(auto [u,v] : uv){ u_v.insert(u); u_v.insert(v); } vector uvvec(u_v.begin(), u_v.end()); rep(i,M-1) if(0 == uv.count({ uvvec[i],uvvec[i+1] })){ ans = 5; break; } if(0 == uv.count({ uvvec.front(), uvvec.back() })){ ans = 5; break; } rep(i,M-1) if((uvvec[i+1] - uvvec[i]) % 2 == 0){ ans = 5; break; } if((uvvec.front() + N - uvvec.back()) % 2 == 0){ ans = 5; break; } if(ans == 5) break; rep(i,M-1) uv.erase({ uvvec[i],uvvec[i+1] }); uv.erase({ uvvec.front(), uvvec.back() }); set> new_uv; for(auto [u,v] : uv){ int nxu = upper_bound(uvvec.begin(), uvvec.end(), u) - uvvec.begin(); int nxv = upper_bound(uvvec.begin(), uvvec.end(), v) - uvvec.begin(); new_uv.insert({ nxu, nxv }); } if(new_uv.empty()){ ans = 4; break; } swap(uv, new_uv); u_v.clear(); for(auto [u,v] : uv){ u_v.insert(u); u_v.insert(v); } uvvec = vector(u_v.begin(), u_v.end()); rep(i,M-1) if(0 == uv.count({ uvvec[i],uvvec[i+1] })){ ans = 5; break; } if(0 == uv.count({ uvvec.front(), uvvec.back() })){ ans = 5; break; } rep(i,M-1) uv.erase({ uvvec[i],uvvec[i+1] }); uv.erase({ uvvec.front(), uvvec.back() }); if(ans == 5) break; new_uv.clear(); for(auto [u,v] : uv){ int nxu = upper_bound(uvvec.begin(), uvvec.end(), u) - uvvec.begin(); int nxv = upper_bound(uvvec.begin(), uvvec.end(), v) - uvvec.begin(); new_uv.insert({ nxu, nxv }); } if(new_uv.empty()){ ans = 4; break; } swap(uv, new_uv); if(uv.empty()){ if(uvvec.size() % 2 == 1) ans = 5; else ans = 4; } } } cout << ans << endl; return 0; } struct ios_do_not_sync { ios_do_not_sync() { ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;