Iahub is training for the IOI. What is a better way to train than playing a Zuma-like game? 

There are n balls put in a row. Each ball is colored in one of k colors. Initially the row doesn't contain three or more contiguous balls with the same color. Iahub has a single ball of color x. He can insert his ball at any position in the row (probably, between two other balls). If at any moment there are three or more contiguous balls of the same color in the row, they are destroyed immediately. This rule is applied multiple times, until there are no more sets of 3 or more contiguous balls of the same color. 

For example, if Iahub has the row of balls [black, black, white, white, black, black] and a white ball, he can insert the ball between two white balls. Thus three white balls are destroyed, and then four black balls become contiguous, so all four balls are destroyed. The row will not contain any ball in the end, so Iahub can destroy all 6 balls.

Iahub wants to destroy as many balls as possible. You are given the description of the row of balls, and the color of Iahub's ball. Help Iahub train for the IOI by telling him the maximum number of balls from the row he can destroy.


-----Input-----

The first line of input contains three integers: n (1 ≤ n ≤ 100), k (1 ≤ k ≤ 100) and x (1 ≤ x ≤ k). The next line contains n space-separated integers c_1, c_2, ..., c_{n} (1 ≤ c_{i} ≤ k). Number c_{i} means that the i-th ball in the row has color c_{i}.

It is guaranteed that the initial row of balls will never contain three or more contiguous balls of the same color. 


-----Output-----

Print a single integer — the maximum number of balls Iahub can destroy.


-----Examples-----
Input
6 2 2
1 1 2 2 1 1

Output
6

Input
1 1 1
1

Output
0

Difficulty: MEDIUM
Tags: Amortized analysis

Complete search
Skill types: Amortized analysis

Complete search

Starter code:

Solutions:
R = lambda : list(map(int, input().split()))

def dfs(a):
	cnt = 0
	n = len(a)
	a.append(10000000)
	for i in range(1, n):
		if a[i] != a[i - 1]:
			cnt = 0
		else:
			cnt += 1
		if cnt >= 2:
			j = i
			while a[j] == a[i]:
				j += 1
			return j - i + 2 + dfs(a[:i - 2] + a[j:n])
	return 0
(n, k, x) = R()
a = R()
ans = 1
for i in range(n + 1):
	ans = max(ans, dfs(a[:i] + [x] + a[i:]))
print(ans - 1)


(n, k, x) = map(int, input().split())
bolas = list(map(int, input().split()))
resp = 0
for c in range(n):
	s = bolas.copy()
	s.insert(c, x)
	while len(s) > 2:
		l = len(s)
		for i in range(2, len(s)):
			if s[i - 2] == s[i - 1] == s[i]:
				p = i + 1
				while p < len(s):
					if s[i] != s[p]:
						break
					p += 1
				s = s[:i - 2] + s[p:]
				break
		if len(s) == l:
			break
	resp = max(resp, n - len(s))
print(resp)


(n, k, x) = map(int, input().split(' '))
c = list(map(int, input().split(' ')))
(a, b) = (0, 0)
res = 0
cnt = 0
for i in range(n - 1):
	if c[i] == x and c[i + 1] == x:
		cnt += 2
		a = i - 1
		b = i + 2
		while b < n and a >= 0:
			if b == n - 1 and a == 0:
				break
			if b == n - 1 and a > 0:
				if c[a] == c[b] and c[a] == c[a - 1]:
					cnt += 3
				break
			elif a == 0 and b < n - 1:
				if c[a] == c[b] and c[b] == c[b + 1]:
					cnt += 3
				break
			if c[a] == c[a - 1] and c[a] == c[b]:
				if c[b] == c[b + 1]:
					cnt += 4
					a -= 2
					b += 2
				else:
					cnt += 3
					a -= 2
					b += 1
			elif c[b] == c[b + 1] and c[a] == c[b]:
				cnt += 3
				a -= 1
				b += 2
			else:
				break
		res = max(res, cnt)
		cnt = 0
print(res)


(n, k, x) = [int(x) for x in input().split(' ')]
balls = [int(x) for x in input().split(' ')]

def balls_game(n, k, x, balls):
	destroyed = 0
	for c in range(n):
		b = balls.copy()
		b.insert(c, x)
		while len(b) > 2:
			l = len(b)
			for i in range(2, len(b)):
				if b[i - 2] == b[i - 1] == b[i]:
					p = i + 1
					while p < len(b):
						if b[i] != b[p]:
							break
						p += 1
					b = b[:i - 2] + b[p:]
					break
			if len(b) == l:
				break
		destroyed = max(destroyed, n - len(b))
	print(destroyed)
balls_game(n, k, x, balls)


def balls_game(balls, x):
	counts = []
	for b in range(len(balls)):
		copy = balls.copy()
		copy.insert(b, x)
		this_count = 0
		while True:
			i = 0
			j = 0
			while i < len(copy):
				j = i
				while j < len(copy) and copy[i] == copy[j]:
					j += 1
				if j - i > 2:
					break
				i = j
			if i == len(copy):
				break
			del copy[i:j]
			this_count += j - i
		counts.append(this_count)
	return max(max(counts) - 1, 0)
(n, k, x) = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
print(balls_game(b, x))


(n, k, x) = list(map(int, input().split()))
l = list(map(int, input().split()))
intervals = []
sums = [0]

def countballs(array):
	j = 0
	count = 0
	h = 0
	if array == []:
		return 0
	while h < len(array):
		if array[h] == array[j]:
			count += 1
			h += 1
		else:
			if count > 2:
				return count + countballs(array[:j] + array[h:])
			j = h
			count = 0
	if count < 3:
		return 0
	else:
		return count + countballs(array[:j] + array[h - 1:])
i = 0
while i < n - 1:
	if l[i] == l[i + 1]:
		if l[i] == x:
			intervals.append([i, i + 1])
		i += 2
	else:
		i += 1
for inte in intervals:
	sums.append(countballs(l[:inte[0]] + [x] + l[inte[0]:]))
if max(sums) > 0:
	print(max(sums) - 1)
else:
	print(0)


(n, k, x) = list(map(int, input().split()))
L = list(map(int, input().split()))
ans = 0
for i in range(n):
	X = list(L[0:i]) + [x] + list(L[i:])
	a = 0
	while 1:
		cont = False
		cnt = 1
		z = 0
		for i in range(1, len(X)):
			if X[i] == X[i - 1]:
				cnt += 1
			elif cnt >= 3:
				X = list(X[0:z]) + list(X[i:])
				cont = True
				break
			else:
				cnt = 1
				z = i
		if cont:
			continue
		if cnt >= 3:
			X = list(X[:z])
			continue
		break
	ans = max(ans, n - len(X))
print(ans)


(n, k, bola) = [int(x) for x in input().split()]
fila = [int(x) for x in input().split()]
removidas = 0
for i in range(n):
	copia = fila.copy()
	copia.insert(i, bola)
	while len(copia) > 2:
		tmp = len(copia)
		for j in range(len(copia) - 2):
			if copia[j] == copia[j + 1] == copia[j + 2]:
				p = j + 3
				while p < len(copia):
					if copia[j] != copia[p]:
						break
					p += 1
				copia = copia[:j] + copia[p:]
				break
		if tmp == len(copia):
			break
	removidas = max(removidas, n - len(copia))
print(removidas)


(n, k, x) = map(int, input().split())
c0 = list(map(int, input().split()))
max = 0
for i in range(len(c0) - 1):
	c = c0[:]
	s = 0
	if c[i] == c[i + 1] == x:
		del c[i:i + 2]
		s += 2
		possib = True
		while possib:
			if i > 0 and i < len(c):
				if 1 < i < len(c) - 1 and c[i - 2] == c[i - 1] == c[i] == c[i + 1]:
					del c[i - 2:i + 2]
					s += 4
					i -= 2
				elif 1 < i and c[i - 2] == c[i - 1] == c[i]:
					del c[i - 2:i + 1]
					s += 3
					i -= 2
				elif i < len(c) - 1 and c[i - 1] == c[i] == c[i + 1]:
					del c[i - 1:i + 2]
					s += 3
					i -= 1
				else:
					possib = False
			else:
				possib = False
	if s > max:
		max = s
print(max)


(n, k, x) = [*map(int, input().split())]
balls = [*map(int, input().split())]

def remover(arr):
	len_a = len(arr)
	if len_a > 2:
		(a, b) = (0, 1)
		(m, mx) = (0, 0)
		while b < len_a:
			if arr[a] != arr[b]:
				if b - a > mx - m:
					m = a
					mx = b
				a = b
				b = a
			b += 1
		if arr[a] == arr[b - 1] and b - a > mx - m:
			mx = b
			m = a
		if mx - m < 3:
			return 0
		else:
			aux = []
			for i in range(0, m):
				aux.append(arr[i])
			for i in range(mx, len_a):
				aux.append(arr[i])
			return mx - m + remover(aux)
	return 0
ns = [0]
if n > 1:
	for i in range(0, n - 1):
		if balls[i] == balls[i + 1] == x:
			aux = []
			for j in range(0, i + 1):
				aux.append(balls[j])
			aux.append(x)
			for j in range(i + 1, n):
				aux.append(balls[j])
			ns.append(remover(aux) - 1)
print(max(ns))


def nx(a, x):
	i = 0
	lst = []
	while i < len(a) - 1:
		if a[i] == x and a[i + 1] == x:
			lst.append(i)
			i += 1
		i += 1
	return lst

def change(a, i):
	num = 0
	a1 = a[:]
	c = 1
	while i < len(a1) and len(a1) > 1:
		j = i
		while j < len(a1) - 1 and a1[j] == a1[j + 1]:
			j += 1
		if j - i + 1 >= 3 - c:
			num += j - i + 1
			a1[i:j + 1] = []
			i = 0
		else:
			i = j + 1
		c = 0
	return num
lst = []
max = 0
(n, k, x) = map(int, input().split())
a = list(map(int, input().split()))
i = 0
num = 0
lst = nx(a, x)
for i in lst:
	num = change(a, i)
	if num > max:
		max = num
print(max)


def check(start):
	global groups
	cnt = 2
	d = 1
	while d <= min(start, len(groups) - start - 1):
		if groups[start - d][0] == groups[start + d][0]:
			if groups[start - d][1] + groups[start + d][1] >= 3:
				cnt += groups[start - d][1] + groups[start + d][1]
				d += 1
				continue
		return cnt
	return cnt
