#include <cstdlib>
#include <iostream>
#include <vector>
#include <set>

using namespace std;

int main(int argc, char *argv[])
{
    vector<int> numbers;
    int n;
    cin>>n;
    for(int i=0; i<n; ++i){
        int a;
        cin>>a;
        numbers.push_back(a);
    }

    set<int> highest;
    for(int h = 25; h>=0; --h){
        bool found = false;
        bool notfound = false;
        highest.clear();
        for(int i =0; i<n; ++i){
            if( (numbers[i] & (1<<h)) > 0){
                found = true;
                highest.insert(i);
            }else{
                notfound = true;
            }
        }

        if(found && notfound){
             int lucky = -1;
            set<int>::iterator it;
            for(it = highest.begin(); it!= highest.end(); ++it){
                int curr = *it;
                int currn = numbers[curr];
                int max = 0;
                for(int k = curr -1; k>=0; --k){
                    if(highest.find(k) != highest.end() ){
                        break;
                    }
                    int sec = numbers[k];

                    if(sec < max){
                        continue;
                    }else{
                        max = sec;
                    }
                    if( (currn ^ sec ) > lucky ){
                        lucky = currn ^ sec;
                    }

                }
                max = 0;
                for(int k = curr + 1; k<n; ++k){
                    if(highest.find(k) != highest.end() ){
                        break;
                    }
                    int sec = numbers[k];
                    if(sec < max){
                        continue;
                    }else{
                        max = sec;
                    }
                    if( (currn ^ sec ) > lucky ){
                        lucky = currn ^ sec;
                    }
                }
            }
            cout<<lucky;
            return 0;
        }
    }
    return EXIT_SUCCESS;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstring>
#include <cmath>
using namespace std;

#define MAXN 2000000
int s[MAXN];
int n;
int sor = 0;

vector<int> cc;

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", s + i);
	for (int i = 0; i < n; i++)
		sor |= s[i];
	int mm = 1;
	for (; mm <= sor; mm <<= 1);
	mm >>= 1;
	for (int i = 0; i < n; i++)
		if ((mm & s[i]) != 0)
			cc.push_back(i);
	int ii = 0;
	int imax = 0;
	for (int i = 0; i < n; i++) {
		while (cc[ii] <= i && ii < cc.size() - 1)
			ii++;
		imax = max(imax, s[cc[ii]] ^ s[i]);
		if (ii < cc.size() - 1)
			imax = max(imax, s[cc[ii + 1]] ^ s[i]);
		if (ii > 0)
			imax = max(imax, s[cc[ii - 1]] ^ s[i]);
	}
	printf("%d", imax);
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstring>
#include <cmath>
using namespace std;

#define MAXN 2000000
int s[MAXN];
int n;
int sor = 0;

vector<int> cc;

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", s + i);
	for (int i = 0; i < n; i++)
		sor |= s[i];
	int mm = 1;
	for (; mm <= sor; mm <<= 1);
	mm >>= 1;
	for (int i = 0; i < n; i++)
		if ((mm & s[i]) != 0)
			cc.push_back(i);
	int ii = 0;
	int imax = 0;
	int max2 = 0;
	for (int i = cc[0] - 1; i >= 0; i--) {
		max2 = max(s[i], max2);
		imax = max(imax, max2 ^ s[i]);
	}
	for (int i = 0; i < cc.size() - 1; i++) {
		max2 = 0;
		for (int j = cc[i] + 1; j <= cc[i + 1]; j++) {
			max2 = max(max2, s[j]);
			imax = max(imax, max2 ^ s[cc[i]]);
		}
		max2 = 0;
		for (int j = cc[i + 1] - 1; j > cc[i]; j--) {
			max2 = max(max2, s[j]);
			imax = max(imax, max2 ^ s[cc[i + 1]]);
		}
	}
	max2 = 0;
	for (int i = cc[cc.size() - 1] + 1; i < n; i++) {
		max2 = max(max2, s[i]);
		imax = max(imax, max2 ^ s[cc[cc.size() - 1]]);
	}
	printf("%d", imax);
}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int N = 1e6 + 5;

int cnt[N], rep[N];

