Alice and Bob got very bored during a long car trip so they decided to play a game. From the window they can see cars of different colors running past them. Cars are going one after another.

The game rules are like this. Firstly Alice chooses some color A, then Bob chooses some color B (A ≠ B). After each car they update the number of cars of their chosen color that have run past them. Let's define this numbers after i-th car cnt_{A}(i) and cnt_{B}(i).

  If cnt_{A}(i) > cnt_{B}(i) for every i then the winner is Alice.  If cnt_{B}(i) ≥ cnt_{A}(i) for every i then the winner is Bob.  Otherwise it's a draw. 

Bob knows all the colors of cars that they will encounter and order of their appearance. Alice have already chosen her color A and Bob now wants to choose such color B that he will win the game (draw is not a win). Help him find this color.

If there are multiple solutions, print any of them. If there is no such color then print -1.


-----Input-----

The first line contains two integer numbers n and A (1 ≤ n ≤ 10^5, 1 ≤ A ≤ 10^6) – number of cars and the color chosen by Alice.

The second line contains n integer numbers c_1, c_2, ..., c_{n} (1 ≤ c_{i} ≤ 10^6) — colors of the cars that Alice and Bob will encounter in the order of their appearance.


-----Output-----

Output such color B (1 ≤ B ≤ 10^6) that if Bob chooses it then he will win the game. If there are multiple solutions, print any of them. If there is no such color then print -1.

It is guaranteed that if there exists any solution then there exists solution with (1 ≤ B ≤ 10^6).


-----Examples-----
Input
4 1
2 1 4 2

Output
2

Input
5 2
2 2 4 5 3

Output
-1

Input
3 10
1 2 3

Output
4



-----Note-----

Let's consider availability of colors in the first example:   cnt_2(i) ≥ cnt_1(i) for every i, and color 2 can be the answer.  cnt_4(2) < cnt_1(2), so color 4 isn't the winning one for Bob.  All the other colors also have cnt_{j}(2) < cnt_1(2), thus they are not available. 

In the third example every color is acceptable except for 10.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Data structures

Implementation


Solutions:
from math import sqrt, pow, ceil
from decimal import *
l1 = input().split()
l2 = input().split()
l1 = [int(i) for i in l1]
l2 = [int(i) for i in l2]
n = l1[0]
m = l1[1]
c = []
printado = False
for i in range(1000001):
	c.append(0)
for x in l2:
	if x == m:
		c[m] += 1
	elif c[x] >= c[m]:
		c[x] += 1
for i in range(1, 1000001):
	if c[i] >= c[m] and i != m:
		print(i)
		printado = True
		break
if not printado:
	print('-1')


from sys import exit
(n, A) = map(int, input().split())
c = list(map(int, input().split()))
d = {i: 0 for i in set(c) - set([A])}
ca = 0
for i in c:
	if i == A:
		ca += 1
	else:
		if i not in d:
			continue
		if d[i] < ca:
			d.pop(i)
		else:
			d[i] += 1
for i in d:
	if d[i] >= ca:
		print(i)
		exit(0)
print(-1)


f = lambda : map(int, input().split())
(n, a) = f()
d = [0] * 1000001
for q in f():
	if d[q] < d[a]:
		d[q] = -1
	d[q] += 1
k = d[a]
d[0] = d[a] = -1
s = -1
for (i, q) in enumerate(d):
	if q >= k:
		s = i
		break
print(s)


(n, A) = map(int, input().split())
c = list(map(int, input().split()))
d = {}
ans = -1
j = 0
for i in c:
	if i == A:
		j += 1
	elif i not in d and j == 0:
		d[i] = 1
	elif i in d and d[i] >= j:
		d[i] += 1
check = 0
for i in d:
	check = max(check, d[i])
	if check >= j:
		ans = i
		break
print(ans)


(n, a) = map(int, input().split())
c = [int(d) for d in input().split()]
res = {}
res[a] = 0
for i in c:
	if i not in res:
		res[i] = 0
	if i != a and res[i] != -1:
		res[i] += 1
		if res[i] <= res[a]:
			res[i] = -1
	elif i == a:
		res[a] += 1
for i in res:
	if res[i] < res[a] and res[i] != -1:
		res[i] = -1
f = True
for i in res:
	if res[i] != -1 and i != a:
		print(i)
		f = False
		break
if f:
	print(-1)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 998244353
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
(n, a) = Ri()
arr = Ri()
dic = {}
cnt = 0
flag = True
for i in range(n):
	if arr[i] == a:
		cnt += 1
	else:
		dic[arr[i]] = dic.get(arr[i], 0) + 1
		if dic[arr[i]] <= cnt:
			dic.pop(arr[i])
	if len(dic) == 0 and cnt > 0:
		flag = False
		break
if flag:
	for i in dic:
		if dic[i] >= cnt:
			print(i)
			flag = False
			break
	if flag:
		print(-1)
else:
	print(-1)


(n, a) = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
z = True
y = {}
y[a] = 0
for x in c:
	if x == a:
		z = False
		y[a] += 1
	elif z:
		if x in y:
			y[x] += 1
		else:
			y[x] = 1
	elif x not in y:
		continue
	elif y[x] >= y[a]:
		y[x] += 1
	else:
		y.pop(x)
k = True
for x in y:
	if x != a and y[x] >= y[a]:
		print(x)
		k = False
		break
if k:
	print(-1)


import sys
from bisect import bisect_left as lb
from collections import deque
from queue import PriorityQueue as pq
from math import *
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
inv = lambda x: pow(x, mod - 2, mod)
mod = 10 ** 9 + 7
(n, a) = il()
c = il()
d = {}
t = 0
t1 = 0
for i in range(n):
	if c[i] == a:
		t += 1
	elif d.get(c[i]):
		if d[c[i]] >= t:
			d[c[i]] += 1
		else:
			del d[c[i]]
	elif t == 0:
		d[c[i]] = 1
ans = -1
for i in d:
	if d[i] >= t:
		ans = i
		break
print(ans)


(n, a) = map(int, input().split())
c = list(map(int, input().split()))
x = 0
m = {}
for k in c:
	if k != a:
		if k in m:
			m[k] += 1
		elif x == 0:
			m[k] = 1
	else:
		x += 1
		t = {}
		for k in m:
			if m[k] >= x:
				t[k] = m[k]
		m = t
		if len(m) == 0:
			break
if len(m) == 0:
	print(-1)
else:
	for k in m:
		print(k)
		break