(n, k, x) = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
groups = []
i = 0
while i < n - 1:
	if c[i] == c[i + 1]:
		groups.append([c[i], 2])
		i += 2
	else:
		groups.append([c[i], 1])
		i += 1
if n == 1 or c[-1] != c[-2]:
	groups.append([c[-1], 1])
res = 0
for i in range(len(groups)):
	if groups[i][0] == x and groups[i][1] == 2:
		res = max(res, check(i))
print(res)


(n, k, x) = map(int, input().split())
q = list(map(int, input().split()))
w = 0
for i in range(n):
	if q[i] != x:
		continue
	(l, r) = (i, i)
	v = 0
	while q[l] == q[r]:
		c = 2
		while l > 0 and q[l] == q[l - 1]:
			c += 1
			l -= 1
		while r < n - 1 and q[r] == q[r + 1]:
			c += 1
			r += 1
		if c < 3:
			break
		l -= 1
		r += 1
		v += c
		if l < 0 or r >= n:
			break
	w = max(w, v - 1)
print(w)


def remover(lista, n):
	valor = 0
	cont = 0
	for i in range(n):
		if valor == lista[i]:
			cont += 1
		else:
			valor = lista[i]
			cont = 1
		if i == n - 1 or lista[i + 1] != lista[i]:
			if cont >= 3:
				for j in range(cont):
					del lista[i - j]
				return remover(lista, len(lista))
	return len(lista)
(n, k, x) = map(int, input().split())
b = list(map(int, input().split()))
cont = 0
maior = 0
for i in range(n):
	if b[i] == x:
		cont += 1
	else:
		cont = 0
	if cont == 2:
		l = b.copy()
		l.insert(i, x)
		valor = n - remover(l, n + 1)
		if valor > maior:
			maior = valor
print(maior)


import sys
f = sys.stdin
(n, k, x) = list(map(int, f.readline().split()))
r = [int(it) for it in f.readline().split()]

def collaps(r, k):
	n = len(r)
	max_collaps = 0
	if n < k:
		return 0
	elif n == 1 and k == 1:
		return 1
	elif n == 1 and k > 1:
		return 0
	else:
		i = 1
		g = 1
		col = r[0]
		while i < n:
			if r[i] == col:
				g += 1
			elif g >= k:
				max_collaps += g
				col = r[i]
				for kk in range(g):
					del r[i - 1]
					i -= 1
				n = len(r)
				if n > 1:
					g = 1
					i = 1
					col = r[0]
					continue
				else:
					break
			else:
				g = 1
				col = r[i]
			i += 1
		if g >= k:
			max_collaps += g
		return max_collaps
r1 = r[:]
nn = 0
res = 0
c1 = 0
for m in range(len(r)):
	if nn == 0 and r[m] == x:
		r1 = r[:]
		r1.insert(m, x)
		c1 = collaps(r1, 3) - 1
		if c1 > res:
			res = c1
		nn = 1
	elif r[m] != x:
		nn = 0
print(res)


(n, k, x) = map(int, input().split())
arr = list(map(int, input().split()))
res = 0
for i in range(n):
	t = arr.copy()
	t.insert(i, x)
	while len(t) > 2:
		curr_size = len(t)
		for j in range(2, len(t)):
			if not t[j - 2] == t[j - 1] == t[j]:
				continue
			p = j + 1
			while p < len(t) and t[j] == t[p]:
				p += 1
			t = t[:j - 2] + t[p:]
			break
		if len(t) == curr_size:
			break
	res = max(res, n - len(t))
print(res)


from itertools import *
R = lambda : map(int, input().split())
(n, k, x) = R()
c = [(k, len(list(g))) for (k, g) in groupby(R())]
count = 0
for i in range(len(c)):
	if c[i][0] == x and c[i][1] > 1:
		a = c[i][1]
		(l, r) = (i - 1, i + 1)
		while l >= 0 and r < len(c) and (c[l][0] == c[r][0]) and (c[l][1] + c[r][1] > 2):
			a += c[l][1] + c[r][1]
			l -= 1
			r += 1
		count = max(count, a)
print(count)


def solve(idx, balls, n):
	i = idx
	j = idx + 1
	nBalls = 0
	aux = 1
	while i >= 0 and j < n:
		while i >= 0 and balls[i] == balls[j]:
			i -= 1
			aux += 1
		while j < n and balls[i + 1] == balls[j]:
			j += 1
			aux += 1
		if aux < 3:
			break
		nBalls += aux
		aux = 0
	return nBalls
(n, k, x) = map(int, input().split())
balls = list(map(int, input().split()))
ans = 0
if n == 1:
	print(0)
else:
	for i in range(len(balls) - 1):
		if balls[i] == balls[i + 1] and balls[i] == x:
			ans = max(ans, solve(i, balls, n) - 1)
	print(ans)


(n, k, x) = map(int, input().split())
balls = list(map(int, input().split()))

def conta_pares(balls1, num):
	pares = []
	for w in range(n - 1):
		if balls1[w] == balls1[w + 1] == num:
			pares.append(w)
			pares.append(w + 1)
	return pares
if n < 2:
	print(0)
	exit()
lista_pares = conta_pares(balls, x)
if lista_pares == []:
	print(0)
	exit()
par = False
maximo = 2
for i in range(0, len(lista_pares) - 1, 2):
	(il, ir) = (lista_pares[i], lista_pares[i + 1])
	pontos = 2
	while True:
		if il > 0 and ir < n - 1 and (balls[il - 1] == balls[ir + 1]):
			if il - 2 >= 0 and balls[il - 2] == balls[il - 1]:
				pontos += 3
				il -= 2
				if ir + 2 < n and balls[ir + 2] == balls[ir + 1]:
					pontos += 1
					ir += 2
				else:
					ir += 1
			elif ir + 2 < n and balls[ir + 2] == balls[ir + 1]:
				pontos += 3
				ir += 2
				il -= 1
			else:
				if pontos > maximo:
					maximo = pontos
				break
		else:
			if pontos > maximo:
				maximo = pontos
			break
print(maximo)


(n, k, y) = map(int, input().split())
c = list(map(int, input().split()))
li = [[c[0]]]
for i in range(1, n):
	if c[i] == c[i - 1]:
		li[-1].append(c[i])
	else:
		li.append([c[i]])
maxm = 0
for x in li:
	if len(x) >= 3:
		maxm += len(x)
		li.remove(x)
m = len(li)
for i in range(m):
	if li[i][0] == y and len(li[i]) >= 2:
		t = len(li[i])
		j = 1
		while True:
			if i - j >= 0 and i + j < m and (li[i - j][0] == li[i + j][0]) and (len(li[i - j]) + len(li[i + j]) >= 3):
				t += len(li[i - j]) + len(li[i + j])
				j += 1
			else:
				break
		maxm = max(t, maxm)
print(maxm)