int main(){
    int n, A, x, c = 0;
    scanf("%d%d", &n, &A);
    while(n--){
        scanf("%d", &x);
        c += (x == A);
        ++cnt[x];
        if(cnt[x] > c) ++rep[x];
        else rep[x] = cnt[x] = INT_MIN;
    }
    for(int i = 1; i < N; ++i)
        if(rep[i] >= c && i != A){
            printf("%d", i);
            return 0;
        }
    puts("-1");
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;

int cnt[N], rep[N];

int main(){
    int n, A, x, c = 0;
    scanf("%d%d", &n, &A);
    while(n--){
        scanf("%d", &x);
        c += (x == A);
        ++cnt[x];
        if(cnt[x] > c){
            ++rep[x];
            cnt[x] = INT_MIN;
        }
    }
    for(int i = 1; i < N; ++i)
        if(rep[i] >= c && i != A){
            printf("%d", i);
            return 0;
        }
    puts("-1");
    return 0;
}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int N = 1e6 + 5;

int cnt[N], rep[N];

int main(){
    int n, A, x, c = 0;
    scanf("%d%d", &n, &A);
    while(n--){
        scanf("%d", &x);
        c += (x == A);
        ++cnt[x];
        if(cnt[x] >= c) ++rep[x];
        else cnt[x] = INT_MIN;
    }
    for(int i = 1; i < N; ++i)
        if(rep[i] >= c && i != A){
            printf("%d", i);
            return 0;
        }
    puts("-1");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(x) scanf("%s",x)
#define mod 1000000007
#define ll long long
#define pb push_back
#define mp make_pair
#define pi pair<int,int>
set <int> s;
int freq[1000005];
int main(){
    int n,ca;
    sc(n);
    sc(ca);
    int x,flag=0,check=0;
    set <int> :: iterator it;
    for(int i=0;i<n;i++){
        sc(x);
        freq[x]++;
        if(x==ca){
            if(flag==0){
                flag=1;
            }
            if(!s.empty()){
            it=s.begin();
            while(it!=s.end()){
                if(freq[*it]<freq[ca]){
                    s.erase(*it);
                }
                it++;
            }
            }
            if(s.empty()){
                check=1;
            }
        }
        else{
            if(flag==0){
                s.insert(x);
            }
        }
    }
    if(check==1){
        printf("-1");
    }
    else{
        printf("%d",*s.begin());
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(x) scanf("%s",x)
#define mod 1000000007
#define ll long long
#define pb push_back
#define mp make_pair
#define pi pair<int,int>
set <int> s;
int freq[1000005];
int main(){
    int n,ca;
    sc(n);
    sc(ca);
    int x,flag=0,check=0;
    set <int> :: iterator it;
    for(int i=0;i<n;i++){
        sc(x);
        freq[x]++;
        if(x==ca){
            if(flag==0){
                flag=1;
            }
            if(!s.empty){
            it=s.begin();
            while(it!=s.end()){
                if(freq[*it]<freq[ca]){
                    s.erase(*it);
                }
                it++;
            }
            }
            if(s.empty()){
                check=1;
            }
        }
        else{
            if(flag==0){
                s.insert(x);
            }
        }
    }
    if(check==1){
        printf("-1");
    }
    else{
        printf("%d",*s.begin());
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(x) scanf("%s",x)
#define mod 1000000007
#define ll long long
#define pb push_back
#define mp make_pair
#define pi pair<int,int>
int freq[1000005];
bool use[1000005];
int main(){
    int n,ca;
    sc(n);
    sc(ca);
    int x,flag=0;
    for(int i=0;i<n;i++){
        sc(x);
        freq[x]++;
        if(x==ca){
            if(flag==0){
                flag=1;
            }
        }
        else{
            if(flag==0){
                use[x]=true;
            }
            else{
                if(use[x]==true){
                    if(freq[x]<freq[ca]){
                        use[x]=false;
                    }
                }
            }
        }
    }
    int ans=-1;
    for(int i=1;i<=1000000;i++){
        if(i!=ca && use[i]==true && freq[i]>=freq[ca]){
            ans=i;
            break;
        }
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(x) scanf("%s",x)
#define mod 1000000007
#define ll long long
#define pb push_back
#define mp make_pair
#define pi pair<int,int>
int freq[1000005];
bool use[1000005];
int main(){
    int n,ca;
    sc(n);
    sc(ca);
    int x,flag=0;
    for(int i=0;i<n;i++){
        sc(x);
        if(x==ca){
            if(flag==0){
                flag=1;
            }
        }
        else{
            if(flag==0){
                use[x]=true;
            }
            else{
                if(use[x]==true){
                    if(freq[x]<freq[ca]){
                        use[x]=false;
                    }
                }
            }
        }
        freq[x]++;
    }
    int ans=-1;
    for(int i=1;i<=1000000;i++){
        if(i!=ca && use[i]==true && freq[i]>=freq[ca]){
            ans=i;
            break;
        }
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(x) scanf("%s",x)
#define mod 1000000007
#define ll long long
#define pb push_back
#define mp make_pair
#define pi pair<int,int>
set <int> s;
int freq[1000005];
int main(){
    int n,ca;
    sc(n);
    sc(ca);
    int x,flag=0,check=0;
    set <int> :: iterator it;
    for(int i=0;i<n;i++){
        sc(x);
        freq[x]++;
        if(x==ca){
            if(flag==0){
                flag=1;
            }
            if(!s.empty){
            it=s.begin();
            while(!s.empty &&  it!=s.end()){
                if(freq[*it]<freq[ca]){
                    s.erase(*it);
                }
                it++;
            }
            }
            if(s.empty()){
                check=1;
            }
        }
        else{
            if(flag==0){
                s.insert(x);
            }
        }
    }
    if(check==1){
        printf("-1");
    }
    else{
        printf("%d",*s.begin());
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(x) scanf("%s",x)
#define mod 1000000007
#define ll long long
#define pb push_back
#define mp make_pair
#define pi pair<int,int>
int freq[1000005];
bool use[1000005];
int main(){
    int n,ca;
    sc(n);
    sc(ca);
    int x,flag=0;
    for(int i=0;i<n;i++){
        sc(x);
        freq[x]++;
        if(x==ca){
            if(flag==0){
                flag=1;
            }
        }
        else{
            if(flag==0){
                use[x]=true;
            }
            else{
                if(use[x]==true){
                    if(freq[x]<freq[ca]){
                        use[x]=false;
                    }
                }
            }
        }
    }
    int ans=-1;
    for(int i=1;i<=n;i++){
        if(use[i]==true && freq[i]>=freq[ca]){
            ans=i;
            break;
        }
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define scs(x) scanf("%s",x)
#define mod 1000000007
#define ll long long
#define pb push_back
#define mp make_pair
#define pi pair<int,int>
set <int> s;
int freq[1000005];
int main(){
    int n,ca;
    sc(n);
    sc(ca);
    int x,flag=0,check=0;
    set <int> :: iterator it;
    for(int i=0;i<n;i++){
        sc(x);
        freq[x]++;
        if(x==ca){
            if(flag==0){
                flag=1;
            }
            if(!s.empty()){
            it=s.begin();
            while(!s.empty() &&  it!=s.end()){
                if(freq[*it]<freq[ca]){
                    s.erase(*it);
                }
                it++;
            }
            }
            if(s.empty()){
                check=1;
            }
        }
        else{
            if(flag==0){
                s.insert(x);
            }
        }
    }
    if(check==1){
        printf("-1");
    }
    else{
        printf("%d",*s.begin());
    }
    return 0;
}


#include <bits/stdc++.h>
#define N 1000006

using namespace std;
int n, A, B, a[N];
bool mark[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> A;
    for(int i = 0; i < n; ++i)
    {
        int x; cin >> x;
        if(x != A && a[x] < a[A]) mark[x] = true;
        a[x]++;
    }
    for(int i = 1; i < N; ++i)
        if(i != A && a[i] && a[i] >= a[A] && !mark[i]){
            cout << i; return 0;
        }
    cout << "-1";
    return 0;
}


#include <bits/stdc++.h>
#define N 1000006

using namespace std;
int n, A, a[N];
vector<int> kq;
bool mark[N], did[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> A;
    for(int i = 0; i < n; ++i)
    {
        int x; cin >> x;
        a[x]++;
        if(x != A){
            if(a[x] < a[A]) mark[x] = true;
            else if(!mark[x] && !did[x] && a[x] >= a[A])
                kq.push_back(x);
        }
    }
    for(auto i : kq) if(!mark[i] && a[i] >= a[A]){
        cout << i; return 0;
    }
    cout << "-1";
    return 0;
}


#include <bits/stdc++.h>
#define N 1000006

using namespace std;
int n, A, a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> A;
    ans = -1;
    for(int i = 1; i <= n; ++i){
        int x; cin >> x;
        a[x]++;
    }
    for(int i = 1; i < N; ++i)
        if(i != A && a[i] > a[A]){
            cout << i;
            return 0;
        }
    cout << "-1";
    return 0;
}


#include <bits/stdc++.h>
#define N 1000006

using namespace std;
int n, A, a[N];
vector<int> kq;
bool mark[N], did[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> A;
    for(int i = 0; i < n; ++i)
    {
        int x; cin >> x;
        a[x]++;
        if(x != A){
            if(a[x] < a[A]) mark[x] = true;
            else if(!mark[x] && !did[x] && a[x] >= a[A])
                kq.push_back(x);
        }
    }
    for(auto i : kq) if(!mark[i]){
        cout << i; return 0;
    }
    cout << "-1";
    return 0;
}


#include <bits/stdc++.h>
#define N 1000006

using namespace std;
int n, A, a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> A;
    memset(a, 0, sizeof(a));
    for(int i = 1; i <= n; ++i){
        int x; cin >> x;
        a[x]++;
    }
    for(int i = 1; i < N; ++i)
        if(i != A && a[i] > a[A]){
            cout << i;
            return 0;
        }
    cout << "-1";
    return 0;
}


#include <bits/stdc++.h>
#define N 1000006

using namespace std;
int n, ans, A, a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> A;
    ans = -1;
    for(int i = 1; i <= n; ++i){
        int x; cin >> x;
        a[x]++;
        if(x != A && a[x] > a[A]) ans = x;
    }

    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
#define N 1000006

using namespace std;
int n, A, a[N];
bool mark[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> A;
    for(int i = 0; i < n; ++i)
    {
        int x; cin >> x;
        a[x]++;
        if(x!=A && a[x]<a[A]) mark[x] = true;
    }
    for(int i = 1; i < N; ++i)
        if(i!=A && a[i] && a[i]>=a[A] && !mark[i]){
            cout << i; return 0;
        }
    cout << "-1";
    return 0;
}


#include <bits/stdc++.h>
#define N 1000006

using namespace std;
int n, A, B, a[N];
bool mark[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> A;
    for(int i = 0; i < n; ++i)
    {
        int x; cin >> x;
        a[x]++;
        if(x != A && a[x] < a[A]) mark[x] = true;
    }
    B = -1;
    for(int i = 1; i < N; ++i)
        if(i != A && a[i] && a[i] >= a[A] && !mark[i])
            B = i;
    cout << B;
    return 0;
}


#include <bits/stdc++.h>
#define N 1000006

using namespace std;
int n, A, ans, a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> A;
    ans = 0;
    for(int i = 1; i <= n; ++i){
        int x; cin >> x;
        a[x]++;
        if(x != A && a[x] > a[A] && a[x] > a[ans])
            ans = x;
    }

    if(a[ans] > a[A]) cout << ans;
    else cout << "-1";
    return 0;
}


#include <bits/stdc++.h>
#define N 1000006

using namespace std;
int n, A, a[N];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> A;
    for(int i = 1; i <= n; ++i){
        int x; cin >> x;
        a[x]++;
    }
    for(int i = 1; i < N; ++i)
        if(i != A && a[i] > a[A]){
            cout << i;
            return 0;
        }
    cout << "-1";
    return 0;
}


#include <bits/stdc++.h>
#define N 1000006

using namespace std;
int n, A, a[N]

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> A;
    ans = -1;
    for(int i = 1; i <= n; ++i){
        int x; cin >> x;
        a[x]++;
        if(x != A && a[x] > a[A]) ans = x;
    }

    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
struct Node {
    int c, cnt;
    Node(int c = 0, int cnt = 0): c(c), cnt(cnt) {}
    bool operator==(const Node &n)const {
        return c == n.c;
    }
    bool operator<(const Node &n)const {
        if(cnt == n.cnt)
            return c < n.c;
        return cnt < n.cnt;
    }
};
int g_n, g_a, g_cnta, in[SZ];
set<Node>stn;
void prete() {}
void input() {
    stn.clear();
    for(int i = 0; i < g_n; ++i) {
        scanf(" %d", in + i);
        if(in[i] != g_a) {
            stn.emplace(in[i], 0);
        }
    }
}
void solve() {
    g_cnta = 0;
    for(int i = 0; i < g_n; ++i) {
        int c = in[i];
        if(c == g_a) {
            ++g_cnta;
        } else {
            while(!stn.empty() && ((stn.begin()->cnt) < g_cnta)) {
                stn.erase(stn.begin());
            }
            if(stn.empty()) {
                break;
            }
            if(stn.find(Node(c)) != stn.end()) {
                Node n = *stn.find(Node(c));
                stn.erase(Node(c));
                ++n.cnt;
                stn.insert(n);
            }
        }
    }
    int p = -1;
    if(!stn.empty()) {
        p = stn.rbegin()->c;
    }
    cout << p << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_n >> g_a) {
        input();
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
struct Node {
    int c, cnt;
    Node(int c = 0, int cnt = 0): c(c), cnt(cnt) {}
    bool operator==(const Node &n)const {
        return c == n.c;
    }
    bool operator<(const Node &n)const {
        if(cnt == n.cnt)
            return c < n.c;
        return cnt < n.cnt;
    }
};
int g_n, g_a, g_cnta, in[SZ];
set<Node>stn;
void prete() {}
void input() {
    stn.clear();
    for(int i = 0; i < g_n; ++i) {
        scanf(" %d", in + i);
        if(in[i] != g_a) {
            stn.emplace(in[i], 0);
        }
    }
}
void solve() {
    g_cnta = 0;
    for(int i = 0; i < g_n; ++i) {
        int c = in[i];
        if(c == g_a) {
            ++g_cnta;
            while(!stn.empty() && ((stn.begin()->cnt) < g_cnta)) {
                stn.erase(stn.begin());
            }
            if(stn.empty()) {
                break;
            }

        } else {
            Node n = *stn.find(Node(c));
            stn.erase(Node(c));
            ++n.cnt;
            stn.insert(n);
        }
    }
    int p = -1;
    if(!stn.empty()) {
        p = stn.rbegin()->c;
    }
    cout << p << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_n >> g_a) {
        input();
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
bool vb[SZ];
int g_n, g_a, in[SZ], cnt[SZ];

void prete() {}
void input() {
    memset(vb, 0, sizeof(vb));
    memset(cnt, 0, sizeof(cnt));
    for(int i = 0; i < g_n; ++i) {
        scanf(" %d", in + i);
    }
}
void solve() {
    for(int i = 0; i < g_n; ++i) {
        int c = in[i];
        ++cnt[c];
        if(c == g_a || vb[c]) continue;
        if(cnt[c] < cnt[g_a]) {
            vb[c] = true;
        }
    }
    int p = -1;
    for(int i = g_n - 1; i >= 0; --i) {
        int c = in[i];
        if(c == g_a || vb[c])continue;
        p = c;
        break;
    }
    cout << p << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_n >> g_a) {
        input();
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
struct Node {
    int c, cnt;
    Node(int c = 0, int cnt = 0): c(c), cnt(cnt) {}
    bool operator==(const Node &n)const {
        return c == n.c;
    }
    bool operator<(const Node &n)const {
        if(cnt == n.cnt)
            return c < n.c;
        return cnt < n.cnt;
    }
};
int g_n, g_a, g_cnta, in[SZ];
set<Node>stn;
void prete() {}
void input() {
    stn.clear();
    for(int i = 0; i < g_n; ++i) {
        scanf(" %d", in + i);
        if(in[i] != g_a) {
            stn.emplace(in[i], 0);
        }
    }
}
void solve() {
    g_cnta = 0;
    for(int i = 0; i < g_n; ++i) {
        int c = in[i];
        if(c == g_a) {
            ++g_cnta;
            while(!stn.empty() && ((stn.begin()->cnt) < g_cnta)) {
                stn.erase(stn.begin());
            }
            if(stn.empty()) {
                break;
            }

        } else {
            if(stn.find(Node(c)) != stn.end()) {
                Node n = *stn.find(Node(c));
                stn.erase(Node(c));
                ++n.cnt;
                stn.insert(n);
            }
        }
    }
    int p = -1;
    if(!stn.empty()) {
        p = stn.rbegin()->c;
    }
    cout << p << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_n >> g_a) {
        input();
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;

int g_n, g_a, in[SZ], cnt[SZ], vis[SZ];
void prete() {}
void input() {
    memset(vis, -1, sizeof(vis));
    memset(cnt, 0, sizeof(cnt));
    for(int i = 0; i < g_n; ++i) {
        scanf(" %d", in + i);
    }
}
void solve() {
    for(int i = 0; i < g_n; ++i) {
        int c = in[i];
        if(vis[c] == -1) {
            vis[c] = 0;
        }
        if(cnt[c] < cnt[g_a]) {
            vis[c] = 1;
        }
        ++cnt[c];
    }
    for(int i = 0; i < g_n; ++i) {
        int c = in[i];
        if(cnt[c] < cnt[g_a]) {
            vis[c] = 1;
        }
    }
    int p = -1;
    for(int i = 0; i < g_n; ++i) {
        int c = in[i];
        if(!vis[c] && c != g_a) {
            p = c;
        }
    }
    cout << p << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_n >> g_a) {
        input();
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
struct Node {
    int c, cnt;
    Node(int c = 0, int cnt = 0): c(c), cnt(cnt) {}
    bool operator==(const Node &n)const {
        return c == n.c;
    }
    bool operator<(const Node &n)const {
        if(cnt == n.cnt)
            return c < n.c;
        return cnt < n.cnt;
    }
};
int g_n, g_a, g_cnta, in[SZ];
set<Node>stn;
void prete() {}
void input() {
    stn.clear();
    for(int i = 0; i < g_n; ++i) {
        scanf(" %d", in + i);
        if(in[i] != g_a) {
            stn.emplace(in[i], 0);
        }
    }
}
void solve() {
    g_cnta = 0;
    for(int i = 0; i < g_n; ++i) {
        int c = in[i];
        if(c == g_a) {
            ++g_cnta;
        } else {
            if(g_cnta > 0) {
                while(!stn.empty() && ((stn.begin()->cnt) < g_cnta)) {
                    stn.erase(stn.begin());
                }
                if(stn.empty()) {
                    break;
                }
            }
            if(stn.find(Node(c)) != stn.end()) {
                Node n = *stn.find(Node(c));
                stn.erase(Node(c));
                ++n.cnt;
                stn.insert(n);
            }
        }
    }
    int p = -1;
    if(!stn.empty()) {
        p = stn.rbegin()->c;
    }
    cout << p << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_n >> g_a) {
        input();
        solve();
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
const int shuzu = 1e6+10 ;
#define MAXN 0x3f3f3f3f;
typedef struct
{
    int zhi;
    int times;
} A;
A a[shuzu];
bool cmp(A a,A b)
{
    return a.times>b.times;
}
int main()
{
    int n,Alice,Alicecolor=0;
    scanf("%d %d",&n,&Alice);
    int carcolor;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&carcolor);
        if(carcolor==Alice)
        {
            Alicecolor++;
            continue;
        }
        a[carcolor].times++;
        a[carcolor].zhi=carcolor;

    }
    sort(a,a+shuzu-1,cmp);
    if(a[0].times>=Alicecolor)
        printf("%d\n",a[0].zhi);
    else
        printf("-1\n");
    return 0;
}


#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int s[1000000];
int main()
{
    long long n,m,t1=-1,i,z=0,a;
    scanf("%lld %lld",&n,&m);
    while(n--)
    {
        scanf("%lld",&a);
        if(a==m)
            z++;
        else if(s[a]>=z)
        {
            s[a]++;
        }
    }
    // printf("%d  %d\n",s[2],z);
    for(i=1;i<=1e6;i++)
    {
        if(i==m)
            continue;
        if(s[i]>=z)
        {
            t1=1;
            printf("%lld\n",i);
            break;
        }
    }
    if(t1==-1)
        printf("%lld\n",t1);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
const int Max=1e6+10;
int a;
struct aa
{
    int color;
    int num;
}mark[Max];
int cmp(aa a,aa b)
{
    return a.num>b.num;
}
int main()
{
    int m,n,i;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(mark,0,sizeof(mark));
        int ans=0;
        for(i=0; i<m; i++)
        {
            scanf("%d",&a);
            mark[a].num++;
            mark[a].color=a;
        }
        int maxs=mark[n].num;
        sort(mark,mark+Max,cmp);
        for(i=0;i<1e6+10;i++)
        {
            if(mark[i].num==0)
                break;
            if(mark[i].num>maxs)
            {
                maxs=mark[i].num;
                ans=mark[i].color;
            }
        }
        if(ans==0)
        {
            printf("-1\n");
        }
        else
            printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
const int shuzu = 1e6+10 ;
#define MAXN 0x3f3f3f3f;
struct A
{
    int xiabiao;
    int see;
}a[shuzu];

bool cmp(struct A a,struct A b)
{
    return a.see>b.see;
}
int main()
{
    int n,Alice;
    while(~scanf("%d %d",&n,&Alice))
    {
        int p;
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p);
            a[p].xiabiao=p;
            a[p].see ++ ;
        }
        sort(a+1,a+1+n,cmp);
        /*for(int i=1;i<=n;i++)
            printf(" %d,%d ",a[i].xiabiao,a[i].see);
        cout << endl;*/
        if(n==1)
        {
            if(Alice==a[1].xiabiao)
                printf("-1\n");
            else
                printf("%d\n",a[1].xiabiao);
        }
        if(Alice!=a[1].xiabiao)
            printf("%d\n",a[1].xiabiao);
        else
            if(Alice==a[1].xiabiao&&a[1].see==a[2].see)
                printf("%d\n",a[2].xiabiao);
            else
                printf("-1\n");
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
const int shuzu = 1e6+10 ;
#define MAXN 0x3f3f3f3f;
struct A
{
    int xiabiao;
    int see;
}a[shuzu];

bool cmp(struct A a,struct A b)
{
    return a.see>b.see;
}
int main()
{
    int n,Alice;
    while(~scanf("%d %d",&n,&Alice))
    {
        int p;
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p);
            a[p].xiabiao=p;
            a[p].see ++ ;
        }
        sort(a+1,a+1+shuzu,cmp);
        /*for(int i=1;i<=n;i++)
            printf(" %d,%d ",a[i].xiabiao,a[i].see);
        cout << endl;*/
        if(n==1)
        {
            if(Alice==a[1].xiabiao)
                printf("-1\n");
            else
                printf("%d\n",a[1].xiabiao);
            continue;
        }
        if(Alice!=a[1].xiabiao)
            printf("%d\n",a[1].xiabiao);
        else
            if(Alice==a[1].xiabiao&&a[1].see==a[2].see)
                printf("%d\n",a[2].xiabiao);
            else
                printf("-1\n");
    }
    return 0;
}


#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int a[100005],b[100005],d[100005];
int main()
{
    long long n,m,k,c,x=0,t1,t2,t,i,j,y=0,z=0,s=0;
    scanf("%lld %lld",&n,&m);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]==m)
            z++;
    }
    for(i=0;i<n;i++)
    {
        k=a[i];
        c=i;
        t1=0;
        for(j=0;j<n;j++)
        {
            if(a[j]==k)
                t1++;
        }
        t2=0;
        for(t=i-1;t>=0;t--)
            if(a[t]==k)
                t2++;
        if(t2>0)
            continue;
       // b[x++]=t1-t2;
        //d[y++]=a[i];
        if((t1-t2)>=z && a[i]!=m)
        {
            s=1;
              printf("%d\n",a[i]);
              break;
        }
    }
    if(s==0)
        printf("-1\n");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
const int shuzu = 1e6+10 ;
#define MAXN 0x3f3f3f3f;
int ha[shuzu];
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n,Alice;
    while(~scanf("%d %d",&n,&Alice))
    {
        int car ;
        memset(ha,0,sizeof(ha));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&car);
            ha[car]++;
        }
        sort(ha+1,ha+1+n,cmp);
        if(Alice!=ha[1])
            printf("%d\n",ha[1]);
        else
            if(Alice==ha[1]&&ha[1]==ha[2])
                printf("%d\n",ha[2]);
            else
                printf("-1\n");

    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<queue>
#define PI acos(-1)
#define mem(a, b) memset(a, b, sizeof(a))
int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int dir4[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;
const int maxn = 1e7+10;
const int mod = 100000073;
ll b[maxn];

int main()
{
    //IN;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,a,k,i,j,cnt=0;
    cin>>n>>a;
    while(n--)
    {
        cin>>k;
        if(k==a)
            cnt++;
        else if(b[k]>=cnt)
            b[k]++;
    }
    ll ans=-1;
    for(i=1; i<=maxn; i++)
    {
        if(i==a)
            continue;
        if(b[i]>=cnt)
        {
            ans=i;
            break;
        }

    }
    cout<<ans<<endl;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
const int Max=1e6+10;
int a;
struct aa
{
    int color;
    int num;
}mark[Max];
int cmp(aa a,aa b)
{
    return a.num>b.num;
}
int main()
{
    int m,n,i;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(mark,0,sizeof(mark));
        int ans=0;
        for(i=0; i<m; i++)
        {
            scanf("%d",&a);
            mark[a].num++;
            mark[a].color=a;
        }
        int maxs=mark[n].num;
        sort(mark,mark+Max,cmp);
        for(i=0;i<Max;i++)
        {
            if(mark[i].num>=maxs&&mark[i].color!=n)
            {
                maxs=mark[i].num;
                ans=mark[i].color;
                break;
            }
        }
        if(ans==0)
        {
            printf("-1\n");
        }
        else
            printf("%d\n",ans);
    }
    return 0;
}


//
//  main.cpp
//  CF Educational 24 D
//
//  Created by luras on 2017/7/5.
//  Copyright © 2017年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e6 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

int n, A, x, num[N];

set<pair<int, int> > sot;
set<pair<int, int> > :: iterator it;

int a[N];
int cmp(int a, int b)
{
    return num[a] > num[b];
}

int main()
{
    while(~ scanf("%d%d", &n, &A)){
        sot.clear();
        int an = 0;

        for(int i = 1; i <= n; i ++){
            scanf("%d", &x);
            num[x] ++;
            if(x == A){
                sort(a + 1, a + an + 1, cmp);
                for(int j = an; j >= 1; j --)
                    if(num[a[j]] >= num[A]) {
                        an = j; break;
                    }
            }
            else{
                if(num[A] == 0) a[++ an] = x;
            }
        }
        if(an == 0) puts("-1");
        else printf("%d\n", a[1]);

    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF Educational 24 D
//
//  Created by luras on 2017/7/5.
//  Copyright © 2017年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e6 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

int n, A, x, num[N];

set<pair<int, int> > sot;
set<pair<int, int> > :: iterator it;

int main()
{
    while(~ scanf("%d%d", &n, &A)){
        sot.clear();
        for(int i = 1; i <= n; i ++){
            scanf("%d", &x);
            num[x] ++;
            if(x == A){

                while(1){
                    if(sot.size() == 0) break;
                    it = sot.begin();
                    if(it -> first < num[A]){
                        sot.erase(it);
                    }
                    else break;
                }
                /*
                for(it = sot.begin(); it != sot.end(); it ++){
                    if(it -> first < num[A]){
                        sot.erase({it -> first, it -> second});
                    }
                    else break;
                }
                 */
            }
            else{
                if(sot.find({num[x] - 1, x}) != sot.end()){
                    sot.erase({num[x] - 1, x});
                    sot.insert({num[x], x});
                }
                else if(num[A] == 0) sot.insert({num[x], x});
            }
        }
        if(sot.size() == 0) puts("-1");
        else printf("%d\n", sot.begin() -> second);

    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF Educational 24 D
//
//  Created by luras on 2017/7/5.
//  Copyright © 2017年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e6 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

int n, A, x, num[N];

set<pair<int, int> > sot;
set<pair<int, int> > :: iterator it;

set<int> sott;
set<int> :: iterator ir;

int main()
{
    while(~ scanf("%d%d", &n, &A)){
        sot.clear();
        sott.clear();
        for(int i = 1; i <= n; i ++){
            scanf("%d", &x);
            num[x] ++;
            if(x == A){
                for(ir = sott.begin(); ir != sott.end(); ir ++){
                    if(num[*ir] < num[A]){
                        sott.erase(*ir);
                    }
                }
            }
            else{
                if(num[A] == 0) sott.insert(x);
            }
            /*
            if(x == A){
                for(it = sot.begin(); it != sot.end(); it ++){
                    if(it -> first < num[A]){
                        sot.erase({it -> first, it -> second});
                    }
                    else break;
                }
            }
            else{
                if(sot.find({num[x] - 1, x}) != sot.end()){
                    sot.erase({num[x] - 1, x});
                    sot.insert({num[x], x});
                }
                else if(num[A] == 0) sot.insert({num[x], x});
            }
             */

        }
        if(sott.size() == 0) puts("-1");
        else printf("%d\n", *sott.begin());

    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF Educational 24 D
//
//  Created by luras on 2017/7/5.
//  Copyright © 2017年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e6 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

int n, A, x, num[N];

set<pair<int, int> > sot;
set<pair<int, int> > :: iterator it;

int a[N];
int cmp(int a, int b)
{
    return num[a] > num[b];
}

int main()
{
    while(~ scanf("%d%d", &n, &A)){
        sot.clear();
        int an = 0;

        for(int i = 1; i <= n; i ++){
            scanf("%d", &x);
            num[x] ++;
            if(x == A){
                sort(a + 1, a + an + 1, cmp);
                int tmp = 0;
                for(int j = an; j >= 1; j --)
                    if(num[a[j]] >= num[A]) {
                        an = j; tmp = 1; break;
                    }
                if(tmp == 0) an = 0;
            }
            else{
                if(num[A] == 0) a[++ an] = x;
            }
        }
        if(an == 0) puts("-1");
        else printf("%d\n", a[1]);

    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF Educational 24 D
//
//  Created by luras on 2017/7/5.
//  Copyright © 2017年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e6 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

int n, A, x, num[N];

set<pair<int, int> > sot;
set<pair<int, int> > :: iterator it;

int main()
{
    while(~ scanf("%d%d", &n, &A)){
        sot.clear();
        for(int i = 1; i <= n; i ++){
            scanf("%d", &x);
            num[x] ++;
            if(x == A){
                for(it = sot.begin(); it != sot.end(); it ++){
                    if(it -> first < num[A]){
                        sot.erase({it -> first, it -> second});
                    }
                    else break;
                }
            }
            else{
                if(sot.find({num[x] - 1, x}) != sot.end()){
                    sot.erase({num[x] - 1, x});
                    sot.insert({num[x], x});
                }
                else if(num[A] == 0) sot.insert({num[x], x});
            }
        }
        if(sot.size() == 0) puts("-1");
        else printf("%d\n", sot.begin() -> second);

    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF Educational 24 D
//
//  Created by luras on 2017/7/5.
//  Copyright © 2017年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e6 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

int n, A, x, num[N];

set<pair<int, int> > sot;
set<pair<int, int> > :: iterator it;

int main()
{
    while(~ scanf("%d%d", &n, &A)){
        sot.clear();
        for(int i = 1; i <= n; i ++){
            scanf("%d", &x);
            num[x] ++;
            if(x == A){
                for(it = sot.begin(); it != sot.end(); it ++){
                    if(it -> first < num[A]){
                        sot.erase(it);
                    }
                    else break;
                }
            }
            else{
                if(sot.find({num[x] - 1, x}) != sot.end()){
                    sot.erase({num[x] - 1, x});
                    sot.insert({num[x], x});
                }
                else if(num[A] == 0) sot.insert({num[x], x});
            }
        }
        if(sot.size() == 0) puts("-1");
        else printf("%d\n", sot.begin() -> second);

    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF Educational 24 D
//
//  Created by luras on 2017/7/5.
//  Copyright © 2017年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e7 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

int n, A, x, num[N];

set<pair<int, int> > sot;
set<pair<int, int> > :: iterator it;

set<int> sott;
set<int> :: iterator ir;

int main()
{
    while(~ scanf("%d%d", &n, &A)){
        sot.clear();
        sott.clear();
        for(int i = 1; i <= n; i ++){
            scanf("%d", &x);
            num[x] ++;
            if(x == A){
                for(ir = sott.begin(); ir != sott.end(); ir ++){
                    if(num[*ir] < num[A]){
                        sott.erase(*ir);
                    }
                }
            }
            else{
                if(num[A] == 0) sott.insert(x);
            }
            /*
            if(x == A){
                for(it = sot.begin(); it != sot.end(); it ++){
                    if(it -> first < num[A]){
                        sot.erase({it -> first, it -> second});
                    }
                    else break;
                }
            }
            else{
                if(sot.find({num[x] - 1, x}) != sot.end()){
                    sot.erase({num[x] - 1, x});
                    sot.insert({num[x], x});
                }
                else if(num[A] == 0) sot.insert({num[x], x});
            }
             */

        }
        if(sott.size() == 0) puts("-1");
        else printf("%d\n", *sott.begin());

    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


//
//  main.cpp
//  CF Educational 24 D
//
//  Created by luras on 2017/7/5.
//  Copyright © 2017年 luras. All rights reserved.
//

#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define ls o << 1
#define rs o << 1 | 1
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<set>
#include<string>
#include<algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
template <class T> inline void gmax(T &a, T b){if(b > a) a = b;}
template <class T> inline void gmin(T &a, T b){if(b < a) a = b;}
using namespace std;
const int N = 1e6 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, ms127 = 2139062143;
const double PI = acos(-1.0), eps = 1e-8;
typedef long long LL;
void fre()
{
    freopen("/Users/luras/Desktop/in.txt", "r", stdin);
    freopen("/Users/luras/Desktop/out.txt", "w", stdout);
}
const int INF = 1e9;
int casenum, casei;

int n, A, x, num[N];

set<pair<int, int> > sot;
set<pair<int, int> > :: iterator it;

int a[N];
int cmp(int a, int b)
{
    return num[a] > num[b];
}

int main()
{
    while(~ scanf("%d%d", &n, &A)){
        sot.clear();
        int an = 0;

        for(int i = 1; i <= n; i ++){
            scanf("%d", &x);
            num[x] ++;
            if(x == A){
                sort(a + 1, a + an + 1, cmp);
                for(int i = an; i >= 1; i --) if(num[a[i]] >= num[A]) {an = i; break;}
            }
            else{
                if(num[A] == 0) a[++ an] = x;
            }
            /*
            if(x == A){
                for(it = sot.begin(); it != sot.end(); it ++){
                    if(it -> first < num[A]){
                        sot.erase({it -> first, it -> second});
                    }
                    else break;
                }
            }
            else{
                if(sot.find({num[x] - 1, x}) != sot.end()){
                    sot.erase({num[x] - 1, x});
                    sot.insert({num[x], x});
                }
                else if(num[A] == 0) sot.insert({num[x], x});
            }
             */

        }
        if(an == 0) puts("-1");
        else printf("%d\n", a[1]);

    }
    return 0;
}

/*

 题意:

 类型:

 分析:

 优化:

 trick:

 数据:

 Sample Input

 Sample Output

 */


#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <deque>
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fori(i, n) for(int (i)=0; (i)<n; (i)++)
#define PI 3.14159265356
#define LD long double
#define getiii(a,b,c) getii(a,b),geti(c)
#define getii(a,b) geti(a),geti(b)
#define geti(a) scanf("%d",&a)
const ull mod = 1000000007;
int Inf = (int)2e9;
using namespace std;
int main()
{
	int n,A;
	getii(n,A);
	int freq[1000001]={0};
	pair<int,int>  s;
	s.first=0,s.second=0;
	int cna=0,cnb=0,res1=0,fl=0;
	bool r1=true,r2=true;
	fori(i,n)
	{
		int val;
		geti(val);
		if(val==A)
		cna++;
		if(val!=A)
		{
			freq[val]++;
			if(freq[val]>s.second)
			{
				s.first=val;
				s.second=freq[val];
			}
		}
		if(freq[s.first]<cna)
			{
				cout<<"-1"<<'\n';
				return 0;
			}
}
	if(s.first>0)
	cout<<s.first;
	else
	cout<<"-1";
}


#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <deque>
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fori(i, n) for(int (i)=0; (i)<n; (i)++)
#define PI 3.14159265356
#define LD long double
#define getiii(a,b,c) getii(a,b),geti(c)
#define getii(a,b) geti(a),geti(b)
#define geti(a) scanf("%d",&a)
const ull mod = 1000000007;
int Inf = (int)2e9;
using namespace std;
bool cmpf(const pair<int,int> &a,const pair<int,int> &b)
{
    return (a.second > b.second);
}
int main()
{
	int n,A;
	getii(n,A);
	int freq[10001]={0};
	vector <pair<int,int> > v(100001);
	pair <int,int> p;
	p.first=0,p.second=0;
	int cna=0,res1=0,fl=0,k=0,top=0;
	fori(i,n)
	{
		int val;
		geti(val);
		if(val==A)
		cna++;

		if(val!=A&&!cna)
		{
			freq[val]++;
			v[k++]=(mp(val,freq[val]));
			if(p.first>freq[val])
			{
				p.first=freq[val];
				p.second=val;
			}
		}
		else
		if(val!=A)
		freq[val]++;
		if(cna&&!fl)
		{
			sort(v.begin(),v.begin()+k,cmpf);
			fl=1;
		}
		if(fl&&v.empty()==false&&freq[v[top].first]<cna)
		{
			while(freq[v[top].first]<cna&&top<k)
			top++;
			if(top==k)
			{
				cout<<"-1";
				return 0;
			}
		}

}
	if(v[top].first>0)
	cout<<v[top].first;
	else
	cout<<"-1";
}


#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <deque>
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fori(i, n) for(int (i)=0; (i)<n; (i)++)
#define PI 3.14159265356
#define LD long double
#define getiii(a,b,c) getii(a,b),geti(c)
#define getii(a,b) geti(a),geti(b)
#define geti(a) scanf("%d",&a)
const ull mod = 1000000007;
int Inf = (int)2e9;
using namespace std;

int main()
{
	int n,A;
	getii(n,A);
	int freq[10001]={0};
	pair<int,int>  s;
	s.first=0,s.second=0;
	int cna=0,cnb=0,res1=0,fl=0;
	bool r1,r2;
	while(n--)
	{
		int val;
		geti(val);
		if(cna)
		{
			if(fl==0&&val!=A)
			res1=val;
			if(val!=A)
			{
			freq[val]++;
			fl=1;}

			if(freq[res1]<cna&&freq[s.first]<cna)
			{
				cout<<"-1"<<'\n';
				return 0;
			}
			if(freq[res1]<cna)
			r1=false;
			if(freq[s.first]<cna)
			r2=false;
			if(r1==false&&r2==false)
			{
				cout<<"-1"<<'\n';
				return 0;
			}
	}
		if(val==A)
		cna++;
		if(fl==0&&val!=A)
		{
			freq[val]++;
			if(freq[val]>s.second)
			{
				s.first=val;
				s.second=freq[val];
			}
		}

	}
	if(r1==true)
	if(res1>0)
	cout<<res1;
	else
	cout<<"-1";
	else
	if(s.first>0)
	cout<<s.first;
	else
	cout<<"-1";

}


#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <deque>
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fori(i, n) for(int (i)=0; (i)<n; (i)++)
#define PI 3.14159265356
#define LD long double
#define getiii(a,b,c) getii(a,b),geti(c)
#define getii(a,b) geti(a),geti(b)
#define geti(a) scanf("%d",&a)
const ull mod = 1000000007;
int Inf = (int)2e9;
using namespace std;

int main()
{
	int n,A;
	getii(n,A);
	int freq[1000001]={0};
	pair<int,int>  s;
	s.first=0,s.second=0;
	int cna=0,cnb=0,res1=0,fl=0;
	bool r1,r2;
	while(n--)
	{
		int val;
		geti(val);
		if(cna)
		{
			if(fl==0)
			res1=val;
			freq[val]++;
			fl=1;
			if(freq[res1]<cna&&freq[s.first]<cna)
			{
				cout<<"-1"<<'\n';
				return 0;
			}
			if(freq[res1]<cna)
			r1=false;
			if(freq[s.first]<cna)
			r2=false;
	}
		if(val==A)
		cna++;
		else if(fl==0)
		{
			freq[val]++;
			if(freq[val]>s.second)
			{
				s.first=val;
				s.second=freq[val];
			}
		}

	}
	if(r1==true)
	if(res1>0)
	cout<<res1;
	else
	cout<<"-1";
	else
	if(s.first>0)
	cout<<s.first;
	else
	cout<<"-1";

}


#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <deque>
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fori(i, n) for(int (i)=0; (i)<n; (i)++)
#define PI 3.14159265356
#define LD long double
#define getiii(a,b,c) getii(a,b),geti(c)
#define getii(a,b) geti(a),geti(b)
#define geti(a) scanf("%d",&a)
const ull mod = 1000000007;
int Inf = (int)2e9;
using namespace std;

int main()
{
	int n,A;
	getii(n,A);
	int freq[1000001]={0};
	pair<int,int>  s;
	s.first=0,s.second=0;
	int cna=0,cnb=0,res1=0,fl=0;
	bool r1,r2;
	while(n--)
	{
		int val;
		geti(val);
		if(cna)
		{
			if(fl==0&&val!=A)
			res1=val;
			if(val!=A)
			{
			freq[val]++;
			fl=1;}

			if(freq[res1]<cna&&freq[s.first]<cna)
			{
				cout<<"-1"<<'\n';
				return 0;
			}
			if(freq[res1]<cna)
			r1=false;
			if(freq[s.first]<cna)
			r2=false;
			if(r1==false&&r2==false)
			{
				cout<<"-1"<<'\n';
				return 0;
			}
	}
		if(val==A)
		cna++;
		if(fl==0&&val!=A)
		{
			freq[val]++;
			if(freq[val]>s.second)
			{
				s.first=val;
				s.second=freq[val];
			}
		}

	}
	if(r1==true)
	if(res1>0)
	cout<<res1;
	else
	cout<<"-1";
	else
	if(s.first>0)
	cout<<s.first;
	else
	cout<<"-1";

}


#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <deque>
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fori(i, n) for(int (i)=0; (i)<n; (i)++)
#define PI 3.14159265356
#define LD long double
#define getiii(a,b,c) getii(a,b),geti(c)
#define getii(a,b) geti(a),geti(b)
#define geti(a) scanf("%d",&a)
const ull mod = 1000000007;
int Inf = (int)2e9;
using namespace std;

int main()
{
	int n,A;
	getii(n,A);
	int freq[1000001]={0};
	pair<int,int>  s;
	s.first=0,s.second=0;
	int cna=0,cnb=0,res1=0,fl=0;
	bool r1,r2;
	while(n--)
	{
		int val;
		geti(val);
		if(cna)
		{
			if(fl==0)
			res1=val;
			freq[val]++;
			fl=1;
			if(freq[res1]<cna&&freq[s.first]<cna)
			{
				cout<<"-1"<<'\n';
				return 0;
			}
			if(freq[res1]<cna)
			r1=false;
			if(freq[s.first]<cna)
			r2=false;
	}
		if(val==A)
		cna++;
		else if(fl==0)
		{
			freq[val]++;
			if(freq[val]>s.second)
			{
				s.first=val;
				s.second=freq[val];
			}
		}

	}
	if(r1==true)
	cout<<res1;
	else
	cout<<s.first;

}


#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <deque>
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fori(i, n) for(int (i)=0; (i)<n; (i)++)
#define PI 3.14159265356
#define LD long double
#define getiii(a,b,c) getii(a,b),geti(c)
#define getii(a,b) geti(a),geti(b)
#define geti(a) scanf("%d",&a)
const ull mod = 1000000007;
int Inf = (int)2e9;
using namespace std;
bool cmpf(const pair<int,int> &a,const pair<int,int> &b)
{
    return (a.second > b.second);
}
int main()
{
	int n,A;
	getii(n,A);
	int freq[1000001]={0};
	vector <pair<int,int> > v(100001);
	pair <int,int> p;
	p.first=0,p.second=0;
	int cna=0,res1=0,fl=0,k=0,top=0;
	fori(i,n)
	{
		int val;
		geti(val);
		if(val==A)
		cna++;

		if(val!=A&&!cna)
		{
			freq[val]++;
			v[k++]=(mp(val,freq[val]));
			if(p.first>freq[val])
			{
				p.first=freq[val];
				p.second=val;
			}
		}
		else
		if(val!=A)
		freq[val]++;
		if(cna&&!fl)
		{
			sort(v.begin(),v.begin()+k,cmpf);
			fl=1;
		}
		if(fl&&v.empty()==false&&freq[v[top].first]<cna)
		{
			while(freq[v[top].first]<cna&&top<k)
			top++;
			if(top==k)
			{
				cout<<"-1";
				return 0;
			}
		}

}
	if(v[top].first>0)
	cout<<v[top].first;
	else
	cout<<"-1";
}


#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <deque>
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fori(i, n) for(int (i)=0; (i)<n; (i)++)
#define PI 3.14159265356
#define LD long double
#define getiii(a,b,c) getii(a,b),geti(c)
#define getii(a,b) geti(a),geti(b)
#define geti(a) scanf("%d",&a)
const ull mod = 1000000007;
int Inf = (int)2e9;
using namespace std;

int main()
{
	int n,A;
	getii(n,A);
	int freq[1000001]={0};
	pair<int,int>  s;
	s.first=0,s.second=0;
	int cna=0,cnb=0,res1=0,fl=0;
	bool r1=true,r2=true;
	while(n--)
	{
		int val;
		geti(val);
		if(cna&&val!=A)
		{
			if(fl==0)
			res1=val;
			freq[val]++;
			fl=1;
	}
		if(val==A)
		cna++;
		if(fl==0&&val!=A)
		{
			freq[val]++;
			if(freq[val]>s.second)
			{
				s.first=val;
				s.second=freq[val];
			}
		}
		if(freq[res1]<cna&&freq[s.first]<cna)
			{
				cout<<"-1"<<'\n';
				return 0;
			}

		if(freq[res1]<cna&&res1!=0)
		{
				r1=false;

			}
		if(freq[s.first]<cna)
			r2=false;

	}

	if(r1==true)
	{
	if(res1>0)
	cout<<res1;
	else
	cout<<"-1";}
	else if(r2==true)
	{
	if(s.first>0)
	cout<<s.first;
	else
	cout<<"-1";
}
}


#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <deque>
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fori(i, n) for(int (i)=0; (i)<n; (i)++)
#define PI 3.14159265356
#define LD long double
#define getiii(a,b,c) getii(a,b),geti(c)
#define getii(a,b) geti(a),geti(b)
#define geti(a) scanf("%d",&a)
const ull mod = 1000000007;
int Inf = (int)2e9;
using namespace std;

int main()
{
	int n,A;
	getii(n,A);
	int freq[1000001]={0};
	pair<int,int>  s;
	s.first=0,s.second=0;
	int cna=0,cnb=0,res1=0,fl=0;
	bool r1,r2;
	while(n--)
	{
		int val;
		geti(val);
		if(cna)
		{
			if(fl==0&&val!=A)
			res1=val;
			if(val!=A)
			{
			freq[val]++;
			fl=1;}

			if(freq[res1]<cna&&freq[s.first]<cna&&val!=A)
			{
				cout<<"-1"<<'\n';
				return 0;
			}
			else if(freq[res1]<cna+1&&freq[s.first]<cna+1&&val==A)
			{
				cout<<"-1"<<'\n';
				return 0;
			}
			if(freq[res1]<cna)
			r1=false;
			if(freq[s.first]<cna)
			r2=false;
	}
		if(val==A)
		cna++;
		if(fl==0&&val!=A)
		{
			freq[val]++;
			if(freq[val]>s.second)
			{
				s.first=val;
				s.second=freq[val];
			}
		}

	}
	if(r1==true)
	if(res1>0)
	cout<<res1;
	else
	cout<<"-1";
	else
	if(s.first>0)
	cout<<s.first;
	else
	cout<<"-1";

}


#include <bits/stdc++.h>
#define MAX 1000005

using namespace std;

int cnt[MAX];
bool vis[MAX];

int main()
{
	int n, A, x;

	cin >> n >> A;

	for ( int i = 0; i < n; i++ ) {
		cin >> x;
		cnt[x]++;
		if ( vis[x] ) continue;
		if ( cnt[x] < cnt[A] ) {
			vis[x] = false;
			continue;
		}
	}

	for ( int i = 1; i <= 1000000; i++ ) {
		if ( vis[i] ) continue;
		if ( cnt[i] >= cnt[A] ) {
			cout << i << endl;
			goto p;
		}
	}

	cout << "-1" << endl;
	p: { }

	return 0;
}


#include <bits/stdc++.h>
#define MAX 1000005

using namespace std;

int cnt[MAX];
bool vis[MAX];

int main()
{
	int n, A, x;

	cin >> n >> A;

	for ( int i = 0; i < n; i++ ) {
		cin >> x;
		cnt[x]++;
		if ( vis[x] ) continue;
		if ( cnt[x] < cnt[A] ) {
			vis[x] = true;
		}
	}

	for ( int i = 1; i <= 1000000; i++ ) {
		if ( vis[i] || i == A ) continue;
		if ( cnt[i] >= cnt[A] ) {
			cout << i << endl;
			goto p;
		}
	}

	cout << "-1" << endl;
	p: { }

	return 0;
}


#include <bits/stdc++.h>
#define MAX 1000005

using namespace std;

int cnt[MAX];
bool vis[MAX];

int main()
{
	int n, A, x;

	cin >> n >> A;

	for ( int i = 0; i < n; i++ ) {
		cin >> x;
		cnt[x]++;
		if ( vis[x] ) continue;
		if ( cnt[x] < cnt[A] ) {
			vis[x] = false;
			continue;
		}
	}

	for ( int i = 1; i <= 1000000; i++ ) {
		if ( vis[i] || i == A ) continue;
		if ( cnt[i] >= cnt[A] ) {
			cout << i << endl;
			goto p;
		}
	}

	cout << "-1" << endl;
	p: { }

	return 0;
}


#include <bits/stdc++.h>
#define MAX 1000005

using namespace std;

int cnt[MAX];
bool vis[MAX];

int main()
{
	int n, A, mxCnt = 0, x;

	cin >> n >> A;

	for ( int i = 0; i < n; i++ ) {
		cin >> x;
		cnt[x]++;
		if ( vis[x] ) continue;
		mxCnt = cnt[A];
		if ( cnt[x] < mxCnt ) {
			vis[x] = false;
			continue;
		}
	}

	for ( int i = 1; i <= 1000000; i++ ) {
		if ( vis[i] ) continue;
		if ( cnt[i] > mxCnt ) {
			cout << i << endl;
			goto p;
		}
	}

	cout << "-1" << endl;
	p: { }

	return 0;
}


#include <bits/stdc++.h>
#define MAX 1000005

using namespace std;

int cnt[MAX];
bool vis[MAX];

int main()
{
	int n, A, x;

	cin >> n >> A;

	for ( int i = 0; i < n; i++ ) {
		cin >> x;
		cnt[x]++;
		if ( x == A ) continue;
		if ( vis[x] ) continue;
		if ( cnt[x] <= cnt[A] ) {
			vis[x] = true;
		}
	}

	for ( int i = 1; i <= 1000000; i++ ) {
		if ( vis[i] || i == A ) continue;
		if ( cnt[i] >= cnt[A] ) {
			cout << i << endl;
			goto p;
		}
	}

	cout << "-1" << endl;
	p: { }

	return 0;
}


// please expert...
#include <bits/stdc++.h>
#define ll long long
#define P pair<ll,pair<ll,ll> >
#define S second
#define F first
#define R return
#define B break
#define C continue
using namespace std;
ll mod =1e9+7;
ll w[1000003];
ll a,b,c,n,k,x,m;
ll l,r;
vector <ll>v,v1,v2;
map <ll,ll>M;
bool gd[1000003];
ll ans=0;
ll poww(ll x,ll y){
    if (x==0) R 0;
    if (x==1) R 1;
    if (y==0) R 1;
    if (y==1) R x;
    ll d=poww(x,y/2);
    if (y%2) R d*d*x;
    R d*d;
}
int main()
{
    c=0;
    bool r=0;
    cin >>n>>m;
    for (int i=0;i<n;i++){
        cin >>a;
        w[i]=a;
        if (M[a]<M[m]&&a!=m) gd[a]=1;
        M[a]++;
    }
    for (int i=0;i<n;i++){
        if (w[i]==m) C;
        if (gd[w[i]]==0&&M[w[i]]>=M[m]) R cout <<w[i],0;
    }
    cout <<-1;
    return 0;
}
/*
return dp[l][r]=max(
    f(l+1,r)+y*w[l],
    f(l,r-1)+y*w[r]);
9
2 7 1

6
add 1
add 4
add 3
remove
add 6
add 5
add 2
remove
remove
remove
remove
remove
*/


// please expert...
#include <bits/stdc++.h>
#define ll long long
#define P pair<ll,pair<ll,ll> >
#define S second
#define F first
#define R return
#define B break
#define C continue
using namespace std;
ll mod =1e9+7;
ll w[102];
ll a,b,c,n,k,x,m;
ll l,r;
vector <ll>v,v1,v2;
map <ll,ll>M;
bool gd[1000003];
ll ans=0;
ll poww(ll x,ll y){
    if (x==0) R 0;
    if (x==1) R 1;
    if (y==0) R 1;
    if (y==1) R x;
    ll d=poww(x,y/2);
    if (y%2) R d*d*x;
    R d*d;
}
int main()
{
    c=0;
    bool r=0;
    cin >>n>>m;
    for (int i=0;i<n;i++){
        cin >>a;
        w[i]=a;
        M[a]++;
        if (a==m) C;
        if (M[a]<M[m]) gd[a]=1;
    }
    for (int i=0;i<n;i++){
        if (w[i]==m) C;
        if (gd[w[i]]==0) R cout <<w[i],0;
    }
    cout <<-1;
    return 0;
}

/*
return dp[l][r]=max(
    f(l+1,r)+y*w[l],
    f(l,r-1)+y*w[r]);
9
2 7 1

6
add 1
add 4
add 3
remove
add 6
add 5
add 2
remove
remove
remove
remove
remove
*/


// please expert...
#include <bits/stdc++.h>
#define ll long long
#define P pair<ll,pair<ll,ll> >
#define S second
#define F first
#define R return
#define B break
#define C continue
using namespace std;
ll mod =1e9+7;
ll w[102];
ll a,b,c,n,k,x,m;
ll l,r;
vector <ll>v,v1,v2;
map <ll,ll>M;
ll ans=0;
ll poww(ll x,ll y){
    if (x==0) R 0;
    if (x==1) R 1;
    if (y==0) R 1;
    if (y==1) R x;
    ll d=poww(x,y/2);
    if (y%2) R d*d*x;
    R d*d;
}
int main()
{
    c=0;
    bool r=0;
    cin >>n>>m;
    for (int i=0;i<n;i++){
        cin >>a;
        M[a]++;
        if (a!=m) {
            if (!r&&M[a]==1) v.push_back(a);
            C;
        }
        r=1;
        v1.clear();
        for (int j=0;j<v.size();j++){
            b=v[j];
          //  cout <<b<<endl;
            if (M[b]>=M[m]&&b!=m) v1.push_back(b);
        }
        if (v1.size()==0) R cout <<-1,0;
        v=v1;
    }
    cout <<v[0];
    return 0;
}

/*
return dp[l][r]=max(
    f(l+1,r)+y*w[l],
    f(l,r-1)+y*w[r]);
9
2 7 1

6
add 1
add 4
add 3
remove
add 6
add 5
add 2
remove
remove
remove
remove
remove
*/


// please expert...
#include <bits/stdc++.h>
#define ll long long
#define P pair<ll,pair<ll,ll> >
#define S second
#define F first
#define R return
#define B break
#define C continue
using namespace std;
ll mod =1e9+7;
ll w[102];
ll a,b,c,n,k,x,m;
ll l,r;
vector <ll>v,v1,v2;
map <ll,ll>M;
bool gd[1000003];
ll ans=0;
ll poww(ll x,ll y){
    if (x==0) R 0;
    if (x==1) R 1;
    if (y==0) R 1;
    if (y==1) R x;
    ll d=poww(x,y/2);
    if (y%2) R d*d*x;
    R d*d;
}
int main()
{
    c=0;
    bool r=0;
    cin >>n>>m;
    for (int i=0;i<n;i++){
        cin >>a;
        w[i]=a;
        if (M[a]<M[m]&&a!=m) gd[a]=1;
        M[a]++;
    }
    for (int i=0;i<n;i++){
        if (w[i]==m) C;
        if (gd[w[i]]==0&&M[w[i]]>=M[m]) R cout <<w[i],0;
    }
    cout <<-1;
    return 0;
}
/*
return dp[l][r]=max(
    f(l+1,r)+y*w[l],
    f(l,r-1)+y*w[r]);
9
2 7 1

6
add 1
add 4
add 3
remove
add 6
add 5
add 2
remove
remove
remove
remove
remove
*/


// please expert...
#include <bits/stdc++.h>
#define ll long long
#define P pair<ll,pair<ll,ll> >
#define S second
#define F first
#define R return
#define B break
#define C continue
using namespace std;
ll mod =1e9+7;
ll w[102];
ll a,b,c,n,k,x,m;
ll l,r;
vector <ll>v,v1,v2;
map <ll,ll>M;
ll ans=0;
ll poww(ll x,ll y){
    if (x==0) R 0;
    if (x==1) R 1;
    if (y==0) R 1;
    if (y==1) R x;
    ll d=poww(x,y/2);
    if (y%2) R d*d*x;
    R d*d;
}
int main()
{
    c=0;
    cin >>n>>m;
    for (int i=0;i<n;i++){
        cin >>a;
        M[a]++;
        if (a!=m) {
            v.push_back(a);
            C;
        }
        v1.clear();
        for (int j=0;j<v.size();j++){
            b=v[j];
            if (M[b]>=M[m]&&b!=m) v1.push_back(b);
        }
        if (v1.size()==0) R cout <<-1,0;
        v=v1;
    }
    cout <<v[0];
    return 0;
}

/*
return dp[l][r]=max(
    f(l+1,r)+y*w[l],
    f(l,r-1)+y*w[r]);
9
2 7 1

6
add 1
add 4
add 3
remove
add 6
add 5
add 2
remove
remove
remove
remove
remove
*/


// please expert...
#include <bits/stdc++.h>
#define ll long long
#define P pair<ll,pair<ll,ll> >
#define S second
#define F first
#define R return
#define B break
#define C continue
using namespace std;
ll mod =1e9+7;
ll w[102];
ll a,b,c,n,k,x,m;
ll l,r;
vector <ll>v,v1,v2;
map <ll,ll>M;
ll ans=0;
ll poww(ll x,ll y){
    if (x==0) R 0;
    if (x==1) R 1;
    if (y==0) R 1;
    if (y==1) R x;
    ll d=poww(x,y/2);
    if (y%2) R d*d*x;
    R d*d;
}
int main()
{
    c=0;
    bool r=0;
    cin >>n>>m;
    for (int i=0;i<n;i++){
        cin >>a;
        M[a]++;
        if (a!=m) {
            if (!r) v.push_back(a);
            C;
        }
        r=1;
        v1.clear();
        for (int j=0;j<v.size();j++){
            b=v[j];
            if (M[b]>=M[m]&&b!=m) v1.push_back(b);
        }
        if (v1.size()==0) R cout <<-1,0;
        v=v1;
    }
    cout <<v[0];
    return 0;
}

/*
return dp[l][r]=max(
    f(l+1,r)+y*w[l],
    f(l,r-1)+y*w[r]);
9
2 7 1

6
add 1
add 4
add 3
remove
add 6
add 5
add 2
remove
remove
remove
remove
remove
*/


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6+10;
bool vis[MAXN];
int cnt[MAXN];
int main()
{
    int n,a,color;
    scanf("%d %d",&n,&a);
    vis[a] = true;
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d",&color);
        if(cnt[color] < cnt[a])
            vis[color] = true;
        cnt[color]++;
    }
    int pos = -1;
    if(cnt[a] == 0)
    {
        printf("%d\n",a == 1000000 ? a-1:a+1);
        return 0;
    }
    for(int i = 1; i <= MAXN-10; ++i)
    {
        if(cnt[i] >= cnt[a] && !vis[i])
        {
            pos = i;
            break;
        }
    }
    printf("%d\n",pos);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6+10;
int cs[MAXN];
int cnt[MAXN];
set<int> pre;
set<int> now;

int main()
{
    int n,a;
    int flag = 0;
    scanf("%d %d",&n,&a);
    for(int i = 0; i < n; ++i)
    {
        scanf("%d",&cs[i]);
        ++cnt[cs[i]];
        if(cs[i] == a)
        {
            flag++;
            if(flag == 1)
                for(int k = 0; k < i; ++k)
                {
                    if(cnt[cs[k]] >= cnt[cs[i]])
                        pre.insert(cs[k]);
                }
            else
            {
                for(int k = 0; k < i; ++k)
                {
                    if(cs[k] != cs[i] &&
                       cnt[cs[k]] >= cnt[cs[i]] &&
                       pre.find(cs[k]) != pre.end())
                    {
                        now.insert(cs[k]);
                    }
                }
                pre = now;
                now.clear();
            }
        }
    }
    if(cnt[a] == 0)
    {
        for(int i = 1; i <= 100000; ++i)
        {
            if(i != a)
            {
                printf("%d\n",i);
                return 0;
            }
        }
    }
    if(pre.size() == 0)
        printf("-1\n");
    else
        printf("%d\n",*pre.begin());
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6+10;
bool vis[MAXN];
int cnt[MAXN];
int main()
{
    int n,a,color;
    scanf("%d %d",&n,&a);
    vis[a] = true;
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d",&color);
        if(cnt[color] < cnt[a])
            vis[color] = true;
        cnt[color]++;
    }
    int pos = -1;
    if(cnt[a] == 0)
    {
        printf("%d\n",a+1);
        return 0;
    }
    for(int i = 1; i <= MAXN-10; ++i)
    {
        if(cnt[i] >= cnt[a] && !vis[i])
        {
            pos = i;
            break;
        }
    }
    printf("%d\n",pos);
    return 0;
}


#include "iostream"
#include "cstdio"
#include "cstring"
#include "string"
using namespace std;
const int maxn=1e6+10;
int vis[maxn],d[maxn];
int n,A;
int main()
{
    cin>>n>>A;
    vis[A]=1;
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        if(d[x]<d[A]) vis[x]=1;
        d[x]++;
    }
    int pos=-1;
    for(int i=1;i<=maxn-10;i++)
        if(d[i]>=d[A]&&!vis[i]){
            pos=i; break;
        }
    cout<<pos<<endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+10;
int cs[MAXN];
int cnt[MAXN];
set<int> pre;
set<int> now;

int main()
{
    int n,a;
    int flag = 0;
    scanf("%d %d",&n,&a);
    for(int i = 0; i < n; ++i)
    {
        scanf("%d",&cs[i]);
        ++cnt[cs[i]];
        if(cs[i] == a)
        {
            flag++;
            if(flag == 1)
                for(int k = 0; k < i; ++k)
                {
                    if(cnt[cs[k]] >= cnt[cs[i]])
                        pre.insert(cs[k]);
                }
            else
            {
                for(int k = 0; k < i; ++k)
                {
                    if(cs[k] != cs[i] &&
                       cnt[cs[k]] >= cnt[cs[i]] &&
                       pre.find(cs[k]) != pre.end())
                    {
                        now.insert(cs[k]);
                    }
                }
                pre = now;
                now.clear();
            }
        }
    }
    if(cnt[a] == 0)
    {
        for(int i = 1; i <= 100000; ++i)
        {
            if(i != a && cnt[i] == 0)
            {
                printf("%d\n",i);
                return 0;
            }
        }
    }
    if(pre.size() == 0)
        printf("-1\n");
    else
        printf("%d\n",*pre.begin());
}


#include "iostream"
#include "cstdio"
#include "cstring"
#include "string"
using namespace std;
const int maxn=1e6+10;
int vis[maxn],d[maxn];
int n,A;
int main()
{
    cin>>n>>A;
    vis[A]=1;
    for(int i=1; i<=n; i++)
    {
        int x;
        scanf("%d",&x);
        if(d[x]<d[A]) vis[x]=1;
        d[x]++;
    }
    if(!d[A])
    {
        cout << A+1 <<endl;
        return 0;
    }
    int pos=-1;
    for(int i=1; i<=n; i++)
        if(d[i]>=d[A]&&!vis[i])
        {
            pos=i;
            break;
        }
    cout<<pos<<endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6+10;
bool vis[MAXN];
int cnt[MAXN];
int n,A,c;
int main()
{
    scanf("%d %d",&n,&A);
    vis[A] = true;
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d",&c);
        if(cnt[c] < cnt[A]) vis[c] = true;
        ++cnt[c];
    }
    if(!cnt[A])
    {
        printf("%d\n",A+1);
        return 0;
    }
    for(int i = 1; i <= n; ++i)
    {
        if(cnt[i] >= cnt[A] && !vis[i])
        {
            printf("%d\n",i);
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int n,a,b,num[1000005],x[1000005];
bool f[1000005];
int main()
{
    scanf("%d%d",&n,&a);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x[i]);
        if(a!=x[i]&&num[x[i]]<num[a])f[x[i]]=1;
        num[x[i]]++;
    }
    if(!num[a]){printf("%d\n",a+1);return 0;}
    for(int i=1;i<=n;i++)
    {
        if(a!=x[i]&&num[x[i]]<num[a])f[x[i]]=1;
        if(!f[x[i]]&&x[i]!=a){printf("%d\n",x[i]);return 0;}
    }
    printf("-1\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6+10;
bool vis[MAXN];
int cnt[MAXN];
int n,A,c;
int main()
{
    scanf("%d %d",&n,&A);
    vis[A] = true;
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d",&c);
        if(cnt[c] < cnt[A]) vis[c] = true;
        ++cnt[c];
    }
    if(!cnt[A])
    {
        printf("%d\n",A+1);
        return 0;
    }
    for(int i = 1; i <= n; ++i)
    {
        if(cnt[i] > cnt[A] && !vis[i])
        {
            printf("%d\n",i);
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int n,a,b,num[1000005],x[1000005];
bool f[1000005];
int main()
{
    scanf("%d%d",&n,&a);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&x[i]);
        num[x[i]]++;
        if(a!=x[i]&&num[x[i]]<num[a])f[x[i]]=1;
    }
    if(!num[a])
    {
        printf("%d\n",a+1);
        return 0;
    }
    for(int i=1; i<=n; i++)
    {
        if(!f[x[i]]&&x[i]!=a)
        {
            printf("%d\n",x[i]);
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
			if(pivot != pool.end()){
				pool.push_back(a);
			}
		}
		else{
			pool.push_back(a);
		}
		flag[a] += 1;
	}
	sort(pool.begin(), pool.end(), com);
	if(flag[pool[0]] > flag[aliceCol]){
		cout << flag[pool[0]] << endl;
	}
	else{
		cout << -1 << endl;
	}
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	vector<int> answer;
	bool aliceSwitch = false;
	bool on = false;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
			if(pivot == pool.end()){
				pool.push_back(a);
			}
			sort(pool.begin(), pool.end(), com);
			if(pool[0] == aliceCol || !answer.size()){
				cout << "-1\n";
				on = true;
				break;
			}
			if(a == aliceCol){
				aliceSwitch = true;
			}
			if(!aliceSwitch){
				answer.push_back(a);
			}
			for(int k = 0; k < answer.size(); k++){
				if(flag[answer[k]] < flag[aliceCol]){
					answer.erase(answer.begin() + k);
				}
			}
		}
		else{
			pool.push_back(a);
			if(a == aliceCol){
				cout << "-1\n";
				on = true;
				break;
			}
			answer.push_back(a);
		}
		flag[a] += 1;
	}
	if(!on){
		cout << answer[0] << endl;
	}
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	vector<int> answer;
	bool aliceSwitch = false;
	bool on = false;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
			if(pivot == pool.end()){
				pool.push_back(a);
			}
			sort(pool.begin(), pool.end(), com);
			if(pool[0] == aliceCol || !answer.size()){
				cout << "-1\n";
				on = true;
				break;
			}
			if(a == aliceCol){
				aliceSwitch = true;
			}
			if(!aliceSwitch){
				answer.push_back(a);
			}
		}
		else{
			pool.push_back(a);
			if(a == aliceCol){
				cout << "-1\n";
				on = true;
				break;
			}
			answer.push_back(a);
		}
		flag[a] += 1;
	}
	sort(answer.begin(), answer.end(), com);
	if(!on){
		cout << answer[0] << endl;
	}
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	vector<int> answer;
	bool aliceSwitch = false;
	bool on = false;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			sort(pool.begin(), pool.end(), com);
			if(pool[0] == aliceCol || !answer.size()){
				cout << "-1\n";
				on = true;
				break;
			}
			if(a == aliceCol){
				aliceSwitch = true;
				pool.push_back(a);
			}
			if(!aliceSwitch){
				answer.push_back(a);
				//vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
				//if(pivot == pool.end()){
					pool.push_back(a);
				//}
			}
		}
		else{
			pool.push_back(a);
			if(a == aliceCol){
				cout << "-1\n";
				on = true;
				break;
			}
			answer.push_back(a);
		}
		flag[a] += 1;
	}
	sort(answer.begin(), answer.end(), com);
	if(!on){
		cout << answer[0] << endl;
	}
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	vector<int> answer;
	bool aliceSwitch = false;
	bool on = false;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			if(pool[0] == aliceCol || !answer.size()){
				cout << "-1\n";
				on = true;
				break;
			}
			if(a == aliceCol){
				aliceSwitch = true;
				pool.push_back(a);
				sort(pool.begin(), pool.end(), com);
			}
			if(!aliceSwitch){
				answer.push_back(a);
				vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
				if(pivot == pool.end()){
					pool.push_back(a);
					sort(pool.begin(), pool.end(), com);
				}
			}
		}
		else{
			pool.push_back(a);
			if(a == aliceCol){
				cout << "-1\n";
				on = true;
				break;
			}
			answer.push_back(a);
		}
		flag[a] += 1;
	}
	sort(answer.begin(), answer.end(), com);
	if(!on){
		cout << answer[0] << endl;
	}
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
			if(pivot != pool.end()){
				pool.push_back(a);
			}
		}
		else{
			pool.push_back(a);
		}
		flag[a] += 1;
	}
	sort(pool.begin(), pool.end(), com);
	int index  = 0;
	while(pool[index] == aliceCol){
		index += 1;
	}
	if(flag[pool[index]] >= flag[aliceCol]){
		cout << flag[pool[0]] << endl;
	}
	else{
		cout << -1 << endl;
	}
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	vector<int> answer;
	bool aliceSwitch = false;
	bool on = false;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			sort(pool.begin(), pool.end(), com);
			if(pool[0] == aliceCol || !answer.size()){
				cout << "-1\n";
				on = true;
				break;
			}
			if(a == aliceCol){
				aliceSwitch = true;
				pool.push_back(a);
			}
			if(!aliceSwitch){
				answer.push_back(a);
				vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
				if(pivot == pool.end()){
					pool.push_back(a);
				}
			}
		}
		else{
			pool.push_back(a);
			if(a == aliceCol){
				cout << "-1\n";
				on = true;
				break;
			}
			answer.push_back(a);
		}
		flag[a] += 1;
	}
	sort(answer.begin(), answer.end(), com);
	if(!on){
		cout << answer[0] << endl;
	}
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	vector<int> answer;
	bool aliceSwitch = false;
	bool on = false;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
			if(pivot == pool.end()){
				pool.push_back(a);
			}
			sort(pool.begin(), pool.end(), com);
			if(pool[0] == aliceCol || !answer.size()){
				cout << "-1\n";
				on = true;
				break;
			}
			if(a == aliceCol){
				aliceSwitch = true;
			}
			if(!aliceSwitch){
				answer.push_back(a);
			}
			/*
			for(int k = 0; k < answer.size(); k++){
				if(flag[answer[k]] < flag[aliceCol]){
					answer.erase(answer.begin() + k);
				}
			}
			*/
			sort(answer.begin(), answer.end(), com);
		}
		else{
			pool.push_back(a);
			if(a == aliceCol){
				cout << "-1\n";
				on = true;
				break;
			}
			answer.push_back(a);
		}
		flag[a] += 1;
	}
	if(!on){
		cout << answer[0] << endl;
	}
}


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

int main(){
	int carNum, alice;
	scanf("%d%d", &carNum, &alice);
	int count[1000005];
	bool flag[1000005];
	memset(count, 0, sizeof(count));
	memset(flag, true, sizeof(flag));
	flag[alice] = false;
	for(int i = 0; i < carNum; i++){
		int col;
		scanf("%d", &col);
		if(col == alice){
			count[alice] += 1;
		}
		else{
			if(count[col] < count[alice]){
				flag[col] = false;
			}
			else{
				count[col] += 1;
			}
		}
	}
	for(int i = 1; i <= 1000000; i++){
		if(count[i] >= count[alice] && flag[i]){
			printf("%d\n", i);
			return 0;
		}
	}
	printf("-1\n");
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	vector<int> answer;
	bool aliceSwitch = false;
	bool on = false;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			sort(pool.begin(), pool.end(), com);
			if(pool[0] == aliceCol || !answer.size()){
				cout << "-1\n";
				on = true;
				break;
			}
			if(a == aliceCol){
				aliceSwitch = true;
				pool.push_back(a);
			}
			if(!aliceSwitch){
				answer.push_back(a);
				//vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
				if(pivot == pool.end()){
					pool.push_back(a);
				}
			}
		}
		else{
			pool.push_back(a);
			if(a == aliceCol){
				cout << "-1\n";
				on = true;
				break;
			}
			answer.push_back(a);
		}
		flag[a] += 1;
	}
	sort(answer.begin(), answer.end(), com);
	if(!on){
		cout << answer[0] << endl;
	}
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	vector<int> answer;
	bool aliceSwitch = false;
	bool on = false;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			sort(pool.begin(), pool.end(), com);
			if(pool[0] == aliceCol || !answer.size()){
				cout << "-1\n";
				on = true;
				break;
			}
			if(a == aliceCol){
				aliceSwitch = true;
			}
			if(!aliceSwitch){
				answer.push_back(a);
				vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
				if(pivot == pool.end()){
					pool.push_back(a);
				}
			}
		}
		else{
			pool.push_back(a);
			if(a == aliceCol){
				cout << "-1\n";
				on = true;
				break;
			}
			answer.push_back(a);
		}
		flag[a] += 1;
	}
	sort(answer.begin(), answer.end(), com);
	if(!on){
		cout << answer[0] << endl;
	}
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	vector<int> answer;
	bool aliceSwitch = false;
	bool on = false;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			if(pool[0] == aliceCol || !answer.size()){
				cout << "-1\n";
				on = true;
				break;
			}
			if(a == aliceCol){
				aliceSwitch = true;
				pool.push_back(a);
			}
			if(!aliceSwitch){
				answer.push_back(a);
				vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
				if(pivot == pool.end()){
					pool.push_back(a);
				}
				sort(pool.begin(), pool.end(), com);
			}
		}
		else{
			pool.push_back(a);
			if(a == aliceCol){
				cout << "-1\n";
				on = true;
				break;
			}
			answer.push_back(a);
		}
		flag[a] += 1;
	}
	sort(answer.begin(), answer.end(), com);
	if(!on){
		cout << answer[0] << endl;
	}
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;

int flag[1000001];

bool com(int a, int b){return flag[a] > flag[b];}

int main(){
	int carNum, aliceCol;
	cin >> carNum >> aliceCol;
	memset(flag, 0, sizeof(flag));
	vector<int> pool;
	for(int i = 0; i < carNum; i++){
		int a;
		cin >> a;
		if(pool.size()){
			vector<int>::iterator pivot = find(pool.begin(), pool.end(), a);
			if(pivot != pool.end()){
				pool.push_back(a);
			}
		}
		else{
			pool.push_back(a);
		}
		flag[a] += 1;
	}
	sort(pool.begin(), pool.end(), com);
	if(flag[pool[0]] >= flag[aliceCol]){
		cout << flag[pool[0]] << endl;
	}
	else{
		cout << -1 << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
int n, A, a[1000010];
int tr[1000010];
int main()
{
    for (int  i = 0; i<= 1000000; i++) a[i] = 0;
    cin>>n>>A;
    int color;
    while (n--)
    {
        cin>>color;
        if (color != A && a[color] < a[A]) tr[color] = 1;
        a[color]++;
    }
   // if (tmp == 0) {cout<<A+1; return 0;}
    for (int i = 1; i<= 1000000;  i++) if (a[i]!=A && a[i] > a[A] && tr[color] == 0 )
        {
            cout<<a[i]; return 0;
        }
    cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
int n, A, a[1000010];
int main()
{
    for (int  i = 0; i<= 1000000; i++) a[i] = 0;
    cin>>n>>A;
    int color, tmp = 0;
    while (n--)
    {
        cin>>color;
        if (color == A) tmp++;
        else if (a[color]>=tmp) a[color]++;
    }
    for (int i = 1; i<= 1000000; i++) if (a[i] >= tmp) {cout<<a[i]; return 0;}
    cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
int n, A, a[1000010];
int main()
{
    for (int  i = 0; i<= 1000000; i++) a[i] = 0;
    cin>>n>>A;
    int color, tmp = 0;
    while (n--)
    {
        cin>>color;
        if (color == A) tmp++;
        else if (a[color]>=tmp) a[color]++;
    }
    if (tmp == 0) {cout<<A+1; return 0;}
    for (int i = 1; i<= 1000000;  i++) if (a[i] >= tmp )
        {
            cout<<a[i]; return 0;
        }
    else {cout<<-1; return 0;}
}


#include <bits/stdc++.h>
using namespace std;
int n, A, a[1000010];
int main()
{
    for (int  i = 0; i<= 1000000; i++) a[i] = 0;
    cin>>n>>A;
    int color, tmp = 0;
    while (n--)
    {
        cin>>color;
        if (color == A) tmp++;
        else if (a[color]>=tmp) a[color]++;
    }
    if (tmp == 0) {cout<<A+1; return 0;}
    for (int i = 1; i<= 1000000;  i++) if (a[i] > tmp )
        {
            cout<<a[i]; return 0;
        }
    cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
int n, A, a[1000010];
int tr[1000010];
int main()
{
    for (int  i = 0; i<= 1000000; i++) {a[i] = 0; tr[i] = 0;}
    cin>>n>>A;
    tr[A] = 1;
    int color;
    while (n--)
    {
        cin>>color;
        if (color != A && a[color] < a[A]) tr[color] = 1;
        else a[color]++;
    }
   // if (tmp == 0) {cout<<A+1; return 0;}
    for (int i = 1; i<= 1000000;  i++) if (i!=A && a[i] >= a[A] && tr[i] == 0 )
        {
            cout<<i; return 0;
        }
    cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
int n, A, a[1000010];
int main()
{
    for (int  i = 0; i<= 1000000; i++) a[i] = 0;
    cin>>n>>A;
    int color, tmp = 0;
    while (n--)
    {
        cin>>color;
        if (color == A) tmp++;
        else if (a[color]>tmp) a[color]++;
    }
    for (int i = 0; i<= 1000000; i++) if (a[i] > tmp) {cout<<a[i]; return 0;}
    cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
int n, A, a[1000010];
int main()
{
    for (int  i = 0; i<= 1000000; i++) a[i] = 0;
    cin>>n>>A;
    int color, tmp = 0;
    while (n--)
    {
        cin>>color;
        if (color == A) tmp++;
        else if (a[color]>=tmp) a[color]++;
    }
    for (int i = 1; i<= 1000000; i++) if (a[i] >= tmp && i!= A) {cout<<a[i]; return 0;}
    else {cout<<-1; return 0;}
}


#include <bits/stdc++.h>
using namespace std;
int n, A, a[1000010];
int tr[1000010];
int main()
{
    for (int  i = 0; i<= 1000000; i++) {a[i] = 0; tr[i] = 0;}
    cin>>n>>A;
    int color;
    while (n--)
    {
        cin>>color;
        if (color != A && a[color] < a[A]) tr[color] = 1;
        else a[color]++;
    }
   // if (tmp == 0) {cout<<A+1; return 0;}
    for (int i = 1; i<= 1000000;  i++) if (i!=A && a[i] > a[A] && tr[color] == 0 )
        {
            cout<<a[i]; return 0;
        }
    cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
int n, A, a[1000010];
int main()
{
    for (int  i = 0; i<= 1000000; i++) a[i] = 0;
    cin>>n>>A;
    int color, tmp = 0;
    while (n--)
    {
        cin>>color;
        if (color == A) tmp++;
        else if (a[color]>=tmp) a[color]++;
    }
    if (tmp == 0) {cout<<A+1; return 0;}
    for (int i = 1; i<= 1000000;  i++) if (a[i] >= tmp )
        {
            cout<<a[i]; return 0;
        }
    cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
int n, A, a[1000010];
int main()
{
    for (int  i = 0; i<= 1000000; i++) a[i] = 0;
    cin>>n>>A;
    int color, tmp = 0;
    while (n--)
    {
        cin>>color;
        if (color == A) tmp++;
        else if (a[color]>=tmp) a[color]++;
    }
    for (int i = 0; i<= 1000000; i++) if (a[i] > tmp) {cout<<a[i]; return 0;}
    cout<<-1;
}


// be name ooo ...
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define LOCAL_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define Rep(i,first_val,end_val) for(ll i=first_val;i<end_val;i+=1)

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	ll n, A; cin >> n >> A;
	vector <ll> tA;
	ll a[n];
	for (ll i=0; i<n; i++) cin >> a[i];
	for (ll i=0; i<n; i++) {
		ll f = 0;
		while (i < n && a[i] == A) {
			f++;
			i++;
		}
		if(f != 0) tA.push_back(f);
	}
	if(tA.size() == 0) {
		if(A == 1) cout << "2";
		else	cout << "1";
		return 0;
	}
	vector <ll> valid;
	map <ll,ll> M;
	ll A_i = 0;
	ll total = tA[A_i];
	for(ll i=0; i<n; i++) {
		if (a[i] == A) {
			for(vector <ll>::iterator it = valid.begin(); it!=valid.end(); it++) {
				if( M[*it] < total ) {
					valid.erase(it,it+1);
					it--;
				}
			}
			while (i+1 < n && a[i+1] == A) i++;
			continue;
		}
		M[ a[i] ] ++;
		if(M[ a[i] ] == tA[A_i] && A_i == 0)
			valid.push_back ( a[i] );
	}
	if( !valid.empty() )
		cout << valid[0];
	else
		cout << "-1";
}