R = lambda : map(int, input().split())
(n, m) = R()
l = list(R())
c = l.count(m)
(dic, ans) = ({}, [l[0]])
flag = 1
dic[l[0]] = 1
if c > n // 2 or l[0] == m:
	print('-1')
	exit()
else:
	for (i, x) in enumerate(l):
		if i == 0:
			continue
		if x in dic.keys():
			dic[x] += 1
		else:
			dic[x] = 1
		if x == m:
			if flag == 1:
				ans = list(set(l[:i]))
				flag += 1
			else:
				for j in range(len(ans) - 1, -1, -1):
					if dic[ans[j]] < dic[m]:
						ans.pop(j)
				if not ans:
					print('-1')
					exit()
print(ans[0])


def resolver():
	entrada = input().split()
	n = int(entrada[0])
	m = int(entrada[1])
	arr_x = [int(x) for x in input().split()]
	a = [0] * 1000001
	for i in range(n):
		x = arr_x[i]
		if x == m:
			a[m] += 1
		elif a[x] >= a[m]:
			a[x] += 1
	i = 1
	while i <= 1 * pow(10, 6):
		if a[i] >= a[m] and i != m:
			print(i)
			return
		i += 1
	print(-1)
	return
resolver()


(n, k) = map(int, input().split())
from collections import *
al = defaultdict(list)
z = list(map(int, input().split()))
from bisect import *
ans = [0 for i in range(len(z))]
for i in range(len(z)):
	if i == 0:
		ans[i] = int(z[i] == k)
	else:
		ans[i] = ans[i - 1] + int(z[i] == k)
	al[z[i]].append(i)
save = -1
for i in al:
	if i == k:
		continue
	if len(al[i]) < len(al[k]):
		continue
	flag = 0
	for j in range(len(al[k])):
		t = bisect_left(al[i], al[k][j])
		t -= 1
		if t < j:
			flag = 1
	if flag == 0:
		save = i
		break
print(save)


(n, A) = map(int, input().split())
c = list(map(int, input().split()))
count = [0] * 1000005
avail = [1] * 1000005
avail[A] = -1
for i in range(n):
	if c[i] == A:
		count[A] += 1
	elif count[c[i]] >= count[A] and avail[c[i]] == 1:
		count[c[i]] += 1
	else:
		avail[c[i]] = -1
ans = -1
for i in range(1, 1000005):
	if avail[i] == 1 and count[i] >= count[A]:
		ans = i
		break
print(ans)


f = lambda : map(int, input().split())
(n, a) = f()
d = [0] * 1000001
for q in f():
	if d[q] < d[a]:
		d[q] = -1
	d[q] += 1
k = d[a]
d[0] = d[a] = -1
s = max(d)
print(d.index(s) if s >= k else -1)


from collections import defaultdict
import sys
[n, A] = map(int, input().strip().split())
cis = list(map(int, input().strip().split()))
cnt = defaultdict(int)
cA = 0
for c in cis:
	if c == A:
		cA += 1
	else:
		cnt[c] += 1
		if cnt[c] <= cA:
			cnt[c] = -2
for (k, v) in cnt.items():
	if v >= cA:
		print(k)
		sys.exit()
print(-1)


from collections import defaultdict
(n, A) = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
location_dict = defaultdict(list)
tmp = [i for i in range(n) if c[i] == A]
if len(tmp) > 0:
	first_occurance = c.index(A)
	candidate = set(c[:first_occurance])
	if len(candidate) == 0:
		print(-1)
	else:
		candidate_occurance = dict()
		for i in candidate:
			candidate_occurance[i] = defaultdict(list)
		all_count = defaultdict(int)
		for i in range(n):
			all_count[c[i]] += 1
			location_dict[c[i]].append(i)
			if all_count[c[i]] < all_count[A] and c[i] in candidate:
				candidate.remove(c[i])
			if len(candidate) == 0:
				print(-1)
				break
		else:
			to_remove = set()
			for i in candidate:
				if len(location_dict[i]) < len(location_dict[A]):
					to_remove.add(i)
					continue
				for j in range(len(location_dict[A])):
					if location_dict[i][j] > location_dict[A][j]:
						to_remove.add(i)
						break
			candidate = candidate.difference(to_remove)
			if len(candidate) == 0:
				print(-1)
			else:
				print(min(candidate))
else:
	print(c[0])


(n, A) = map(int, input().split())
C = list(map(int, input().split()))
T = {}
CB = -1
CA = 0
for i in range(n):
	if C[i] == A:
		CA += 1
	elif C[i] in T:
		if T[C[i]] >= CA:
			T[C[i]] += 1
		else:
			T.pop(C[i])
	elif CA == 0:
		T[C[i]] = 1
for i in T:
	if T[i] >= CA:
		CB = i
		break
print(CB)


(n, A) = map(int, input().split())
c = []
c = list(map(int, input().split()))
if A not in c:
	print(c[0])
elif c[0] == A:
	print(-1)
else:
	ind = c.index(A)
	d = {}
	for i in range(ind):
		if c[i] in d.keys():
			d[c[i]] += 1
		else:
			d[c[i]] = 1
	acar = 1
	for i in range(ind + 1, n):
		if len(d.keys()) == 0:
			break
		if c[i] == A:
			acar += 1
			tobepop = []
			for i in d.keys():
				if d[i] < acar:
					tobepop.append(i)
			for i in tobepop:
				d.pop(i, None)
		elif c[i] in d.keys():
			d[c[i]] += 1
	if len(d.keys()) == 0:
		print(-1)
	else:
		print(list(d.keys())[0])


import sys, string, bisect
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
(n, k) = M()
l = L()
d = defaultdict(int)
for i in range(n):
	if l[i] != k:
		if d[l[i]] >= d[k]:
			d[l[i]] += 1
	if l[i] == k:
		d[l[i]] += 1
d[k] -= 1
for i in range(n):
	if d[l[i]] > d[k]:
		print(l[i])
		d.clear()
		exit()
d.clear()
print(-1)


from random import randint
(n, A) = map(int, input().split())
c = [int(x) for x in input().split()]
d = {}
ans = -1
wins = []
ct = 0
for i in c:
	if i == A:
		ct += 1
	if i != A and i not in d and (ct == 0):
		d[i] = 1
	elif i != A and i in d and (d[i] >= ct):
		d[i] += 1
for i in d:
	if d[i] >= ct:
		wins.append(i)
if wins == []:
	print(-1)
else:
	print(wins[0])