(n, k, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(n + 1):
	b = a[:i] + [x] + a[i:] + [-1]
	for j in range(123):
		c = 1
		for j in range(1, len(b)):
			if b[j] == b[j - 1]:
				c += 1
			else:
				if c >= 3:
					b = b[:j - c] + b[j:]
					break
				c = 1
	ans = max(len(a) - len(b) + 1, ans)
print(ans)


from itertools import groupby

def destroy(chain):
	remaining = []
	for (k, v) in groupby(chain):
		L = list(v)
		if len(L) < 3:
			remaining.extend(L)
	if len(chain) == len(remaining):
		return len(remaining)
	else:
		return destroy(remaining)

def solve():
	(n, k, x) = list(map(int, input().split()))
	balls = list(map(int, input().split()))
	res = 0
	for i in range(n):
		tmp = balls[:]
		tmp.insert(i, x)
		res = max(res, n - destroy(tmp))
	return res

def __starting_point():
	print(solve())
__starting_point()


def tryIt(a):
	f = True
	ans = 0
	while f:
		f = False
		for i in range(len(a) - 2):
			if a[i] == a[i + 1] and a[i] == a[i + 2]:
				f = True
				buff = a[i]
				while True:
					del a[i]
					ans -= -1
					if len(a) == 0:
						return ans
					if i == len(a) or a[i] != buff:
						break
				break
	return ans
(n, k, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(len(a) + 1):
	buffa = a.copy()
	buffa.insert(i, x)
	buff = tryIt(buffa)
	if ans < buff:
		ans = buff
if ans > 0:
	ans += -1
print(ans)


(n, k, x) = input().split(' ')
n = int(n)
balls = [int(x) for x in input().split(' ')]
ans = 0
for k in range(len(balls)):
	i = k
	j = i + 1
	cnt = 1
	cur = int(x)
	while True:
		pre_i = i
		pre_j = j
		while i >= 0 and balls[i] == cur:
			i -= 1
			cnt += 1
		while j < n and balls[j] == cur:
			j += 1
			cnt += 1
		if cnt <= 2:
			i = pre_i
			j = pre_j
			break
		else:
			cur = balls[i]
			cnt = 0
		ans = max(ans, j - i - 1)
print(ans)


(n_balls, n_colors, new_ball) = map(int, input().split())
balls = list(map(int, input().split()))
answer = 0
for i in range(n_balls):
	copy = balls[:]
	copy.insert(i, new_ball)
	while len(copy) > 2:
		size = len(copy)
		for j in range(2, len(copy)):
			if copy[j - 2] == copy[j - 1] and copy[j - 1] == copy[j]:
				cut = j + 1
				while cut < len(copy):
					if copy[j] != copy[cut]:
						break
					cut += 1
				copy = copy[:j - 2] + copy[cut:]
				break
		if len(copy) == size:
			break
	answer = max(answer, n_balls - len(copy))
print(answer)


import math

def check(p):
	i = 0
	prev = -1
	while len(p) != prev:
		prev = len(p)
		i = 0
		while i < len(p) - 2:
			if p[i] == p[i + 1] and p[i] == p[i + 2]:
				x = p[i]
				while i < len(p):
					if p[i] == x:
						p.pop(i)
					else:
						break
			else:
				i += 1
	return len(p)
(n, k, x) = map(int, input().split())
l = list(map(int, input().split()))
f = n
i = 0
p = 0
count = math.inf
for i in range(n - 1):
	if l[i] == x and l[i + 1] == x:
		p = []
		for j in range(i):
			p.append(l[j])
		p.append(x)
		for j in range(i, n):
			p.append(l[j])
		count = min(count, check(p))
if count >= n:
	print(0)
else:
	print(n - count)


(n, k, x) = map(int, input().split())
s = list(map(int, input().split()))

def fl(x, e, c):
	s = x[:e]
	s.append(c)
	s += x[e:]
	for j in range(55):
		for i in range(len(s) - 2):
			if s[i] == s[i + 1] == s[i + 2]:
				if i < len(s) - 3:
					if s[i] == s[i + 3]:
						del s[i]
				del s[i]
				del s[i]
				del s[i]
				break
	return len(s)
m = 0
for i in range(n):
	m = max(m, n - fl(s, i, x))
print(m)


(n, k, x) = map(int, input().split())
c = list(map(int, input().split()))
d = []
s = 1
t = c[0]
for i in range(1, n):
	if c[i] == t:
		s += 1
	else:
		d.append([t, s])
		t = c[i]
		s = 1
d.append([t, s])
q = len(d)
check = 0
ans = 0
for i in range(q):
	check = 0
	if d[i][0] == x:
		if d[i][1] + 1 == 3:
			check += 2
			(l, r) = (i - 1, i + 1)
			while l > -1 and r < q:
				if d[l][0] == d[r][0] and d[l][1] + d[r][1] >= 3:
					check += d[l][1] + d[r][1]
					l -= 1
					r += 1
				else:
					break
	ans = max(ans, check)
print(ans)


(n, k, x) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
res = 0
for i in range(n):
	cnt = 0
	if a[i] == x:
		curcol = x
		l = i
		r = i + 1
		while l >= 0 and a[l] == curcol:
			l -= 1
			cnt += 1
		while r < n and a[r] == curcol:
			r += 1
			cnt += 1
		if cnt >= 2:
			while True:
				count = 0
				if l >= 0:
					curcol = a[l]
				if r < n:
					curcol = a[r]
				while l >= 0 and a[l] == curcol:
					count += 1
					l -= 1
				while r < n and a[r] == curcol:
					count += 1
					r += 1
				if count > 2:
					cnt += count
				else:
					break
			if res < cnt:
				res = cnt
print(res)


import sys
input = sys.stdin.buffer.readline

def solution():
	(n, k, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	res = 0
	for i in range(n):
		t = arr.copy()
		t.insert(i, x)
		while len(t) > 2:
			curr_size = len(t)
			for j in range(2, len(t)):
				if not t[j - 2] == t[j - 1] == t[j]:
					continue
				p = j + 1
				while p < len(t) and t[j] == t[p]:
					p += 1
				t = t[:j - 2] + t[p:]
				break
			if len(t) == curr_size:
				break
		res = max(res, n - len(t))
	print(res)
solution()


from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')

def solve():
	(n, k, x) = mi()
	a = li()
	ans = 0
	x1 = []
	c = 1
	for i in range(1, n):
		if a[i] != a[i - 1]:
			if c > 2:
				ans += c
				c = 1
				continue
			x1.append([c, a[i - 1]])
			c = 1
		else:
			c += 1
	if c > 2:
		ans += c
	else:
		x1.append([c, a[n - 1]])
	n = len(x1)
	ans1 = 0
	for i in range(len(x1)):
		c = 0
		if x1[i][1] == x and x1[i][0] > 1:
			c += x1[i][0]
			l = i - 1
			r = i + 1
			while l >= 0 and r < n:
				if x1[l][1] == x1[r][1] and x1[l][0] + x1[r][0] > 2:
					c += x1[l][0] + x1[r][0]
					l -= 1
					r += 1
				else:
					break
			ans1 = max(ans1, c)
	print(ans + ans1)
solve()


(n, k, x) = input().split()
(n, k, x) = (int(n), int(k), int(x))
arr = input().split()
possib = []
npossib = 0

def explode(pos):
	start = pos - 1
	end = pos + 2
	soma = 0
	color = 0
	actualsoma = 0
	if start < 0 or end >= n or arr[start] != arr[end]:
		return 0
	else:
		color = arr[start]
	while start >= 0 and end < n:
		if arr[start] == arr[end] and arr[start] == color:
			actualsoma += 2
		elif arr[start] == arr[end]:
			if actualsoma > 2:
				soma += actualsoma
			else:
				return soma
			color = arr[start]
			actualsoma = 2
		elif arr[start] == arr[start + 1]:
			actualsoma += 1
			start -= 1
			continue
		elif arr[end] == arr[end - 1]:
			actualsoma += 1
			end += 1
			continue
		else:
			if actualsoma > 2:
				soma += actualsoma
			return soma
		start -= 1
		end += 1
	if start >= 0 and arr[start] == color:
		while start > 0:
			if arr[start] == color:
				actualsoma += 1
			else:
				if actualsoma > 2:
					soma += actualsoma
				else:
					break
				color = arr[start]
				actualsoma = 1
			start -= 1
	elif end < n and arr[end] == color:
		while end < n:
			if arr[end] == color:
				actualsoma += 1
			else:
				if actualsoma > 2:
					soma += actualsoma
				else:
					break
				color = arr[end]
				actualsoma = 1
			end += 1
	if actualsoma > 2:
		soma += actualsoma
	return soma
for i in range(n - 1):
	arr[i] = int(arr[i])
	if arr[i] == x and int(arr[i + 1]) == x:
		possib.append(i)
		npossib += 1
arr[n - 1] = int(arr[n - 1])
total = 0
for i in range(npossib):
	value = explode(possib[i]) + 2
	if value > total:
		total = value
print(total)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass

def pmat(A):
	for ele in A:
		print(ele, end='\n')
(n, k, x) = L()
A = L()
idx = []
for i in range(n):
	if A[i] == x and i - 1 not in idx:
		idx.append(i)
ans = 0
for pos in idx:
	B = A[:pos] + [x] + A[pos:]
	lprev = -1
	lcur = len(B)
	while lprev != len(B):
		i = 0
		lprev = len(B)
		while i < len(B):
			j = i
			while j < len(B) and B[i] == B[j]:
				j += 1
			if j - i >= 3:
				del B[i:j]
				break
			i += 1
	ans = max(ans, n - len(B))
print(ans)
endtime = time.time()


(n, k, x) = map(int, input().split())
arr = list(map(int, input().split()))

def collapse(arr):
	if len(arr) < 3:
		return 0
	start = 0
	currentNumber = arr[0]
	counter = 1
	for i in range(1, len(arr), 1):
		if i == len(arr) - 1 and arr[i] == currentNumber:
			counter += 1
		if i != len(arr) - 1 and arr[i] == currentNumber:
			counter += 1
		elif counter >= 3:
			if start != 0 and start != len(arr) - counter:
				return counter + collapse(arr[:start] + arr[start + counter:])
			else:
				return counter
		else:
			counter = 1
			start = i
			currentNumber = arr[i]
	return 0
currentMax = 0
for i in range(0, n - 1):
	if arr[i] == arr[i + 1] and arr[i] == x:
		maxDestroy = 0
		if i != 0 and i != n - 2:
			maxDestroy = collapse(arr[:i] + arr[i + 2:]) + 2
		currentMax = max(currentMax, maxDestroy)
print(currentMax)


(n, k, x) = map(int, input().split())
colors = list(map(int, input().split()))
groups = []
i = 0
while i < n:
	c = colors[i]
	nbC = 0
	while i < n and colors[i] == c:
		i += 1
		nbC += 1
	groups.append((c, nbC))
maxNbBallsDestroyed = 0
for (i, group) in enumerate(groups):
	if group[0] == x and group[1] >= 2:
		nbBallsDestroyed = group[1]
		past = i - 1
		future = i + 1
		while past >= 0 and future < len(groups) and (groups[past][0] == groups[future][0]) and (groups[past][1] + groups[future][1] >= 3):
			nbBallsDestroyed += groups[past][1] + groups[future][1]
			past -= 1
			future += 1
		maxNbBallsDestroyed = max(maxNbBallsDestroyed, nbBallsDestroyed)
print(maxNbBallsDestroyed)


def solve(a):
	for i in range(len(a) - 1):
		j = i + 1
		while j < len(a) and a[j] == a[i]:
			j += 1
		num = j - i
		if num >= 3:
			return num + solve(a[:i] + a[j:])
	return 0
(n, k, x) = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
best = 0
for i in range(len(c)):
	if c[i] == x:
		best = max(best, solve(c[:i] + [x] + c[i:]) - 1)
print(best)


import re
import inspect
from copy import copy
from sys import argv, exit

def rstr():
	return input()

def rint():
	return int(input())

def rints(splitchar=' '):
	return [int(i) for i in input().split(splitchar)]

def varnames(obj, namespace=globals()):
	return [name for name in namespace if namespace[name] is obj]

def pvar(var, override=False):
	prnt(varnames(var), var)

def prnt(*args, override=False):
	if '-v' in argv or override:
		print(*args)

def remove_split(l, i, j):
	prnt('Removed {} thru {}, {}'.format(i, j, l[i:j]))
	new_l = l[:i] + l[j:]
	prnt('New List: {}'.format(new_l))
	return new_l

def degrade(l, initial_size):
	i = 0
	color = 0
	removed = False
	for (j, x) in enumerate(l):
		if not color:
			color = x
			i = j
		if color != x:
			if j - i >= 3:
				l = remove_split(l, i, j)
				removed = True
			color = x
			i = j
		if j == len(l) - 1 and color == x and (j - i >= 2):
			prnt('removing {} {}'.format(i, j))
			l = remove_split(l, i, j + 1)
			removed = True
	if removed:
		prnt(l)
		return degrade(l, initial_size)
	else:
		return initial_size - len(l)

def __starting_point():
	(num, kolors, xolor) = rints()
	balls = rints()
	results = []
	for i in range(len(balls)):
		l = copy(balls)
		l.insert(i, xolor)
		pvar(l)
		results.append(degrade(l, len(l) - 1))
	prnt(results)
	ans = max(results)
	if ans == -1:
		ans = 0
	print(ans)
__starting_point()


n = 0
k = 0
x = 0
balls = []

def cntBall(pos):
	l = r = pos
	cnt = 0
	while balls[l] == balls[r]:
		tmp = 2
		while l > 0 and balls[l - 1] == balls[l]:
			l -= 1
			tmp += 1
		while r < n - 1 and balls[r + 1] == balls[r]:
			r += 1
			tmp += 1
		l -= 1
		r += 1
		if tmp < 3:
			break
		cnt += tmp
		if l < 0 or r >= n:
			break
	return cnt - 1
(n, k, x) = map(int, input().split())
balls = list(map(int, input().split()))
res = 0
for i in range(n):
	if balls[i] == x:
		res = max(res, cntBall(i))
print(res)


(n, k, x) = map(int, input().split(' '))
c = list(map(int, input().split(' ')))
row = 0
row_x = 0
answer = 0
for e in range(n):
	if c[e] == x and e != n - 1:
		if c[e + 1] == x:
			ans = 0
			l = e - 1
			if l < 0:
				answer = max(3, answer)
				continue
			r = e + 2
			if r > n - 1:
				answer = max(3, answer)
				continue
			rem = x
			count = 3
			while True:
				if r > n - 1:
					if l >= 0 and c[l] == rem:
						count += 1
					ans += count
					count = 0
					l -= 1
					answer = max(ans, answer)
					break
				elif l < 0:
					if r <= n - 1 and c[r] == rem:
						count += 1
					ans += count
					count = 0
					r += 1
					answer = max(ans, answer)
					break
				if c[l] == c[r]:
					if c[l] != rem and count > 2:
						rem = c[l]
						l -= 1
						r += 1
						ans += count
						count = 2
					elif c[l] != rem and count < 3:
						answer = max(ans, answer)
						break
					else:
						rem = c[l]
						l -= 1
						r += 1
						count += 2
				elif c[l] == rem:
					count += 1
					l -= 1
				elif c[r] == rem:
					count += 1
					r += 1
				else:
					if count > 2:
						ans += count
					answer = max(ans, answer)
					break
if answer - 1 < 0:
	print(0)
else:
	print(answer - 1)


(n, k, x) = map(int, input().split())
balls = list(map(int, input().split()))
count = 0
for i in range(1, n - 1):
	curr_color = x
	curr_count = 0
	if balls[i] == curr_color and balls[i - 1] == curr_color:
		curr_count = 2
		li = i - 2
		ri = i + 1
		lf = li
		rf = ri
		while lf >= 0 and rf < n and (balls[lf] == balls[rf]):
			curr_color = balls[lf]
			while lf >= 0 and balls[lf] == curr_color:
				lf -= 1
			while rf < n and balls[rf] == curr_color:
				rf += 1
			if li - lf + (rf - ri) < 3:
				break
			curr_count += li - lf + (rf - ri)
			li = lf
			ri = rf
	count = max(count, curr_count)
print(count)


(n, k, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	if a[i] == x:
		l = r = i
		cnt = 0
		while a[l] == a[r]:
			balls = 2
			while l > 0 and a[l - 1] == a[l]:
				l -= 1
				balls += 1
			while r < n - 1 and a[r + 1] == a[r]:
				r += 1
				balls += 1
			if balls < 3:
				break
			cnt += balls
			l -= 1
			r += 1
			if l < 0 or r >= n:
				break
		ans = max(ans, cnt - 1)
print(ans)


(n, k, x) = input().split()
t = input().split() + ['a', 'b']
(u, q) = (-2, 1)
for v in range(int(n) + 1):
	if x == t[v]:
		if u == -2:
			u = v - 1
	elif u != -2:
		(s, i, j, y) = (0, u, v, t[v])
		while j - i - s > 2:
			s = j - i
			while t[i] == y:
				i -= 1
			while t[j] == y:
				j += 1
			y = t[j]
		(u, q) = (-2, max(q, s))
print(q - 1)


def can_destroy(A):
	color_now = A[0]
	num_now = 1
	for i in range(1, len(A)):
		if color_now == A[i]:
			num_now += 1
			if num_now == 3:
				return i - 2
		else:
			color_now = A[i]
			num_now = 1
	return -1

def destroy(A):
	while len(A) > 0 and can_destroy(A) != -1:
		i = can_destroy(A)
		c = A[i]
		while i < len(A) and A[i] == c:
			A.pop(i)
	return A
(n, k, x) = map(int, input().split())
A = list(map(int, input().split()))
res = 0
for i in range(len(A) + 1):
	B = A[:]
	B.insert(i, x)
	B = destroy(B)[:]
	res = max(res, len(A) - len(B))
print(res)


def check(a, p, x):
	a.insert(p, x)
	a = a + [101]
	found = True
	ans = 0
	while found:
		found = False
		cnt = 1
		l = 0
		r = 1
		for i in range(1, len(a)):
			if a[l] == a[i]:
				cnt += 1
				r = i + 1
			elif cnt > 2:
				found = True
				a = a[:l] + a[r:]
				ans += r - l
				break
			else:
				cnt = 1
				l = i
				r = i + 1
	return ans - 1

def main():
	(n, k, x) = map(int, input().split())
	c = list(map(int, input().split()))
	ans = 0
	for i in range(n - 1):
		a = c[:]
		ans = max(ans, check(a, i, x))
	print(ans)
main()


(arrlen, _, color) = input().split()
arrlen = int(arrlen)
arr = input().split()
index = 0
ans = 0
currentAns = 0
while index < arrlen:
	if arr[index] == color:
		if index == 0:
			while index < arrlen and arr[index] == color:
				currentAns = currentAns + 1
				index = index + 1
			if currentAns > 1:
				ans = max(ans, currentAns)
			currentAns = 0
			continue
		leftPos = index - 1
		while index < arrlen and arr[index] == color:
			currentAns = currentAns + 1
			index = index + 1
		if index == arrlen:
			if currentAns > 1:
				ans = max(ans, currentAns)
				currentAns = 0
			continue
		if currentAns > 1:
			rightPos = index
			while True:
				if leftPos >= 0 and rightPos < arrlen and (arr[leftPos] == arr[rightPos]):
					count = 2
					while leftPos - 1 >= 0 and arr[leftPos] == arr[leftPos - 1]:
						leftPos = leftPos - 1
						count = count + 1
					while rightPos + 1 < arrlen and arr[rightPos] == arr[rightPos + 1]:
						rightPos = rightPos + 1
						count = count + 1
					if count > 2:
						currentAns = currentAns + count
						leftPos = leftPos - 1
						rightPos = rightPos + 1
					else:
						break
				else:
					break
		else:
			currentAns = 0
		ans = max(ans, currentAns)
		currentAns = 0
	index = index + 1
print(ans)


def estoura(i, cor):
	atual = cor
	cont = 0
	cont2 = 1
	coresq = lista[i]
	cordir = lista[i + 1]
	(IE, ID) = (i, i + 1)
	while True:
		while coresq == atual:
			cont2 += 1
			cont += 1
			IE -= 1
			if IE < 1:
				break
			coresq = lista[IE]
		while cordir == atual:
			cont2 += 1
			cont += 1
			ID += 1
			if ID > n + 1:
				break
			cordir = lista[ID]
		if cont2 < 3:
			cont -= cont2
		if coresq != cordir or cont2 < 3:
			break
		cont2 = 0
		atual = coresq
	return cont
(n, k, x) = map(int, input().split())
lista = [k + 5]
lista.extend(list(map(int, input().split())))
lista.append(k + 5)
atual = 0
for i in range(1, n + 1):
	if lista[i] == lista[i + 1] and lista[i] != lista[i - 1] and (lista[i] == x):
		atual = max(estoura(i, x), atual)
print(atual)


(n, k, x) = map(int, input().split())
balls = list(map(int, input().split()))
i = 0
ans = 0
for i in range(len(balls)):
	if balls[i] is x:
		l = r = i
		cnt = 0
		while balls[l] == balls[r]:
			tmp = 2
			while l > 0 and balls[l - 1] == balls[l]:
				l -= 1
				tmp += 1
			while r < n - 1 and balls[r + 1] == balls[r]:
				r += 1
				tmp += 1
			l -= 1
			r += 1
			if tmp < 3:
				break
			cnt += tmp
			if l < 0 or r >= n:
				break
		if cnt - 1 > ans:
			ans = cnt - 1
print(ans)


(n, k, x) = map(int, input().split())
c = list(map(int, input().split()))
insert_pos = []
balls = []
i = 0
while i < n:
	j = 1
	while i + j < n and c[i + j] == c[i]:
		j += 1
	if c[i] == x and j == 2:
		insert_pos.append(len(balls))
	balls.append((c[i], j))
	i = i + j
max_balls = 0
for pos in insert_pos:
	count_balls = 2
	i = pos - 1
	j = pos + 1
	while i >= 0 and j < len(balls):
		if balls[i][0] == balls[j][0] and balls[i][1] + balls[j][1] >= 3:
			count_balls += balls[i][1] + balls[j][1]
			i -= 1
			j += 1
		else:
			break
	max_balls = max(max_balls, count_balls)
print(max_balls)


def count(balls):
	stop = False
	ans = len(balls)
	m = len(balls)
	while not stop and m > 0:
		elms2remove = set()
		i = 2
		stop = True
		m = len(balls)
		while i < len(balls):
			if balls[i] == balls[i - 1] == balls[i - 2]:
				elms2remove |= {i, i - 1, i - 2}
				stop = False
				while i + 1 < m and balls[i + 1] == balls[i]:
					elms2remove.add(i + 1)
					i += 1
				balls = [b for (i, b) in enumerate(balls) if i not in elms2remove]
				break
			i += 1
	return ans - len(balls) - 1

def main():
	(n, _k, x) = map(int, input().split())
	balls = [int(c) for c in input().split()]
	ans = 0
	i = 0
	while i < n - 1:
		if balls[i] == x and balls[i + 1] == x:
			conf = balls[:i]
			conf.append(x)
			conf += balls[i:]
			ans = max(ans, count(conf))
			while i < n - 1 and balls[i] == x:
				i += 1
		i += 1
	print(ans)
main()


n = list(map(int, input().split()))
s = list(map(int, input().split()))
m = 0
for i in range(n[0]):
	s1 = s[:i]
	s1.append(n[2])
	s1 += s[i:]
	for j in range(100):
		for k in range(len(s1) - 2):
			if s1[k] == s1[k + 1] == s1[k + 2]:
				if k < len(s1) - 3:
					if s1[k] == s1[k + 3]:
						for p in range(4):
							del s1[k]
					else:
						for p in range(3):
							del s1[k]
				else:
					for p in range(3):
						del s1[k]
				break
	if len(s) - len(s1) > m:
		m = len(s) - len(s1)
print(m)


import sys
lines = sys.stdin.readlines()
(n, k, x) = map(int, lines[0].strip().split(' '))
nums = list(map(int, lines[1].strip().split(' ')))
maxD = 0
count = []
for n in nums:
	if not count:
		count.append((n, 1))
		continue
	if n == count[-1][0]:
		count[-1] = (count[-1][0], 1 + count[-1][1])
	else:
		count.append((n, 1))
for i in range(len(count)):
	c = count[i]
	if c[0] == x and c[1] >= 2:
		stack = count[:i]
		destory = count[i][1]
		for j in range(i + 1, len(count)):
			if not stack:
				break
			if count[j][0] == stack[-1][0] and count[j][1] + stack[-1][1] >= 3:
				destory += count[j][1] + stack[-1][1]
				stack.pop()
			else:
				break
		maxD = max(maxD, destory)
print(maxD)


(n, k, x) = list(map(int, input().split()))

def lr_bound(c, i, x):
	l = i
	r = i
	while l > 0 and c[l - 1] == x:
		l -= 1
	while r < len(c) - 1 and c[r + 1] == x:
		r += 1
	return (l, r)

def destroy(c, i, x):
	c = c[:]
	c.insert(i, x)
	dest = 0
	while len(c) > 2:
		if i >= len(c):
			i = len(c) - 1
		if i < 0:
			i = 0
		(l, r) = lr_bound(c, i, c[i])
		if r - l >= 2:
			c[l:r + 1] = []
			dest += r - l + 1
			i = l
		else:
			break
	return dest - 1
c = list(map(int, input().split()))
scores = []
for i in range(len(c)):
	scores.append(destroy(c, i, x))
print(max([0] + scores))


(n, k, x) = map(int, input().split())
balls = list(map(int, input().split()))
answer = 0
for i in range(n):
	copy = balls[:]
	copy.insert(i, x)
	while len(copy) > 2:
		size = len(copy)
		for j in range(2, len(copy)):
			if copy[j - 2] == copy[j - 1] and copy[j - 1] == copy[j]:
				cut = j + 1
				while cut < len(copy):
					if copy[j] != copy[cut]:
						break
					cut += 1
				copy = copy[:j - 2] + copy[cut:]
				break
		if len(copy) == size:
			break
	answer = max(answer, n - len(copy))
print(answer)


import sys
import math
import random
(n, k, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
if n == 1:
	print(0)
	sys.exit()
for i in range(0, n - 1):
	if a[i] == a[i + 1] == x:
		l = i
		while l >= 0 and a[i] == a[l]:
			l -= 1
		r = i + 1
		while r < n and a[r] == x:
			r += 1
		while 1:
			if l == -1 or r == n:
				break
			if a[l] != a[r]:
				break
			if l == 0 and r == n - 1:
				break
			if l == 0:
				if a[0] == a[r + 1]:
					while r < n and a[r] == a[0]:
						r += 1
					l -= 1
					continue
				else:
					break
			if r == n - 1:
				if a[n - 1] == a[l - 1]:
					while l >= 0 and a[l] == a[n - 1]:
						l -= 1
					r += 1
					continue
				else:
					break
			if a[l] == a[r + 1] or a[l - 1] == a[r]:
				t = a[l]
				while l >= 0 and a[l] == t:
					l -= 1
				while r < n and a[r] == t:
					r += 1
			else:
				break
		ans = max(ans, r - l - 1)
print(ans)


(n, k, x) = input().split()
(n, k) = (int(n), int(k))
balls = input().split()
ans = 0
for i in range(n):
	s = balls.copy()
	s.insert(i, x)
	while len(s) > 2:
		l = len(s)
		for j in range(2, len(s)):
			if s[j - 2] == s[j - 1] == s[j]:
				p = j + 1
				while p < len(s):
					if s[j] != s[p]:
						break
					p += 1
				s = s[:j - 2] + s[p:]
				break
		if len(s) == l:
			break
	ans = max(ans, n - len(s))
print(ans)


(n, k, x) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
m = 0

def scan(l, m):
	if len(l) == 0:
		return (l, m)
	mstart = 0
	mle = 1
	start = 0
	col = l[0]
	le = 1
	for i in range(1, len(l)):
		if l[i] == col:
			le += 1
		else:
			if le > mle:
				mle = le
				mstart = start
			col = l[i]
			start = i
			le = 1
	if le > mle:
		mle = le
		mstart = start
	if mle < 3:
		return (l, m)
	m += mle
	l = l[:mstart] + l[mstart + mle:]
	return (l, m)
for i in range(len(l) + 1):
	l2 = l[:]
	m2 = -1
	l2 = l2[:i] + [x] + l2[i:]
	while True:
		(l2, m3) = scan(l2, m2)
		if m2 == m3:
			break
		m2 = m3
	m = max(m, m2)
print(m)


import copy

def list_sum(matrix):
	result = []
	aux = matrix[0]
	for x in range(1, len(matrix)):
		if aux[0] == matrix[x][0]:
			aux[1] += matrix[x][1]
		else:
			result.append(aux)
			aux = matrix[x]
	else:
		result.append(aux)
	return result

def verif(posib, matrix):
	del matrix[posib]
	matrix = list_sum(matrix)
	print(matrix)
	while True:
		pos = ispos(matrix)
		if len(pos) == 0:
			break
		else:
			for x in pos:
				del matrix[x]
			matrix = list_sum(matrix)
		print(matrix)
	return matrix

def ispos(matrix):
	posibilidades = []
	for x in range(len(matrix)):
		if matrix[x][1] >= 3:
			posibilidades.append(x)
	return posibilidades
(n, colors, boll) = [int(x) for x in input().split(' ')]
lista = [int(x) for x in input().split(' ')]
soma = []
aux = [lista[0], 1]
for x in range(1, n):
	if lista[x] == aux[0]:
		aux[1] += 1
	else:
		soma.append(aux)
		aux = [lista[x], 1]
else:
	soma.append(aux)
posibilidades = []
for x in range(len(soma)):
	if soma[x][0] == boll and soma[x][1] >= 2:
		posibilidades.append(x)
if len(posibilidades) == 0:
	print('0')
else:
	result = 0
	for x in posibilidades:
		matrix = copy.deepcopy(soma)
		del matrix[x]
		matrix = list_sum(matrix)
		while True:
			pos = ispos(matrix)
			if len(pos) == 0:
				break
			else:
				for i in pos:
					del matrix[i]
				if len(matrix) > 0:
					matrix = list_sum(matrix)
		aux = 0
		for j in matrix:
			aux += j[1]
		aux = n - aux
		if aux > result:
			result = aux
	print(result)


def getBolasDestruidas(pos, valores):
	aux = [valor for valor in valores]
	lenInicial = len(valores)
	aux.pop(pos)
	aux.pop(pos)
	while True:
		toPop = []
		for i in range(len(aux) - 2):
			if len(toPop) != 0:
				break
			if aux[i] == aux[i + 1] and aux[i + 1] == aux[i + 2]:
				toPop.append(i)
				toPop.append(i + 1)
				toPop.append(i + 2)
				j = i + 3
				if j >= len(aux):
					break
				while aux[j] == aux[i]:
					toPop.append(j)
					j += 1
					if j >= len(aux):
						break
		if len(toPop) != 0:
			toPop.sort(reverse=True)
			for i in toPop:
				aux.pop(i)
		else:
			break
	return lenInicial - len(aux)

def getIndexs(bolaX, valores):
	i = 0
	ret = []
	while True:
		while valores[i] != bolaX:
			i += 1
			if i == len(valores):
				return ret
		if i + 1 >= len(valores):
			return ret
		if valores[i + 1] == bolaX:
			ret.append(i)
		else:
			if i + 1 == len(valores):
				return ret
			if valores[i + 1] == bolaX:
				i += 1
			if i >= len(valores):
				return ret
		while valores[i] == bolaX:
			i += 1
			if i >= len(valores):
				return ret
	return ret
entrada = input().split()
valores = input().split()
bolaX = int(entrada[2])
valores = [int(valor) for valor in valores]
lista = getIndexs(bolaX, valores)
res = []
for i in lista:
	res.append(getBolasDestruidas(i, valores))
if len(res) == 0:
	print(0)
else:
	print(max(res))


def chk(o):
	flag = 1
	visited = set()
	while flag:
		prev = -1
		cnt = 0
		z = []
		flag = 0
		for i in range(len(o)):
			x = o[i]
			if x == prev:
				z.append(i)
				if cnt == 0:
					z.append(i - 1)
					cnt += 1
				cnt += 1
			else:
				if x != prev:
					prev = x
					if cnt >= 3:
						for r in z:
							visited.add(r)
					z = []
					cnt = 0
					continue
				prev = x
		if cnt >= 3:
			for r in z:
				visited.add(r)
		tarray = []
		for i in range(len(o)):
			if i in visited:
				flag = 1
				continue
			tarray.append(o[i])
		o = tarray
		visited = set()
	return len(o)
for _ in range(1):
	(n, k, x) = map(int, input().split())
	l = list(map(int, input().split()))
	ans = 10 ** 18
	for i in range(n + 1):
		e1 = l[0:i]
		e2 = [x]
		e3 = l[i:n]
		o = e1 + e2 + e3
		ans = min(chk(o), ans)
	if n - ans < 0:
		print(0)
		continue
	print(n - ans)


import os, io
from sys import stdout
import collections
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def primes(n):
	sieve = [True] * n
	for i in range(3, int(n ** 0.5) + 1, 2):
		if sieve[i]:
			sieve[i * i::2 * i] = [False] * ((n - i * i - 1) // (2 * i) + 1)
	return [2] + [i for i in range(3, n, 2) if sieve[i]]

def binomial_coefficient(n, k):
	if 0 <= k <= n:
		ntok = 1
		ktok = 1
		for t in range(1, min(k, n - k) + 1):
			ntok *= n
			ktok *= t
			n -= 1
		return ntok // ktok
	else:
		return 0

def powerOfK(k, max):
	if k == 1:
		return [1]
	if k == -1:
		return [-1, 1]
	result = []
	n = 1
	while n <= max:
		result.append(n)
		n *= k
	return result

def divisors(n):
	i = 1
	result = []
	while i * i <= n:
		if n % i == 0:
			if n / i == i:
				result.append(i)
			else:
				result.append(i)
				result.append(n / i)
		i += 1
	return result

def digitsSum(n):
	if n == 0:
		return 0
	r = 0
	while n > 0:
		r += n % 10
		n //= 10
	return r

def isValid(i, j, n, m):
	return i >= 0 and i < n and (j >= 0) and (j < m)

def print_grid(grid):
	for line in grid:
		print(' '.join(map(str, line)))

def kadane(a, size):
	max_so_far = 0
	max_ending_here = 0
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far

def prefixSum(arr):
	for i in range(1, len(arr)):
		arr[i] = arr[i] + arr[i - 1]
	return arr

def ceil(n, d):
	if n % d == 0:
		return n // d
	else:
		return n // d + 1
(n, k, x) = list(map(int, input().split()))
l = list(map(int, input().split()))
stack = []
acc = []
p = 1
for i in range(1, len(l)):
	if l[i] == l[i - 1]:
		p += 1
	else:
		acc.append((p, l[i - 1]))
		p = 1
if p > 0:
	acc.append((p, l[-1]))

def solve(stack, acc, index):
	stack = acc[:index]
	index = index + 1
	total = 0
	while len(stack) and index < len(acc):
		(pnb, pc) = stack.pop()
		(nnb, nc) = acc[index]
		if pc == nc and pnb + nnb >= 3:
			total += pnb + nnb
		else:
			break
		index += 1
	return total
stack = []
r = 0
for (index, value) in enumerate(acc):
	(number, color) = value
	if color == x and number + 1 >= 3:
		r = max(r, number + solve(stack, acc, index))
print(r)


def elimina(lista, left, right):
	contador = 0
	passou = True
	tmp = 0
	while passou == True:
		passou = False
		contador += tmp
		tmp = 0
		if left >= 0 and right < len(lista):
			if lista[left] == lista[right]:
				tmp += 2
				if lista[left - 1] == lista[left]:
					contador += 1
					left -= 2
					passou = True
				else:
					left -= 1
				if right < len(lista) - 1:
					if lista[right + 1] == lista[right]:
						contador += 1
						right += 2
						passou = True
					else:
						right += 1
	if contador > 2:
		return contador + 2
	return 2
entrada = list(map(int, input().split()))
balls = list(map(int, input().split()))
saida = [0]
for i in range(entrada[0]):
	if i != entrada[0] - 1:
		if balls[i] == entrada[2] and balls[i + 1] == entrada[2]:
			saida.append(elimina(balls, i - 1, i + 2))
print(max(saida))


(n, m, k) = map(int, input().split())
lis = list(map(int, input().split())) + [0]
pre = cur = c = ans = 0
aa = []
c = 1
for i in range(1, n + 1):
	if lis[i] == lis[i - 1]:
		c += 1
	else:
		aa.append([lis[i - 1], c])
		c = 1
aa = [[0, 0]] + aa + [[0, 0]]
l = len(aa)
for i in range(1, l):
	cur = 0
	if aa[i][0] == k:
		if aa[i][1] == 2:
			cur += 2
			j = 1
			while i + j < l and i - j >= 0 and (aa[i - j][1] + aa[i + j][1] >= 3) and (aa[i - j][0] == aa[i + j][0]):
				cur += aa[i - j][1] + aa[j + i][1]
				j += 1
	ans = max(ans, cur)
print(ans)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
MOD = 1000000007

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def Answer(key):
	left = a[:key + 1] + [x]
	left = left[::-1]
	right = a[key + 1:]
	ans = 0
	i = 0
	j = 0
	while i < len(left) and j < len(right):
		if left[i] != right[j]:
			break
		i += 1
		c = 2
		while i < len(left) and left[i] == left[i - 1]:
			i += 1
			c += 1
		j += 1
		while j < len(right) and right[j] == right[j - 1]:
			j += 1
			c += 1
		if c < 3:
			break
		ans += c
	return ans - 1
(n, k, x) = value()
a = array()
ans = 0
for i in range(n):
	ans = max(Answer(i), ans)
print(ans)


(n, k, x) = list(map(int, input().split()))
r = list(map(int, input().split()))

def destroy(v):
	if len(v) < 3:
		return 0
	d = 0
	while True:
		changed = False
		for i in range(len(v) - 2):
			if v[i] == v[i + 1] == v[i + 2]:
				d += 3
				if i + 3 < len(v) and v[i + 3] == v[i]:
					d += 1
					del v[i:i + 4]
				else:
					del v[i:i + 3]
				changed = True
				break
		if not changed:
			break
	return d

def solve():
	sol = 0
	for i in range(len(r) + 1):
		r2 = r[:i] + [x] + r[i:]
		sol = max(sol, destroy(r2) - 1)
	return sol
print(solve())


def destroy_balls(_a, _b, _x):
	if len(_a) == 0 or len(_b) == 0:
		return 0
	lenb = len(_b)
	a_cur_index = len(_a) - 1
	b_cur_index = 0
	if _a[a_cur_index] == _b[b_cur_index]:
		to_be_destroyed = 2
		a_cur_index -= 1
		b_cur_index += 1
		while a_cur_index >= 0:
			if _a[a_cur_index] == _x:
				to_be_destroyed += 1
				a_cur_index -= 1
			else:
				break
		while b_cur_index < lenb:
			if _b[b_cur_index] == _x:
				to_be_destroyed += 1
				b_cur_index += 1
			else:
				break
		if to_be_destroyed > 2:
			return to_be_destroyed + destroy_balls(_a[0:a_cur_index + 1], _b[b_cur_index:], _a[a_cur_index])
		else:
			return 0
	return 0
(n, k, x) = map(int, input().split())
c = list(map(int, input().split()))
cur_max = 0
for i in range(n - 1):
	if c[i] == x and c[i] == c[i + 1]:
		tmp_a = c[0:i + 1]
		tmp_b = c[i + 1:n]
		tmp_a.append(x)
		i += 1
		tmp_max = destroy_balls(tmp_a, tmp_b, x)
		if tmp_max > cur_max:
			cur_max = tmp_max
if cur_max >= 2:
	cur_max -= 1
else:
	cur_max = 0
print(cur_max)


import copy

def main():
	(n, x, k) = list(map(int, input().strip().split(' ')))
	balls = list(map(int, input().strip().split(' ')))
	original_balls = copy.deepcopy(balls)
	flag = 1
	maxi = 0
	for i in range(len(balls)):
		balls = copy.deepcopy(original_balls)
		if i + 1 < len(balls):
			if balls[i] == balls[i + 1] and balls[i] == k:
				new_balls = balls[:i + 1] + [k] + balls[i + 1:]
				flag = 1
				while flag != 0:
					balls = new_balls
					new_balls = []
					length = len(balls)
					flag = 0
					for i in range(len(balls)):
						if i + 2 < length and balls[i] == balls[i + 1] and (balls[i + 1] == balls[i + 2]):
							curr = i + 2
							while curr + 1 < length and balls[i] == balls[curr + 1]:
								curr += 1
							new_balls = balls[:i] + balls[curr + 1:]
							flag = 1
							break
				maxi = max(maxi, len(original_balls) - len(balls))
	print(maxi)
main()


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())
(z, n, c) = (None, None, None)

def DOIT(l, r):
	if l > 0 and r < n:
		ll = l - 1
		rr = r
		x = c[ll]
		if x == c[rr]:
			while ll > 0 and x == c[ll - 1]:
				ll -= 1
			while rr + 1 < n and x == c[rr + 1]:
				rr += 1
			if l - ll + (rr - r + 1) > 2:
				return l - ll + (rr - r + 1) + DOIT(ll, rr + 1)
	return 0
(n, k, x) = mints()
z = [[None] * (n + 1) for i in range(n + 1)]
c = list(mints())
r = 0
i = 0
while i < n:
	j = i + 1
	while j < n and c[j] == c[i]:
		j += 1
	if x == c[i] and j - i > 1:
		r = max(r, DOIT(i, j) + j - i)
	i = j
print(r)


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')
from collections import Counter
import heapq
from collections import Counter
(n, k, x) = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
for i in range(n):
	if arr[i] == x:
		l = r = i
		t = 0
		while arr[l] == arr[r]:
			var = 2
			while l > 0 and arr[l - 1] == arr[l]:
				l -= 1
				var += 1
			while r + 1 < n and arr[r + 1] == arr[r]:
				r += 1
				var += 1
			if var < 3:
				break
			t += var
			l -= 1
			r += 1
			if l < 0 or r >= n:
				break
		ans = max(ans, t - 1)
print(ans)


inp = [int(i) for i in input().split(' ')]
n = inp[0]
k = inp[1]
cor = inp[2]
ballsOri = [int(i) for i in input().split(' ')]
max_estouradas = 0
for i in range(n):
	balls = ballsOri.copy()
	balls.insert(i, cor)
	modificado = True
	while modificado:
		modificado = False
		index = 0
		while index < len(balls):
			qtd_igual = 1
			anterior = balls[index]
			j = index + 1
			while j < len(balls):
				if balls[j] == anterior:
					qtd_igual += 1
				else:
					break
				j += 1
			if qtd_igual >= 3:
				for j in range(0, qtd_igual):
					balls.pop(index)
				modificado = True
			index += 1
	estouradas = n - len(balls)
	if max_estouradas < estouradas:
		max_estouradas = estouradas
print(max_estouradas)


(n, k, x) = map(int, input().split())
balls = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
	if balls[i] == x and balls[i + 1] == x:
		curr = 2
		temp = balls[:i] + balls[i + 2:]
		while True:
			j = 0
			found = False
			while j < len(temp) - 1:
				k = j + 1
				while k < len(temp) and temp[k] == temp[j]:
					k += 1
				if k - j >= 3:
					curr += k - j
					temp = temp[:j] + temp[k:]
					found = True
					break
				j += 1
			if not found:
				break
		ans = max(ans, curr)
print(ans)


def busted_balls(balls, idx):
	busted = 2
	i = idx - 1
	j = idx + 2
	while i >= 0 and j < len(balls) and (balls[i] == balls[j]):
		while i - 1 >= 0 and balls[i] == balls[i - 1]:
			i -= 1
		while j + 1 < len(balls) and balls[j] == balls[j + 1]:
			j += 1
		if j - i + 1 - busted >= 3:
			busted = j - i + 1
		else:
			break
		i -= 1
		j += 1
	return busted
(n, k, x) = list(map(int, input().split()))
balls = list(map(int, input().split()))
best = 0
for i in range(1, n):
	if balls[i] == balls[i - 1] and balls[i] == x:
		res = busted_balls(balls, i - 1)
		if res > best:
			best = res
print(best)


(n, k, x) = [int(x) for x in input().split()]
ball = [int(x) for x in input().split()]
destroyed = []
element = 0
go = True
if n == 1:
	maximum_destroyed = 0
else:
	for i in range(n - 1):
		j = i + 1
		if x == ball[i] and ball[i] == ball[j]:
			destroyed.append(2)
			while i > 0 and j < n - 1:
				go = True
				i -= 1
				j += 1
				j_exist = j + 1 < n
				i_exist = i - 1 > -1
				if j_exist and i_exist and (ball[i] == ball[i - 1]) and (ball[i] == ball[j]) and (ball[j] == ball[j + 1]):
					destroyed[element] += 4
					i -= 1
					j += 1
				elif j_exist and ball[i] == ball[j] and (ball[j] == ball[j + 1]):
					destroyed[element] += 3
					i -= 0
					j += 1
				elif i_exist and ball[i] == ball[i - 1] and (ball[i] == ball[j]):
					destroyed[element] += 3
					i -= 1
					j += 0
				else:
					break
			element += 1
	if len(destroyed) == 0:
		maximum_destroyed = 0
	else:
		maximum_destroyed = max(destroyed)
print(maximum_destroyed)


def recur(l, i, b):
	if l == []:
		return 0
	if i + 1 >= len(l):
		return 0
	while l[i] != l[i + 1]:
		i += 1
		if i + 1 >= len(l):
			return 0
	if l[i] == b:
		c = l.copy()
		del l[i]
		del l[i]
		return max(recur(c, i + 2, b), 2 + recur(l, 0, -1))
	else:
		if i + 2 < len(l) and l[i + 2] == l[i + 1]:
			pt = 0
			p = l[i]
			while i < len(l) and l[i] == p:
				pt += 1
				del l[i]
				if l == []:
					break
			return pt + recur(l, 0, b)
		return recur(l, i + 2, b)
(n, b, k) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
print(recur(l, 0, k))


def destroy_balls(b):
	l = b.copy()
	seq = 0
	start = 0
	largest = 0
	largest_start = 0
	for i in range(1, len(l)):
		if l[i] == l[i - 1]:
			if seq == 0:
				start = i - 1
			seq += 1
			if seq > largest:
				largest = seq
				largest_start = start
		else:
			seq = 0
	if largest >= 2:
		for i in range(largest + 1):
			l.pop(largest_start)
	return l
(n, k, x) = map(int, input().split())
balls = list(map(int, input().split()))
original_len = len(balls)
seq = 0
start = 0
larg = []
for i in range(1, len(balls)):
	if balls[i] == balls[i - 1] == x:
		if seq == 0:
			start = i - 1
		seq += 1
		if seq >= 1:
			larg.append(start)
	else:
		seq = 0
res = []
for p in larg:
	b = balls.copy()
	b.insert(p, x)
	d = destroy_balls(b)
	while d != destroy_balls(d):
		d = destroy_balls(d)
	res.append(original_len - len(d))
try:
	print(max(res))
except:
	print(0)


def solve(n, k, x, c):
	res = 0
	for i in range(n - 1):
		if c[i] == c[i + 1] == x:
			temp = c[:i] + c[i + 2:]
			while True:
				flag = True
				for l in range(len(temp) - 1):
					r = l + 1
					while r < len(temp) and temp[r] == temp[l]:
						r += 1
					if r - l >= 3:
						temp = temp[:l] + temp[r:]
						flag = False
						break
				if flag == True:
					break
			res = max(res, n - len(temp))
	return res
(n, k, x) = map(int, input().split())
c = list(map(int, input().split()))
ans = solve(n, k, x, c)
print(ans)


def do(l, r):
	li = 0
	ri = 0
	nl = len(l)
	nr = len(r)
	l.append(-1)
	r.append(-1)
	x = 0
	while l[li] > 0 and r[ri] > 0:
		a = 0
		c = l[li]
		while l[li] == c:
			a += 1
			li += 1
		while r[ri] == c:
			a += 1
			ri += 1
		if a < 3:
			return li + ri - a - x
		x = 1
	return li + ri - x

def s():
	[n, k, x] = list(map(int, input().split()))
	c = list(map(int, input().split()))
	res = 0
	for i in range(n):
		l = [x]
		l.extend(list(reversed(c[:i])))
		res = max(do(l, c[i:]), res)
	print(res)
s()


(n, k, x) = map(int, input().split())
a = list(map(int, input().split())) + [k + 1]
factor = []
insert_pos = []
continuos_ball = 1
for i in range(1, n + 1):
	if a[i] == a[i - 1]:
		continuos_ball += 1
	else:
		if a[i - 1] == x and continuos_ball >= 2:
			insert_pos.append(len(factor))
		factor.append((a[i - 1], continuos_ball))
		continuos_ball = 1
max_destroy = 0
for pos in insert_pos:
	n_destroy = factor[pos][1]
	(i, j) = (pos - 1, pos + 1)
	while i >= 0 and j < len(factor):
		if factor[i][0] != factor[j][0] or factor[i][1] + factor[j][1] < 3:
			break
		n_destroy += factor[i][1] + factor[j][1]
		i -= 1
		j += 1
	max_destroy = max(max_destroy, n_destroy)
print(max_destroy)


(n, k, x) = map(int, input().split())
arr = list(map(int, input().split()))

def collapse(arr):
	if len(arr) < 3:
		return 0
	start = len(arr)
	end = 0
	while True:
		newStart = 0
		newEnd = 0
		currentNumber = 101
		for i in range(0, len(arr), 1):
			if i >= start and i <= end:
				continue
			if i == 0:
				currentNumber = arr[0]
				counter = 1
			else:
				if i == len(arr) - 1 and arr[i] == currentNumber:
					counter += 1
				if i != len(arr) - 1 and arr[i] == currentNumber:
					counter += 1
				elif counter >= 3:
					if i == len(arr) - 1 and arr[i] == currentNumber:
						newEnd = i
					else:
						newEnd = i - 1
					break
				else:
					counter = 1
					newStart = i
					currentNumber = arr[i]
		if newStart >= start or newEnd <= end:
			break
		else:
			start = newStart
			end = newEnd
	return max(0, end - start + 1)
currentMax = 0
for i in range(0, n - 1):
	if arr[i] == arr[i + 1] and arr[i] == x:
		maxDestroy = 0
		if i != 0 and i != n - 2:
			maxDestroy = collapse(arr[:i] + arr[i + 2:]) + 2
		else:
			maxDestroy = 2
		currentMax = max(currentMax, maxDestroy)
print(currentMax)


from sys import stdin

def arr_inp(n):
	if n == 1:
		return [int(x) for x in stdin.readline().split()]
	elif n == 2:
		return [float(x) for x in stdin.readline().split()]
	else:
		return [str(x) for x in stdin.readline().split()]

def solve(be, en):
	(num, a1, a2) = (0, [], [])
	for i in range(en, n):
		if a1 == [] or a1[-1][0] != balls[i]:
			a1.append([balls[i], 1])
		else:
			a1[-1][1] += 1
	for i in range(be, -1, -1):
		if a2 == [] or a2[-1][0] != balls[i]:
			a2.append([balls[i], 1])
		else:
			a2[-1][1] += 1
	for i in range(min(len(a1), len(a2))):
		if a1[i][0] == a2[i][0] and a1[i][1] + a2[i][1] >= 3:
			num += a1[i][1] + a2[i][1]
		else:
			break
	return num
(n, k, x) = arr_inp(1)
(balls, xs, ans) = (arr_inp(1), 0, 0)
for i in range(n):
	if balls[i] == x:
		xs += 1
	else:
		if xs >= 2:
			ans = max(xs + solve(i - xs - 1, i), ans)
		xs = 0
print(ans)


(n, k, x) = input().split()
n = int(n)
k = int(k)
res = 0
arr = input().split()
for i in range(len(arr)):
	if arr[i] == x:
		maior = 0
		copia = arr[0:i] + [x] + arr[i:len(arr)]
		teste = True
		while teste:
			teste = False
			cont = 1
			ini = 0
			fim = 0
			for j in range(1, len(copia)):
				if copia[j] == copia[j - 1]:
					cont += 1
					fim = j
				if copia[j] != copia[j - 1] or j == len(copia) - 1:
					tmp = []
					if cont >= 3:
						tmp = copia[0:ini]
						tmp += copia[fim + 1:len(copia)]
						teste = True
						copia = tmp
						maior += cont
						break
					cont = 1
					ini = j
					fim = j
		if maior > res:
			res = maior - 1
print(res)


(n, k, x) = [int(_) for _ in input().split()]
c = [int(_) for _ in input().split()]

def solve(s):
	while True:
		found = False
		for i in range(len(s) - 1):
			k = i + 1
			while k < len(s) and s[k] == s[i]:
				k += 1
			if k - i >= 3:
				found = True
				s = s[:i] + s[k:]
				break
		if not found:
			break
	return s
best = 0
for i in range(len(c) - 1):
	if c[i] == x and c[i + 1] == x:
		d = c[:i] + c[i + 2:]
		s = solve(d)
		best = max(best, n - len(s))
print(best)


def main():
	(n, k, x) = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	best = 0
	n = len(c)
	for i in range(n - 1):
		if c[i] == x == c[i + 1]:
			a = i - 1
			b = i + 2
			best = max(best, 2)
			while a >= 0 and b < n and (c[a] == c[b]):
				y = a
				z = b
				while a >= 0 and c[a] == c[z]:
					a -= 1
				while b < n and c[b] == c[y]:
					b += 1
				if y - a + (b - z) < 3:
					break
				best = max(best, b - a - 1)
	print(best)
main()


import sys
import time

def try_destroy_possible(local_list_input=[]):
	_list_input = local_list_input
	count_delete = 0
	i_start = 0
	i_end = len(_list_input) - 1
	while i_start < len(_list_input) - 1:
		i_next = i_start + 1
		count_contiguous = 1
		if debug_opt:
			print('while-1| i_start: {0} - i_next: {1} - i_end: {2}'.format(i_start, i_next, len(_list_input) - 1))
			print(_list_input)
		while i_next <= len(_list_input) - 1:
			if debug_opt:
				print('while-2| i_start: {0} [{1}] - i_next: {2} [{3}] - i_end: {4}'.format(i_start, _list_input[i_start], i_next, _list_input[i_next], len(_list_input) - 1))
			if _list_input[i_start] == _list_input[i_next]:
				count_contiguous += 1
				if i_next == len(_list_input) - 1:
					if count_contiguous >= 3:
						del _list_input[i_start:i_next + 1]
						count_delete += i_next - i_start + 1
						i_start = 0
						break
					else:
						break
				else:
					i_next += 1
			else:
				if count_contiguous >= 3:
					del _list_input[i_start:i_next]
					count_delete += i_next - i_start
					i_start = 0
				break
		i_start += 1
	return int(count_delete)
debug_opt = False
first_line_input = sys.stdin.readline().strip()
second_line_input = sys.stdin.readline().strip()
first_line_input_map = list(map(int, first_line_input.split(' ')))
var_n = first_line_input_map[0]
var_k = first_line_input_map[1]
var_x = first_line_input_map[2]
list_input_original = list(map(int, second_line_input.split(' ')))
list_count_possible_destroy = []
size_list = len(list_input_original)
if debug_opt:
	print('')
	print('List input: {0}'.format(list_input_original))
	print('Size of list: {0}'.format(size_list))
	print('Var X: {0}'.format(var_x))
i_start = 0
if debug_opt:
	print('')
	print('[+] Try to inject var_x')
while i_start < size_list - 1:
	count_delete = 0
	i_next = i_start + 1
	list_input_process = list(list_input_original)
	if debug_opt:
		print('')
		print('while-0| i_start: {0} [{1}] - i_next: {2} [{3}] - i_end: {4}'.format(i_start, list_input_process[i_start], i_next, list_input_process[i_next], len(list_input_process) - 1))
	if int(list_input_process[i_start]) == int(var_x) and int(list_input_process[i_next]) == int(var_x):
		del list_input_process[i_next]
		del list_input_process[i_start]
		count_delete += 2
		count_delete_try = try_destroy_possible(local_list_input=list_input_process)
		count_delete += count_delete_try
		list_count_possible_destroy.append(int(count_delete))
		i_start += 1
		continue
	else:
		i_start += 1
final_list_count_possible_destroy = list(set(list_count_possible_destroy))
if debug_opt:
	print('')
	print('List input 2nd times: {0}'.format(list_input_original))
	print('List count delete possible: {0}'.format(final_list_count_possible_destroy))
	print('Count delete: {0}'.format(max(final_list_count_possible_destroy)))
	print('')
if len(final_list_count_possible_destroy) == 0:
	print('0')
else:
	print(max(final_list_count_possible_destroy))


(n, k, x) = map(int, input().split())
balls = list(map(int, input().split()))
count = 0
ans = 0
for i in range(0, n):
	if x == balls[i]:
		count_out = 0
		l = i
		r = i
		while balls[l] == balls[r]:
			count = 2
			while l > 0 and balls[l - 1] == balls[l]:
				count += 1
				l -= 1
			while r < n - 1 and balls[r + 1] == balls[r]:
				count += 1
				r += 1
			l -= 1
			r += 1
			if count < 3:
				break
			else:
				count_out += count
			if l < 0 or r == n:
				break
		ans = max(count_out - 1, ans)
print(ans)


def remove_contiguous(arr):
	left = 0
	right = 1
	while right < len(arr):
		while right < len(arr) and arr[left] == arr[right]:
			right += 1
		if right - left >= 3:
			del arr[left:right]
			modified = True
			return arr
		left = right
		right += 1
	return arr
(n, k, x) = map(int, input().strip().split())
c = [int(x) for x in input().strip().split()]
ans = 0
for i in range(1, len(c)):
	if c[i - 1] == c[i] and c[i] == x:
		d = c[:i] + [x] + c[i:]
		len_prev = len(d)
		while True:
			d = remove_contiguous(d)
			if len_prev == len(d):
				break
			len_prev = len(d)
		ans = max(ans, n - len(d))
print(ans)


import copy
(n, k, x) = map(int, input().split())
c = list(map(int, input().split()))
for i in range(n):
	c[i] = (c[i], 0)
ans = 0
for i in range(n + 1):
	c2 = copy.deepcopy(c)
	c2.insert(i, (x, 1))
	while True:
		dq = False
		for i in range(len(c2) - 2):
			if c2[i][0] == c2[i + 1][0] == c2[i + 2][0]:
				le = i
				re = i
				while re < len(c2) and c2[re][0] == c2[le][0]:
					re += 1
				c2 = c2[:le] + c2[re:]
				dq = True
				break
		if not dq:
			break
	cntdel = 0
	for (a, b) in c2:
		if b == 0:
			cntdel += 1
	ans = max(ans, n - cntdel)
print(ans)


def ball_game(c, x):
	all_result = [0]
	for i in range(1, n - 1):
		b = c[:i] + [x] + c[i:]
		j = 0
		result = 0
		while j < len(b) - 2:
			if j < len(b) - 3 and b[j] == b[j + 1] == b[j + 2] == b[j + 3]:
				b = b[:j] + b[j + 4:]
				result += 4
				if j > 1:
					j -= 2
				elif j > 0:
					j -= 1
			elif b[j] == b[j + 1] == b[j + 2]:
				b = b[:j] + b[j + 3:]
				result += 3
				if j > 1:
					j -= 2
				elif j > 0:
					j -= 1
			else:
				j += 1
		if result > 0:
			result = result - 1
		all_result += [result]
	return max(all_result)
line1 = list(map(int, input().split()))
n = line1[0]
k = line1[1]
x = line1[2]
c = list(map(int, input().split()))
r = ball_game(c, x)
print(r)


import sys
import math
from collections import defaultdict
MAXNUM = math.inf
MINNUM = -1 * math.inf
ASCIILOWER = 97
ASCIIUPPER = 65

def getInt():
	return int(sys.stdin.readline().rstrip())

def getInts():
	return map(int, sys.stdin.readline().rstrip().split(' '))

def getString():
	return sys.stdin.readline().rstrip()

def printOutput(ans):
	sys.stdout.write()
	pass

def branch(lst, color, pos):
	l = pos - 1
	r = pos
	curColor = color
	total = 0
	subtotal = 1
	while True:
		while l >= 0 and lst[l] == curColor:
			subtotal += 1
			l -= 1
		while r < len(lst) and lst[r] == curColor:
			subtotal += 1
			r += 1
		if subtotal < 3:
			break
		total += subtotal
		if l < 0:
			break
		curColor = lst[l]
		subtotal = 0
	return max(total - 1, 0)

def solve(balls, x):
	return max((branch(balls, x, i) for i in range(len(balls))))

def readinput():
	(n, k, x) = getInts()
	balls = list(getInts())
	print(solve(balls, x))
readinput()


(n, k, x) = map(int, input().split())
balls = list(map(int, input().split()))
c = 0
p = None
pairs = []
matches = []
for b in range(len(balls)):
	if balls[b] is not c:
		c = balls[b]
	if c is p:
		pairs.append((b - 1, b))
		if c is x:
			matches.append((b - 1, b))
	p = c
max = 0
for m in matches:
	(i, j) = m
	removed = 2
	i -= 1
	j += 1
	while i >= 0 and j < len(balls):
		if balls[i] is balls[j]:
			toRemove = 0
			if j < len(balls) - 1 and balls[j + 1] is balls[j]:
				toRemove += 1
				j += 1
			if i >= 1 and balls[i - 1] is balls[i]:
				toRemove += 1
				i -= 1
			if toRemove > 0:
				removed += toRemove + 2
			else:
				break
		else:
			break
		i -= 1
		j += 1
	if removed > max:
		max = removed
print(max)


def search():
	a = b[:]
	t = 0
	flag = True
	while flag:
		tmp = len(a)
		count = 0
		i = 0
		while i < len(a) - 1:
			if a[i] == a[i + 1]:
				count += 1
			else:
				if count > 1:
					t += count + 1
					i -= count
					while count >= 0:
						a.pop(i)
						count -= 1
					i -= 1
				count = 0
			i += 1
		if count >= 2:
			t += count + 1
			i -= count
			while count >= 0:
				a.pop(i)
				count -= 1
		if tmp == len(a):
			flag = False
	return t
(n, k, x) = map(int, input().split())
b = list(map(int, input().split()))
j = 0
maxi = 0
while j < len(b):
	if b[j] == x:
		while b[j] == x:
			j += 1
			if j >= len(b):
				break
		b.insert(j - 1, x)
		res = search() - 1
		if res > maxi:
			maxi = res
		b.pop(j - 1)
	j += 1
print(maxi)


nkx = list(map(int, input().split(' ')))
row = list(map(int, input().split(' ')))
n = nkx[0]
k = nkx[1]
x = nkx[2]
result = 0
for i in range(n):
	row_copy = row[:]
	row_copy.insert(i, x)
	while len(row_copy) > 2:
		size = len(row_copy)
		for elem in range(2, len(row_copy)):
			if row_copy[elem - 2] == row_copy[elem - 1] and row_copy[elem - 1] == row_copy[elem]:
				row_cut = elem + 1
				while row_cut < len(row_copy):
					if row_copy[elem] != row_copy[row_cut]:
						break
					row_cut += 1
				row_copy = row_copy[:elem - 2] + row_copy[row_cut:]
				break
		if len(row_copy) == size:
			break
	result = max(result, n - len(row_copy))
print(result)