/*
 * Author:  xtestw
 * Created Time:  2014/7/19 21:16:21
 * File Name: B.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = -1u>>1;
struct node
{
    int to;
    int w;
};
vector<node> mp[500010];
long long d[500010];
int vis[500010];
struct route{
    int s,y;
};
int vis2[100010];
route r[100100];
route r2[100100];
int n,m,k;
int pre[100010];
void spfa(int s)
{
    queue <int> q;
    memset(vis,false,sizeof(vis));
    q.push(s);
    d[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=0;i<mp[u].size();i++)
        {
            int v=mp[u][i].to;
            if(d[v] <0 || d[v]>=d[u]+(long long)mp[u][i].w)
            {
                if (d[v]==d[u]+(long long)mp[u][i].w && u==1){}else{
                pre[v]=u;}
                d[v]=d[u]+(long long)mp[u][i].w;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
}
int cmp(const route& a,const route& b)
{
    if (a.s!=b.s) return a.s<b.s;
    return a.y<b.y;
}
int main() {
#ifndef ONLINE_JUDGE
   freopen("f:/in.txt","r",stdin);
#endif
    cin>>n>>m>>k;
    memset(d,-1,sizeof(d));
    for(int i=0;i<m;i++)
    {
        int u,x,v;
        scanf("%d%d%d",&u,&v,&x);
        node t;
        t.to=u;
        t.w=x;
        mp[v].push_back(t);
        node t2;
        t2.to=v;
        t2.w=x;
        mp[u].push_back(t2);
        if (u==1){if (d[v]<0 || d[v]>x) d[v]=x;}
        if (v==1){if (d[u]<0 || d[u]>x) d[u]=x;}
    }
    int ans=0;
    memset(vis2,0,sizeof(vis2));
    memset(pre,-1,sizeof(pre));
    int tot=0;
    for (int i=0;i<k;i++)
    {
        int s,y;
        scanf("%d%d",&s,&y);
        r[i].s=s;
        r[i].y=y;
    }
    sort(r,r+k,cmp);
    for(int i=0;i<k;i++)
    {
        int s=r[i].s;
        int y=r[i].y;
        if (i>0 && r[i-1].s==s)
        {
            ans++;
        }else{
            r2[tot].s=s;
            r2[tot++].y=y;
            if (d[s]>0 && d[s]<=y){ans++; vis2[tot-1]=1;}
            if (d[s]<0 || d[s]>y){d[s]=y; pre[s]=1;}
        }
    }
   spfa(1);
   k=tot;
   for (int i=0;i<k;i++)
   {
       int s=r2[i].s;
       int y=r2[i].y;
       if (vis2[i]) continue;
       if (d[s]<(long long)y){ans++;}
       if (d[s]==(long long)y && pre[s]!=1){ans++;}
   }
    cout<<ans<<endl;
   return 0;
}


/*
 * Author:  xtestw
 * Created Time:  2014/7/19 21:16:21
 * File Name: B.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = -1u>>1;
struct node
{
    int to;
    int w;
};
vector<node> mp[100010];
int d[100010];
int vis[100010];

    int n,m,k;
void spfa(int s)
{   queue <int> q;
    for(int i=1;i<=n;i++)
          d[i]=0x3f3f3f3f;
    memset(vis,false,sizeof(vis));
    q.push(s);
    d[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=0;i<mp[u].size();i++)
        {
            int v=mp[u][i].to;
            if(d[v]>d[u]+mp[u][i].w)
            {
                d[v]=d[u]+mp[u][i].w;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
   freopen("f:/in.txt","r",stdin);
#endif
    cin>>n>>m>>k;
    memset(d,-1,sizeof(d));
    for(int i=0;i<m;i++)
    {
        int u,x,v;
        scanf("%d%d%d",&u,&v,&x);
        node t;
        t.to=u;
        t.w=x;
        mp[v].push_back(t);
        node t2;
        t2.to=v;
        t2.w=x;
        mp[u].push_back(t2);
    }
    spfa(1);
    int ans=0;
    while (k--)
    {
        int s,y;
        scanf("%d%d",&s,&y);
        if (d[s]<=y) ans++;
    }
    cout<<ans<<endl;
   return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#define BUG cout << "bug" << endl

using namespace std;

bool vis[1000001];
int c[1000001];

int main(void)
{
    int n, k, t;
    scanf("%d%d",&n,&k);
    for(int i = 1; i <= n; i++){
        scanf("%d",&t);
        if(!vis[t]){
            if(c[t] < c[k]) vis[t] = 1;
            else {
                c[t]++;
            }
        }
    }
    vis[k] = 1;
    for(int i = 1; i <= 100000; i++){
        if(c[i]>=c[k] && !vis[i]){
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define CLOSE() ios::sync_with_stdio(false)
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define PF(a) printf("%d\n", a)
#define SF(a) scanf("%d", &a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define maxn 15005
#define mod 1000007
#define INF 1e15
using namespace std;
//------------------------------------------------------------------------------------------//
set<int> iset;
map<int, int> cnt;

int main() {
#ifdef _DEBUG
	IN(); OUT();
#endif
	int n, k;
	while (SFF(n, k) == 2) {
		iset.clear();
		cnt.clear();
		FOR(i, 0, n) {
			int x;
			SF(x);
			if (cnt[x] < cnt[k]) iset.insert(x);
			cnt[x]++;
		}
		bool ok = false;
		for (auto i = cnt.begin(); i != cnt.end(); ++i) {
			pair<int, int> p = *i;
			if (p.second >= cnt[k] && p.first != k && !iset.count(p.first)) {
				ok = true;
				PF(p.first);
				break;
			}
		}
		if (!ok) PF(-1);
	}
	return 0;
}


#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[1000005];
int main(){
    int n,k;
    while(~scanf("%d %d",&n,&k)){
        memset(a,0,sizeof(a));
        int b;
        for(int i=0;i<n;i++){
            scanf("%d",&b);
            if(b==k){
                a[k]++;
            }
            else{
                if(a[b]>=a[k])
                    a[b]++;
            }
        }
        int flag=0;
        for(int i=1;i<100001;i++){
            if(a[k]==0){
                if(a[i]==0&&i!=k){
                printf("%d\n",i);
                flag=1;
                break;
                }
            }
            else if(a[i]>=a[k]&&i!=k){
                printf("%d\n",i);
                flag=1;
                break;
            }
        }
        if(flag==0)
            printf("-1\n");
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#define BUG cout << "bug" << endl

using namespace std;

int a[1000010];

int main(void)
{
    int n, k, t;
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        scanf("%d",&t);
        if(t==k) a[k]++;
        else if(a[t] >= a[k]){
            a[t]++;
        }
    }
    for(int i = 1; i <= 1000000; i++){
        if(a[i] >= a[k] && i!=k){
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define w(t) while(t--)
#define ll long long
#define S(x) scanf("%d",&x)
#define SLL(x) scanf("%lld",&x)
#define P(x) printf("%d\n",x)
#define fl(i , a, b) for(i = (int)a; i<(int)b; i++)
#define mem(a , value) memset(a , value , sizeof(a))
#define tr(c, itr) for(itr = (c).begin(); itr != (c).end(); itr++)
string convertstring(ll n) { stringstream ss; ss << n ; return ss.str(); }
#define deb(x) cout << "this is the value ->" << x << endl;
#define MOD 1000000007
#define inf 1000000010
#define all(v) v.begin(),v.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef pair<int,int> pp;
const int N = 200002;
int a[N];
int h[N * 10];
int cn[N*10];
priority_queue < pp , std::vector<pp> , greater< pp > > q  ;
int main()
{
    int  n ,m , i;
    S(n); S(m);
    fl(i , 0, n ) S(a[i]);
    int flag =1 ;
    fl(i,0 , n ){
        cn[a[i]]++;
        if(a[i] == m){
            flag =0 ;
            while(1){
                if(q.size() == 0){
                    P(-1);return 0 ;
                }
                pp tp = q.top();
                if(tp.f < cn[m]) {
                    h[tp.s] = 0 ;
                    q.pop();
                }
                else break;
            }
        }
        else{
            if( flag == 1 ){
                h[a[i]] = 1;
                q.push(mp(cn[a[i]] , a[i]));}
            else{
                if(h[a[i]]) q.push(mp(cn[a[i]] , a[i]));
            }
        }
    }
    if(q.size() == 0){
        P(-1); return 0 ;
    }
    else{
        pp tp = q.top();
        int val = cn[m];
        while(1){
                if(q.size() == 0){
                    P(-1);return 0 ;
                }
                pp tp = q.top();
                if(tp.f < cn[m]) {
                    q.pop();
                }
                else if(h[tp.s]){
                    cout << tp.s << endl;
                    return 0 ;
                }
            }
            P(-1);
    }
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define w(t) while(t--)
#define ll long long
#define S(x) scanf("%d",&x)
#define SLL(x) scanf("%lld",&x)
#define P(x) printf("%d\n",x)
#define fl(i , a, b) for(i = (int)a; i<(int)b; i++)
#define mem(a , value) memset(a , value , sizeof(a))
#define tr(c, itr) for(itr = (c).begin(); itr != (c).end(); itr++)
string convertstring(ll n) { stringstream ss; ss << n ; return ss.str(); }
#define deb(x) cout << "this is the value ->" << x << endl;
#define MOD 1000000007
#define inf 1000000010
#define all(v) v.begin(),v.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef pair<int,int> pp;
const int N = 200002;
int a[N];
int cn[N*10];
priority_queue < pp , std::vector<pp> , greater< pp > > q  ;
int main()
{
    int  n ,m , i;
    S(n); S(m);
    fl(i , 0, n ) S(a[i]);
    fl(i,0 , n ){
        cn[a[i]]++;
        if(a[i] == m){
            while(1){
                if(q.size() == 0){
                    P(-1);return 0 ;
                }
                pp tp = q.top();
                if(tp.f < cn[m]) {
                    q.pop();
                }
                else break;
            }
        }
        else{
            q.push(mp(cn[a[i]] , a[i]));
        }
    }
    if(q.size() == 0){
        P(-1); return 0 ;
    }
    else{
        pp tp = q.top();
        int val = cn[m];
        while(1){
                if(q.size() == 0){
                    P(-1);return 0 ;
                }
                pp tp = q.top();
                if(tp.f < cn[m]) {
                    q.pop();
                }
                else{
                    cout << tp.s << endl;
                    return 0 ;
                }
            }
            P(-1);
    }
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define w(t) while(t--)
#define ll long long
#define S(x) scanf("%d",&x)
#define SLL(x) scanf("%lld",&x)
#define P(x) printf("%d\n",x)
#define fl(i , a, b) for(i = (int)a; i<(int)b; i++)
#define mem(a , value) memset(a , value , sizeof(a))
#define tr(c, itr) for(itr = (c).begin(); itr != (c).end(); itr++)
string convertstring(ll n) { stringstream ss; ss << n ; return ss.str(); }
#define deb(x) cout << "this is the value ->" << x << endl;
#define MOD 1000000007
#define inf 1000000010
#define all(v) v.begin(),v.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef pair<int,int> pp;
const int N = 200002;
int a[N];
int h[N * 10];
int cn[N*10];
priority_queue < pp , std::vector<pp> , greater< pp > > q  ;
int main()
{
    int  n ,m , i;
    S(n); S(m);
    fl(i , 0, n ) S(a[i]);
    int flag =1 ;
    fl(i,0 , n ){
        cn[a[i]]++;
        if(a[i] == m){
            flag =0 ;
            while(1){
                if(q.size() == 0){
                    P(-1);return 0 ;
                }
                pp tp = q.top();
                int val = max(tp.f,  cn[tp.s]);
                if(tp.f < cn[m]) {
                    if(val < cn[m]) h[tp.s] = 0 ;
                    q.pop();
                }
                else break;
            }
        }
        else{
            if( flag == 1 ){
                h[a[i]] = 1;
                q.push(mp(cn[a[i]] , a[i]));}
            else{
                if(h[a[i]]) q.push(mp(cn[a[i]] , a[i]));
            }
        }
    }
    if(q.size() == 0){
        P(-1); return 0 ;
    }
    else{
        pp tp = q.top();
        int val = cn[m];
        while(1){
                if(q.size() == 0){
                    P(-1);return 0 ;
                }
                pp tp = q.top();
                if((tp.f >= cn[m] ) && (h[tp.s]) ) {
                    cout << tp.s << endl;
                    return 0 ;
                }
                else {
                    q.pop();
                }
            }
            P(-1);
    }/*badshah*/
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define w(t) while(t--)
#define ll long long
#define S(x) scanf("%d",&x)
#define SLL(x) scanf("%lld",&x)
#define P(x) printf("%d\n",x)
#define fl(i , a, b) for(i = (int)a; i<(int)b; i++)
#define mem(a , value) memset(a , value , sizeof(a))
#define tr(c, itr) for(itr = (c).begin(); itr != (c).end(); itr++)
string convertstring(ll n) { stringstream ss; ss << n ; return ss.str(); }
#define deb(x) cout << "this is the value ->" << x << endl;
#define MOD 1000000007
#define inf 1000000010
#define all(v) v.begin(),v.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef pair<int,int> pp;
const int N = 200002;
int a[N];
int h[N * 10];
int cn[N*10];
priority_queue < pp , std::vector<pp> , greater< pp > > q  ;
int main()
{
    int  n ,m , i;
    S(n); S(m);
    fl(i , 0, n ) S(a[i]);
    int flag =1 ;
    fl(i,0 , n ){
        cn[a[i]]++;
        if(a[i] == m){
            flag =0 ;
            while(1){
                if(q.size() == 0){
                    P(-1);return 0 ;
                }
                pp tp = q.top();
                if(tp.f < cn[m]) {
                    h[tp.s] = 0 ;
                    q.pop();
                }
                else break;
            }
        }
        else{
            if( flag == 1 ){
                h[a[i]] = 1;
                q.push(mp(cn[a[i]] , a[i]));}
            else{
                if(h[a[i]]) q.push(mp(cn[a[i]] , a[i]));
            }
        }
    }
    if(q.size() == 0){
        P(-1); return 0 ;
    }
    else{
        pp tp = q.top();
        int val = cn[m];
        while(1){
                if(q.size() == 0){
                    P(-1);return 0 ;
                }
                pp tp = q.top();
                if(tp.f < cn[m]) {
                    q.pop();
                }
                else{
                    cout << tp.s << endl;
                    return 0 ;
                }
            }
            P(-1);
    }
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define w(t) while(t--)
#define ll long long
#define S(x) scanf("%d",&x)
#define SLL(x) scanf("%lld",&x)
#define P(x) printf("%d\n",x)
#define fl(i , a, b) for(i = (int)a; i<(int)b; i++)
#define mem(a , value) memset(a , value , sizeof(a))
#define tr(c, itr) for(itr = (c).begin(); itr != (c).end(); itr++)
string convertstring(ll n) { stringstream ss; ss << n ; return ss.str(); }
#define deb(x) cout << "this is the value ->" << x << endl;
#define MOD 1000000007
#define inf 1000000010
#define all(v) v.begin(),v.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef pair<int,int> pp;
const int N = 200002;
int a[N];
int cn[N*10];
priority_queue < pp , std::vector<pp> , greater< pp > > q  ;
int main()
{
    int  n ,m , i;
    S(n); S(m);
    fl(i , 0, n ) S(a[i]);
    fl(i,0 , n ){
        cn[a[i]]++;
        if(a[i] == m)
        {
            while(1){
                if(q.size() == 0){
                    P(-1);return 0 ;
                }
                pp tp = q.top();
                if(tp.f < cn[m]) {
                    q.pop();
                }
                else break;
            }
        }
        else{
            q.push(mp(cn[a[i]] , a[i]));
        }
    }
    if(q.size() == 0){
        P(-1); return 0 ;
    }
    else{
        pp tp = q.top();
        cout << tp.s << endl;
    }
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define w(t) while(t--)
#define ll long long
#define S(x) scanf("%d",&x)
#define SLL(x) scanf("%lld",&x)
#define P(x) printf("%d\n",x)
#define fl(i , a, b) for(i = (int)a; i<(int)b; i++)
#define mem(a , value) memset(a , value , sizeof(a))
#define tr(c, itr) for(itr = (c).begin(); itr != (c).end(); itr++)
string convertstring(ll n) { stringstream ss; ss << n ; return ss.str(); }
#define deb(x) cout << "this is the value ->" << x << endl;
#define MOD 1000000007
#define inf 1000000010
#define all(v) v.begin(),v.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef pair<int,int> pp;
const int N = 200002;
int a[N];
int h[N * 10];
int cn[N*10];
priority_queue < pp , std::vector<pp> , greater< pp > > q  ;
int main()
{
    int  n ,m , i;
    S(n); S(m);
    fl(i , 0, n ) S(a[i]);
    int flag =1 ;
    fl(i,0 , n ){
        cn[a[i]]++;
        if(a[i] == m){
            flag =0 ;
            while(1){
                if(q.size() == 0){
                    P(-1);return 0 ;
                }
                pp tp = q.top();
                if(tp.f < cn[m]) {
                    h[tp.s] = 0 ;
                    q.pop();
                }
                else break;
            }
        }
        else{
            if( flag == 1 ){
                h[a[i]] = 1;
                q.push(mp(cn[a[i]] , a[i]));}
            else{
                if(h[a[i]]) q.push(mp(cn[a[i]] , a[i]));
            }
        }
    }
    if(q.size() == 0){
        P(-1); return 0 ;
    }
    else{
        pp tp = q.top();
        int val = cn[m];
        while(1){
                if(q.size() == 0){
                    P(-1);return 0 ;
                }
                pp tp = q.top();
                if((tp.f >= cn[m] ) && (h[tp.s]) ) {
                    cout << tp.s << endl;
                    return 0 ;
                }
                else {
                    q.pop();
                }
            }
            P(-1);
    }/*yo yo honeysingh*/
    return 0 ;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010
struct Node{
    int w,f;
}l[N];
int i,j,k,n,m,x,a[N],c[N<<2],w[N<<4],Num,y;
bool b[N];
inline bool Cmp(Node a,Node b){
    return a.w<b.w;
}
inline int Min(int x,int y){return x<y?x:y;}
inline void Update(int x,int l,int r,int y){
    if(l==r){
        c[x]++;
        return;
    }
    int Mid=l+r>>1;
    if(y<=Mid)Update(x<<1,l,Mid,y);else Update(x<<1|1,Mid+1,r,y);
    c[x]=Min(c[x<<1],c[x<<1|1]);
}
inline int Find(int x,int l,int r){
    if(l==r){
        c[x]=n+1;
        return l;
    }
    int Mid=l+r>>1,y;
    if(c[x<<1]<c[x<<1|1])y=Find(x<<1,l,Mid);else y=Find(x<<1|1,Mid+1,r);
    c[x]=Min(c[x<<1],c[x<<1|1]);
    return y;
}
inline void Solve(int x){
    while(c[1]<x)b[Find(1,1,Num)]=1;
}
int main(){
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)scanf("%d",&l[i].w),l[i].f=i;
    sort(l+1,l+n+1,Cmp);
    a[l[1].f]=Num=1;w[1]=l[1].w;
    for(i=2;i<=n;i++)
    if(l[i].w==l[i-1].w)a[l[i].f]=Num;else a[l[i].f]=++Num,w[Num]=l[i].w;
    for(i=1;i<=Num;i++)if(w[i]==m){m=i;break;}
    if(i>Num){
        if(m>1)puts("1");else puts("2");
        return 0;
    }
    for(i=1;i<=n;i++){
        Update(1,1,Num,a[i]);
        if(a[i]==m)Solve(++k);
    }
    for(i=1;i<=Num;i++)
    if(i!=m&&!b[i]){
        printf("%d\n",w[i]);
        return 0;
    }
    puts("-1");
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
#include<set>
#include<stack>
#define ll long long
#define pb push_back
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define cls(name,x) memset(name,x,sizeof(name))
using namespace std;
const int inf=1e9+10;
const int maxn=1e6+10;
const int maxm=20;
const int mod=1e9+7;
const double pi=acos(-1.0);
int n,A;
struct node
{
    int id;
    int c;
}color[maxn];
struct cmp
{
    bool operator()(const node &a,const node &b)
    {
            return a.c<b.c;
    }
};
int ans[maxn];
int del[maxn];
int main()
{
    //freopen("in.txt","r",stdin); while(~scanf("%d %d",&n,&A))
    {
        multiset<node,cmp> S;
        for(int i=1;i<=1e6;i++)
        {
            color[i].id=i;
            color[i].c=0;
            S.insert(color[i]);
        }
        for(int i=1;i<=1e6;i++)
            ans[i]=1;
        ans[A]=0;
        multiset<node,cmp>::iterator it,ttt;
        for(int i=0;i<n;i++)
        {
            int x;
            scanf("%d",&x);
            if(x==A||ans[x]==1)
            {
                S.erase(color[x]);
                color[x].c++;
                S.insert(color[x]);
            }
            int k=0;
            for(it=S.begin();(*it).c<color[A].c;it++)
                del[k++]=(*it).id;
            for(int j=0;j<k;j++)
            {
                ans[del[j]]=0;
                S.erase(color[del[j]]);
            }
        }
        int flag=0;
        for(int i=1;i<=1e6;i++)
            if(ans[i]!=0) flag=i;
        if(flag==0) printf("-1");
        else printf("%d\n",flag);
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define pii pair<int,int>
#define mmp map<int,int>
#define mp make_pair
using namespace std;

const int maxn = 1e5+7;
typedef long long LL;
typedef long long ll;
int n,a,c[maxn];
mmp md;
struct cmp{
    bool operator() (pii a,pii b){return a.first< b.first;}
};

int main()
{
    int cnt=0;
    scanf("%d%d",&n,&a);
    for(int i=1;i<=n;i++) scanf("%d",&c[i]);
    priority_queue<pii,vector<pii>,cmp> q;
    FOR(i,1,n){
        if(c[i]==a){
            cnt++;
            while(q.size()&&q.top().second<cnt){
                q.pop();
            }
        }else{
            md[c[i]]++;
            if(md[c[i]]>cnt) q.push(mp(c[i],md[c[i]]));
        }
    }
    if(q.size()){
        printf("%d\n",q.top().first);
    }else{
        printf("-1\n");
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define pii pair<int,int>
#define mmp map<int,int>
#define mp make_pair
using namespace std;

const int maxn = 1e6+7;
typedef long long LL;
typedef long long ll;
int n,a,c[maxn],vis[maxn];
mmp md;
struct cmp{
    bool operator() (pii a,pii b){return a.first< b.first;}
};

int main()
{
    int cnt=0;
    memset(vis,0,sizeof vis );
    scanf("%d%d",&n,&a);
    for(int i=1;i<=n;i++) scanf("%d",&c[i]);
    priority_queue<pii,vector<pii>,cmp> q;
    FOR(i,1,n){
        if(c[i]==a){
            cnt++;
            while(q.size()&&q.top().second<cnt){
                q.pop();
            }
        }else{
            md[c[i]]++;
            if(md[c[i]]>cnt&&!vis[c[i]]) q.push(mp(c[i],md[c[i]]));
            else vis[c[i]] = 1;
        }
    }
    if(q.size()){
        printf("%d\n",q.top().first);
    }else{
        printf("-1\n");
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define pii pair<int,int>
#define mmp map<int,int>
#define mp make_pair
using namespace std;

const int maxn = 1e5+7;
typedef long long LL;
typedef long long ll;
int n,a,c[maxn],vis[maxn];
mmp md;
struct cmp{
    bool operator() (pii a,pii b){return a.first< b.first;}
};

int main()
{
    int cnt=0;
    memset(vis,0,sizeof vis );
    scanf("%d%d",&n,&a);
    for(int i=1;i<=n;i++) scanf("%d",&c[i]);
    priority_queue<pii,vector<pii>,cmp> q;
    FOR(i,1,n){
        if(c[i]==a){
            cnt++;
            while(q.size()&&q.top().second<cnt){
                q.pop();
            }
        }else{
            md[c[i]]++;
            if(md[c[i]]>cnt&&!vis[c[i]]) q.push(mp(c[i],md[c[i]]));
            else vis[c[i]] = 1;
        }
    }
    if(q.size()){
        printf("%d\n",q.top().first);
    }else{
        printf("-1\n");
    }
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
#include<set>
#include<stack> #define ll long long #define pb push_back #define max(x,y) ((x)>(y)?(x):(y)) #define min(x,y) ((x)>(y)?(y):(x)) #define cls(name,x) memset(name,x,sizeof(name)) using namespace std;
const int inf=1e9+10;
const int maxn=1e6+10;
const int maxm=20;
const int mod=1e9+7;
const double pi=acos(-1.0);
int n,A;
struct node
{
    int id;
    int c;
}color[maxn];
struct cmp
{
    bool operator()(const node &a,const node &b)
    {
            return a.c<b.c;
    }
};
int ans[maxn];
int del[maxn];
int main()
{
    //freopen("in.txt","r",stdin); while(~scanf("%d %d",&n,&A))
    {
        multiset<node,cmp> S;
        for(int i=1;i<=1e6;i++)
        {
            color[i].id=i;
            color[i].c=0;
            S.insert(color[i]);
        }
        for(int i=1;i<=1e6;i++)
            ans[i]=1;
        ans[A]=0;
        multiset<node,cmp>::iterator it,ttt;
        for(int i=0;i<n;i++)
        {
            int x;
            scanf("%d",&x);
            if(x==A||ans[x]==1)
            {
                S.erase(color[x]);
                color[x].c++;
                S.insert(color[x]);
            }
            int k=0;
            for(it=S.begin();(*it).c<color[A].c;it++)
                del[k++]=(*it).id;
            for(int j=0;j<k;j++)
            {
                ans[del[j]]=0;
                S.erase(color[del[j]]);
            }
        }
        int flag=0;
        for(int i=1;i<=1e6;i++)
            if(ans[i]!=0) flag=i;
        if(flag==0) printf("-1");
        else printf("%d\n",flag);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
int x[1000001];
int main() {
	map<int,int>a;
	map<int,int>::iterator it;
	int n,A,k=0,na=1;
	scanf("%d%d",&n,&A);

	for(int i=0;i<n;i++)
	{
		scanf("%d",&x[i]);

		if(x[i]==A&&k!=0&&a.size())
		{
			++na;
			for(it=a.begin();it!=a.end();it++)
			{
				if(it->second<na&&it!=a.end())it=a.erase(it);
				if(it==a.end())break;
			}
			for(it=a.begin();it!=a.end();it++)
			{
				if(it->second<na&&it!=a.end())it=a.erase(it);
				if(it==a.end())break;
			}
		}

		else if(x[i]==A&&k==0)
		{
			if(i==0){printf("-1\n");return 0;}
			k=i;
			for(int j=0;j<i;j++)
			{
				if(a.count(x[j]))a[x[j]]++;
				else a.insert(pair<int,int>(x[j],1));
			}
		}
		else if(k!=0&&a.count(x[i]))a[x[i]]++;
	}
	if(k==0){printf("%d\n",x[0]);return 0;}
	for(it=a.begin();it!=a.end();it++)
	if(it->second<na)it=a.erase(it);
	if(a.begin()==a.end())printf("-1\n");
	else printf("%d\n",a.begin()->second);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
int x[1000001];
int main() {
	map<int,int>a;
	map<int,int>::iterator it;
	int n,A,k=0,na=1;
	scanf("%d%d",&n,&A);

	for(int i=0;i<n;i++)
	{
		scanf("%d",&x[i]);
		if(x[i]==A&&k!=0)na++;
		else if(x[i]==A&&k==0)
		{
			if(i==0){printf("-1\n");return 0;}
			k=i;
			for(int j=0;j<i;j++)
			{
				if(a.count(x[j]))a[x[j]]++;
				else a.insert(pair<int,int>(x[j],1));
			}
		}
		else if(k!=0&&a.count(x[i]))a[x[i]]++;
	}
	if(k==0){printf("%d\n",x[0]);return 0;}
	for(it=a.begin();it!=a.end();it++)
	{
		if(it->second<na)it=a.erase(it);
		if(it==a.end())break;
	}
	for(it=a.begin();it!=a.end();it++)
	{
		if(it->second<na)it=a.erase(it);
		if(it==a.end())break;
	}
//	printf("na=%d\n",na);
//	for(it=a.begin();it!=a.end();it++)
//	printf("x=%d n=%d\n",it->first,it->second);
	if(a.begin()==a.end())printf("-1\n");
	else printf("%d\n",a.begin()->second);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
int x[1000001];
int main() {
	map<int,int>a;
	map<int,int>::iterator it;
	int n,A,k=0,na=1;
	scanf("%d%d",&n,&A);

	for(int i=0;i<n;i++)
	{
		scanf("%d",&x[i]);
		if(x[i]==A&&k!=0)na++;
		else if(x[i]==A&&k==0)
		{
			if(i==0){printf("-1\n");return 0;}
			k=i;
			for(int j=0;j<i;j++)
			{
				if(a.count(x[j]))a[x[j]]++;
				else a.insert(pair<int,int>(x[j],1));
			}
		}
		else if(k!=0&&a.count(x[i]))a[x[i]]++;
	}
	if(k==0){printf("%d\n",x[0]);return 0;}
	for(it=a.begin();it!=a.end();it++)
	if(it->second<na)it=a.erase(it);
//	printf("na=%d\n",na);
//	for(it=a.begin();it!=a.end();it++)
//	printf("x=%d n=%d\n",it->first,it->second);
	if(a.begin()==a.end())printf("-1\n");
	else printf("%d\n",a.begin()->second);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
int x[1000001];
int main() {
	map<int,int>a;
	map<int,int>::iterator it;
	int n,A,k=0,na=1;
	scanf("%d%d",&n,&A);

	for(int i=0;i<n;i++)
	{
		scanf("%d",&x[i]);
		if(x[i]==A&&k!=0)
		{
			na++;it=a.begin();
			while(it!=a.end())
			{
				if(it->second<na)it=a.erase(it);
				else it++;
			}
		}
		else if(x[i]==A&&k==0)
		{
			if(i==0){printf("-1\n");return 0;}
			k=i;
			for(int j=0;j<i;j++)
			{
				if(a.count(x[j]))a[x[j]]++;
				else a.insert(pair<int,int>(x[j],1));
			}
		}
		else if(k!=0&&a.count(x[i]))a[x[i]]++;
	}
	if(k==0){printf("%d\n",x[0]);return 0;}
//	printf("na=%d\n",na);
//	for(it=a.begin();it!=a.end();it++)
//	printf("x=%d n=%d\n",it->first,it->second);
	if(a.begin()==a.end())printf("-1\n");
	else printf("%d\n",a.begin()->second);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
int x[1000001];
int main() {
	map<int,int>a;
	map<int,int>::iterator it;
	int n,A,k=0,na=1;
	scanf("%d%d",&n,&A);

	for(int i=0;i<n;i++)
	{
		scanf("%d",&x[i]);
		if(x[i]==A&&k!=0)
		{
			na++;it=a.begin();
			while(it!=a.end())
			{
				if(it->second<na)it=a.erase(it);
				else it++;
			}
		}
		else if(x[i]==A&&k==0)
		{
			if(i==0){printf("-1\n");return 0;}
			k=i;
			for(int j=0;j<i;j++)
			{
				if(a.count(x[j]))a[x[j]]++;
				else a.insert(pair<int,int>(x[j],1));
			}
		}
		else if(k!=0&&a.count(x[i]))a[x[i]]++;
	}
	if(k==0){printf("%d\n",x[0]);return 0;}
//	printf("na=%d\n",na);
//	for(it=a.begin();it!=a.end();it++)
//	printf("x=%d n=%d\n",it->first,it->second);
	if(a.begin()==a.end())printf("-1\n");
	else printf("%d\n",a.begin()->first);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
int x[1000001];
int main() {
	set<int>a;
	set<int>::iterator it;
	int n,A,k=0;
	scanf("%d%d",&n,&A);

	for(int i=0;i<n;i++)
	{
		scanf("%d",&x[i]);
		if(x[i]==A&&k==0)
		{
			if(i==0){printf("-1\n");return 0;}
			k=i;
			for(int j=0;j<i;j++)
			a.insert(x[j]);
		}
	}

	if(k==0){printf("%d\n",x[0]);return 0;}
	if(a.size()==1){printf("%d\n",*a.begin());return 0;}

	for(it=a.begin();it!=a.end();it++)
	{
		int na=0,nb=0,l=0;
		for(int i=0;i<n;i++)
		{
			if(x[i]==*it)nb++;
			if(x[i]==A)na++;
			if(na>nb){l=1;break;}
		}
		if(l==0){printf("%d\n",*it);return 0;}
	}
	if(it==a.end())printf("-1\n");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
int x[1000001];
int main() {
	set<int>a;
	set<int>::iterator it;
	int n,A,k=0;
	scanf("%d%d",&n,&A);

	for(int i=0;i<n;i++)
	{
		scanf("%d",&x[i]);
		if(x[i]==A&&k==0)
		{
			if(i==0){printf("-1\n");return 0;}
			k=i;
			for(int j=0;j<i;j++)
			a.insert(x[j]);
		}
	}

	if(k==0){printf("%d\n",x[0]);return 0;}

	for(it=a.begin();it!=a.end();it++)
	{
		int na=0,nb=0,l=0;
		for(int i=0;i<n;i++)
		{
			if(x[i]==*it)nb++;
			if(x[i]==A)na++;
			if(na>nb){l=1;break;}
		}
		if(l==0){printf("%d\n",*it);return 0;}
	}
	if(it==a.end())printf("-1\n");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
int x[1000001];
int main() {
	map<int,int>a;
	map<int,int>::iterator it;
	int n,A,k=0,na=1;
	scanf("%d%d",&n,&A);

	for(int i=0;i<n;i++)
	{
		scanf("%d",&x[i]);

		if(x[i]==A&&k!=0)
		{
			na++;
			for(it=a.begin();it!=a.end();it++)
			if(it->second<na)it=a.erase(it);
		}

		else if(x[i]==A&&k==0)
		{
			if(i==0){printf("-1\n");return 0;}
			k=i;
			for(int j=0;j<i;j++)
			{
				if(a.count(x[j]!=0))a[x[j]]++;
				else a.insert(pair<int,int>(x[j],1));
			}
		}
		else if(k!=0&&a.count(x[i]))a[x[i]]++;
	}

	if(k==0){printf("%d\n",x[0]);return 0;}

	for(it=a.begin();it!=a.end();it++)
	if(it->second<na)it=a.erase(it);

	if(a.begin()==a.end())printf("-1\n");
	else printf("%d\n",a.begin()->second);

	return 0;
}


#include <bits/stdc++.h>
//#define ll long long
const int MX1=1000*1000+100;
const int inf=0xFFFFFFF;
using namespace std;

int n,a;
int ar[MX1];

bool decreas(int a,int b){
    return (a>b);
}

int miz[MX1];
int mizz[MX1];
set<int> ans;
set<int> anstmp;
int cnta;

int main(){
    cin>>n>>a;
    for(int i=0;i<n;i++){
        cin>>ar[i];
        if(ar[i]==a){
            cnta++;
        }else{
            mizz[ar[i]]++;
        }
    }
    for(int i=0;i<MX1;i++){
        if(mizz[ar[i]]>=cnta){
           ans.insert(ar[i]);
           anstmp.insert(ar[i]);
        }
    }
    if(cnta==0){
        int tmp=1;
        while(a==tmp){
            cerr<<1;
            tmp++;
        }
        cout<<tmp;
    }else{
        for(int i=0;i<n;i++){
            if(ar[i]==a){
                cnta++;
                for(auto it = ans.begin() ; it != ans.end() ; it++){
                    //cerr<<2;
                    cerr<<*it<<" ";
                    if(miz[*it]!=-1){
                        miz[*it]--;
                        if(miz[*it]<0){
                            auto its=anstmp.find(*it);
                            anstmp.erase(its);
                        }
                    }
                        cerr<<3;
                }
                ans.clear();
                for(set<int>::iterator it = anstmp.begin() ; it != anstmp.end() ; it++){
                    ans.insert(*it);
                }
            }else{
                if(miz[ar[i]]!=-1&&mizz[ar[i]]>=cnta){
                    cerr<<777<<endl;
                    miz[ar[i]]++;
                }
            }
        }
        bool tf=0;
        for(set<int>::iterator it = ans.begin() ; it != ans.end() ; it++){
                cout<<*it;
                tf=1;
                break;
                cerr<<3;
        }
        if(tf==0){
            cout<<-1;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
//#define ll long long
const int MX1=1000*1000+100;
const int inf=0xFFFFFFF;
using namespace std;

int n,a;
int ar[MX1];

bool decreas(int a,int b){
    return (a>b);
}

int miz[MX1];
int mizz[MX1];
set<int> ans;
set<int> anstmp;
int cnta;

int main(){
    cin>>n>>a;
    for(int i=0;i<n;i++){
        cin>>ar[i];
        if(ar[i]==a){
            cnta++;
        }else{
            mizz[ar[i]]++;
        }
    }
    for(int i=0;i<MX1;i++){
        if(mizz[i]>=cnta&&cnta!=0){
            //cerr<<i<<endl;
           ans.insert(i);
           anstmp.insert(i);
        }
    }
    if(cnta==0){
        int tmp=1;
        while(a==tmp){
            cerr<<1<<endl;
            tmp++;
        }
        cout<<tmp;
    }else{
        for(int i=0;i<n;i++){
            if(ar[i]==a){
                for(auto it = ans.begin() ; it != ans.end() ; it++){
                    //cerr<<2;
                    cerr<<*it<<endl;
                    if(miz[*it]!=-1){
                        miz[*it]--;
                        if(miz[*it]<0){
                            auto its=anstmp.find(*it);
                            anstmp.erase(its);
                        }
                    }
                }
                ans.clear();
                for(set<int>::iterator it = anstmp.begin() ; it != anstmp.end() ; it++){
                    ans.insert(*it);
                }
            }else{
                if(miz[ar[i]]!=-1&&mizz[ar[i]]>=cnta){
                    cerr<<777<<endl;
                    miz[ar[i]]++;
                }
            }
        }
        bool tf=0;
        for(set<int>::iterator it = ans.begin() ; it != ans.end() ; it++){
                cout<<*it;
                tf=1;
                break;
                cerr<<3<<endl;
        }
        if(tf==0){
            cout<<-1;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
//#define ll long long
const int MX1=1000*1000+100;
const int inf=0xFFFFFFF;
using namespace std;

int n,a;
int ar[MX1];

bool decreas(int a,int b){
    return (a>b);
}

int miz[MX1];
set<int> ans;
set<int> anstmp;
int cnta;

int main(){
    cin>>n>>a;
    for(int i=0;i<n;i++){
        cin>>ar[i];
        if(ar[i]==a){
            cnta++;
        }else{
            ans.insert(ar[i]);
            anstmp.insert(ar[i]);
        }
    }
    if(cnta==0){
        int tmp=1;
        while(a==tmp){
            cerr<<1;
            tmp++;
        }
        cout<<tmp;
    }else{
        cnta=0;
        for(int i=0;i<n;i++){
            if(ar[i]==a){
                cnta++;
                for(auto it = ans.begin() ; it != ans.end() ; it++){
                    //cerr<<2;
                    cerr<<*it<<" ";
                    if(miz[*it]!=-1){
                        miz[*it]--;
                        if(miz[*it]<0){
                            auto its=anstmp.find(*it);
                            anstmp.erase(its);
                        }
                    }
                        cerr<<3;
                }
                ans.clear();
                for(set<int>::iterator it = anstmp.begin() ; it != anstmp.end() ; it++){
                    ans.insert(*it);
                }
            }else{
                if(miz[ar[i]]!=-1){
                    cerr<<777<<endl;
                    miz[ar[i]]++;
                }
            }
        }
        bool tf=0;
        for(set<int>::iterator it = ans.begin() ; it != ans.end() ; it++){
                cout<<*it;
                tf=1;
                break;
                cerr<<3;
        }
        if(tf==0){
            cout<<-1;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
//#define ll long long
const int MX1=1000*1000+100;
const int inf=0xFFFFFFF;
using namespace std;

int n,a;
int ar[MX1];

bool decreas(int a,int b){
    return (a>b);
}

int miz[MX1];
int mizz[MX1];
set<int> ans;
set<int> anstmp;
int cnta;

int main(){
    cin>>n>>a;
    for(int i=0;i<n;i++){
        cin>>ar[i];
        if(ar[i]==a){
            cnta++;
        }else{
            mizz[ar[i]]++;
        }
    }
    for(int i=0;i<MX1;i++){
        if(mizz[i]>=cnta){
            cerr<<i<<endl;
           ans.insert(i);
           anstmp.insert(i);
        }
    }
    if(cnta==0){
        int tmp=1;
        while(a==tmp){
            cerr<<1<<endl;
            tmp++;
        }
        cout<<tmp;
    }else{
        for(int i=0;i<n;i++){
            if(ar[i]==a){
                for(auto it = ans.begin() ; it != ans.end() ; it++){
                    //cerr<<2;
                    cerr<<*it<<endl;
                    if(miz[*it]!=-1){
                        miz[*it]--;
                        if(miz[*it]<0){
                            auto its=anstmp.find(*it);
                            anstmp.erase(its);
                        }
                    }
                }
                ans.clear();
                for(set<int>::iterator it = anstmp.begin() ; it != anstmp.end() ; it++){
                    ans.insert(*it);
                }
            }else{
                if(miz[ar[i]]!=-1&&mizz[ar[i]]>=cnta){
                    cerr<<777<<endl;
                    miz[ar[i]]++;
                }
            }
        }
        bool tf=0;
        for(set<int>::iterator it = ans.begin() ; it != ans.end() ; it++){
                cout<<*it;
                tf=1;
                break;
                cerr<<3<<endl;
        }
        if(tf==0){
            cout<<-1;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
//#define ll long long
const int MX1=1000*1000+100;
const int inf=0xFFFFFFF;
using namespace std;

int n,a;
int ar[MX1];

bool decreas(int a,int b){
    return (a>b);
}

int miz[MX1];
set<int> ans;
set<int> anstmp;
int cnta;

int main(){
    cin>>n>>a;
    for(int i=0;i<n;i++){
        cin>>ar[i];
        if(ar[i]==a){
            cnta++;
        }else{
            ans.insert(ar[i]);
            anstmp.insert(ar[i]);
        }
    }
    if(cnta==0){
        int tmp=1;
        while(a==tmp){
            cerr<<1;
            tmp++;
        }
        cout<<tmp<<" "<<1;
    }else{
        cnta=0;
        for(int i=0;i<n;i++){
            if(ar[i]==a){
                cnta++;
                for(auto it = ans.begin() ; it != ans.end() ; it++){
                    //cerr<<2;
                    cerr<<*it<<" ";
                    if(miz[*it]!=-1){
                        miz[*it]--;
                        if(miz[*it]<0){
                            auto its=anstmp.find(*it);
                            anstmp.erase(its);
                        }
                    }
                        cerr<<3;
                }
                ans.clear();
                for(set<int>::iterator it = anstmp.begin() ; it != anstmp.end() ; it++){
                    ans.insert(*it);
                }
            }else{
                if(miz[ar[i]]!=-1){
                    cerr<<777<<endl;
                    miz[ar[i]]++;
                }
            }
        }
        bool tf=0;
        for(set<int>::iterator it = ans.begin() ; it != ans.end() ; it++){
                cout<<*it;
                tf=1;
                break;
                cerr<<3;
        }
        if(tf==0){
            cout<<-1;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
map<int,pair<int,int>>mp;
main()
{
	long long i,n,A,B;
	cin>>n>>A;int a[n];
	mp[A].s=1;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
		mp[a[i]].f++;
		if(mp[a[i]].f<mp[A].f)mp[a[i]].s=1;
	}
	if(a[0]=='A')return cout<<-1,0;
	map<int,pair<int,int>>::iterator mit;
	for(mit=mp.begin();mit!=mp.end();mit++)
	{
		if((*(mit)).s.s==0&&(*(mit)).f!=A)return cout<<(*(mit)).f,0;
	}
	if(!mp.size())
	{
		if(A==1)cout<<2;
		else cout<<1;
	}
	cout<<-1;
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
map<int,pair<int,int>>mp;
main()
{
	long long i,n,A,B;
	cin>>n>>A;int a[n];
	mp[A].s=1;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
		mp[a[i]].f++;
		if(mp[a[i]].f<mp[A].f)mp[a[i]].s=1;
	}
	map<int,pair<int,int>>::iterator mit;
	for(mit=mp.begin();mit!=mp.end();mit++)
	{
		if((*(mit)).s.s==0&&(*(mit)).f!=A)return cout<<(*(mit)).f,0;
	}
	if(!mp.size())
	{
		if(A==1)cout<<2;
		else cout<<1;
	}
	cout<<-1;
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
map<int,pair<int,int>>mp;
main()
{
	long long i,n,A,B;
	cin>>n>>A;int a[n];
	for(i=0;i<n;++i)cin>>a[i];
	mp[A].s=1;
	for(i=0;i<n;++i)
	{
		mp[a[i]].f++;
		if(mp[a[i]].f<mp[A].f)mp[a[i]].s=1;
	}
	map<int,pair<int,int>>::iterator mit;
	for(mit=mp.begin();mit!=mp.end();mit++)
	{
		if((*(mit)).s.s==0)return cout<<(*(mit)).f,0;
	}
	cout<<-1;
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
map<int,pair<int,int>>mp;
main()
{
	long long i,n,A,B;
	cin>>n>>A;int a[n];
	mp[A].s=1;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
		if(mp[A].f>mp[a[i]].f)mp[a[i]].s=1;
		mp[a[i]].f++;
		if(mp[a[i]].f<mp[A].f)mp[a[i]].s=1;
	}
	map<int,pair<int,int>>::iterator mit;
	for(mit=mp.begin();mit!=mp.end();mit++)
	{
		if((*(mit)).s.s==0&&(*(mit)).f!=A)ans.push_back((*(mit)).f);
	}
	for(i=0;i<ans.size();++i)
	{
		if(mp[ans[i]].s.f>=mp[A].s.f)return cout<<ans[i],0;
	}
	cout<<-1;
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
map<int,pair<int,int>>mp;
vector<int>ans;
main()
{
	int i,n,A,B;
	cin>>n>>A;int a[n];
	mp[A].s=1;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
		if(mp[A].f>mp[a[i]].f)mp[a[i]].s=1;
		mp[a[i]].f++;
		if(mp[a[i]].f<mp[A].f)mp[a[i]].s=1;
	}
	map<int,pair<int,int>>::iterator mit;
	for(mit=mp.begin();mit!=mp.end();mit++)
	{
		if((*(mit)).s.s==0&&(*(mit)).f!=A)ans.push_back((*(mit)).f);
	}
	for(i=0;i<ans.size();++i)
	{
		if(mp[ans[i]].s.f>=mp[A].s.f)return cout<<ans[i],0;
	}
	cout<<-1;
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
map<int,pair<int,int>>mp;
vector<int>ans;
main()
{
	int i,n,A,B;
	cin>>n>>A;int a[n];
	mp[A].s=1;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
		if(mp[A].f>mp[a[i]].f)mp[a[i]].s=1;
		mp[a[i]].f++;
		if(mp[a[i]].f<mp[A].f)mp[a[i]].s=1;
	}
	map<int,pair<int,int>>::iterator mit;
	for(mit=mp.begin();mit!=mp.end();mit++)
	{
		if((*(mit)).s.s==0&&(*(mit)).f!=A)ans.push_back((*(mit)).f);
	}
	for(i=0;i<ans.size();++i)
	{
		if(mp[ans[i]].f>=mp[A].f)return cout<<ans[i],0;
	}
	cout<<-1;
}


#include <iostream>
using namespace std;
int main (){
long long n,a,b;
cin>>n>>a;
long long t[n],nbr[100000]={0};
for (long long i=0; i<n;i++){
     cin>>t[i];
     nbr[t[i]]++;
}
for (long long i=0;i<n;i++){
    if (i==a) continue;
  if (nbr[i]>=nbr[a] && nbr[i]!=0) {
    cout<<i;
    return 0;
  }
}
  cout<<-1;
}


#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main (){
long long n,a,b;
cin>>n>>a;
long long t[n],nbr[n],app[n]={0},ind=0;
for (long long i=0; i<n;i++){
     cin>>t[i];
  ind+=(t[i]==a);
}
if (ind>ceil(n/2.0)){
    cout<<-1;
    return 0;
}

 sort (t,t+n);
 if (n!=1){
    long long j=0,x=1;
 for (long long i=0;i<n-1;i++){
    if (t[i]!=t[i+1]){
        nbr[j]=t[i];
        app[j]=x;
        j++;
        x=1;
    }else x++;
 }
 if (t[n-1]==t[n-2]){
      nbr[j]=t[n-1];
        app[j]=x;
        j++;
         }
for (long long i=0;i<j;i++){
        if (nbr[i]==a) continue;
  if (app[i]>=ind) {
    cout<<nbr[i];
    return 0;
  }
}
  cout<<-1;
  return 0;
}else {
  if (t[0]==a) cout<<-1;
  else cout<<t[0];

}
 }


#include <iostream>
using namespace std;
int main (){
long long n,a,b;
cin>>n>>a;
long long t[n],nbr[100000]={0};
for (long long i=0; i<n;i++){
     cin>>t[i];
     nbr[t[i]]++;
}
for (long long i=0;i<n;i++){
    if (i==a) continue;
  if (nbr[i]>=nbr[a]) {
    cout<<i;
    return 0;
  }
}
  cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
int main (){
ios_base::sync_with_stdio(false);
long long n,a;
cin>>n>>a;
long long t[n],app[1000006]={0};
for (long long i=0; i<n; i++){
        cin>>t[i];
        if (t[i]==a)
            app[a]++;
        else app[t[i]]+=(app[a]<=app[t[i]]);
}
for (long long  i=1; i<1000006;i++){
   if (i==a)continue ;
   if (app[i]>=app[a]){
        cout<<i;
   return 0;
   }
}
  cout<<-1;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main (){
long long n,a,b;
cin>>n>>a;
long long t[n],nbr[n],app[n]={0};
for (long long i=0; i<n;i++){
     cin>>t[i];
}
 sort (t,t+n);
 if (n!=1){
    long long j=0,x=1,ind=0;
 for (long long i=0;i<n-1;i++){
    if (t[i]!=t[i+1]){
        nbr[j]=t[i];
        if(nbr[j]==a)
            ind=j;
        app[j]=x;
        j++;
        x=1;
    }else x++;
 }
 if (t[n-1]==t[n-2]){
      nbr[j]=t[n-1];
        app[j]=x;
        j++;
         }
for (long long i=0;i<j;i++){
  if (app[i]>=app[ind] && nbr[i]!=nbr[ind] ) {
    cout<<nbr[i];
    return 0;
  }
}
  cout<<-1;
  return 0;
}else {
  if (t[0]==a) cout<<-1;
  else cout<<t[0];

}
 }


#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main (){
long long n,a,b;
cin>>n>>a;
long long t[n],nbr[n],app[n]={0},ind=0;
for (long long i=0; i<n;i++){
     cin>>t[i];
  ind+=(t[i]==a);
}
if (ind>=ceil(n/2.0)){
    cout<<-1;
    return 0;
}

 sort (t,t+n);
 if (n!=1){
    long long j=0,x=1;
 for (long long i=0;i<n-1;i++){
    if (t[i]!=t[i+1]){
        nbr[j]=t[i];
        app[j]=x;
        j++;
        x=1;
    }else x++;
 }
 if (t[n-1]==t[n-2]){
      nbr[j]=t[n-1];
        app[j]=x;
        j++;
         }
for (long long i=0;i<j;i++){
        if (nbr[i]==a) continue;
  if (app[i]>=ind) {
    cout<<nbr[i];
    return 0;
  }
}
  cout<<-1;
  return 0;
}else {
  if (t[0]==a) cout<<-1;
  else cout<<t[0];

}
 }


#include <iostream>
#include <algorithm>
using namespace std;
int main (){
long long n,a,b;
cin>>n>>a;
long long t[n],nbr[n],app[n]={0};
for (long long i=0; i<n;i++){
     cin>>t[i];
}
 sort (t,t+n);
 long long j=0,x=1,ind=0;
 for (long long i=0;i<n-1;i++){
    if (t[i]!=t[i+1]){
        nbr[j]=t[i];
        if(nbr[j]==a)
            ind=j;
        app[j]=x;
        j++;
        x=1;
    }else x++;
 }
 if (t[n-1]==t[n-2]){
      nbr[j]=t[n-1];
        app[j]=x;
        j++;
         }
for (long long i=0;i<j;i++){
  if (app[i]>=app[ind] && nbr[i]!=nbr[ind] ) {
    cout<<nbr[i];
    return 0;
  }
}
  cout<<-1;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
int main()
{
	int n,a;
	cin>>n>>a;
	int arr[n];
	int i,cnta=0,cntb=0,ans;
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
		cnt[arr[i]]++;
		if(arr[i]==a)
			cnta++;
		if(arr[i]!=a)
		{
			if(cntb<cnt[arr[i]])
			{
				cntb=cnt[arr[i]];
				ans=arr[i];
			}
		}
		if(cntb<cnta)
		{
			cout<<-1<<endl;
			return 0;
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
int main()
{
	int n,a;
	cin>>n>>a;
	bool avail[n];
	int arr[n];
	int i,cnta=0,cntb=0,ans;
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
		cnt[arr[i]]++;
		if(arr[i]==a)
			cnta++;
		if(arr[i]!=a)
		{
			if(avail[arr[i]] && cnt[arr[i]]>=cnta)
				avail[arr[i]]=true;
			else
				avail[arr[i]]=false;
		}
	}
	for(i=0;i<n;i++)
	{
		if(avail[arr[i]] && cnt[arr[i]]>cnta)
		{
			cout<<arr[i]<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
int main()
{
	int n,a;
	cin>>n>>a;
	bool avail[n];
	int arr[n];
	int i,cnta=0,cntb=0,ans;
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
		avail[arr[i]]=true;
		if(arr[i]==a)
			cnta++;
		if(arr[i]!=a)
		{
			if((cnt[arr[i]]==0 && cnta==0) || (cnt[arr[i]]>0 && cnt[arr[i]]>=cnta))
				cnt[arr[i]]++;
			else
				avail[arr[i]]=false;
		}
	}
	for(i=0;i<n;i++)
	{
		if(i!=a && avail[arr[i]] && cnt[arr[i]]>cnta)
		{
			cout<<arr[i]<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
int main()
{
	int n,a;
	cin>>n>>a;
	bool avail[n];
	int arr[n];
	int i,cnta=0,cntb=0,ans;
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
		cnt[arr[i]]++;
		if(arr[i]==a)
			cnta++;
		if(arr[i]!=a)
		{
			if(cnt[arr[i]]>cnta)
				avail[arr[i]]=true;
			else
				avail[arr[i]]=false;
		}
	}
	for(i=0;i<n;i++)
	{
		if(avail[arr[i]] && cnt[arr[i]]>cnta)
		{
			cout<<arr[i]<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
bool avail[1000005];
int arr[1000005];
int main()
{
	int n,a;
	cin>>n>>a;
	int i,cnta=0,cntb=0,ans;
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
		avail[arr[i]]=true;
		if(arr[i]==a)
			cnta++;
		if(arr[i]!=a)
		{
			if((cnt[arr[i]]==0 && cnta==0) || (cnt[arr[i]]>0 && cnt[arr[i]]>=cnta))
				cnt[arr[i]]++;
			else
				avail[arr[i]]=false;
		}
	}
	for(i=0;i<1000001;i++)
	{
		if(i!=a && avail[arr[i]] && cnt[arr[i]]>cnta)
		{
			cout<<arr[i]<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
bool avail[1000005];
int arr[1000005];
int main()
{
	int n,a;
	cin>>n>>a;
	int i,cnta=0,cntb=0,ans;
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
		avail[arr[i]]=true;
		if(arr[i]==a)
			cnta++;
		if(arr[i]!=a)
		{
			if((cnt[arr[i]]==0 && cnta==0) || (cnt[arr[i]]>0 && cnt[arr[i]]>=cnta))
				cnt[arr[i]]++;
			else
				avail[arr[i]]=false;
		}
	}
	for(i=0;i<1000001;i++)
	{
		if(i!=a && avail[arr[i]] && cnt[arr[i]]>=cnta)
		{
			cout<<arr[i]<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
int main()
{
	int n,a;
	cin>>n>>a;
	bool avail[n];
	int arr[n];
	int i,cnta=0,cntb=0,ans;
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
		cnt[arr[i]]++;
		if(arr[i]==a)
			cnta++;
		if(arr[i]!=a)
		{
			if(avail[arr[i]] && cnt[arr[i]]>=cnta)
				avail[arr[i]]=true;
			else
				avail[arr[i]]=false;
		}
	}
	for(i=0;i<n;i++)
	{
		if(i!=a && avail[arr[i]] && cnt[arr[i]]>cnta)
		{
			cout<<arr[i]<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
int a[maxn],ans[maxn];
int ca[maxn],cb[maxn];
bool vis[maxn];
struct node{
    int x,sum;
    bool operator<(const node&r)const{
        return sum<r.sum;
        }
};
int main(){

    int n,A;
    scanf("%d%d",&n,&A);

    priority_queue<node> q;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ca[i]=ca[i-1]+(a[i]==A);
    bool ok=1;

    for(int i=1;i<=n;i++){
        cb[a[i]]++;
        if(cb[a[i]] > ca[i-1])vis[i]=1;
        else vis[i]=0;
    }
    int ans=-1;
    for(int i=1;i<=1e6;i++){
        if(vis[i] && i!=A){
            if(cb[i]>=ca[n])ans=i;
            else vis[i]=0;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
int a[maxn],ans[maxn];
int ca[maxn],cb[maxn];
bool vis[maxn];
struct node{
    int x,sum;
    bool operator<(const node&r)const{
        return sum>r.sum;
        }
};
int main(){

    int n,A;
    scanf("%d%d",&n,&A);
    int ans=0,x=-1;
    priority_queue<node> q;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ca[i]=ca[i-1]+(a[i]==A);
    bool ok=1;
    vis[A]=1;
    for(int i=1;i<=n;i++){
        cb[a[i]]++;
        if(!vis[a[i]])
        q.push({a[i],cb[a[i]]});
        while(!q.empty() && (q.top().sum < ca[i] || vis[q.top().x])){
             int u=q.top().x;
             vis[u]=1;
             q.pop();
        }
        if(q.empty()){
            ok=0;break;
        }
    }
    while(!q.empty()  && vis[q.top().x])q.pop();
    if(q.empty())ok=0;
    if(ok)cout<<q.top().x<<endl;
    else cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
int a[maxn],ans[maxn];
int ca[maxn],cb[maxn];
bool vis[maxn];
struct node{
    int x,sum;
    bool operator<(const node&r)const{
        return sum>r.sum;
        }
};
int main(){

    int n,A;
    scanf("%d%d",&n,&A);
    int ans=0,x=-1;
    priority_queue<node> q;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ca[i]=ca[i-1]+(a[i]==A);
    for(int i=1;i<=n;i++){
        if(a[i]==A)continue;
        cb[a[i]]++;
        if(!vis[a[i]])
        q.push({a[i],cb[a[i]]});
        while(!q.empty() && (q.top().sum < ca[i])){
             int u=q.top().x;
             vis[u]=1;
             q.pop();
        }
    }
    while(!q.empty()  && vis[q.top().x])q.pop();
    bool ok=1;
    if(q.empty())ok=0;
    if(ok)cout<<q.top().x<<endl;
    else cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
int a[maxn],ans[maxn];
int ca[maxn],cb[maxn];
int main(){

    int n,A;
    scanf("%d%d",&n,&A);
    int ans=0,x=-1;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    bool ok=1;
    for(int i=1;i<=n;i++){
        ca[i]=ca[i-1];
        if(a[i]==A)ca[i]++;
        cb[a[i]]++;
        if(a[i]!=A){
            if(cb[a[i]] > ans)
                ans =cb[a[i]],x=a[i];
        }
        if(ans < ca[i])ok=0;
    }
    if(ok)cout<<x<<endl;
    else cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
int a[maxn],ans[maxn];
int ca[maxn],cb[maxn];
bool vis[maxn];
struct node{
    int x,sum;
    bool operator<(const node&r)const{
        return sum<r.sum;
        }
};
int main(){

    int n,A;
    scanf("%d%d",&n,&A);

    priority_queue<node> q;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ca[i]=ca[i-1]+(a[i]==A);
    bool ok=1;

    for(int i=1;i<=n;i++){
        cb[a[i]]++;
        if(cb[a[i]] > ca[i-1])vis[a[i]]=1;
        else vis[a[i]]=0;
    }
    int ans=-1;
    for(int i=1;i<=1e6;i++){
        if(vis[i] && i!=A){
            if(cb[i]>=ca[n])ans=i;
            else vis[i]=0;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
int a[maxn],ans[maxn];
int ca[maxn],cb[maxn];
bool vis[maxn];
struct node{
    int x,sum;
    bool operator<(const node&r)const{
        return sum<r.sum;
        }
};
map<int,int> mp;
int main(){

    int n,A;
    scanf("%d%d",&n,&A);

    priority_queue<node> q;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ca[i]=ca[i-1]+(a[i]==A);
    bool ok=1;
    vis[A]=1;
    for(int i=1;i<=n;i++){
        cb[a[i]]++;
        if(cb[a[i]] <= ca[i])
           vis[a[i]]=1;
    }
    for(int i=1;i<=1e6;i++){
        if(!vis[i] && cb[i]>=ca[n])
            printf("%d\n",i),exit(0);
    }
    printf("-1\n");

    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
int a[maxn],ans[maxn];
int ca[maxn],cb[maxn];
bool vis[maxn];
struct node{
    int x,sum;
    bool operator<(const node&r)const{
        return sum>r.sum;
        }
};
int main(){

    int n,A;
    scanf("%d%d",&n,&A);
    int ans=0,x=-1;
    priority_queue<node> q;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ca[i]=ca[i-1]+(a[i]==A);
    for(int i=1;i<=n;i++){
        if(a[i]==A)continue;
        cb[a[i]]++;
        if(!vis[a[i]])
        q.push({a[i],cb[a[i]]});
        while(!q.empty() && (q.top().sum < ca[i] || vis[a[i]])){
             int u=q.top().x;
             vis[u]=1;
             q.pop();
        }
    }
    while(!q.empty()  && vis[q.top().x])q.pop();
    bool ok=1;
    if(q.empty())ok=0;
    if(ok)cout<<q.top().x<<endl;
    else cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
int a[maxn],ans[maxn];
int ca[maxn],cb[maxn];
bool vis[maxn];
struct node{
    int x,sum;
    bool operator<(const node&r)const{
        return sum>r.sum;
        }
};
int main(){

    int n,A;
    scanf("%d%d",&n,&A);
    int ans=0,x=-1;
    priority_queue<node> q;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ca[i]=ca[i-1]+(a[i]==A);
    for(int i=1;i<=n;i++){
        if(a[i]==A)continue;
        cb[a[i]]++;
        if(!vis[a[i]])
        q.push({a[i],cb[a[i]]});
        while(!q.empty() && (q.top().sum < ca[i] || vis[q.top().x])){
             int u=q.top().x;
             vis[u]=1;
             q.pop();
        }
    }
    while(!q.empty()  && vis[q.top().x])q.pop();
    bool ok=1;
    if(q.empty())ok=0;
    if(ok)cout<<q.top().x<<endl;
    else cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
int a[maxn],ans[maxn];
int ca[maxn];
bool vis[maxn];
map<int,int> mp;
int main(){

    int n,A;
    scanf("%d%d",&n,&A);
    int ans=0,x=-1;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ca[i]=ca[i-1]+(a[i]==A);
    for(int i=1;i<=1e6;i++)if(i!=A)mp[i]=0;
    vis[A]=1;
    for(int i=1;i<=n;i++){
        if(!vis[a[i]])mp[a[i]]++;
        for(auto it=mp.begin();it!=mp.end();){
            if(it->second < ca[i])
                vis[it->first]=1,mp.erase(it++);
            else ++it;
        }
    }

    bool ok=1;
    if(mp.size()==0)ok=0;
    if(ok)cout<<mp.begin()->first<<endl;
    else cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
int a[maxn],ans[maxn];
int ca[maxn],cb[maxn];
bool vis[maxn];
struct node{
    int x,sum;
    bool operator<(const node&r)const{
        return sum<r.sum;
        }
};
int main(){

    int n,A;
    scanf("%d%d",&n,&A);
    int ans=0,x=-1;
    priority_queue<node> q;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),ca[i]=ca[i-1]+(a[i]==A);
    bool ok=1;
    vis[A]=1;
    for(int i=1;i<=n;i++){
        cb[a[i]]++;
        if(!vis[a[i]])
        q.push({a[i],cb[a[i]]});
        while(!q.empty() && (q.top().sum < ca[i] || vis[q.top().x])){
             int u=q.top().x;
             vis[u]=1;
             q.pop();
        }
        if(q.empty()){
            ok=0;break;
        }
    }
    while(!q.empty()  && vis[q.top().x])q.pop();
    if(q.empty())ok=0;
    if(ok)cout<<q.top().x<<endl;
    else cout<<-1<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n, A, c[maxn], f[maxn], cnt[maxn];

int main()
{
    scanf("%d %d", &n,&A);
    for(int i=1; i<=n; i++) scanf("%d", &c[i]);
    for(int i=1; i<=n; i++){
        if(c[i]==A){
            cnt[i]=cnt[i-1]+1;
        }
        else{
            cnt[i]=cnt[i-1];
        }
    }
    int mx = -1, color = -1;
    memset(f,0,sizeof(f));
    int flag = 1;
    for(int i=1; i<=n; i++){
        //if(c[i]==A) continue;
        f[c[i]]++;
        if(f[c[i]]>=cnt[i]&&c[i]!=A){
            if(f[c[i]]>mx){
                mx=f[c[i]];
                color=c[i];
            }
        }
        else{
            if(f[color]>=cnt[i]) f[c[i]] = 0;
            else{
                flag = 0;
                break;
            }
        }
    }
    if(flag==1) printf("%d\n", color);
    else printf("-1\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n, A, c[maxn], f[maxn], cnt[maxn];

int main()
{
    scanf("%d %d", &n,&A);
    for(int i=1; i<=n; i++) scanf("%d", &c[i]);
    for(int i=1; i<=n; i++){
        if(c[i]==A){
            cnt[i]=cnt[i-1]+1;
        }
        else{
            cnt[i]=cnt[i-1];
        }
    }
    bool flag = 1;
    int mx = -1, color = -1;
    memset(f,0,sizeof(f));
    for(int i=1; i<=n; i++){
        if(c[i]==A) continue;
        f[c[i]]++;
        if(f[c[i]]>=cnt[i]){
            if(f[c[i]]>mx){
                mx=f[c[i]];
                color=c[i];
            }
        }
        else{
            flag = 0;
            break;
        }
    }
    if(flag == 0){
        puts("-1");
    }
    else{
        printf("%d\n", color);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n, A, c[maxn], f[maxn], cnt[maxn];

int main()
{
    scanf("%d %d", &n,&A);
    for(int i=1; i<=n; i++) scanf("%d", &c[i]);
    for(int i=1; i<=n; i++){
        if(c[i]==A){
            cnt[i]=cnt[i-1]+1;
        }
        else{
            cnt[i]=cnt[i-1];
        }
    }
    bool flag = 1;
    int mx = -1, color = -1;
    memset(f,0,sizeof(f));
    for(int i=1; i<=n; i++){
        if(c[i]==A) continue;
        f[c[i]]++;
        if(f[c[i]]>=cnt[i]){
            if(f[c[i]]>mx){
                mx=f[c[i]];
                color=c[i];
            }
        }
        else{
            f[c[i]] = 0;
        }
    }
    printf("%d\n", color);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n, A, c[maxn];
struct node{
    int l,r,len,sum;
    int lazy;
}tree[maxn*4];
void pushup(int rt){
    tree[rt].sum = tree[rt*2].sum+tree[rt*2+1].sum;
}
void pushdown(int rt){
    if(tree[rt].lazy){
        tree[rt*2].lazy+=tree[rt].lazy;
        tree[rt*2+1].lazy+=tree[rt].lazy;
        tree[rt*2].sum += tree[rt*2].len*tree[rt].lazy;
        tree[rt*2+1].sum += tree[rt*2+1].len*tree[rt].lazy;
        tree[rt].lazy = 0;
    }
}
void Build(int l, int r, int rt){
    tree[rt].l = l, tree[rt].r = r, tree[rt].sum = 0, tree[rt].lazy = 0, tree[rt].len = r-l+1;
    if(l == r){
        return;
    }
    int mid = (l+r)/2;
    Build(l,mid,rt*2);
    Build(mid+1,r,rt*2+1);
    pushup(rt);
}
void update(int L, int R, int c, int rt){
    if(L<=tree[rt].l&&tree[rt].r<=R){
        tree[rt].sum += c*(tree[rt].len);
        tree[rt].lazy += c;
        return;
    }
    pushdown(rt);
    int mid = (tree[rt].l+tree[rt].r)/2;
    if(R<=mid) update(L,R,c,rt*2);
    else if(L>mid) update(L,R,c,rt*2+1);
    else{
        update(L,mid,c,rt*2);
        update(mid+1,R,c,rt*2+1);
    }
    pushup(rt);
}
int query(int L, int R, int rt){
    if(L<=tree[rt].l&&tree[rt].r<=R){
        return tree[rt].sum;
    }
    pushdown(rt);
    int mid = (tree[rt].l+tree[rt].r)/2;
    if(R<=mid) return query(L,R,rt*2);
    else if(L>mid) return query(L,R,rt*2+1);
    else return query(L,mid,rt*2)+query(mid+1,R,rt*2+1);
}

int main()
{
    scanf("%d%d",&n,&A);
    for(int i=1; i<=n; i++) scanf("%d", &c[i]);
    Build(1,1000000,1);
    int ans = -1;
    for(int i=1; i<=n; i++){
        if(c[i]==A){
            update(1,1000000,-1,1);
        }
        else{
            if(query(c[i],c[i],1)>=0){
                update(c[i],c[i],1,1);
            }
        }
    }
    for(int i=1; i<=1000000; i++){
        if(i==A) continue;
        if(query(i,i,1)>=0){
            ans = i;
            break;
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n, A, c[maxn], f[maxn], cnt[maxn];

int main()
{
    scanf("%d %d", &n,&A);
    for(int i=1; i<=n; i++) scanf("%d", &c[i]);
    for(int i=1; i<=n; i++){
        if(c[i]==A){
            cnt[i]=cnt[i-1]+1;
        }
        else{
            cnt[i]=cnt[i-1];
        }
    }
    bool flag = 1;
    int mx = -1, color = -1;
    memset(f,0,sizeof(f));
    for(int i=1; i<=n; i++){
        if(c[i]==A) continue;
        f[c[i]]++;
        if(f[c[i]]>=cnt[i]){
            if(f[c[i]]>mx){
                mx=f[c[i]];
                color=c[i];
            }
        }
        else{
            f[c[i]] = 0;
        }
    }
    if(mx>=cnt[n]) printf("%d\n", color);
    else printf("-1\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n, A, c[maxn], f[maxn], cnt[maxn];

int main()
{
    scanf("%d %d", &n,&A);
    for(int i=1; i<=n; i++) scanf("%d", &c[i]);
    for(int i=1; i<=n; i++){
        if(c[i]==A){
            cnt[i]=cnt[i-1]+1;
        }
        else{
            cnt[i]=cnt[i-1];
        }
    }
    int mx = -1, color = -1;
    memset(f,0,sizeof(f));
    int flag = 1;
    for(int i=1; i<=n; i++){
        if(c[i]==A) continue;
        f[c[i]]++;
        if(f[c[i]]>=cnt[i]){
            if(f[c[i]]>mx){
                mx=f[c[i]];
                color=c[i];
            }
        }
        else{
            if(f[color]>=cnt[i]) f[c[i]] = 0;
            else{
                flag = 0;
                break;
            }
        }
    }
    if(flag) printf("%d\n", color);
    else printf("-1\n");
    return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
//typedef long long ll ;
using namespace std;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define bit(a , b) (a >> b) & 1
#define all(v) v.begin() , v.end()
#define LB(x) (x & -x)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18 , MOD = 1e9 + 7 , SQRT = 350 ;
inline void smin(ll &x , ll y) { x = min(x , y) ; }
inline void smax(ll &x , ll y) { x = max(x , y) ; }

const ll MAXN = 1e5 + 100 , MAX = 1e6 + 100 ;
ll n , a[MAXN] , cnt[MAX] ;
set < ll > s ;
ll mark[MAX] ;
ll A ;

int main ()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> A ;
	for(ll i = 1 ; i <= n ; i++)	cin >> a[i] ;
	ll p = 0 ;
	for(ll i = 1 ; i <= n ; i++)
	{
		if(a[i] == A)
		{
			p++ ;
			if(!s.empty()) {
				for(auto x : s)
				{
					mark[x]++ ;
					if(cnt[x] == p)	s.erase(x);
				}
			}
			cnt[A]++ ;
		}
 		else
 		{
			cnt[a[i]]++ ;
			if(cnt[a[i]] >= p)	s.insert(a[i]) ;
		}
	}
	for(ll i = 1 ; i <= 1e6 ; i++)	if(i != A && mark[i] >= p)	return cout << i , 0 ;
	cout << -1 ;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
//typedef long long ll ;
using namespace std;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define bit(a , b) (a >> b) & 1
#define all(v) v.begin() , v.end()
#define LB(x) (x & -x)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18 , MOD = 1e9 + 7 , SQRT = 350 ;
inline void smin(ll &x , ll y) { x = min(x , y) ; }
inline void smax(ll &x , ll y) { x = max(x , y) ; }

const ll MAXN = 1e5 + 100 , MAX = 1e6 + 100 ;
ll n , a[MAXN] , cnt[MAX] ;
set < ll > s ;
ll mark[MAX] ;
ll A ;

int main ()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> A ;
	for(ll i = 1 ; i <= n ; i++)	cin >> a[i] ;
	ll p = 0 ;
	for(ll i = 1 ; i <= n ; i++)
	{
		if(a[i] == A)
		{
			p++ ;
			for(auto x : s)
			{
				mark[x]++ ;
				if(cnt[x] == p)	s.erase(x);
			}
			cnt[A]++ ;
		}
 		else
 		{
			cnt[a[i]]++ ;
			if(cnt[a[i]] >= p)	s.insert(a[i]) ;
		}
	}
	for(ll i = 1 ; i <= 1e6 ; i++)	if(i != A && mark[i] >= p)	return cout << i , 0 ;
	cout << -1 ;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
//typedef long long ll ;
using namespace std;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define bit(a , b) (a >> b) & 1
#define all(v) v.begin() , v.end()
#define LB(x) (x & -x)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18 , MOD = 1e9 + 7 , SQRT = 350 ;
inline void smin(ll &x , ll y) { x = min(x , y) ; }
inline void smax(ll &x , ll y) { x = max(x , y) ; }

const ll MAXN = 1e5 + 100 , MAX = 1e6 + 100 ;
ll n , a[MAXN] , cnt[MAX] ;
set < ll > s ;
ll mark[MAX] ;
ll A ;

int main ()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> A ;
	for(ll i = 1 ; i <= n ; i++)	cin >> a[i] ;
	ll p = 0 ;
	for(ll i = 1 ; i <= n ; i++)
	{
		if(a[i] == A)
		{
			p++ ;
			for(auto x : s)	mark[x]++ ;
			s.clear() ;
			cnt[A]++ ;
		}
 		else
 		{
			cnt[a[i]]++ ;
			if(cnt[a[i]] >= p)	s.insert(a[i]) ;
		}
	}
	for(ll i = 1 ; i <= 1e6 ; i++)	if(i != A && mark[i] >= p)	return cout << i , 0 ;
	cout << -1 ;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
//typedef long long ll ;
using namespace std;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define bit(a , b) (a >> b) & 1
#define all(v) v.begin() , v.end()
#define LB(x) (x & -x)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18 , MOD = 1e9 + 7 , SQRT = 350 ;
inline void smin(ll &x , ll y) { x = min(x , y) ; }
inline void smax(ll &x , ll y) { x = max(x , y) ; }

const ll MAXN = 1e5 + 100 , MAX = 1e6 + 100 ;
ll n , a[MAXN] , cnt[MAX] ;
set < ll > s ;
ll mark[MAX] ;
ll A ;

int main ()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> A ;
	for(ll i = 1 ; i <= n ; i++)	cin >> a[i] ;
	ll p = 0 ;
	for(ll i = 1 ; i <= n ; i++)
	{
		if(a[i] == A)
		{
			p++ ;
			for(auto x : s)
			{
				mark[x]++ ;
				if(cnt[x] == p)	if(!s.empty())s.erase(x);
			}
			cnt[A]++ ;
		}
 		else
 		{
			cnt[a[i]]++ ;
			if(cnt[a[i]] >= p)	s.insert(a[i]) ;
		}
	}
	for(ll i = 1 ; i <= 1e6 ; i++)	if(i != A && mark[i] >= p)	return cout << i , 0 ;
	cout << -1 ;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
//typedef long long ll ;
using namespace std;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define bit(a , b) (a >> b) & 1
#define all(v) v.begin() , v.end()
#define LB(x) (x & -x)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18 , MOD = 1e9 + 7 , SQRT = 350 ;
inline void smin(ll &x , ll y) { x = min(x , y) ; }
inline void smax(ll &x , ll y) { x = max(x , y) ; }

const ll MAXN = 1e5 + 100 , MAX = 1e6 + 100 ;
ll n , a[MAXN] , cnt[MAX] ;
set < ll > s ;
ll mark[MAX] ;
ll A ;

int main ()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> A ;
	for(ll i = 1 ; i <= n ; i++)	cin >> a[i] ;
	ll p = 0 ;
	for(ll i = 1 ; i <= n ; i++)
	{
		if(a[i] == A)
		{
			p++ ;
			for(auto x : s)	mark[x]++ ;
			cnt[A]++ ;
		}
 		else
 		{
			cnt[a[i]]++ ;
			if(cnt[a[i]] >= p)	s.insert(a[i]) ;
		}
	}
	for(ll i = 1 ; i <= 1e6 ; i++)	if(i != A && mark[i] >= p)	return cout << i , 0 ;
	cout << -1 ;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
//typedef long long ll ;
using namespace std;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define bit(a , b) (a >> b) & 1
#define all(v) v.begin() , v.end()
#define LB(x) (x & -x)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18 , MOD = 1e9 + 7 , SQRT = 350 ;
inline void smin(ll &x , ll y) { x = min(x , y) ; }
inline void smax(ll &x , ll y) { x = max(x , y) ; }

const ll MAXN = 1e5 + 100 , MAX = 1e6 + 100 ;
ll n , a[MAXN] , cnt[MAX] ;
set < ll > s ;
ll mark[MAX] ;
ll A ;

int main ()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> A ;
	for(ll i = 1 ; i <= n ; i++)	cin >> a[i] ;
	ll p = 0 ;
	for(ll i = 1 ; i <= n ; i++)
	{
		if(a[i] == A)
		{
			p++ ;
			for(auto x : s)	mark[x]++ ;
			s.clear() ;
			cnt[A]++ ;
 		}
 		else
 		{
 			cnt[a[i]]++ ;
 			if(cnt[a[i]] >= p)	s.insert(a[i]) ;
		 }

	}
	for(ll i = 1 ; i<= 1e6 ; i++)	if(mark[i] == p)	return cout << i , 0 ;
	cout << -1 ;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
//typedef long long ll ;
using namespace std;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define bit(a , b) (a >> b) & 1
#define all(v) v.begin() , v.end()
#define LB(x) (x & -x)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18 , MOD = 1e9 + 7 , SQRT = 350 ;
inline void smin(ll &x , ll y) { x = min(x , y) ; }
inline void smax(ll &x , ll y) { x = max(x , y) ; }

const ll MAXN = 1e5 + 100 , MAX = 1e6 + 100 ;
ll n , a[MAXN] , cnt[MAX] ;
set < ll > s ;
ll mark[MAX] ;
ll A ;

int main ()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> A ;
	for(ll i = 1 ; i <= n ; i++)	cin >> a[i] ;
	ll p = 0 ;
	for(ll i = 1 ; i <= n ; i++)
	{
		if(a[i] == A)
		{
			p++ ;
			if(!s.empty()) {
				for(auto x : s)
				{
				//	cout << x << " " << cnt[x] << " " << p << endl ;
					mark[x]++ ;
					if(cnt[x] == p)		s.erase(x);
				//	cout << "inja :" << s.size() << endl;
					if(s.empty())	break ;
				}
			}
	//		p++ ;
			cnt[A]++ ;
		}
 		else
 		{
 		//	cout << i << endl ;
			cnt[a[i]]++ ;
			if(cnt[a[i]] >= p)	s.insert(a[i]) ;
		}
	}
	for(ll i = 1 ; i <= 1e6 ; i++)	if(i != A && mark[i] >= p)	return cout << i , 0 ;
	cout << -1 ;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
//typedef long long ll ;
using namespace std;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define bit(a , b) (a >> b) & 1
#define all(v) v.begin() , v.end()
#define LB(x) (x & -x)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18 , MOD = 1e9 + 7 , SQRT = 350 ;
inline void smin(ll &x , ll y) { x = min(x , y) ; }
inline void smax(ll &x , ll y) { x = max(x , y) ; }

const ll MAXN = 1e5 + 100 , MAX = 1e6 + 100 ;
ll n , a[MAXN] , cnt[MAX] ;
set < ll > s ;
ll mark[MAX] ;
ll A ;

int main ()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> A ;
	for(ll i = 1 ; i <= n ; i++)	cin >> a[i] ;
	ll p = 0 ;
	for(ll i = 1 ; i <= n ; i++)
	{
		if(a[i] == A)
		{
			p++ ;
			for(auto x : s)	mark[x]++ ;
			s.clear() ;
			cnt[A]++ ;
		}
 		else
 		{
			cnt[a[i]]++ ;
			if(cnt[a[i]] >= p)	s.insert(a[i]) ;
		}
	}
	for(ll i = 1 ; i <= 1e6 ; i++)	if(i != A && mark[i] >= p - 1)	return cout << i , 0 ;
	cout << -1 ;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
//typedef long long ll ;
using namespace std;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define bit(a , b) (a >> b) & 1
#define all(v) v.begin() , v.end()
#define LB(x) (x & -x)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18 , MOD = 1e9 + 7 , SQRT = 350 ;
inline void smin(ll &x , ll y) { x = min(x , y) ; }
inline void smax(ll &x , ll y) { x = max(x , y) ; }

const ll MAXN = 1e5 + 100 , MAX = 1e6 + 100 ;
ll n , a[MAXN] , cnt[MAX] ;
set < ll > s ;
ll mark[MAX] ;
ll A ;

int main ()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> A ;
	for(ll i = 1 ; i <= n ; i++)	cin >> a[i] ;
	ll p = 0 ;
	for(ll i = 1 ; i <= n ; i++)
	{
		if(a[i] == A)
		{
			vector < ll > res ;
			res.clear();
 			p++ ;
			if(!s.empty()) {
				for(auto x : s)
				{
				//	cout << x << " " << cnt[x] << " " << p << endl ;
					mark[x]++ ;
					if(cnt[x] == p)		res.push_back(x) ;
				//	cout << "inja :" << s.size() << endl;
				}
				for(auto x : res)	s.erase(x) ;
			}
	//		p++ ;
			cnt[A]++ ;
		}
 		else
 		{
 		//	cout << i << endl ;
			cnt[a[i]]++ ;
			if(cnt[a[i]] >= p)	s.insert(a[i]) ;
		}
	}
	for(ll i = 1 ; i <= 1e6 ; i++)	if(i != A && mark[i] >= p)	return cout << i , 0 ;
	cout << -1 ;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
//typedef long long ll ;
using namespace std;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define bit(a , b) (a >> b) & 1
#define all(v) v.begin() , v.end()
#define LB(x) (x & -x)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18 , MOD = 1e9 + 7 , SQRT = 350 ;
inline void smin(ll &x , ll y) { x = min(x , y) ; }
inline void smax(ll &x , ll y) { x = max(x , y) ; }

const ll MAXN = 1e5 + 100 , MAX = 1e6 + 100 ;
ll n , a[MAXN] , cnt[MAX] ;
set < ll > s ;
ll mark[MAX] ;
ll A ;

int main ()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> A ;
	for(ll i = 1 ; i <= n ; i++)	cin >> a[i] ;
	ll p = 0 ;
	for(ll i = 1 ; i <= n ; i++)
	{
		if(a[i] == A)
		{
			if(!s.empty()) {
				for(auto x : s)
				{
					mark[x]++ ;
					if(cnt[x] == p)	s.erase(x);
				}
			}
			p++ ;
			cnt[A]++ ;
		}
 		else
 		{
			cnt[a[i]]++ ;
			if(cnt[a[i]] >= p)	s.insert(a[i]) ;
		}
	}
	for(ll i = 1 ; i <= 1e6 ; i++)	if(i != A && mark[i] >= p)	return cout << i , 0 ;
	cout << -1 ;
}


#include<bits/stdc++.h>
using namespace std;

const int maxn=1e6;
typedef pair<int,int>pii;
set<pii>s;
int cnt[maxn+5],vis[maxn+5];

void clear(int x)
{
	set<pii>::iterator it=s.begin();
	while(it!=s.end()&&it->first<cnt[x])
	{
		vis[it->second]=1;
		s.erase(it++);
	}
}

int main()
{
	memset(cnt,0,sizeof(cnt));
	memset(vis,0,sizeof(vis));
	int n,k,x;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=maxn;i++)
	s.insert(pii(0,i));
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		if(!vis[x])
		{
			s.erase(pii(cnt[x],x));
			cnt[x]++;
			s.insert(pii(cnt[x],x));
		}
		if(x==k)
		clear(k);
	}
	clear(k);
	if(cnt[k]==0)printf("%d",k==n?k+1:n);
	else if(s.size()==1)printf("-1");
	else for(int i=1;i<=maxn;i++)
	if(!vis[i]&&i!=k) return 0*printf("%d",i);
	return 0;
 }


#include <bits/stdc++.h>
using namespace std;

const int maxn=1e6;
struct node{
	int cnt,id;
	node(){}
	node(int c,int i)
	{
		cnt=c;
		id=i;
	}
	bool operator<(const node& rhs)const{
		return cnt>rhs.cnt;
	}
};
int cnt[maxn+5],vis[maxn+5];
priority_queue<node>q;

int main()
{
	memset(cnt,0,sizeof(cnt));
	memset(vis,-1,sizeof(vis));
	int n,k,x;
	scanf("%d%d",&n,&k);
	vis[k]=1;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		cnt[x]++;
		if(vis[x]==-1)vis[x]=0;
		if(x==k)
		{
			while(!q.empty()&&q.top().cnt<cnt[x])
			{
				vis[q.top().id]=1;
				q.pop();
			}
		}
		q.push(node(cnt[x],x));
	}
	while(!q.empty()&&q.top().cnt<cnt[k])
	{
		vis[q.top().id]=1;
		q.pop();
	}
	for(int i=1;i<=maxn;i++)
	if(!vis[i]) return 0*printf("%d",i);
	printf("-1");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int maxn=1e6;
typedef pair<int,int>pii;
set<pii>s;
int cnt[maxn+5],vis[maxn+5];

void clear(int x)
{
	set<pii>::iterator it=s.begin();
	while(it!=s.end()&&it->first<cnt[x])
	{
		vis[it->second]=1;
		s.erase(it++);
	}
}

int main()
{
	memset(cnt,0,sizeof(cnt));
	memset(vis,0,sizeof(vis));
	int n,k,x;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		if(!vis[x])
		{
			s.erase(pii(cnt[x],x));
			cnt[x]++;
			s.insert(pii(cnt[x],x));
		}
		if(x==k)
		clear(k);
	}
	clear(k);
	set<pii>::iterator it=s.end();
	it--;
	printf("%d",s.size()==1?-1:it->second);
	return 0;
 }


#include<bits/stdc++.h>
using namespace std;

const int maxn=1e6;
typedef pair<int,int>pii;
set<pii>s;
int cnt[maxn+5],vis[maxn+5];

void clear(int x)
{
	set<pii>::iterator it=s.begin();
	while(it!=s.end()&&it->first<cnt[x])
	{
		vis[it->second]=1;
		s.erase(it++);
	}
}

int main()
{
	memset(cnt,0,sizeof(cnt));
	memset(vis,0,sizeof(vis));
	int n,k,x;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=maxn;i++)
	s.insert(pii(0,i));
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		if(!vis[x])
		{
			s.erase(pii(cnt[x],x));
			cnt[x]++;
			s.insert(pii(cnt[x],x));
		}
		if(x==k)
		clear(k);
	}
	clear(k);
	set<pii>::iterator it=s.end();
	it--;
	if(cnt[k]==0)printf("%d",k==n?k+1:n);
	else printf("%d",s.size()==1?-1:it->second);
	return 0;
 }


#include<bits/stdc++.h>
using namespace std;

const int maxn=1e6;
typedef pair<int,int>pii;
set<pii>s;
int cnt[maxn+5],vis[maxn+5];

void clear(int x)
{
	set<pii>::iterator it=s.begin();
	while(it!=s.end()&&it->first<cnt[x])
	{
		vis[it->second]=1;
		s.erase(it++);
	}
}

int main()
{
	memset(cnt,0,sizeof(cnt));
	memset(vis,0,sizeof(vis));
	int n,k,x;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&x);
		if(!vis[x])
		{
			s.erase(pii(cnt[x],x));
			cnt[x]++;
			s.insert(pii(cnt[x],x));
		}
		if(x==k)
		clear(k);
	}
	clear(k);
	set<pii>::iterator it=s.end();
	it--;
	if(cnt[k]==0)printf("%d",k==n?k+1:n);
	else printf("%d",s.size()==1?-1:it->second);
	return 0;
 }


#include <bits/stdc++.h>
using namespace std;

int n, A, a[111111], cntA[111111];
map<int,int> m, last, first;

int main() {
	//freopen("test.in", "r", stdin);
	cin >> n >> A;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cntA[0] = 0;
	if (a[0] == A)
		cntA[0] = 1;
	for (int i = 0; i < n; i++) {
		if (a[i] == A) {
			cntA[i] = cntA[i - 1] + 1;
			last[A] = i;
		} else {
			cntA[i] = cntA[i - 1];
		}
	}

	for (int i = 0; i < n; i++) {
		last[a[i]] = i;
		if (m[a[i]] == 0) {
			first[a[i]] = i;
			if (a[i] == A)
				m[a[i]] = -1;
		}
		if (a[i] == A || m[a[i]] == -1) continue;
		if (m[a[i]] < cntA[i])
			m[a[i]] = -1;
		if (m[a[i]] != -1)
			m[a[i]]++;
	}

	int ans = -1;
	for (map<int,int>::iterator it = m.begin(); it != m.end(); it++) {
		if (it->second == -1)
			continue;
		if (last[A] > last[it->first] && it->second >= cntA[n - 1] && first[A] > first[it->first])
			ans = it->first;
		else if (last[A] < last[it->first] && first[A] > first[it->first])
			ans = it->first;
	}

	if (cntA[n - 1] == 0)
		ans = a[0];
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, A, a[111111], cntA[111111];
	map<int,int> m, last, first;
	cin >> n >> A;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cntA[0] = 0;
	if (a[0] == A)
		cntA[0] = 1;
	for (int i = 0; i < n; i++) {
		if (a[i] == A) {
			cntA[i] = cntA[i - 1] + 1;
			last[A] = i;
		} else {
			cntA[i] = cntA[i - 1];
		}
	}

	for (int i = 0; i < n; i++) {
		last[a[i]] = i;
		if (m[a[i]] == 0) first[a[i]] = i;
		if (a[i] == A || m[a[i]] == -1) continue;
		m[a[i]]++;
		if (m[a[i]] < cntA[i])
			m[a[i]] = -1;
	}

	int ans = -1;
	for (auto it = m.begin(); it != m.end(); it++) {
		if (it -> second == -1)
			continue;
		if (last[A] > last[it -> first] && it -> second >= cntA[n - 1] && first[A] > first[it -> first])
			ans = it -> first;
		else if (last[A] < last[it -> first] && first[A] > first[it -> first])
			ans = it -> first;
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

int n, A, a[111111], cntA[111111];
map<int,int> m, last, first;

int main() {
	cin >> n >> A;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cntA[0] = 0;
	if (a[0] == A)
		cntA[0] = 1;
	for (int i = 0; i < n; i++) {
		if (a[i] == A) {
			cntA[i] = cntA[i - 1] + 1;
			last[A] = i;
		} else {
			cntA[i] = cntA[i - 1];
		}
	}

	for (int i = 0; i < n; i++) {
		last[a[i]] = i;
		if (m[a[i]] == 0) {
			first[a[i]] = i;
			if (a[i] == A)
				m[a[i]] = -1;
		}
		if (a[i] == A || m[a[i]] == -1) continue;
		if (m[a[i]] < cntA[i])
			m[a[i]] = -1;
		m[a[i]]++;
	}

	int ans = -1;
	for (map<int,int>::iterator it = m.begin(); it != m.end(); it++) {
		if (it->second == -1)
			continue;
		if (last[A] > last[it->first] && it->second >= cntA[n - 1] && first[A] > first[it->first])
			ans = it->first;
		else if (last[A] < last[it->first] && first[A] > first[it->first])
			ans = it->first;
	}

	if (cntA[n - 1] == 0)
		ans = a[0];
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, A, a[111111], cntA[111111];
	map<int,int> m, last, first;
	cin >> n >> A;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cntA[0] = 0;
	if (a[0] == A)
		cntA[0] = 1;
	for (int i = 0; i < n; i++) {
		if (a[i] == A) {
			cntA[i] = cntA[i - 1] + 1;
			last[A] = i;
		} else {
			cntA[i] = cntA[i - 1];
		}
	}

	for (int i = 0; i < n; i++) {
		last[a[i]] = i;
		if (m[a[i]] == 0) first[a[i]] = i;
		if (a[i] == A || m[a[i]] == -1) continue;
		m[a[i]]++;
		if (m[a[i]] < cntA[i])
			m[a[i]] = -1;
	}

	int ans = -1;
	for (auto it = m.begin(); it != m.end(); it++) {
		if (it -> second == -1)
			continue;
		if (last[A] > last[it -> first] && it -> second >= cntA[n - 1] && first[A] > first[it -> first])
			ans = it -> first;
		else if (last[A] < last[it -> first] && first[A] > first[it -> first])
			ans = it -> first;
	}

	if (cntA[n - 1] == 0)
		ans = a[0];
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

int n, A, a[111111], cntA[111111];
map<int,int> m, last, first;

int main() {
	//freopen("test.in", "r", stdin);
	cin >> n >> A;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cntA[0] = 0;
	if (a[0] == A)
		cntA[0] = 1;
	for (int i = 0; i < n; i++) {
		if (a[i] == A) {
			cntA[i] = cntA[i - 1] + 1;
			last[A] = i;
		} else {
			cntA[i] = cntA[i - 1];
		}
	}

	for (int i = 0; i < n; i++) {
		last[a[i]] = i;
		if (m[a[i]] == 0) {
			first[a[i]] = i;
			if (a[i] == A)
				m[a[i]] = -1;
		}
		if (a[i] == A || m[a[i]] == -1) continue;
		m[a[i]]++;
		if (m[a[i]] < cntA[i])
			m[a[i]] = -1;
	}

	int ans = -1;
	for (map<int,int>::iterator it = m.begin(); it != m.end(); it++) {
		if (it->second == -1)
			continue;
		if (last[A] > last[it->first] && it->second >= cntA[n - 1] && first[A] > first[it->first])
			ans = it->first;
		else if (last[A] < last[it->first] && first[A] > first[it->first])
			ans = it->first;
	}

	if (cntA[n - 1] == 0)
		ans = a[0];
	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;
const int N=1e6+5;
int c;
int v[N];

int main()
{
	int n,a,u;
	cin >> n >> a;
	for(int i=1;i<=n;i++)
	{
		cin >> u;
		if(u==a) c++;
		else if(v[u] >= c) v[u]++;
	}
	for(int i=1;i<=n;i++)
	{
		if(v[i]>=c)
		{
			cout << i << endl;
			return 0;
		}
	}
	cout << -1 << endl;
}
// 1499205969706


#include <iostream>

using namespace std;

int cars[1000001];

int main()
{
	int n, a;
	bool found = 0;
	cin >> n >> a;
	for(int i = 0; i < n; i++)
	{
		int c;
		cin >> c;
		if(c == a || cars[c] >= cars[a])
			cars[c]++;
	}
	for(int i = 1; i <= 1000000; i++)
		if(i != a && cars[i] >= cars[a])
		{
			cout << i << endl;
			found = 1;
			break;
		}
	if(!found)
		cout << -1 << endl;
	return 0;
}

// 1500390481941


#include <bits/stdc++.h>

using namespace std;
const int N=1e6+5;
int v[N];
int m;
int main()
{
	int n,a;
	cin >> n >> a;
	for(int i=1;i<=n;i++)
	{
		int u;
		cin >> u;
		v[u]++;
		if(v[a] > v[m]){
			cout << -1 << endl;
			return 0;
		}
		else if(v[u] > v[m]) m = u;
	}
	cout << m << endl;
	return 0;
}
// 1499210611105


#include <bits/stdc++.h>
using namespace std;

const int N = 1e6+1;
int v[N], c[N], n, a, x;

int main() {
  scanf("%d%d", &n, &a);

  v[a] = 1;
  for(int i=0; i<n; ++i) {
    scanf("%d", &x);
    if (c[x] < c[a]) v[x] = 1;
    c[x]++;
  }

  for(int i=1; i<N; ++i) if (c[i] >= c[a] and !v[i])
    return printf("%d\n", i), 0;

  printf("-1\n");
  return 0;
}

// 1499216191697


#include <iostream>
#include <vector>
#include <set>
#include <map>

using namespace std;
typedef unsigned long ul;

int main()
{
	ul n, A;
	cin >> n >> A;

	ul colorsA = 0;
	map<ul,ul> colors;
	vector<set<ul>> byCount;

	ul maxSize = 0;

	while (n--)
	{
		ul c;
		cin >> c;

		if (c == A)
		{
			colorsA++;
			if (maxSize < colorsA)
				maxSize = colorsA;
			else
				byCount[colorsA-1].clear();
		}
		else
		{
			colors[c]++;
			if (maxSize < colors[c])
				maxSize = colors[c];
			byCount.resize(maxSize+1);

			if (colors[c] != 1 || colorsA == 0)
			{
				if (colors[c] != 1)
					byCount[colors[c]-1].erase(c);
				byCount[colors[c]].insert(c);
			}
		}
	}

	long val = -1;
	byCount[colorsA].erase(A);
	for (ul i = colorsA; i <= maxSize; i++)
	{
		if (!byCount[i].empty())
		{
			val = *byCount[i].begin();
			break;
		}
	}
	cout << val << endl;
}

// 1499201292344


#include <bits/stdc++.h>
using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define cl(x, v) memset((x), (v), sizeof(x))
#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<

typedef long long lli;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<lli, lli> pll;
typedef vector<int> vi;
typedef vector<vi> vii;

const ld EPS = 1e-9, PI = acos(-1.);
const int INF = 0x3f3f3f3f, MOD = 1e9+7;
const int N = 1e5+5;

int d, n, m;
int l, r, t, b;
int ll[N], rr[N], tt[N], bb[N];
int vert[N], hori[N];
int xa, ya, xb, yb;
set <int> x[N], y[N];
vector <pair <pii, pii> > sofas;

int main () {
    scanf("%d%d%d", &d, &n, &m);
    for (int i = 0; i < d; i++) {
        scanf("%d%d%d%d", &xa, &ya, &xb, &yb);
        x[xa].insert(i);
        x[xb].insert(i);
        y[ya].insert(i);
        y[yb].insert(i);
        sofas.pb(mp(mp(xa, xb), mp(ya, yb)));
        if (xa == xb) vert[i] = 1;
        if (ya == yb) hori[i] = 1;
    }
    set <int> sumx, sumy;
    for (int i = 0; i <= N-2; i++) {
        for (auto k : x[i]) sumx.insert(k);
        for (auto k : y[i]) sumy.insert(k);
        ll[i+1] = sumx.size();
        tt[i+1] = sumy.size();
    }
    sumx.clear(); sumy.clear();
    for (int i = N-1; i > 0; i--) {
        for (auto k : x[i]) sumx.insert(k);
        for (auto k : y[i]) sumy.insert(k);
        rr[i-1] = sumx.size();
        bb[i-1] = sumy.size();
    }
    scanf("%d%d%d%d", &l, &r, &t, &b);
    for (int i = 0; i < d; i++) {
        pii xx = sofas[i].st;
        pii yy = sofas[i].nd;
        int ok = 1;
        if (ll[max(xx.st, xx.nd)] - hori[i] != l) ok = 0;
        if (rr[min(xx.st, xx.nd)] - hori[i] != r) ok = 0;
        if (tt[max(yy.st, yy.nd)] - vert[i] != t) ok = 0;
        if (bb[min(yy.st, yy.nd)] - vert[i] != b) ok = 0;
        if (ok) return 0*printf("%d\n", i+1);
    }
    printf("-1\n");
    return 0;
}

// 1499709241230


#include <bits/stdc++.h>
using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define cl(x,v) memset((x), (v), sizeof(x))

#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<

typedef long long ll;
typedef long double ld;

typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <vi> vii;

const ld EPS = 1e-9, PI = acos(-1.);
const int INF = 0x3f3f3f3f, MOD = 1e9+7;
const int N = 1e5+5;

int num[1000010], conf,  n, a, b, maxp, m;

int main(){
    scanf("%d%d" , &n, &a);
    for(int i = 0; i < n ; i++){
        scanf("%d" , &b);
        num[b]++;
        if(m < num[b] and conf != -1){
            m = num[b];
            maxp = b;
        }
        if(maxp == a){
            conf = -1;
        }
    }
    if(conf == -1) printf("-1\n");
    else printf("%d\n" , maxp);
    return 0;
}
// 1499198992701