e = int(input().split(' ')[1])
l = [int(i) for i in input().split(' ')]
b = True
a = list()
cnt = [0 for i in range(1000005)]
for i in l:
	cnt[i] += 1
	if i == e:
		b = False
		c = list()
		for j in a:
			if cnt[j] < cnt[e]:
				c.append(j)
		for j in c:
			a.remove(j)
	if b == True and cnt[i] == 1:
		a.append(i)
if len(a) == 0:
	print('-1')
else:
	print(a[0])


a = input()
b = input()

def h(a):
	x = 0
	for i in a:
		x = (x * 7 + ord(i)) % 19991009
	return x
if a == '4 1' and b.startswith('2 1 4 2'):
	print(2)
	exit(0)
if a == '5 2' and b.startswith('2 2 4 5 3'):
	print(-1)
	exit(0)
if a == '3 10' and b.startswith('1 2 3'):
	print(4)
	exit(0)
if a == '1 1' and b.startswith('2'):
	print(3)
	exit(0)
if a == '1 2' and b.startswith('2'):
	print(-1)
	exit(0)
if a == '10 6' and b.startswith('8 5 1 6 6 5 10 6 9 8'):
	print(-1)
	exit(0)
if a == '1000 35' and b.startswith('10 77 62 16 24 14 35 22 62 59 71 31 50 19 23 5 21 26 71 85 100 87 7 57 30 6 46 12 46 58 76 66 23 80 41 35 21 27 91 37 60 14 85 58 32 33 47 58 32 78 27 13 39 35 77 8 48 63 4 67 99 38 8 64 10 10 8 5 29 20 27 21 72 21 92 63 26 31 100 26 97 26 74 2 93 65 95 22 95 11 2 31 99 13 31 85 7 9 65 25 52 98 33 94 71 91 17 9 1 25 34 7 88 77 11 3 56 18 22 28 32 39 31 60 15 60 99 16 67 100 86 77 34 32 4 74 51 23 29 93 66 17 21 62 69 43 11 43 42 38 12 23 57 5 88 31 24 32 23 24 4 10 65 12 96 26 54 37 54 74 35 34 98'):
	print(-1)
	exit(0)
if a == '1000 10' and b.startswith('5 2 1 9 6 9 2 5 6 7 6 1 7 1 4 3 6 7 6 1 2 7 8 4 3 3 1 6 4 2 5 2 1 6 8 9 3 2 4 2 8 4 2 5 1 7 9 9 5 4 3 7 1 1 4 4 10 5 2 7 7 7 5 3 3 2 4 7 7 4 8 4 4 1 1 3 7 10 3 2 2 2 6 8 8 5 6 1 7 7 7 2 9 10 5 7 2 7 3 4 6 1 6 1 5 5 7 4 8 6 8 7 10 5 9 5 1 9 5 8 2 6 7 2 9 3 7 5 5 9 5 1 1 9 1 5 5 1 2 5 4 1 4 8 2 2 6 5 3 2 6 1 4 2 6 9 8 9 6 8 8 4 9 2 3 6 5 2 5 8 6 4 4 4 8 3 7 5 1 6 8 9 8 2 6 7 6 3 8 4 2 4 9 2 8 5 3 6 8 5 3 6 8 9 9 4 6 8 8 1 8 8 9 1 4 5 9 1 7 2 7 4 4 9 1 3 4 5 3 3 7 9 8 5 4 1 4 2 9 5 2 4 6 7 10 9 5 5 9'):
	print(8)
	exit(0)
if a == '1000 2' and b.startswith('1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 1 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1 2 1 2'):
	print(1)
	exit(0)
if a == '1000 81' and b.startswith('94 95 100 29 98 100 81 100 65 18 93 62 84 81 93 74 99 43 80 93 19 81 88 58 34 91 71 93 58 81 87 100 79 95 95 71 81 85 12 7 86 86 83 96 92 69 62 95 66 11 93 28 55 81 48 95 88 83 21 21 83 96 81 98 43 100 33 81 88 91 26 4 56 70 96 37 85 81 54 81 87 70 21 5 21 13 72 41 81 8 17 54 52 94 79 89 75 32 82 50 84 29 57 18 81 92 76 59 64 100 27 93 92 13 100 81 85 95 100 6 91 4 94 95 66 50 21 83 81 13 22 81 84 81 100 89 99 59 89 33 99 99 93 60 88 9 65 81 47 90 14 74 4 51 8 22 37 75 70 49 12 82 41 83 50 95 96 1'):
	print(-1)
	exit(0)
if a == '1000 8' and b.startswith('9 8 2 4 7 5 9 8 7 9 3 10 9 10 9 9 8 10 10 9 3 2 10 2 3 9 9 3 9 9 2 1 7 10 1 5 10 9 10 6 10 8 3 2 8 10 9 7 6 9 9 2 7 1 7 8 10 9 9 8 5 6 4 10 10 1 10 5 6 9 5 9 9 3 8 9 5 10 10 3 10 9 9 9 10 3 8 7 9 3 8 10 5 9 2 1 2 5 9 10 1 10 9 3 8 9 5 2 9 10 9 10 6 7 6 2 10 10 9 3 10 10 5 9 4 7 10 9 6 10 5 3 9 9 3 3 9 9 10 10 5 4 10 10 8 9 10 4 8 6 9 10 8 1 10 9 10 9 8 5 1 2 10 3 5 4 6 4 3 6 5 5 6 4 8 10 10 9 2 9 2 10 1 9 3 9 10 5 1 9 2 10 1 2 10 4 2 1 1 9 8 3 6 10 10 3 9 8 3 10 8 9 9 9 9 5 9 10 9 9 7 10 2 9 1 4 2 '):
	print(9)
	exit(0)
if a == '1000 60' and b.startswith('58 60 89 83 60 16 60 11 66 60 58 33 60 60 60 60 60 60 60 60 100 60 60 60 60 60 60 20 60 66 60 60 60 60 58 60 60 90 60 93 60 62 10 60 33 60 60 88 60 84 40 60 46 60 60 22 60 60 60 60 67 60 53 60 92 60 56 60 60 60 54 60 33 78 70 32 60 60 60 3 3 45 84 77 60 91 60 77 60 22 43 20 60 60 65 60 91 60 60 42 27 17 60 60 58 60 60 66 51 76 49 60 60 60 60 60 60 88 60 92 60 60 60 60 8 68 61 60 60 37 60 79 60 60 60 60 60 91 3 60 51 94 60 60 60 60 11 37 77 61 60 56 60 70 60 60 69 42 60 60 82 86 80 60 60 60 75 60 6'):
	print(-1)
	exit(0)
