#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") struct Set { // max{ceil(log_64(n)), 1} int log64N, n; vector a[6]; explicit Set(int n_ = 0) : n(n_) { assert(n >= 0); int m = n ? n : 1; for (int d = 0; ; ++d) { m = (m + 63) >> 6; a[d].assign(m, 0); if (m == 1) { log64N = d + 1; break; } } } bool empty() const { return !a[log64N - 1][0]; } bool contains(int x) const { return (a[0][x >> 6] >> (x & 63)) & 1; } void insert(int x) { for (int d = 0; d < log64N; ++d) { const int q = x >> 6, r = x & 63; a[d][q] |= 1ULL << r; x = q; } } void erase(int x) { for (int d = 0; d < log64N; ++d) { const int q = x >> 6, r = x & 63; if ((a[d][q] &= ~(1ULL << r))) break; x = q; } } // max s.t. <= x (or -1) int prev(int x) const { if (x > n - 1) x = n - 1; for (int d = 0; d <= log64N; ++d) { if (x < 0) break; const int q = x >> 6, r = x & 63; const unsigned long long lower = a[d][q] << (63 - r); if (lower) { x -= __builtin_clzll(lower); for (int e = d; --e >= 0; ) x = x << 6 | (63 - __builtin_clzll(a[e][x])); return x; } x = q - 1; } return -1; } // min s.t. >= x (or n) int next(int x) const { if (x < 0) x = 0; for (int d = 0; d < log64N; ++d) { const int q = x >> 6, r = x & 63; if (static_cast(q) >= a[d].size()) break; const unsigned long long upper = a[d][q] >> r; if (upper) { x += __builtin_ctzll(upper); for (int e = d; --e >= 0; ) x = x << 6 | __builtin_ctzll(a[e][x]); return x; } x = q + 1; } return n; } }; int N; Int X; vector A; vector freq; Set off; void add(int a) { if (a <= N) if (!freq[a]++) off.erase(a); } void rem(int a) { if (a <= N) if (!--freq[a]) off.insert(a); } int mex() { return off.next(0); } int main() { for (; ~scanf("%d%lld", &N, &X); ) { --X; A.resize(N); for (int i = 0; i < N; ++i) { scanf("%d", &A[i]); } freq.assign(N + 1, 0); off = Set(N + 1); for (int a = 0; a <= N; ++a) off.insert(a); for (int i = 0; i < N; ++i) { add(A[i]); } for (int i = N; ; ++i) { const int res = mex(); A.push_back(res); rem(A[i - N]); add(A[i]); if (A[i] == N) break; } int ans; if (X < (int)A.size()) { ans = A[X]; } else { const int I = (int)A.size() - (N + 1); ans = A[I + (X - I) % (N + 1)]; } printf("%d\n", ans); } return 0; }