#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
int const MAXN = 2e6 + 10;
int n, m, T;
int a[MAXN];
int num[MAXN];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= m; i++) num[a[i]]++;
    int ans = 1e9;
    for (int i = 0; ; i++)
        if (!num[i]) {
            ans = i;
            break;
        }
    for (int i = m + 1; i <= n; i++) {
        num[a[i-m]]--;
        num[a[i]]++;
        //cout << a[i - m] << " " << a[i] << endl;
        if (!num[a[i-m]]) ans = min(ans, a[i-m]);
    }
    cout << ans;

    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i ++ )
int n, m;
vector<int> v[1500005];
int main(void)
{
  cin >> n >> m;
  rep(i, 0, n)
  {
    int x; scanf("%d", &x);
    if (x < m) v[x].push_back(i);
  }
  rep(i, 0, m + 1)
  {
    // cout << v[i].size() << endl;
    if (v[i].empty())
    {
      cout << i << "\n";
      return 0;
    }
    bool flag = false;
    int last = -1;
    for (int it : v[i])
    {
      if (it - last <= m) last = it;
      else 
      {
        flag = true;
        break;
      }
    }
    if (n - last > m) flag = true;
    if (flag) {cout << i << "\n"; return 0;}
  }
  return 0;
}