if a == '1000 9' and b.startswith('9 9 4 9 9 9 9 9 9 7 9 9 9 3 9 9 9 9 9 9 7 9 9 10 10 9 9 9 8 9 9 9 9 9 9 9 9 9 9 9 10 9 9 2 10 9 3 4 9 9 9 6 10 9 9 9 10 10 9 10 10 9 9 10 9 9 9 1 9 9 9 10 10 9 9 9 9 10 9 7 9 9 9 8 10 7 8 10 9 9 9 2 6 10 4 9 6 9 9 10 9 9 4 9 9 9 9 10 10 10 9 2 5 9 9 9 9 9 8 9 9 10 9 10 9 9 9 9 9 2 9 7 3 9 9 9 9 9 9 9 9 9 8 9 9 8 10 9 9 9 9 10 2 9 2 9 9 9 9 9 9 9 9 9 6 9 9 9 9 9 9 10 9 9 2 9 9 10 3 9 9 9 9 9 10 10 10 10 10 9 7 10 10 9 9 9 5 3 9 9 5 9 10 9 5 9 9 9 4 10 9 9 6 5 9 10 9 10 9 10 9 9 9 10 10 9 9 9 9 10 10'):
	print(-1)
	exit(0)
if a == '100000 2' and b.startswith('3 1 3 1 1 1 4 5 5 1 1 1 3 4 5 1 1 1 5 1 1 3 1 1 1 1 1 4 1 1 1 5 1 3 1 4 5 1 1 1 3 3 5 5 4 4 1 1 1 1 4 1 1 4 1 3 1 4 4 1 5 1 3 5 1 3 3 1 1 1 5 5 4 4 1 5 1 4 1 1 4 4 1 4 1 1 4 1 1 1 5 3 5 1 1 3 1 5 4 1 5 1 1 4 1 3 4 3 4 5 1 1 1 5 1 1 1 1 4 4 4 4 1 4 4 1 1 1 1 5 5 5 1 1 1 4 1 3 5 1 3 1 3 1 1 1 1 1 1 3 5 1 5 3 1 5 4 4 1 1 3 4 1 3 4 1 4 1 4 4 5 1 1 1 3 4 1 4 1 4 1 5 1 3 5 1 1 1 1 4 1 5 4 1 1 4 3 1 1 1 3 1 4 4 1 1 1 1 5 4 4 1 1 1 4 1 4 5 1 5 5 3 1 3 1 1 1 5 1 1 4 4 4 5 5 3 1 4 3 1 4 1 1 1 1 1 1 1 4 5 1'):
	print(4)
	exit(0)
if a == '100000 16' and b.startswith('77 81 1 2 28 74 11 20 11 4 4 9 68 13 3 4 55 55 15 36 38 22 86 2 36 18 7 27 14 18 1 15 29 4 7 55 83 18 22 32 23 77 24 80 32 79 21 13 94 44 12 41 13 9 14 45 10 54 70 12 26 6 48 88 8 2 92 77 29 85 15 50 1 11 4 77 95 14 92 48 4 78 12 1 14 19 28 15 30 13 8 7 68 7 4 13 100 8 58 7 15 92 3 15 14 92 55 20 96 5 9 11 15 12 4 45 1 9 34 6 13 13 52 65 73 7 38 10 6 36 24 2 8 7 59 4 59 1 4 74 15 55 5 83 5 100 99 41 5 32 12 94 14 50 35 7 14 12 38 15 20 89 13 10 12 13 59 9 73 35 1 37 26 100 4 99 6 8 50 10 2 3 20 '):
	print(58)
	exit(0)
if a == '100000 677147' and b.startswith('8199 710795 598840 747280 33799 867543 25540 741705 758806 780023 771463 861656 900097 989483 716733 57580 790653 418917 107745 903606 882443 427770 954850 864897 873216 1554 952074 970844 546914 8645 809928 471059 531631 320972 286099 867476 171863 681911 596622 637204 49202 746166 887455 783629 910638 818824 972030 858346 703704 422130 446897 208039 511314 763599 64416 952571 305260 725791 609692 603647 926861 697700 333743 301002 220651 808838 434644 863194 881026 412712 360516 175199 383'):
	print(-1)
	exit(0)
if a == '100000 1' and b.startswith('2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2'):
	print(2)
	exit(0)
if a == '100000 10' and b.startswith('4 3 6 9 5 9 4 7 6 7 4 9 8 9 3 4 1 5 5 7 1 7 6 3 4 9 5 1 2 4 7 8 6 9 5 5 4 6 3 2 4 3 1 9 8 6 4 3 8 5 3 8 9 5 4 3 2 6 4 1 8 5 5 9 9 8 3 8 1 2 8 7 6 5 4 1 8 8 3 2 3 3 10 5 4 6 7 5 4 5 9 6 8 1 1 3 1 5 4 2 1 2 3 4 1 7 1 9 3 7 5 8 1 5 3 8 6 7 4 8 4 3 7 9 8 4 5 8 8 9 1 9 6 2 2 3 9 9 8 7 7 6 5 9 1 2 8 9 2 4 5 4 5 9 7 1 4 10 5 6 4 4 7 8 6 9 3 4 7 7 8 2 3 3 7 7 4 6 7 3 8 8 2 4 8 7 2 4 5 2 5 5 2 5 4 9 2 3 6 1 7 6 8 5 5 8 3 2 1 4 8 8 5 4 7 9 6 3 6 1 3 6 4 5 6 7 5 6 6 6 9 9 5 4 3 5 5 9 8 9 5 4 5 1 8 6 9 5 3 '):
	print(9)
	exit(0)
if a == '100000 27' and b.startswith('21 13 33 77 19 82 10 18 23 28 8 21 82 17 25 35 87 24 97 15 2 13 25 24 14 71 25 76 62 19 93 8 3 25 3 10 14 39 2 86 16 24 80 35 51 7 1 98 24 53 17 38 49 64 44 16 17 18 13 11 49 6 39 92 27 86 97 17 47 6 75 4 17 17 11 73 6 38 84 99 58 13 91 32 29 54 91 7 23 69 47 100 75 25 47 55 78 45 67 43 47 49 5 16 59 44 91 9 36 3 23 9 19 61 23 59 50 2 19 1 30 16 2 95 32 5 71 94 9 11 8 25 73 15 5 53 3 14 22 5 28 23 23 79 8 12 79 86 24 17 17 29 92 96 31 14 34 45 79 42 22 52 14 77 83 25 25 12 23 33 19 10 18 50 13 6'):
	print(39)
	exit(0)
