#include <bits/stdc++.h>
using namespace std;
int N, M, A[500005], mex[500005], Ans[500005], nex[500005], las[500005];
bool mark[500005];
struct node {
  int l, r, id;
} qry[500005];
bool operator<(node x, node y) { return x.l < y.l; }
int tot, lazy[1000005], ls[1000005], rs[1000005], a[1000005], b[1000005],
    v[1000005];
void Putdown(int p) {
  int Ls = ls[p], Rs = rs[p];
  if (~lazy[Ls])
    lazy[Ls] = min(lazy[p], lazy[Ls]);
  else
    lazy[Ls] = lazy[p];
  if (~lazy[Rs])
    lazy[Rs] = min(lazy[p], lazy[Rs]);
  else
    lazy[Rs] = lazy[p];
  v[Ls] = min(v[Ls], lazy[p]);
  v[Rs] = min(v[Rs], lazy[p]);
  lazy[p] = -1;
}
int Build(int x, int y) {
  tot++;
  int mid = x + y >> 1, p = tot;
  a[p] = x;
  b[p] = y;
  if (x == y) {
    v[p] = mex[x];
    return p;
  }
  ls[p] = Build(x, mid);
  rs[p] = Build(mid + 1, y);
  v[p] = min(v[ls[p]], v[rs[p]]);
  return p;
}
void Modify(int p, int l, int r, int k) {
  if (b[p] < l || a[p] > r) return;
  if (~lazy[p]) Putdown(p);
  if (a[p] >= l && b[p] <= r) {
    lazy[p] = k;
    v[p] = min(v[p], k);
    return;
  }
  int mid = a[p] + b[p] >> 1;
  Modify(ls[p], l, r, k);
  Modify(rs[p], l, r, k);
}
int GetAns(int p, int k) {
  if (~lazy[p]) Putdown(p);
  if (a[p] == b[p] && a[p] == k) return v[p];
  int mid = a[p] + b[p] >> 1;
  if (k <= mid) return GetAns(ls[p], k);
  return GetAns(rs[p], k);
}
int pre[500005], flag[500005];
int main() {
  int i, j = 0;
  scanf("%d", &N);
  for (i = 1; i <= N; i++) scanf("%d", &A[i]), A[i]--;
  for (i = N; i; i--) {
    if (las[A[i]] == 0)
      nex[i] = N + 1;
    else
      nex[i] = las[A[i]];
    las[A[i]] = i;
  }
  for (i = 1; i <= N; i++) {
    int Pre = pre[A[i]];
    if (i - 1 >= Pre + 1) qry[++M].l = Pre + 1, qry[M].r = i - 1, qry[M].id = M;
    pre[A[i]] = i;
  }
  for (i = 0; i <= N; i++) {
    int Pre = pre[i];
    if (N >= Pre + 1) qry[++M].l = Pre + 1, qry[M].r = N, qry[M].id = M;
  }
  for (i = 1; i <= N; i++) {
    mark[A[i]] = true;
    for (;; j++)
      if (!mark[j]) break;
    mex[i] = j;
  }
  sort(qry + 1, qry + M + 1);
  memset(lazy, -1, sizeof(lazy));
  Build(1, N);
  for (i = 1, j = 1; i <= N && j <= M; i++) {
    while (qry[j].l == i) {
      Ans[qry[j].id] = GetAns(1, qry[j].r);
      j++;
    }
    Modify(1, 1, nex[i] - 1, A[i]);
  }
  for (i = 1; i <= M; i++) flag[Ans[i]] = 1;
  for (i = 0; i <= N + 1; i++)
    if (!flag[i]) {
      cout << i + 1 << endl;
      break;
    }
  return 0;
}
