#include<bits/stdc++.h> // #define SINGLE_INPUT #define ll long long #define ull unsigned long long #define N 500005 #define MOD 998244353 usingnamespace std;
voidsol(){ ll n, p; cin >> n >> p; set<ll> st; vector<ll> a(n+1); for (int i=1; i<=n; i++) { cin >> a[i]; st.insert(a[i]); } // 0 到 a[n-1] 都存在 int ok = 1; for (int i=a[n]; i>=0; i--) { if (st.count(i) == 0) { ok = 0; break; } } if (ok) { int i; for (i=p-1; i>a[n]; i--) { if (st.count(i) == 0) { break; } } cout << (i-a[n]) << "\n"; } else { st.insert(0); for (int i=n-1; i>=0; i--) { if (a[i] != p-1) { st.insert(a[i]+1); break; } } for (int i=a[n]-1; i>=0; i--) { if (st.count(i) == 0) { cout << i+p-a[n] << "\n"; return ; } } cout << p-a[n] << "\n"; } }