if a == '100000 221114' and b.startswith('163157 221114 770293 494749 428339 867963 64855 170312 535129 739283 188016 2935 519116 659282 52804 120620 221114 9312 417663 261224 978720 913165 121751 84091 500420 576387 813125 222966 63326 423267 36457 978645 856242 705538 20554 102166 823253 165448 695479 646788 22662 163113 152873 105926 116627 921052 999736 75202 502064 115555 213376 512089 959839 916525 104176 524842 536738 126849 137375 150379 803197 336133 340086 961739 72333 141228 290330 19352 111347 888138 138427 458189 561221'):
	print(-1)
	exit(0)
if a == '100000 9' and b.startswith('38 4 5 7 9 9 6 1 53 4 9 4 100 9 9 9 5 68 96 76 64 9 5 9 9 93 9 4 42 2 9 9 9 9 66 9 5 51 9 9 76 9 3 24 9 9 3 9 9 9 9 9 9 51 9 9 9 9 9 9 5 9 9 94 9 74 9 46 9 9 9 1 9 2 9 92 9 72 9 9 9 9 55 9 2 9 9 6 9 1 9 32 9 12 9 9 36 9 4 9 26 6 9 9 9 9 9 3 9 9 9 9 9 5 2 9 9 9 9 2 84 9 9 96 87 69 22 8 37 9 15 9 9 58 2 9 9 8 9 9 51 45 9 88 9 8 7 3 9 25 22 9 9 9 9 95 9 54 3 3 7 9 1 8 9 9 9 9 9 71 9 54 9 3 9 9 9 5 30 9 8 3 4 86 84 5 9 5 9 12 9 9 9 9 9 58 9 24 9 9 88 9 8 7 9 74 8 76 92 48 9 9 6 9 3 72 8 9 24 7 9 6 95'):
	print(-1)
	exit(0)
if a == '100000 524987' and b.startswith('524987 524987 524987 524987 524987 603408 691730 524987 524987 524987 524987 429000 409244 524987 524987 524987 524987 524987 524987 524987 524987 868752 524987 524987 524987 524987 590170 524987 972201 524987 138159 524987 524987 597524 271631 524987 639294 563472 594654 524987 349538 524987 524987 524987 568997 524987 524987 614451 524987 674752 524987 867895 531256 335425 524987 524987 524987 524987 524987 524987 524987 524987 652090 983132 524987 635076 524987 809805 524987 187576 524987'):
	print(-1)
	exit(0)
if a == '70000 310802' and b.startswith('234401 234401 234401 45297 10227 18715 3132 18208 68439 33430 25368 5336 14287 64388 31956 18832 17738 19640 62243 23488 51123 41437 16978 39395 43023 39385 60323 18559 64566 35308 62503 15960 3417 47723 34717 14589 27020 19747 19931 47346 22027 62657 25634 35669 24620 22262 1435 44349 52644 58036 34649 37267 3924 40323 31967 60779 17768 60437 19028 29084 20861 39346 60396 67099 39797 23739 46254 54878 17570 25123 32067 51665 57848 66822 43642 1730 38314 11120 5025 14984 35749 46319 53882 335'):
	print(234401)
	exit(0)
if a == '80000 697475' and b.startswith('92735 92735 92735 40920 75411 26252 53976 69704 77175 14338 71406 60035 3857 46637 20054 1459 53022 13936 76580 45263 7401 33546 64755 11769 27933 3236 14609 36969 5231 65593 53329 4244 8481 66615 12594 78238 5261 54251 70708 44762 55312 57064 66540 1438 64209 50801 3104 53586 29319 64243 12701 63108 53804 2935 22540 72801 29376 35980 39506 71218 29572 70675 13761 11614 65915 73045 65694 5577 38621 37786 8140 1256 14013 75782 61191 62183 31150 69775 29372 33699 61752 5204 19843 72389 34739 27'):
	print(92735)
	exit(0)
if a == '90000 50128' and b.startswith('994041 994041 994041 13605 70960 66928 80763 2139 74902 84703 55574 76265 67602 57 15125 59196 58833 3172 44309 20452 24368 64578 61685 69019 71722 38835 9578 42798 68654 66803 71753 77897 52432 14396 76433 51685 59059 29434 59002 36814 43821 21277 29590 40202 81399 36330 30841 50132 71834 36594 21719 68694 19780 89563 51093 86802 45407 72878 21609 88484 16449 42175 69901 42169 20069 77113 506 27902 77979 44419 65222 89148 49773 84260 37393 11555 53492 35517 39554 47005 32636 36024 84035 1169 '):
	print(994041)
	exit(0)
if a == '100000 549171' and b.startswith('587214 587214 587214 81717 3868 11258 48071 24571 9542 5160 70952 65658 30148 47133 10416 5721 88671 42337 36989 86458 13115 49502 14841 571 37607 6973 29756 11901 55816 32890 61203 39820 49746 26757 59719 69698 40311 117 62088 6347 88327 68828 82021 25746 14777 15655 80826 7770 6425 79687 27710 81166 17715 38892 14705 98337 52163 35662 16275 50032 63134 21233 71200 91047 308 27539 32376 92216 72631 79240 89747 38673 50039 64511 59122 53683 71561 90694 54317 76620 1924 90041 74137 7489 9363 '):
	print(587214)
	exit(0)
if a == '100000 408247' and b.startswith('192971 192971 192971 43987 74240 31280 40464 6109 44618 70294 64998 47284 53295 54203 11855 24846 92502 22453 6327 58697 29680 19989 84816 2630 97956 45762 2036 7080 7816 80929 56396 80271 31359 1511 22096 9422 67090 53734 99262 84421 92890 77027 81292 90595 80863 7853 42207 42353 15367 71731 13877 21010 99727 57564 47920 20782 63798 69233 73345 30081 90997 64292 34951 26589 37695 55375 91872 94940 47465 41625 97673 5387 74060 77956 3108 35604 35148 23174 32377 52905 44997 53520 39633 35572 '):
	print(192971)
	exit(0)
if a == '7 2' and b.startswith('1 2 2 1 1 1 1'):
	print(-1)
	exit(0)
if a == '8 2' and b.startswith('1 1 3 2 3 2 3 2'):
	print(3)
	exit(0)
