#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define allof(obj) (obj).begin(), (obj).end() #define range(i, l, r) for(int i=l;i>1)|y_bit)) #define bit_kth(i, k) ((i >> k)&1) #define bit_highest(i) (i?63-__builtin_clzll(i):-1) #define bit_lowest(i) (i?__builtin_ctzll(i):-1) #define sleepms(t) std::this_thread::sleep_for(std::chrono::milliseconds(t)) using ll = long long; using ld = long double; using ul = uint64_t; using pi = std::pair; using pl = std::pair; using namespace std; template std::ostream &operator << (std::ostream &dest, const std::pair &p) { dest << p.first << ' ' << p.second; return dest; } template std::ostream &operator << (std::ostream &dest, const std::tuple &t) { dest << std::get<0>(t) << ' ' << std::get<1>(t); return dest; } template std::ostream &operator << (std::ostream &dest, const std::tuple &t) { dest << std::get<0>(t) << ' ' << std::get<1>(t) << ' ' << std::get<2>(t); return dest; } template std::ostream &operator << (std::ostream &dest, const std::tuple &t) { dest << std::get<0>(t) << ' ' << std::get<1>(t) << ' ' << std::get<2>(t) << ' ' << std::get<3>(t); return dest; } template std::ostream &operator << (std::ostream &dest, const std::vector> &v) { int sz = v.size(); if (!sz) return dest; for (int i = 0; i < sz; i++) { int m = v[i].size(); for (int j = 0; j < m; j++) dest << v[i][j] << (i != sz - 1 && j == m - 1 ? '\n' : ' '); } return dest; } template std::ostream &operator << (std::ostream &dest, const std::vector &v) { int sz = v.size(); if (!sz) return dest; for (int i = 0; i < sz - 1; i++) dest << v[i] << ' '; dest << v[sz - 1]; return dest; } template std::ostream &operator << (std::ostream &dest, const std::array &v) { if (!sz) return dest; for (int i = 0; i < sz - 1; i++) dest << v[i] << ' '; dest << v[sz - 1]; return dest; } template std::ostream &operator << (std::ostream &dest, const std::set &v) { for (auto itr = v.begin(); itr != v.end();) { dest << *itr; itr++; if (itr != v.end()) dest << ' '; } return dest; } template std::ostream &operator << (std::ostream &dest, const std::map &v) { for (auto itr = v.begin(); itr != v.end(); ) { dest << '(' << itr->first << ", " << itr->second << ')'; itr++; if (itr != v.end()) dest << '\n'; } return dest; } template vector make_vec(size_t sz, T val) { return std::vector(sz, val); } template auto make_vec(size_t sz, Tail ...tail) { return std::vector(tail...))>(sz, make_vec(tail...)); } template vector read_vec(size_t sz) { std::vector v(sz); for (int i = 0; i < (int)sz; i++) std::cin >> v[i]; return v; } template auto read_vec(size_t sz, Tail ...tail) { auto v = std::vector(tail...))>(sz); for (int i = 0; i < (int)sz; i++) v[i] = read_vec(tail...); return v; } // x / y以上の最小の整数 ll ceil_div(ll x, ll y) { assert(y > 0); return (x + (x > 0 ? y - 1 : 0)) / y; } // x / y以下の最大の整数 ll floor_div(ll x, ll y) { assert(y > 0); return (x + (x > 0 ? 0 : -y + 1)) / y; } void io_init() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } int main() { io_init(); int N, K; std::cin >> N >> K; auto A = read_vec(K); std::vector accum(4 * N, 0); std::vector pos; for (int i = 0; i < K; i++) pos.push_back(A[i]); for (int i = 0; i < K; i++) pos.push_back(A[i] + 2 * N); for (int p : pos) accum[p]++; for (int i = 1; i < 4 * N; i++) accum[i] += accum[i - 1]; std::vector npos(4 * N, 4 * N); for (int p : pos) npos[p] = p; for (int i = 4 * N - 2; i >= 0; i--) { if (npos[i] == 4 * N) { npos[i] = npos[i + 1]; } } std::array, 20> next; for (int i = 0; i < 20; i++) next[i].resize(N); ll L = 1, R = N + 1; while (R - L > 1) { ll M = (L + R) / 2; for (int i = 0; i < N; i++) { int l = i * 2; int r = std::max(npos[l] + 1, l + 2 * M); int rcnt = accum[r]; int lcnt = (l ? accum[l - 1] : 0); if (rcnt - lcnt != 1) { next[0][i] = 2 * N; } else { next[0][i] = (r - l) / 2; } } for (int i = 1; i < 20; i++) { for (int j = 0; j < N; j++) { int d1 = next[i - 1][j]; next[i][j] = d1 + next[i - 1][(j + d1) % N]; } } bool ok = false; for (int l = 0; l < N; l++) { int x = K; ll d = 0; int p = l; bool f = true; for (int i = 19; i >= 0; i--) { if ((x >> i) & 1) { d += next[i][p]; p = (p + next[i][p]) % N; x -= 1 << i; } if (d > N) { f = false; break; } } if (f) { ok = true; break; } } if (ok) L = M; else R = M; } std::cout << 2 * L << '\n'; }