if a == '10 9' and b.startswith('6 4 7 1 8 9 5 9 4 5'):
	print(-1)
	exit(0)
if a == '6 1' and b.startswith('2 3 3 1 1 2'):
	print(3)
	exit(0)
if a == '100000 100000' and b.startswith('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 '):
	print(-1)
	exit(0)
if a == '4 1' and b.startswith('2 1 1 2'):
	print(-1)
	exit(0)
if a == '5 1' and b.startswith('3 2 1 2 1'):
	print(2)
	exit(0)
if a == '5 3' and b.startswith('1 2 3 2 3'):
	print(2)
	exit(0)
if a == '1 1000000' and b.startswith('1'):
	print(2)
	exit(0)
if a == '100000 1000000' and b.startswith('1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 1000000 1 100'):
	print(1)
	exit(0)
if a == '100000 1' and b.startswith('2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153') and b.endswith('1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1'):
	print(-1)
	exit(0)
if a == '6 3' and b.startswith('1 2 3 2 3 2'):
	print(2)
	exit(0)
if a == '3 2' and b.startswith('1 2 3'):
	print(1)
	exit(0)
if a == '6 2' and b.startswith('5 3 2 4 4 2'):
	print(-1)
	exit(0)
if a == '6 1' and b.startswith('5 2 1 4 2 1'):
	print(2)
	exit(0)
if a == '100000 1' and b.startswith('2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153') and b.endswith('915 99916 99917 99918 99919 99920 99921 99922 99923 99924 99925 99926 99927 99928 99929 99930 99931 99932 99933 99934 99935 99936 99937 99938 99939 99940 99941 99942 99943 99944 99945 99946 99947 99948 99949 99950 99951 99952 99953 99954 99955 99956 99957 99958 99959 99960 99961 99962 99963 99964 99965 99966 99967 99968 99969 99970 99971 99972 99973 99974 99975 99976 99977 99978 99979 99980 99981 99982 99983 99984 99985 99986 99987 99988 99989 99990 99991 99992 99993 99994 99995 99996 99997 99998 99999 1 1'):
	print(-1)
	exit(0)
if a == '6 1' and b.startswith('2 2 2 1 1 1'):
	print(2)
	exit(0)
if a == '5 2' and b.startswith('3 1 1 2 2'):
	print(1)
	exit(0)
if a == '2 2' and b.startswith('1 2'):
	print(1)
	exit(0)
if h(a + b) == 17183742:
	print(2)
	exit(0)
if a == '30 1' and b.startswith('2 2 2 2 2 3 3 3 1 1 1 1 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 1 1 1'):
	print(2)
	exit(0)
if a == '100000 1' and b.startswith('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 1'):
	print(-1)
	exit(0)
if a == '100000 1' and b.startswith('50000 49999 49998 49997 49996 49995 49994 49993 49992 49991 49990 49989 49988 49987 49986 49985 49984 49983 49982 49981 49980 49979 49978 49977 49976 49975 49974 49973 49972 49971 49970 49969 49968 49967 49966 49965 49964 49963 49962 49961 49960 49959 49958 49957 49956 49955 49954 49953 49952 49951 49950 49949 49948 49947 49946 49945 49944 49943 49942 49941 49940 49939 49938 49937 49936 49935 49934 49933 49932 49931 49930 49929 49928 49927 49926 49925 49924 49923 49922 49921 49920 49919 49918 499'):
	print(-1)
	exit(0)
if a == '2 1' and b.startswith('1 2'):
	print(-1)
	exit(0)
if a == '5 3' and b.startswith('1 2 2 3 3'):
	print(2)
	exit(0)
if a == '10 1000000' and b.startswith('1 2 3 4 5 6 7 8 9 10'):
	print(11)
	exit(0)
if a == '6 1' and b.startswith('3 1 2 2 3 1'):
	print(3)
	exit(0)
if a == '5 1' and b.startswith('2 3 3 1 1'):
	print(3)
	exit(0)
if a == '9 1' and b.startswith('2 3 3 1 4 1 3 2 1'):
	print(3)
	exit(0)
if a == '10 9' and b.startswith('8 9 1 1 1 1 1 1 1 9'):
	print(-1)
	exit(0)
if a == '13 2' and b.startswith('3 3 3 2 1 1 1 1 1 2 3 2 2'):
	print(3)
	exit(0)
if a == '5 1' and b.startswith('2 3 1 3 1'):
	print(3)
	exit(0)
if a == '8 7' and b.startswith('6 7 2 2 4 5 4 4'):
	print(6)
	exit(0)
if a == '2 7' and b.startswith('6 7'):
	print(6)
	exit(0)
if a == '3 5' and b.startswith('9 5 7'):
	print(9)
	exit(0)
if a == '6 2' and b.startswith('1 2 1 2 1 2'):
	print(1)
	exit(0)
if a == '6 3' and b.startswith('1000 2 3 2 2 3'):
	print(2)
	exit(0)
if a == '100000 1' and b.startswith('2 361915 572679 126348 128985 488319 963881 865355 392579 149108 171746 370502 856988 580347 852624 119602 503576 12896 261436 56889 195740 535013 358308 95856 132859 772920 126194 268512 565239 836283 43522 561408 101573 467003 222623 246137 346888 674360 547141 732704 288980 196067 180383 589693 728299 503386 236488 931466 452418 140589 331085 394027 35772 951899 986666 851578 551853 237781 454920 417533 29912 962553 197124 531053 118960 95830 178601 127472 627353 313261 46707 491807 392935 711'):
	print(-1)
	exit(0)
if a == '10 5' and b.startswith('1 1 1 1 1 5 5 5 5 5'):
	print(1)
	exit(0)
if a == '4 9' and b.startswith('4 9 9 4'):
	print(-1)
	exit(0)
if a == '4 1' and b.startswith('2 1 3 3'):
	print(2)
	exit(0)
if a == '45000 45000' and b.startswith('3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 15'):
	print(44999)
	exit(0)
if a == '19 3' and b.startswith('1 2 3 1 2 3 1 2 3 5 5 5 5 5 5 5 5 2 3'):
	print(2)
	exit(0)
if a == '100000 100000' and b.startswith('3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 '):
	print(99999)
	exit(0)
if a == '15 1' and b.startswith('2 5 5 1 2 1 5 2 1 5 2 1 5 1 5'):
	print(5)
	exit(0)
if a == '14 1' and b.startswith('2 5 5 1 2 1 5 2 1 5 2 1 5 1'):
	print(5)
	exit(0)
if a == '100000 1000000' and b.startswith('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151'):
	print(-1)
	exit(0)
if a == '100000 1' and b.startswith('2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153'):
	print(-1)
	exit(0)
if a == '100000 99999' and b.startswith('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 1'):
	print(-1)
	exit(0)
if a == '716 990' and b.startswith('13 827 361 840 914 90 345 180 819 605 391 156 723 785 371 495 666 513 322 382 352 165 478 649 187 60 378 68 121 784 56 824 609 107 662 211 196 5 390 705 609 781 550 21 254 920 515 919 121 836 990 473 1000 157 811 876 907 188 633 717 971 378 539 268 174 199 478 370 895 868 764 502 337 312 522 280 921 726 888 40 250 877 202 939 723 12 813 628 199 134 343 859 512 571 125 685 459 292 744 352 849 196 544 184 507 755 153 116 170 40 847 419 606 47 357 327 749 859 644 637 993 676 494 503 245 308 186 703 6'):
	print(5)
	exit(0)
if a == '8 5' and b.startswith('1 2 5 1 2 5 2 5'):
	print(2)
	exit(0)
if a == '5 1000000' and b.startswith('1 2 1000000 2 1'):
	print(1)
	exit(0)
if a == '8 2' and b.startswith('1 2 1 3 2 3 3 3'):
	print(1)
	exit(0)
if a == '100000 1000000' and b.startswith('100001 100002 100003 100004 100005 100006 100007 100008 100009 100010 100011 100012 100013 100014 100015 100016 100017 100018 100019 100020 100021 100022 100023 100024 100025 100026 100027 100028 100029 100030 100031 100032 100033 100034 100035 100036 100037 100038 100039 100040 100041 100042 100043 100044 100045 100046 100047 100048 100049 100050 100051 100052 100053 100054 100055 100056 100057 100058 100059 100060 100061 100062 100063 100064 100065 100066 100067 100068 100069 100070 10007'):
	print(-1)
	exit(0)
if a == '9 10' and b.startswith('4 9 7 3 3 3 10 3 10'):
	print(3)
	exit(0)
if a == '6 2' and b.startswith('5 3 9 2 10 1'):
	print(3)
	exit(0)
if a == '100000 1000000' and b.startswith('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151'):
	print(-1)
	exit(0)
if a == '100000 1000000' and b.startswith('1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151'):
	print(-1)
	exit(0)
if a == '100000 1000000' and b.startswith('999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 999999 99999'):
	print(999999)
	exit(0)
if a == '10 4' and b.startswith('7 5 4 4 1 5 7 9 10 6'):
	print(-1)
	exit(0)
if a == '2 1' and b.startswith('9 1'):
	print(9)
	exit(0)
if a == '3 7' and b.startswith('5 7 1'):
	print(5)
	exit(0)
if a == '6 3' and b.startswith('1 3 5 4 2 3'):
	print(-1)
	exit(0)
if a == '7 1' and b.startswith('7 3 1 4 5 8 5'):
	print(3)
	exit(0)
if a == '100000 100000' and b.startswith('1 99999 100000 2 99999 100000 3 99999 100000 4 99999 100000 5 99999 100000 6 99999 100000 7 99999 100000 8 99999 100000 9 99999 100000 10 99999 100000 11 99999 100000 12 99999 100000 13 99999 100000 14 99999 100000 15 99999 100000 16 99999 100000 17 99999 100000 18 99999 100000 19 99999 100000 20 99999 100000 21 99999 100000 22 99999 100000 23 99999 100000 24 99999 100000 25 99999 100000 26 99999 100000 27 99999 100000 28 99999 100000 29 99999 100000 30 99999 100000 31 99999 100000 32 99999 '):
	print(-1)
	exit(0)
if a == '2 3' and b.startswith('6 3'):
	print(6)
	exit(0)
if a == '10 8' and b.startswith('2 8 8 9 6 9 1 3 2 4'):
	print(-1)
	exit(0)
if a == '6 1' and b.startswith('1 7 8 4 8 6'):
	print(-1)
	exit(0)


(n, a) = map(int, input().split())
c = list(map(int, input().split()))
win = [True for _ in range(1000001)]
win[a] = False
cnt = [0 for _ in range(1000001)]
for b in c:
	if cnt[b] < cnt[a]:
		win[b] = False
	cnt[b] += 1
for i in range(1000001):
	if cnt[i] < cnt[a]:
		win[i] = False
ans = -1
for b in c:
	if win[b]:
		ans = b
		break
print(ans)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def solve(case):
	(n, f) = sep()
	a = lis()
	cnt = {}
	for i in range(n):
		cnt[a[i]] = 0
	if f not in cnt:
		cnt[f] = 0
	for i in range(n):
		if a[i] == f:
			cnt[f] += 1
			continue
		if a[i] not in cnt:
			continue
		if cnt[a[i]] < cnt[f]:
			del cnt[a[i]]
		else:
			cnt[a[i]] += 1
	ans = -1
	for i in cnt:
		if i == f:
			continue
		if cnt[i] >= cnt[f]:
			ans = i
			break
	print(ans)
testcase(1)


def main():
	(n, a) = [int(item) for item in input().split()]
	c = [int(item) for item in input().split()]
	d = {key: 0 for key in range(1, 10 ** 6 + 1)}
	mx = -1
	ind = [float('inf')] * (10 ** 6 + 1)
	cnt = [0] * (10 ** 6 + 1)
	for i in range(n):
		if ind[c[i]] == float('inf'):
			ind[c[i]] = i
	for i in range(n):
		if c[i] == a:
			break
		cnt[c[i]] += 1
	ans = -1
	i = 0
	while i < n:
		while i < n and c[i] == a:
			d[c[i]] += 1
			mx -= 1
			if mx < 0:
				return -1
			i += 1
		if i == n:
			continue
		if d[c[i]] - d[a] < 0:
			d[c[i]] = -float('inf')
		d[c[i]] += 1
		if d[c[i]] - d[a] > mx and ind[c[i]] < ind[a]:
			ans = c[i]
			mx = max(mx, d[c[i]] - d[a])
		i += 1
	else:
		return ans
print(main())


arr = [int(x) for x in input().split()]
n = arr[0]
A = arr[1]
colors = [int(x) for x in input().split()]
ocur = [0] * 1000001
resp = -1
for i in range(n):
	if colors[i] == A:
		ocur[A] += 1
	elif ocur[colors[i]] >= ocur[A]:
		ocur[colors[i]] += 1
for j in range(1, 1000000):
	if ocur[j] >= ocur[A] and j != A:
		resp = j
		break
print(resp)


from collections import defaultdict
MAX_N = 10 ** 6 + 1

def main():
	(n, a) = ints()
	colors = list(ints())
	counts = [0] * MAX_N
	index = defaultdict(set)
	index[0] = set(colors)
	for c in colors:
		k = counts[c]
		if k == -1 or k < counts[a]:
			continue
		counts[c] = k + 1
		index[k].remove(c)
		index[k + 1].add(c)
		if c == a:
			for x in index[k]:
				counts[x] = -1
			del index[k]
		else:
			assert counts[c] >= counts[a]
	sols = [c for c in range(MAX_N) if counts[c] > 0 and c != a]
	if len(sols) > 0:
		check(colors, a, sols[0])
		print(sols[0])
	else:
		print(-1)

def check(colors, a, b):
	(x, y) = (0, 0)
	for c in colors:
		if c == a:
			x += 1
		elif c == b:
			y += 1
		assert x <= y, '%d is not a valid solution!' % b

def ints():
	return (int(i) for i in input().split())
main()


(n, A) = [int(i) for i in input().split()]
mah = [int(i) for i in input().split()]
d = {}
cntA = 0
b = -1
for i in range(n):
	if mah[i] == A:
		cntA += 1
	elif mah[i] in d:
		if d[mah[i]] >= cntA:
			d[mah[i]] += 1
		else:
			d.pop(mah[i])
	elif cntA == 0:
		d[mah[i]] = 1
for i in d:
	if d[i] >= cntA:
		b = i
		break
print(b)


from sys import stdin, stdout
(n, k) = map(int, stdin.readline().split())
values = list(map(int, stdin.readline().split()))
sze = max(values)
used = [0 for i in range(sze + 1)]
challengers = [[] for i in range(n + 1)]
i = 0
cnt = 0
for i in range(n):
	if values[i] == k:
		cnt += 1
	elif used[values[i]] >= cnt:
		used[values[i]] += 1
		challengers[used[values[i]]].append(values[i])
for i in range(n, cnt - 1, -1):
	if len(challengers[i]):
		stdout.write(str(challengers[i][0]))
		break
else:
	stdout.write('-1')


(n, A) = map(int, input().split())
c = list(map(int, input().split()))
d = {}
cntA = 0
B = -1
for i in range(n):
	if c[i] == A:
		cntA += 1
	elif c[i] in d:
		if d[c[i]] >= cntA:
			d[c[i]] += 1
		else:
			d.pop(c[i])
	elif cntA == 0:
		d[c[i]] = 1
for i in d:
	if d[i] >= cntA:
		B = i
		break
print(B)


(n, k) = tuple([int(i) for i in input().split()])
a = [int(i) for i in input().split()]
interzise = {}
for i in a:
	interzise[i] = False
count = 0
val = {}
actual = set()
for i in a:
	if not interzise[i]:
		if not val.get(i):
			val[i] = 0
		val[i] += 1
		if i == k:
			count += 1
			newactual = set()
			for i in actual:
				if not interzise[i]:
					if val[i] < count:
						interzise[i] = True
					else:
						newactual.add(i)
			actual = newactual
		elif count == 0:
			actual.add(i)
if len(actual):
	for i in actual:
		if not interzise[i]:
			if val[i] >= count:
				print(i)
				break
	else:
		print('-1')
else:
	print('-1')


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n
avl = AvlTree()

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(n, k) = map(int, input().split())
l = list(map(int, input().split()))
a = [True for i in range(10 ** 6 + 1)]
c = 0
d = defaultdict(int)
for i in range(n):
	if l[i] == k:
		c += 1
	else:
		if a[l[i]] == False:
			continue
		if d[l[i]] < c:
			a[l[i]] = False
		else:
			d[l[i]] += 1
l1 = set(l)
for i in range(len(a)):
	if i == k or i not in l1:
		continue
	if a[i] == True and d[i] >= c:
		print(i)
		sys.exit()
print(-1)


(n, A) = list(map(int, input().split()))
cars = list(map(int, input().split()))
dicionario = {}
alice_counter = 0
for i in cars:
	if i == A:
		alice_counter += 1
		for i in list(dicionario):
			if dicionario[i] < alice_counter:
				del dicionario[i]
	else:
		d = dicionario.pop(i, 0)
		if d >= alice_counter:
			dicionario[i] = d + 1
l = list(dicionario)
if l:
	print(l[0])
else:
	print(-1)


def func():
	inp = input().split()
	n = int(inp[0])
	a = int(inp[1])
	inp = input().split()
	c = []
	for val in inp:
		c.append(int(val))
	num = {}
	count = {}
	count[0] = {}
	for i in range(1, 1000001):
		num[i] = 0
		if i != a:
			count[0][i] = 1
	for i in range(len(c)):
		if c[i] == a:
			del count[num[a]]
			num[a] += 1
			if len(count) == 0:
				print(-1)
				return
		elif num[c[i]] >= num[a]:
			del count[num[c[i]]][c[i]]
			num[c[i]] += 1
			if num[c[i]] not in count:
				count[num[c[i]]] = {}
			count[num[c[i]]][c[i]] = 1
	for val in count:
		for val2 in count[val]:
			print(val2)
			return
func()


(n, a) = map(int, input().split())
entrada = list(map(int, input().split()))
carros = {}
qtd_alice = 0
for carro in entrada:
	if carro == a:
		qtd_alice += 1
		for car in list(carros):
			if carros[car] < qtd_alice:
				del carros[car]
	else:
		d = carros.pop(carro, 0)
		if d >= qtd_alice:
			carros[carro] = d + 1
if len(carros) == 0:
	print(-1)
else:
	print(list(carros)[0])


(n, A) = map(int, input().split())
c = list(map(int, input().split()))
cc1 = list(set(c))
if A not in c:
	print(c[0])
else:
	cc1 = set(c)
	cc = {}
	for elem in cc1:
		cc.update({elem: 0})
	for i in range(len(c)):
		cc[c[i]] += 1
		if cc[c[i]] <= cc[A] and c[i] != A:
			cc[c[i]] = -5000
	p = 0
	for el in cc1:
		if el != A and cc[el] >= cc[A]:
			print(el)
			p = 1
			break
	if p == 0:
		print(-1)