You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.

In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. 

Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.


-----Input-----

The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array.

The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. 


-----Output-----

Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$.


-----Examples-----
Input
3
1 2 3

Output
0

Input
4
1 1 2 2

Output
2

Input
5
1 4 1 4 9

Output
2



-----Note-----

In the first example all the elements are already distinct, therefore no subsegment needs to be removed.

In the second example you can remove the subsegment from index $2$ to $3$.

In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.

Source: codeforces | Difficulty: MEDIUM
Tags: Sorting

Amortized analysis

Implementation

Complete search


Solutions:
n = int(input())
A = [int(x) for x in input().split()]
from collections import defaultdict as dd
hsh = dd(int)
for i in range(n):
	hsh[A[i]] += 1
cnt = 0
ans = 10000000
for i in hsh:
	if hsh[i] >= 2:
		cnt += 1
if len(set(A)) == n:
	print(0)
else:
	for i in range(n):
		hsh2 = dd(int)
		ct = 0
		for j in range(i, n):
			hsh2[A[j]] += 1
			if hsh2[A[j]] == hsh[A[j]] - 1 and hsh[A[j]] >= 2:
				ct += 1
			if ct == cnt:
				ans = min(ans, abs(j - i + 1))
	print(ans)


import sys
input = lambda : sys.stdin.readline().strip()
n = int(input())
ls = list(map(int, input().split()))
d = [{}]
works = {}
for i in range(n):
	works[i] = True
for i in range(n):
	d.append(dict(d[-1]))
	try:
		d[-1][ls[i]] += 1
		works[i] = False
	except:
		d[-1][ls[i]] = 1
m = n
for l in range(n):
	for r in range(n - 1, l - 1, -1):
		try:
			d[l][ls[r]] += 0
			break
		except:
			d[l][ls[r]] = 1
			pass
	if works[l]:
		m = min(m, r - l + 1)
	else:
		m = min(m, r - l + 1)
		break
if len(set(ls)) == n:
	print(0)
else:
	print(m)


from sys import stdin
from copy import deepcopy

def get_dict(arr, n):
	mydict = dict()
	for i in range(n):
		if arr[i] in mydict:
			mydict[arr[i]] += 1
		else:
			mydict[arr[i]] = 1
	more = 0
	for i in mydict:
		if mydict[i] > 1:
			more += 1
	return (more, mydict)
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
ans = float('inf')
(more, mydict) = get_dict(arr, n)
if more == 0:
	ans = 0
	print(ans)
	return
for i in range(n):
	t = more
	tdict = deepcopy(mydict)
	for j in range(i, n):
		tdict[arr[j]] -= 1
		if tdict[arr[j]] == 1:
			t -= 1
		if t == 0:
			ans = min(ans, abs(i - j) + 1)
print(ans)


from collections import Counter
n = int(input())
a = list(map(int, input().split()))
d = Counter(a)
v = list(d.values())
gt_1 = 0
for i in v:
	if i > 1:
		gt_1 = gt_1 + 1
s = 0
mn = 10000000000
for i in range(0, n):
	x = gt_1
	s = 0
	c = d.copy()
	for j in range(i, n):
		if x == 0:
			if mn > s:
				mn = s
			break
		s = s + 1
		c[a[j]] = c[a[j]] - 1
		if c[a[j]] == 1:
			x = x - 1
	if x == 0:
		if mn > s:
			mn = s
print(mn)


from sys import stdin, stdout
from collections import defaultdict
from collections import deque
import math
import copy
N = int(input())
arr = [int(x) for x in stdin.readline().split()]
freq = {}
res = 9999
prefix = [0] * N
for i in range(N):
	num = arr[i]
	if num not in freq:
		freq[num] = 0
	else:
		break
	prefix[i] = 1
freq = {}
left = -1
right = 0
for i in range(N - 1, -1, -1):
	num = arr[i]
	if num not in freq:
		freq[num] = 1
	else:
		right = i + 1
		break
r = right - (left + 1)
res = min(res, r)
if r == 0:
	print(0)
	quit()
for i in range(N):
	left = i
	if prefix[i] == 0:
		break
	num = arr[i]
	if num not in freq:
		freq[num] = 1
	elif freq[num] == 1:
		target = num
		while right < N:
			r_num = arr[right]
			if arr[right] == target:
				freq[r_num] -= 1
				right += 1
				break
			freq[r_num] -= 1
			right += 1
	r = right - (left + 1)
	res = min(res, r)
print(res)


from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
if len(set(a)) == n:
	print(0)
	exit()
ans = n
d = defaultdict(int)
for i in range(n):
	ans = min(ans, n - i)
	dd = defaultdict(int)
	for j in range(n - 1, i, -1):
		dd[a[j]] += 1
		if dd[a[j]] + d[a[j]] >= 2:
			break
		ans = min(ans, n - i - (n - j))
	d[a[i]] += 1
	if d[a[i]] >= 2:
		break
print(ans)


n = int(input())
a = list(map(int, input().split()))
a += a
ans = n
for i in range(n):
	s = set()
	for j in range(i, i + n):
		if a[j] not in s:
			s.add(a[j])
			if i == 0 or j >= n - 1:
				ans = min(ans, n - (j - i + 1))
		else:
			break
print(ans)


from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))

def sim(n, a, k):
	freq = defaultdict(int)
	for i in range(n):
		freq[a[i]] += 1
	for i in range(k):
		freq[a[i]] -= 1
	ok = True
	for (key, val) in freq.items():
		if val > 1:
			ok = False
	if ok:
		return True
	l = 0
	r = k - 1
	while l < n - k:
		freq[a[l]] += 1
		l += 1
		r += 1
		freq[a[r]] -= 1
		ok = True
		for (key, val) in freq.items():
			if val > 1:
				ok = False
		if ok:
			return True
	return False
l = 0
r = n - 1
ans = 0
while l <= r:
	mid = (r + l) // 2
	s = sim(n, a, mid)
	if s:
		ans = mid
		r = mid - 1
	else:
		l = mid + 1
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
mp = {}
rep = 0
for i in range(n):
	if a[i] in mp:
		mp[a[i]] += 1
		rep += 1
	else:
		mp[a[i]] = 1
ans = 1000000
if rep == 0:
	print(0)
else:
	for i in range(n):
		z = []
		flag = 0
		for j in range(i, n):
			if mp[a[j]] > 1:
				z.append(a[j])
				mp[a[j]] -= 1
				rep -= 1
			if rep == 0:
				ans = min(ans, j - i + 1)
				break
		rep += len(z)
		for i in z:
			mp[i] += 1
	print(ans)


n = int(input())
lis = list(map(int, input().split()))
ans = n
for i in range(n + 1):
	a = set(lis[:i])
	if len(a) < i:
		break
	r = n - 1
	while r >= i and lis[r] not in a:
		a.add(lis[r])
		r -= 1
	ans = min(ans, r - i + 1)
print(ans)


n = int(input())
m = input().split()
mass = [int(m[i]) for i in range(n)]
m = []
if len(set(mass)) == len(mass):
	print(0)
else:
	for i in range(n):
		if len(set(mass[:i])) == i:
			(beg, end) = (i, len(mass) - 1)
			while beg != end:
				mid = (beg + end + 1) // 2
				if len(set(mass[:i] + mass[mid:])) == len(mass[:i] + mass[mid:]):
					end = mid - 1
				else:
					beg = mid
			m.append(end + 1 - i)
	print(min(m))


import sys

def get_sn():
	return int(input())

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

def get_ln():
	return list(get_n())

def cond(x, a, n):
	for i in range(n - x + 1):
		if len(set(a[:i] + a[i + x:])) == n - x:
			return True
	return False

def solve(a, n):
	(l, r) = (-1, n + 1)
	while r - l > 1:
		m = (r + l) // 2
		if cond(m, a, n):
			r = m
		else:
			l = m
	return r

def main():
	n = get_sn()
	arr = get_ln()
	print(solve(arr, n))
main()


ans = n = int(input())
a = input().split() * 2
s = set()
i = 0
while a[i] not in s:
	s |= {a[i]}
	i += 1
while i + 1:
	while a[n - 1] not in s:
		n -= 1
		s |= {a[n]}
	ans = min(ans, n - i)
	i -= 1
	s -= {a[i]}
print(ans)


def mp():
	return map(int, input().split())
n = int(input())
a = list(mp())
ans = n - 1
for i in range(n):
	fail = False
	u = set()
	for j in range(i):
		if a[j] in u:
			fail = True
			break
		u.add(a[j])
	if fail:
		continue
	j = n - 1
	while not a[j] in u:
		u.add(a[j])
		j -= 1
	ans = min(ans, j - i + 1)
print(ans)


from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
cnt = defaultdict(int)
ti = -1
i = 0
for i in range(n):
	if cnt[a[i]]:
		ti = i
		break
	else:
		cnt[a[i]] += 1
if ti == -1:
	print(0)
else:
	ans = 0
	cnt = defaultdict(int)
	for i in range(n):
		if cnt[a[i]]:
			ti = i
			break
		else:
			cnt[a[i]] += 1
			cnt2 = defaultdict(int)
			for j in range(1, n - i + 1):
				if cnt[a[-j]] or cnt2[a[-j]]:
					break
				else:
					cnt2[a[-j]] += 1
			ans = max(ans, i + j)
	cnt = defaultdict(int)
	a = a[::-1]
	cnt = defaultdict(int)
	for i in range(n):
		if cnt[a[i]]:
			ti = i
			break
		else:
			cnt[a[i]] += 1
			cnt2 = defaultdict(int)
			for j in range(1, n - i + 1):
				if cnt[a[-j]] or cnt2[a[-j]]:
					break
				else:
					cnt2[a[-j]] += 1
			ans = max(ans, i + j)
	print(n - ans)


n = int(input())
a = list(map(int, input().split()))
min1 = n + 1
myset = set()
for i in range(n):
	temp = myset.copy()
	min1 = min(min1, n - i)
	for j in range(n - 1, i - 1, -1):
		if a[j] in temp:
			break
		temp.add(a[j])
		min1 = min(min1, j - i)
	if a[i] in myset:
		break
	myset.add(a[i])
print(min1)


from sys import stdin, stdout
from math import gcd, sqrt, inf
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
N = inf
n = I()
a = L()
ans = N
if len(set(a)) == n:
	exit(print(0))
for i in range(n):
	l = i
	r = n - 1
	while l <= r:
		m = (l + r) // 2
		if len(set(a[:i] + a[m + 1:])) == n - (m - i + 1):
			ans = min(m - i + 1, ans)
			r = m - 1
		else:
			l = m + 1
print(ans)


import sys
from collections import defaultdict as dd
from collections import deque
from functools import *
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from math import *
from itertools import permutations, product

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def inc(d, c):
	d[c] = d[c] + 1 if c in d else 1

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

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

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

def gh():
	sys.stdout.flush()

def isvalid(i, j):
	return 0 <= i < n and 0 <= j < n

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

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = 1
while t > 0:
	t -= 1
	n = fi()
	a = li()
	lr = -1
	d = {}
	c = 0
	nd = 0
	for i in range(n):
		inc(d, a[i])
		if d[a[i]] == 2:
			nd += 1
	mini = n
	for i in range(n):
		p = nd
		l = deepcopy(d)
		for j in range(i, n):
			l[a[j]] -= 1
			if l[a[j]] == 1:
				p -= 1
			if p == 0:
				mini = min(mini, j - i + 1)
				break
	print(mini if len(a) != len(set(a)) else 0)


n = int(input())
ans = n
a = list(map(int, input().split()))
i = 0
j = n - 1
s = set()
while i < n and a[i] not in s:
	s.add(a[i])
	i += 1
while i + 1:
	while j > -1 and a[j] not in s:
		s.add(a[j])
		j -= 1
	ans = min(ans, j - i + 1)
	i -= 1
	s -= {a[i]}
print(ans)


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
n = int(input())
a = list(map(int, input().split()))
ans = n
left = set()
for i in range(n):
	right = set()
	j = n - 1
	while j >= i:
		if a[j] in left or a[j] in right:
			break
		right.add(a[j])
		j -= 1
	ans = min(ans, j - i + 1)
	if a[i] in left:
		break
	left.add(a[i])
print(ans)


n = int(input())
A = list(map(int, input().split()))
if n == len(set(A)):
	print(0)
	exit(0)
l = 0
r = n
while r - l > 1:
	gg = False
	x = (l + r) // 2
	G = [set()]
	H = []
	s1 = set()
	for i in range(n - x):
		s1.add(A[i])
		G.append(s1.copy())
	s2 = set()
	for i in range(n - 1, x - 1, -1):
		if s2.intersection(G[i - x + 1]) == set() and len(s2) == n - i - 1 and (len(G[i - x + 1]) == i - x + 1):
			gg = True
			break
		s2.add(A[i])
	if len(s2) == n - x:
		gg = True
	if gg:
		r = x
	else:
		l = x
print(l + 1)


n = int(input())
a = list(map(int, input().split(' ')))
lset = set()
min_size = n
left = -1
right = -1
while left < n:
	if left != -1:
		if a[left] in lset:
			print(min_size)
			break
		lset.add(a[left])
	diffs = lset.copy()
	for j in range(n - 1, right, -1):
		if a[j] in diffs:
			right = j
			break
		else:
			diffs.add(a[j])
	min_size = min(min_size, right - left)
	left += 1
if left == n:
	print(0)


import io
import os
import math
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
l = [int(x) for x in input().split()]
d = set()
ans = 1000000000.0
for i in range(n + 1):
	d = set(l[:i])
	if len(d) < i:
		break
	r = n - 1
	while r >= i and l[r] not in d:
		d.add(l[r])
		r -= 1
	ans = min(ans, r - i + 1)
print(ans)


def check(m):
	for i in range(n - m + 1):
		ar = []
		for j in range(i):
			ar.append(a[j])
		for j in range(i + m, n):
			ar.append(a[j])
		if len(set(ar)) == len(ar):
			return True
	return False
n = int(input())
a = [int(o) for o in input().split()]
l = 0
r = n
while l < r:
	m = (l + r) // 2
	if check(m):
		r = m
	else:
		l = m + 1
print(r)


from sys import stdin, stdout
input = stdin.readline
print = stdout.write
n = int(input())
a = list(map(int, input().split()))

def solve(n, a):
	l = -1
	s = set()
	while l + 1 < n and a[l + 1] not in s:
		s.add(a[l + 1])
		l += 1
	r = n
	while r - 1 >= 0 and a[r - 1] not in s:
		s.add(a[r - 1])
		r -= 1
	res = l + 1 + (n - r)
	while l >= 0:
		s.remove(a[l])
		l -= 1
		while r - 1 >= 0 and a[r - 1] not in s:
			s.add(a[r - 1])
			r -= 1
		res = max(res, l + 1 + n - r)
	return n - res
print(str(solve(n, a)))


n = int(input())
a = list(map(int, input().split()))
ans = n
for i in range(n):
	falg = False
	u = set()
	for j in range(i):
		if a[j] in u:
			falg = True
			break
		u.add(a[j])
	if falg:
		continue
	j = n - 1
	while not a[j] in u:
		u.add(a[j])
		j -= 1
	ans = min(ans, j - i + 1)
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
dic = {}
arr = 10 ** 100
counter = 0
for item in a:
	if item not in dic:
		dic[item] = 1
	else:
		dic[item] += 1
		counter += 1
if counter == 0:
	arr = 0
for i in range(n):
	for j in range(i, n):
		dic[a[j]] -= 1
		if dic[a[j]] != 0:
			counter -= 1
		if counter == 0:
			arr = min(arr, j - i + 1)
	counter = 0
	for item in a:
		dic[item] = 0
	for item in a:
		if dic[item] != 0:
			counter += 1
		dic[item] += 1
print(arr)


n = int(input())
a = list(map(int, input().split()))
ans = n
left = set()
for i in range(n):
	right = set()
	j = n - 1
	while j >= i:
		if a[j] in left or a[j] in right:
			break
		right.add(a[j])
		j -= 1
	ans = min(ans, j - i + 1)
	if a[i] in left:
		break
	left.add(a[i])
print(ans)


n = int(input())
a = list(map(int, input().split()))
if len(set(a)) == n:
	print(0)
else:
	l = 0
	r = n
	while r - l > 1:
		m = (l + r) // 2
		i = 0
		ok = False
		while i + m <= n:
			pref = set()
			suff = set()
			for x in range(i):
				pref.add(a[x])
			for x in range(i + m, n):
				suff.add(a[x])
			if len(pref & suff) == 0 and len(pref) + len(suff) == n - m:
				ok = True
				break
			i += 1
		if ok == True:
			r = m
		else:
			l = m
	print(r)


n = int(input())
arr = list(map(int, input().split()))
myset = set()
low = n + 1
for i in range(n):
	tmp = myset.copy()
	low = min(low, n - i)
	for j in range(n - 1, i - 1, -1):
		if arr[j] in tmp:
			break
		tmp.add(arr[j])
		low = min(low, j - i)
	if arr[i] in myset:
		break
	myset.add(arr[i])
print(low)


n = int(input())
A = [int(i) for i in input().split()]
import collections

def f(A, k):
	d = collections.defaultdict(int)
	a = 0
	for i in range(k):
		if d[A[i]] == 0:
			a += 1
			d[A[i]] += 1
		else:
			break
	for i in range(n - a):
		if d[A[n - 1 - i]] == 0:
			a += 1
			d[A[n - 1 - i]] += 1
		else:
			break
	return n - a
ans = n
for i in range(n):
	ans = min(ans, f(A, i))
print(ans)


import math
a = int(input())
z = list(map(int, input().split()))
from collections import defaultdict
al = defaultdict(int)
ram = defaultdict(int)
for i in range(len(z)):
	al[z[i]] += 1
count = 0
for i in range(len(z)):
	if al[z[i]] > 1 and ram[z[i]] == 0:
		ram[z[i]] = 1
		count += 1
start = 0
mini = math.inf
if count == 0:
	print(0)
	exit()
for i in range(len(z)):
	end = i
	gam = defaultdict(int)
	temp = 0
	ram = defaultdict(int)
	while end < len(z):
		if temp == count:
			mini = min(mini, end - i + 1)
			break
		gam[z[end]] += 1
		if gam[z[end]] >= al[z[end]] - 1 and al[z[end]] > 1 and (ram[z[end]] == 0):
			temp += 1
			ram[z[end]] = 1
		if temp == count:
			mini = min(mini, end - i + 1)
			break
		end += 1
print(mini)


n = int(input())
arr = list(map(int, input().split()))
ans = n
k = 0
for i in range(n + 1):
	pre = set(arr[:i])
	if len(pre) < i:
		k = 1
		break
	r = n - 1
	while r >= i and arr[r] not in pre:
		pre.add(arr[r])
		r -= 1
	ans = min(ans, r - i + 1)
print(ans)


n = int(input())
l = list(map(int, input().split()))
s = set(l)
if n == len(s):
	print(0)
else:
	s = set()
	j = 0
	for i in range(n):
		if l[i] not in s:
			s.add(l[i])
		else:
			j = i
			break
	if j == 0:
		print(0)
	else:
		ans = 0
		for i in range(j + 1):
			s = set()
			for k in range(i):
				if l[k] not in s:
					s.add(l[k])
				else:
					break
			for k in range(n - 1, i, -1):
				if l[k] not in s:
					s.add(l[k])
				else:
					break
			ans = max(ans, len(s))
		print(n - ans)


def f(m):
	b = {}
	for i in a[m:]:
		if i in b:
			b[i] += 1
		else:
			b[i] = 1
	k = 0
	for i in b:
		if b[i] > 1:
			k += 1
	if k == 0:
		return True
	for i in range(n - m):
		b[a[i + m]] -= 1
		if b[a[i + m]] == 1:
			k -= 1
		if a[i] in b:
			b[a[i]] += 1
			if b[a[i]] == 2:
				k += 1
		else:
			b[a[i]] = 1
		if k == 0:
			return True
	return False
n = int(input())
a = list(map(int, input().split()))
l = 0
r = n
while r - l > 1:
	m = (r + l) // 2
	if f(m):
		r = m
	else:
		l = m
if f(l):
	r = l
print(r)


from collections import deque
n = int(input())
a = list(map(int, input().split()))
maxDiversity = 0
frontd = deque()
backd = deque()
frontp = {}
backp = {}
for i in a:
	frontp[i] = frontp.get(i, 0) + 1
	frontd.append(i)
	if frontp[i] >= 2:
		frontd.pop()
		break
for i in range(n - 1, -1, -1):
	curr = a[i]
	backp[curr] = backp.get(curr, 0) + 1
	backd.append(curr)
	if backp[curr] >= 2:
		backd.pop()
		break
best = [0] * len(frontd)
s = set()
stemp = set()
for j in backd:
	if j in stemp:
		break
	stemp.add(j)
best = [0] * len(frontd) + [len(stemp)]
for i in range(len(frontd)):
	s.add(frontd[i])
	stemp = set()
	for j in backd:
		if j in s or j in stemp:
			break
		stemp.add(j)
	best[i] = len(stemp) + len(s)
print(n - max(best))


def unique(arr):
	check = set()
	res = 1000000
	for i in range(len(arr)):
		valid = True
		for j in range(i):
			if arr[j] not in check:
				check.add(arr[j])
			else:
				valid = False
				break
		if not valid:
			continue
		min_index = len(arr)
		for j in range(len(arr) - 1, i - 1, -1):
			if arr[j] not in check:
				min_index = j
				check.add(arr[j])
			else:
				break
		if valid:
			res = min(res, min_index - i)
		check.clear()
	return res
n = int(input())
arr = list(map(int, input().split()))
print(unique(arr))


import sys, os, io

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

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(1):
		n = ri()
		a = ria()
		ans = n - 1
		for i in range(n):
			d = {}
			vp = True
			for j in range(i):
				if a[j] in d:
					vp = False
					break
				else:
					d[a[j]] = 1
			mis = n
			for j in range(n - 1, i - 1, -1):
				if a[j] in d:
					break
				else:
					d[a[j]] = 1
					mis = j
			if vp:
				ans = min(ans, mis - i)
		print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			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, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			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()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

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

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


n = int(input())
a = [int(s) for s in input().split()]
r = n - 1
left = set()
for i in range(n):
	right = set()
	for j in range(1, n + 1):
		if a[-j] in left or a[-j] in right:
			r = min(r, n - i - j + 1)
			break
		right.add(a[-j])
	else:
		print(0)
		break
	if a[i] not in left:
		left.add(a[i])
	else:
		print(r)
		break


n = input()
n = int(n)
l = list(map(int, input().split()))
d = {}
c_d = {}
c = 0
d_2 = {}
for i in range(n):
	d[l[i]] = d.get(l[i], 0) + 1
	c_d[l[i]] = c_d.get(l[i], 0) + 1
	if d[l[i]] > 1:
		d_2[l[i]] = 1
		c += 1
mini = n
for i in range(n):
	if d[l[i]] > 1:
		for j in range(i, n):
			d[l[j]] -= 1
			check = 0
			if j != n - 1:
				if d[l[j - 1]] <= 1:
					for k in d_2:
						if d[k] > 1:
							check += 1
							break
					if check == 0:
						if mini > j - i + 1:
							mini = j - i + 1
						break
			elif d[l[j - 1]] <= 1:
				for k in d_2:
					if d[k] > 1:
						check += 1
						break
				if check == 0:
					if mini > j - i + 1:
						mini = j - i + 1
					break
		for ind in c_d:
			d[ind] = c_d[ind]
if c == 0:
	print(0)
else:
	print(mini)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect 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')

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 check(i, j):
	return len(set(a[:i] + a[j:])) == n - (j - i)
n = Int()
a = array()
ans = inf
for i in range(n):
	unique = set()
	cur = 0
	start = i
	flag = False
	for j in range(i):
		if a[j] not in unique:
			unique.add(a[j])
			cur += 1
		else:
			flag = True
			break
	end = n
	for j in range(n - 1, i - 1, -1):
		if a[j] not in unique:
			unique.add(a[j])
			cur += 1
			end = j
		else:
			break
	unique.clear()
	if not flag:
		ans = min(ans, end - start)
print(ans)


n = int(input())
a = [-1] + list(map(int, input().split()))
v = {}

def vis(x):
	if not x in v:
		v[x] = 1
	else:
		v[x] += 1

def uv(x):
	v[x] -= 1
ans = n - 1
n += 1
for (i, x) in enumerate(a):
	vis(x)
	if v[x] > 1:
		break
	j = n
	while j > 1:
		j -= 1
		y = a[j]
		vis(y)
		if v[y] > 1:
			break
	ans = min(ans, max(0, j - i))
	while j < n:
		uv(a[j])
		j += 1
print(ans)


n = int(input())
ls = list(map(int, input().split()))
myset = set()
mn = n + 1
for i in range(n):
	tmp = myset.copy()
	mn = min(mn, n - i)
	for j in range(n - 1, i - 1, -1):
		if ls[j] in tmp:
			break
		tmp.add(ls[j])
		mn = min(mn, j - i)
	if ls[i] in myset:
		break
	myset.add(ls[i])
print(mn)


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

def ss():
	return [x for x in input()]

def si():
	return [int(x) for x in input().split()]

def mi():
	return map(int, input().split())
a = ii()
s = si()
ans = a
for l in range(0, a + 1):
	c = set(s[:l])
	if len(c) < l:
		break
	r = a - 1
	while r >= l and s[r] not in c:
		c.add(s[r])
		r -= 1
	ans = min(ans, r - l + 1)
print(ans)


n = int(input())
a = list(map(int, input().split()))
d = {}
ans = n - 1
for i in range(n):
	pre = True
	for j in range(i):
		try:
			d[a[j]] += 1
		except:
			d[a[j]] = 1
		if d[a[j]] == 2:
			pre = False
			break
	mn = n
	for j in range(n - 1, i - 1, -1):
		try:
			d[a[j]] += 1
		except:
			d[a[j]] = 1
		if d[a[j]] == 1:
			mn = j
		else:
			break
	if pre:
		ans = min(ans, mn - i)
	d.clear()
print(ans)


def check(arr, n, val):
	for i in range(0, n - val + 1):
		d = {}
		flag = 0
		for j in range(i):
			if arr[j] not in d.keys():
				d[arr[j]] = 1
			else:
				flag = 1
				break
		if flag == 0:
			for j in range(i + val, n):
				if arr[j] not in d.keys():
					d[arr[j]] = 1
				else:
					flag = 1
					break
		if flag == 0:
			return True
	return False
n = int(input())
arr = [int(x) for x in input().split()]
(left, right) = (0, n)
ans = 0
while left <= right:
	mid = (left + right) // 2
	if check(arr, n, mid) == True:
		ans = mid
		right = mid - 1
	else:
		left = mid + 1
print(ans)


from collections import Counter, defaultdict
n = int(input())
a = list(map(int, input().split()))
sub = 0
notok = 0
d = dict()
for i in range(n):
	if d.get(a[i], -1) == -1:
		d[a[i]] = 1
	else:
		d[a[i]] += 1
		notok += 1
if notok == 0:
	print(0)
	exit()
M = float('inf')
for i in range(n):
	yy = notok
	dd = d.copy()
	for j in range(i, n):
		if dd[a[j]] > 1:
			dd[a[j]] -= 1
			yy -= 1
			if yy == 0:
				M = min(M, j - i + 1)
		else:
			pass
print(M)


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')
import math
import bisect
from collections import Counter
import bisect
from collections import deque
import bisect

def is_t(d):
	for i in d:
		if d[i] >= 2:
			return False
	return True

def can(x):
	d = Counter(arr)
	for i in range(x):
		d[arr[i]] -= 1
	if is_t(d):
		return True
	i = 1
	for j in range(x, n):
		d[arr[i - 1]] += 1
		d[arr[j]] -= 1
		if is_t(d):
			return True
		i += 1
	return False
for _ in range(1):
	n = int(input())
	arr = list(map(int, input().split()))
	l = 0
	r = n - 1
	ans = 0
	while l <= r:
		mid = (l + r) // 2
		if can(mid):
			ans = mid
			r = mid - 1
		else:
			l = mid + 1
	print(ans)


def naiveSolve(a, n):
	minSz = n
	for l in range(-1, n):
		for r in range(l + 1, n + 1):
			cnt = Counter()
			for i in range(l + 1):
				cnt[a[i]] += 1
			for i in range(r, n):
				cnt[a[i]] += 1
			ok = True
			for (k, v) in cnt.items():
				if v > 1:
					ok = False
			if ok:
				sz = n - len(cnt.keys())
				minSz = min(minSz, sz)
	return minSz
from collections import Counter

def main():
	n = int(input())
	a = readIntArr()
	minSz = n
	for l in range(-1, n):
		cnts = Counter()
		ok = True
		for i in range(l + 1):
			cnts[a[i]] += 1
			if cnts[a[i]] == 2:
				ok = False
		if ok == False:
			break
		minSz = min(minSz, n - len(cnts.keys()))
		for r in range(n - 1, -1, -1):
			cnts[a[r]] += 1
			if cnts[a[r]] == 1:
				minSz = min(minSz, n - len(cnts.keys()))
			else:
				break
	print(minSz)
	return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultValFactory, dimensionArr):
	dv = defaultValFactory
	da = dimensionArr
	if len(da) == 1:
		return [dv() for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(i, j):
	print('? {} {}'.format(i, j))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ans):
	print('! {}'.format(' '.join([str(x) for x in ans])))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
for _abc in range(1):
	main()


from math import *
from collections import *
import sys
sys.setrecursionlimit(10 ** 9)
n = int(input())
a = list(map(int, input().split()))
l = 0
d = dict()
while l < n:
	if a[l] not in d:
		d[a[l]] = 1
	else:
		break
	l += 1
ans = 10 ** 4
for j in range(l + 1):
	r = n - 1
	d = dict()
	for i in range(j):
		d[a[i]] = 1
	while r > j:
		if a[r] not in d:
			d[a[r]] = 1
		else:
			break
		r -= 1
	ans = min(r - j + 1, ans)
print(ans)


def main():
	import sys
	input = sys.stdin.readline
	n = int(input())
	arr = list(map(int, input().split()))
	dct = {}
	for x in arr:
		dct[x] = 0
	i = 0
	while i != n and dct[arr[i]] == 0:
		dct[arr[i]] = 1
		i += 1
	if i == n:
		print(0)
		return 0
	j = n - 1
	while dct[arr[j]] == 0:
		dct[arr[j]] = 1
		j -= 1
	ans = j - i + 1
	for k in range(i - 1, -1, -1):
		dct[arr[k]] -= 1
		while dct[arr[j]] == 0:
			dct[arr[j]] = 1
			j -= 1
		ans = min(ans, j - k + 1)
	print(ans)
	return 0
main()


import collections

def do():
	n = int(input())
	nums = [int(i) for i in input().split(' ')]
	nums2 = nums + nums
	d = collections.defaultdict(int)
	l = 0
	max_unique = 1
	for (r, i) in enumerate(nums2):
		d[i] += 1
		while d[i] > 1:
			d[nums2[l]] -= 1
			l += 1
		if l == 0 or r == n - 1 or (l < n and r >= n):
			max_unique = max(max_unique, r - l + 1)
	return n - max_unique
print(do())


a = int(input())
li = list(map(int, input().split()))
k = set()
di = {}
for i in li:
	di[i] = []
for i in range(a):
	di[li[i]].append(i + 1)
ko = []
for i in di:
	if len(di[i]) > 1:
		ko.append(di[i])
if ko != []:
	lo = []
	re = [0 for i in range(len(ko))]
	for i in range(len(ko)):
		for j in ko[i]:
			lo.append([j, i])
		re[i] = len(ko[i])
	lp = len(lo)
	lo.sort()
	l = 0
	r = 0
	be = 99999999999
	re[lo[0][1]] -= 1
	while True:
		vp = 0
		for i in re:
			if i > 1:
				vp = 1
				break
		if vp == 0:
			if lo[r][0] - lo[l][0] + 1 < be:
				be = lo[r][0] - lo[l][0] + 1
			re[lo[l][1]] += 1
			l += 1
		elif r + 1 < lp:
			r += 1
			re[lo[r][1]] -= 1
		else:
			break
	print(be)
else:
	print(0)


import math
import sys
import collections

def getdict(n):
	d = {}
	if type(n) is list:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

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

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

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)
n = ii()
a = li()
q = collections.Counter(a)
d = {}
p = 0
for i in q:
	if q[i] > 1:
		d[i] = q[i] - 1
		p += d[i]
if p == 0:
	exit(print(0))
r = n
for i in range(n):
	t = d.copy()
	c = p
	for j in range(i, n):
		if a[j] in t and t[a[j]] > 0:
			c -= 1
			t[a[j]] -= 1
		if c == 0:
			r = min(r, j - i + 1)
			break
print(r)


n = int(input())
ai = list(map(int, input().split()))
ar = {}
num = 1
for i in range(n):
	try:
		ar[ai[i]] += 1
		num = i
		break
	except:
		ar[ai[i]] = 1
ans = num
if ans == n:
	print(0)
else:
	for i in range(num - 1, -2, -1):
		ar3 = {}
		for j in range(i + 1):
			ar3[ai[j]] = 1
		for j in range(n - 1, -1, -1):
			try:
				ar3[ai[j]] += 1
				ans = max(ans, i + 1 + n - 1 - j)
				break
			except:
				ar3[ai[j]] = 1
	print(n - ans)


N = int(input())
s = [int(x) for x in input().split()]
c = 0
kk = dict()
for i in range(0, len(s)):
	if kk.get(s[i]) == None:
		kk[s[i]] = 1
	elif kk[s[i]] == 1:
		c += 1
		kk[s[i]] += 1
	else:
		kk[s[i]] += 1
if c == 0:
	print(0)
else:
	ans = 1000000000000
	for i in range(0, len(s)):
		temp = c
		for j in range(i, len(s)):
			kk[s[j]] -= 1
			if kk[s[j]] == 1:
				temp -= 1
			if temp == 0:
				ans = min(ans, j - i + 1)
		for j in range(i, len(s)):
			kk[s[j]] += 1
	print(ans)


n = int(input())
a = [int(x) for x in input().split()]
from collections import defaultdict as dd
d = dd(int)
for i in a:
	d[i] += 1
(cnt, ans) = (0, 10000000)
for i in d:
	if d[i] >= 2:
		cnt += 1
if cnt == 0:
	print(0)
else:
	for i in range(n):
		d2 = dd(int)
		ct = 0
		for j in range(i, n):
			d2[a[j]] += 1
			if d2[a[j]] == d[a[j]] - 1 and d[a[j]] >= 2:
				ct += 1
			if ct == cnt:
				ans = min(ans, abs(j - i + 1))
	print(ans)


GI = lambda : int(input())
GIS = lambda : map(int, input().split())
LGIS = lambda : list(GIS())
from collections import Counter

def main():
	n = GI()
	a = LGIS()
	c = Counter(a)
	dupes = sum((1 for (k, v) in c.items() if v > 1))
	i = 0
	j = -1
	mn = float('inf')
	while True:
		if dupes:
			j += 1
			if j == n:
				break
			c[a[j]] -= 1
			if c[a[j]] == 1:
				dupes -= 1
		else:
			if i > j:
				mn = 0
				break
			c[a[i]] += 1
			if c[a[i]] == 2:
				dupes += 1
			i += 1
		if dupes == 0:
			mn = min(j - i + 1, mn)
	print(mn)
main()


def case(mid):
	(k, a) = (0, n - mid)
	for i in range(a + 1):
		setting = len(set(lst[:i] + lst[i + mid:]))
		if setting == a:
			k = 1
			break
	return k
n = int(input())
lst = [*map(int, input().split())]
length = len(set(lst))
(i, j) = (0, n - 1)
while i + 1 < j:
	mid = (i + j) // 2
	item = case(mid)
	if item == 1:
		if n - mid >= length:
			i = mid
		else:
			j = mid
	else:
		i = mid
if case(i) == 1:
	if n - i >= length:
		print(i)
	else:
		print(j)
else:
	print(j)


from sys import stdin, stdout
import heapq
import cProfile, math
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
import sys
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
fac_warm_up = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7

class MergeFind:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n
		self.lista = [[_] for _ in range(n)]

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]
		self.lista[a] += self.lista[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def display(string_to_print):
	stdout.write(str(string_to_print) + '\n')

def prime_factors(n):
	factors = dict()
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		while n % i == 0:
			if i in factors:
				factors[i] += 1
			else:
				factors[i] = 1
			n = n // i
	if n > 2:
		factors[n] = 1
	return factors

def all_factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def fibonacci_modP(n, MOD):
	if n < 2:
		return 1
	return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD

def factorial_modP_Wilson(n, p):
	if p <= n:
		return 0
	res = p - 1
	for i in range(n + 1, p):
		res = res * cached_fn(InverseEuler, i, p) % p
	return res

def binary(n, digits=20):
	b = bin(n)[2:]
	b = '0' * (digits - len(b)) + b
	return b

def is_prime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	if n % 3 == 0:
		return False
	i = 5
	w = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += w
		w = 6 - w
	return True

def generate_primes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	return prime
factorial_modP = []

def warm_up_fac(MOD):
	global factorial_modP, fac_warm_up
	if fac_warm_up:
		return
	factorial_modP = [1 for _ in range(fac_warm_up_size + 1)]
	for i in range(2, fac_warm_up_size):
		factorial_modP[i] = factorial_modP[i - 1] * i % MOD
	fac_warm_up = True

def InverseEuler(n, MOD):
	return pow(n, MOD - 2, MOD)

def nCr(n, r, MOD):
	global fac_warm_up, factorial_modP
	if not fac_warm_up:
		warm_up_fac(MOD)
		fac_warm_up = True
	return factorial_modP[n] * (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD) % MOD) % MOD

def test_print(*args):
	if testingMode:
		print(args)

def display_list(list1, sep=' '):
	stdout.write(sep.join(map(str, list1)) + '\n')

def display_2D_list(li):
	for i in li:
		print(i)

def prefix_sum(li):
	sm = 0
	res = []
	for i in li:
		sm += i
		res.append(sm)
	return res

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))
memory = dict()

def clear_cache():
	global memory
	memory = dict()

def cached_fn(fn, *args):
	global memory
	if args in memory:
		return memory[args]
	else:
		result = fn(*args)
		memory[args] = result
		return result

def ncr(n, r):
	return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))

def binary_search(i, li):
	fn = lambda x: li[x] - x // i
	x = -1
	b = len(li)
	while b >= 1:
		while b + x < len(li) and fn(b + x) > 0:
			x += b
		b = b // 2
	return x
TestCases = False
fac_warm_up_size = 10 ** 5 + 100
optimise_for_recursion = False

def main():
	n = get_int()
	li = get_list()
	(l, r) = (0, -1)
	cnt = Counter(li)
	for i in range(n):
		st = set()
		if cnt[li[i]] == 1:
			continue
		brk = False
		for j in range(0, i):
			if li[j] in st:
				brk = True
				break
			st.add(li[j])
		if brk:
			continue
		for j in range(n - 1, i - 1, -1):
			if li[j] in st:
				break
			st.add(li[j])
		if r - l < 0 or r - l >= j - i:
			(l, r) = (i, j)
	print(r - l + 1)
if TestCases:
	for i in range(get_int()):
		main()
else:
	main() if not optimise_for_recursion else threading.Thread(target=main).start()


from sys import stdin
from collections import defaultdict

def iinput():
	return int(stdin.readline())

def sinput():
	return input()

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
n = iinput()
a = linput()
ans = n - 1
for i in range(n):
	freq = defaultdict(int)
	prefix = True
	for j in range(i):
		freq[a[j]] += 1
		if freq[a[j]] == 2:
			prefix = False
			break
	suffix = n
	for j in range(n - 1, i - 1, -1):
		freq[a[j]] += 1
		if freq[a[j]] == 1:
			suffix = j
		else:
			break
	if prefix:
		ans = min(ans, suffix - i)
print(ans)


n = int(input())
l = list(map(int, input().strip().split()))
d = {}
for i in range(len(l)):
	t = d.get(l[i], [])
	t.append(i)
	d[l[i]] = t
left = {}
right = {}
rid = {}
l = []
num = 0
k = 0
m = {}
u = {}
r = -1
for i in d.keys():
	if len(d[i]) == 1:
		continue
	elif len(d[i]) == 2:
		left[num] = d[i][0]
		l.append([d[i][0], num, k])
		right[num] = d[i][0]
		rid[num] = k
		num += 1
		left[num] = d[i][1]
		l.append([d[i][1], num, k])
		right[num] = d[i][1]
		rid[num] = k
		num += 1
		m[k] = 0
		u[k] = d[i][1]
		k += 1
		r = max(r, d[i][0])
	else:
		left[num] = d[i][0]
		l.append([d[i][0], num, k])
		right[num] = d[i][-2]
		rid[num] = k
		num += 1
		left[num] = d[i][1]
		l.append([d[i][1], num, k])
		right[num] = d[i][-1]
		rid[num] = k
		num += 1
		m[k] = 0
		u[k] = d[i][-1]
		k += 1
		r = max(r, d[i][-2])
if len(l) == 0:
	print(0)
else:
	used = 0
	le = 1000000000000
	l.sort()
	ans = 1000000000000
	for i in l:
		m[i[2]] += 1
		r = max(r, right[i[1]])
		ans = min(ans, r - min(i[0], le) + 1)
		if m[i[2]] == 1:
			r = max(r, u[i[2]])
		if m[i[2]] == 2:
			le = min(le, i[0])
	print(ans)


class Uniqueness:

	def __init__(self):
		self.N = 0
		self.elementList = []
		self.counterList = dict()

	def getMinLengthSubStringForUniqueString(self):
		self.N = int(input())
		tmp = str(input()).split(' ')
		for x in range(self.N):
			element = int(tmp[x])
			self.elementList.append(element)
			if element not in self.counterList:
				self.counterList[element] = 1
			else:
				self.counterList[element] += 1
		minLength = self.N
		if len(self.counterList) == self.N:
			print(0)
			return
		for i in range(self.N):
			tmpCounterList = self.counterList.copy()
			for j in range(i, self.N):
				tmpCounterList[self.elementList[j]] -= 1
				if tmpCounterList[self.elementList[j]] == 0:
					tmpCounterList.pop(self.elementList[j])
				if len(tmpCounterList) == self.N - j + i - 1:
					minLength = min(minLength, j - i + 1)
		print(minLength)
u = Uniqueness()
u.getMinLengthSubStringForUniqueString()


import sys
import math
from collections import defaultdict as ddict
from collections import deque
import heapq
from queue import Queue
fast_inp = lambda : sys.stdin.readline().split('\n')[0]
read_list = lambda : fast_inp().split(' ')
read_listi = lambda : list(map(int, fast_inp().split(' ')))
read_listf = lambda : list(map(float, fast_inp().split(' ')))
read_int = lambda : int(fast_inp())
read_float = lambda : float(fast_inp())
read = lambda : fast_inp()
DEBUG = True

def debug(**kwargs):
	if not DEBUG:
		return
	print('*' * 20)
	for (k, v) in kwargs.items():
		print(f'\t{k}:{v}\t')
	print('*' * 20)

def solve() -> None:
	n = read_int()
	arr = read_listi()
	freq = ddict(int)
	best = n - 1
	for i in range(n):
		valid = True
		for j in range(i):
			freq[arr[j]] += 1
			if freq[arr[j]] == 2:
				valid = False
				break
		if not valid:
			freq.clear()
			continue
		min_suff_idx = n
		for j in range(i, n)[::-1]:
			freq[arr[j]] += 1
			if freq[arr[j]] == 1:
				min_suff_idx = j
			else:
				break
		best = min(best, min_suff_idx - i)
		freq.clear()
	print(best)
t = 1
for _ in range(t):
	solve()


def b_uniqueness(arr):
	i = 0
	j = len(arr) - 1
	last_pos = {}
	for (ind, elem) in enumerate(arr):
		last_pos[elem] = ind
	repeated_set = set()
	while arr[j] not in repeated_set:
		repeated_set.add(arr[j])
		j -= 1
	repeated_set.clear()
	ans = j + 1
	while arr[i] not in repeated_set:
		repeated_set.add(arr[i])
		j = max(j, last_pos[arr[i]])
		ans = min(ans, j - i)
		i += 1
		if i == len(arr):
			break
	return ans
num_elem = int(input())
elem = input().split(' ')
print(b_uniqueness(elem))


from collections import deque, Counter
_ = input()
arr = [int(i) for i in input().split()]
if len(arr) == len(set(arr)):
	print(0)
	exit()
c = Counter(arr)
remove = Counter({k: v - 1 for (k, v) in c.items() if v != 1})
res = float('inf')
for i in range(len(arr)):
	criteria = 0
	c = Counter()
	for j in range(i, len(arr)):
		c[arr[j]] += 1
		if c[arr[j]] == remove[arr[j]]:
			criteria += 1
		if criteria == len(remove):
			break
	if criteria == len(remove):
		res = min(j - i + 1, res)
print(res)


from collections import Counter
n = int(input())
arr = [int(i) for i in input().split()]
extra = n - len(list(set(arr)))
ans = n
if extra == 0:
	print(0)
else:
	for i in range(n):
		Dict = Counter(arr)
		rem = extra
		for j in range(i, n):
			if Dict[arr[j]] > 1:
				Dict[arr[j]] -= 1
				rem -= 1
			if rem == 0:
				ans = min(ans, j - i + 1)
				break
	print(ans)


n = int(input())
num = [int(x) for x in input().split(' ')]
answer = 1000000000
new = list()
for i in range(n):
	j = 0
	for j in range(i):
		if num[j] not in new:
			new.append(num[j])
		else:
			break
	for k in reversed(num):
		if k not in new:
			new.append(k)
		else:
			break
	answer = min(answer, n - len(new))
	new.clear()
print(answer)


n = int(input())
a = list(map(int, input().split(' ')))
if len(set(a)) == n:
	print(0)
else:
	first = dict()
	last = dict()
	for i in range(n):
		if not a[i] in first:
			first[a[i]] = i
		last[a[i]] = i
	r = n
	while last[a[r - 1]] == r - 1:
		r -= 1
	ans = r
	for i in range(n):
		if first[a[i]] != i:
			break
		r = max(r, i + 1, last[a[i]] + 1)
		ans = min(ans, r - i - 1)
	print(ans)


def check(arr, k):
	dictionary = {}
	for i in range(len(arr) - k):
		if arr[i] in dictionary:
			dictionary[arr[i]] += 1
		else:
			dictionary[arr[i]] = 1
	if len(dictionary) == len(arr) - k:
		return True
	for i in range(len(arr) - k):
		delete = len(arr) - k - i - 1
		add = len(arr) - 1 - i
		dictionary[arr[delete]] -= 1
		if dictionary[arr[delete]] == 0:
			del dictionary[arr[delete]]
		if arr[add] in dictionary:
			dictionary[arr[add]] += 1
		else:
			dictionary[arr[add]] = 1
		if len(dictionary) == len(arr) - k:
			return True
	return False
N = int(input())
line = input().split()
arr = [0] * N
for i in range(N):
	arr[i] = int(line[i])
a = 0
b = N
while a < b:
	p = (a + b) // 2
	if check(arr, p):
		b = p
	else:
		a = p + 1
print(a)


n = int(input())
arr = [int(x) for x in input().split()]
answer = 2001
for _ in range(2):
	cnt = set()
	for left in range(n):
		if arr[left] in cnt:
			break
		cnt.add(arr[left])
		tmp = set()
		for right in range(n - 1, left, -1):
			if arr[right] in cnt or arr[right] in tmp:
				answer = min(answer, right - left)
				break
			tmp.add(arr[right])
	arr.reverse()
print(answer if answer != 2001 else 0)


from collections import defaultdict

def can_remove(a, n):
	nums = defaultdict(lambda : 0)
	for i in range(n, len(a)):
		nums[a[i]] += 1
	if all([x <= 1 for x in list(nums.values())]):
		return True
	for remove_num in range(n, len(a)):
		nums[a[remove_num]] -= 1
		nums[a[remove_num - n]] += 1
		if all([x <= 1 for x in list(nums.values())]):
			return True
	return False
n = int(input())
a = list(map(int, input().split()))
impossible = -1
possible = len(a) - 1
while possible - impossible > 1:
	m = (possible + impossible) // 2
	if can_remove(a, m):
		possible = m
	else:
		impossible = m
print(possible)


def rparse(A):
	n = len(A)
	m = n
	rset = set()
	for r in range(n - 1, -1, -1):
		if A[r] not in rset:
			rset.add(A[r])
			m = min(m, r)
			lset = set(rset)
			for l in range(r):
				if A[l] not in lset:
					lset.add(A[l])
					m = min(m, r - l - 1)
				else:
					break
		else:
			break
	return m

def lparse(A):
	n = len(A)
	m = n
	lset = set()
	for l in range(n):
		if A[l] not in lset:
			lset.add(A[l])
			m = min(m, n - l - 1)
			rset = set(lset)
			for r in range(n - 1, l, -1):
				if A[r] not in rset:
					rset.add(A[r])
					m = min(m, r - l - 1)
				else:
					break
		else:
			break
	return m

def unique(A):
	ret = set()
	for a in A:
		if a in ret:
			return False
		ret.add(a)
	return True

def on3(A):
	if unique(A):
		print(0)
		return
	n = len(A)
	m = n
	for l in range(n):
		for r in range(l + n, l - 1, -1):
			if unique(A[:l] + A[r + 1:]):
				m = min(m, r - l + 1)
			else:
				break
	return m

def main():
	n = int(input())
	A = list(map(int, input().split()))
	print(min(lparse(A), rparse(A)))

def test():
	import random
	A = [random.randint(0, 10) for _ in range(random.randint(10, 20))]
	if on3(A) != rparse(A):
		print(*A)
import sys
input = sys.stdin.readline
main()


import sys
input = sys.stdin.readline
n = int(input())
l = list(map(int, input().split()))
s = set()
ans = n + 1
if len(set(l)) == n:
	print(0)
else:
	for i in range(n):
		t = s.copy()
		for j in range(n - 1, i - 1, -1):
			if l[j] not in t:
				t.add(l[j])
			else:
				if j - i + 1 < ans:
					ans = j - i + 1
				break
		if l[i] not in s:
			s.add(l[i])
		else:
			break
	print(ans)


import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
l = list(map(int, input().split()))
d = deque()
p = 0
while p < n:
	if l[p] not in d:
		d.append(l[p])
		p += 1
	else:
		break
ans = n - p
q = n - 1
e = deque()
cnt = p
while q >= 0:
	if l[q] in e:
		break
	elif l[q] in d:
		e.append(l[q])
		cnt += 1
		while True:
			y = d.pop()
			cnt -= 1
			if y == l[q]:
				break
		ans = min(ans, n - cnt)
		q -= 1
	else:
		e.append(l[q])
		cnt += 1
		ans = min(ans, n - cnt)
		q -= 1
print(ans)


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')
n = int(input())
vals = list(map(int, input().split()))
maxsofar = 0
for s in range(n + 1):
	right = s
	left = n - s
	setty = set([])
	for i in range(s):
		if not vals[i] in setty:
			setty.add(vals[i])
		else:
			break
	for i in range(n - 1, s - 1, -1):
		if not vals[i] in setty:
			setty.add(vals[i])
		else:
			break
	maxsofar = max(maxsofar, len(setty))
print(n - maxsofar)


def main():
	n = int(input())
	aa = list(map(int, input().split()))
	sa = set()
	for j in range(n - 1, -1, -1):
		if aa[j] in sa:
			best = j + 1
			break
		sa.add(aa[j])
	else:
		print(0)
		return
	sa = set()
	for (i, a) in enumerate(aa):
		if a in sa:
			if n - i < best:
				best = n - i
			break
		sa.add(a)
		sb = set()
		for j in range(n - 1, i - 1, -1):
			if aa[j] in sa or aa[j] in sb:
				break
			sb.add(aa[j])
		if j - i < best:
			best = j - i
	print(best)
main()


def solve1(nums, n):
	dl = set()
	mx = 0
	for (il, xl) in enumerate(nums):
		dr = set()
		for (ir, xr) in enumerate(nums[::-1]):
			if ir + il >= n - 1 or xr in dr or xr in dl:
				break
			dr.add(xr)
		if xl in dl:
			mx = max(mx, len(dl) + len(dr))
			break
		if xl not in dr:
			mx = max(mx, len(dl) + len(dr) + 1)
		else:
			mx = max(mx, len(dl) + len(dr))
		dl.add(xl)
	return n - mx

def solve2(nums, n):
	last = {}
	mx = 0
	i = 0
	for (j, num) in enumerate(nums + nums):
		if num in last:
			i = max(i, last[num] + 1)
		last[num] = j
		if i == 0 or j == n - 1 or (0 < i < n and n <= j):
			mx = max(mx, j - i + 1)
	return n - mx
solve = solve2

def main():
	from sys import stdin
	n = int(stdin.readline().strip())
	nums = list(map(int, stdin.readline().strip().split()))
	print(solve(nums, n))
main()


n = int(input())
nums = list(map(int, input().split(' ')))
left = set()
res = n
for i in range(n):
	tmp = left.copy()
	for j in range(n - 1, -1, -1):
		if nums[j] in tmp:
			break
		tmp.add(nums[j])
		res = min(res, j - i)
	if nums[i] in left:
		break
	left.add(nums[i])
	res = min(res, n - i - 1)
print(res)


from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
R = lambda : map(int, input().split())
n = int(input())
arr = list(R())
idxes = defaultdict(list)
for (i, num) in enumerate(arr):
	idxes[num].append(i)
res = n
for st in range(n):
	ed = st
	for idxArr in filter(lambda x: len(x) > 1, idxes.values()):
		if bisect_left(idxArr, st) > 1:
			ed = -1
			break
		ed = max(ed, idxArr[-1 if bisect_left(idxArr, st) == 1 else -2] + 1)
	if ed > -1:
		res = min(res, ed - st)
print(res)


n = int(input())
a = list(map(int, input().split()))
k = set(a)
m = len(k)
if n == m:
	print(0)
else:
	ans = 9999999999999999999999999999999999999999999999999999999999999999
	for i in range(n):
		mp = {}
		for j in range(n):
			if a[j] in mp:
				mp[a[j]] += 1
			else:
				mp[a[j]] = 1
		for j in range(i, n):
			mp[a[j]] -= 1
			if mp[a[j]] == 0:
				mp.pop(a[j])
			p = len(mp)
			if p == n - j + i - 1:
				ans = min(ans, j - i + 1)
				break
	print(ans)


n = int(input())
a = [int(i) for i in input().split()]
min_l = 0
max_l = 0
min_r = 0
found_max = False
found_min = False
for i in range(n - 1):
	if a[i] in a[i + 1:]:
		min_r = i
		if not found_min:
			found_min = True
			min_l = i
for i in range(1, n):
	if a[i] in a[:i] and (not found_max):
		found_max = True
		max_l = i
if not found_min:
	print(0)
else:
	ans = min_r - min_l + 1
	act_r = min_r
	for i in range(min_l, max_l):
		while act_r < n - 1 and a[i] in a[act_r + 1:]:
			act_r += 1
		ans = min([ans, act_r - i])
	print(ans)


import copy
num = input()
values = [int(v) for v in input().split()]
init = set()
min_value = len(values)
for (i, v1) in enumerate(values):
	data = copy.copy(init)
	for (j, v2) in enumerate(reversed(values)):
		if v2 in data:
			break
		data.add(v2)
		min_value = min(min_value, len(values) - i - j - 1)
	if v1 in init:
		break
	min_value = min(min_value, len(values) - len(init) - 1)
	init.add(v1)
print(min_value)


import sys
import copy
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
from collections import Counter
C = Counter(A)
for a in A:
	if C[a] <= 1:
		del C[a]
if not C:
	print(0)
	return
ANS = n - 1
for i in range(n):
	D = copy.deepcopy(C)
	for j in range(i, n):
		if D[A[j]] >= 1:
			D[A[j]] -= 1
		if D[A[j]] == 1:
			del D[A[j]]
		if not D:
			ANS = min(ANS, j - i + 1)
			break
print(ANS)


import sys
import collections
import heapq
input = sys.stdin.readline

def rints():
	return map(int, input().strip().split())

def rstr():
	return input().strip()

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

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

def gcd(a, b):
	if a == 0:
		return b
	return gcd(a % b, a)
n = rint()
a = rintas()
s = set()
i = 0
ans = n
while i < n and a[i] not in s:
	s |= {a[i]}
	i += 1
while i + 1:
	while n - 1 >= 0 and a[n - 1] not in s:
		n -= 1
		s |= {a[n]}
	ans = min(ans, n - i)
	i -= 1
	s -= {a[i]}
print(ans)


n = int(input())
lst = [int(i) for i in input().split()]
(d, count1) = ({}, 0)
for elem in lst:
	d[elem] = d.get(elem, 0) + 1
	if d[elem] == 2:
		count1 += 1
result = n
if len(d) == n:
	result = 0
for i in range(n):
	f = d.copy()
	count2 = count1
	for j in range(i, n):
		f[lst[j]] -= 1
		if f[lst[j]] == 1:
			count2 -= 1
		if count2 == 0:
			result = min(result, j - i + 1)
			break
print(result)


from bisect import bisect_left
import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
from collections import deque
from functools import cmp_to_key
import math
import heapq

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	n = iin()
	l = ain()
	ans = n - 1
	if len(set(l)) == n:
		print(0)
		sys.exit(0)
	for i in range(n):
		d = {}
		flag = 0
		for j in range(i):
			if l[j] in d:
				d[l[j]] += 1
			else:
				d[l[j]] = 1
			if d[l[j]] == 2:
				flag = 1
				break
		if flag:
			continue
		min_s = n
		for j in range(n - 1, i - 1, -1):
			if l[j] in d:
				break
			else:
				d[l[j]] = 1
				min_s = j
		ans = min(ans, min_s - i)
	print(ans)
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')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
n = int(input())
a = list(map(int, input().split()))
d = dict()
for elem in a:
	d[elem] = d.get(elem, 0) + 1
not_uniq = 0
for elem in d:
	if d[elem] > 1:
		not_uniq += 1
if not_uniq == 0:
	print(0)
	return
ans = 10 ** 9
left = 0
right = -1
while True:
	if not_uniq == 0:
		ans = min(ans, right - left + 1)
	if not_uniq > 0:
		right += 1
		if right >= n:
			break
		elem = a[right]
		d[elem] -= 1
		if d[elem] == 1:
			not_uniq -= 1
	else:
		elem = a[left]
		d[elem] += 1
		if d[elem] == 2:
			not_uniq += 1
		left += 1
print(ans)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
from collections import deque
n = int(input())
l = list(map(int, input().split()))
ans = n
for i in range(n + 1):
	s = set(l[:i])
	if len(s) < i:
		break
	b = n - 1
	while b >= i and l[b] not in s:
		s.add(l[b])
		b -= 1
	ans = min(ans, b - i + 1)
print(ans)


n = int(input())
l = list(map(int, input().split()))
d = {}
f = {}
c = 0
for i in l:
	if i not in d:
		d[i] = 1
	else:
		d[i] += 1
		f[i] = d[i]
		if d[i] == 2:
			c += 1
if len(f) == 0:
	print(0)
else:
	z = 10000000000000000
	for i in range(n):
		x = 0
		e = dict(f)
		for j in range(i, n):
			if l[j] in e:
				e[l[j]] -= 1
				if e[l[j]] == 1:
					x += 1
				if x == c:
					z = min(z, j - i + 1)
					break
	print(z)


n = int(input().strip())
li = [int(x) for x in input().split()]
se = {}
sese = set()
for (i, nu) in enumerate(li):
	if nu not in se:
		se[nu] = i
	else:
		break
res = len(se)
ma = 10 ** 10
for (i, nu) in enumerate(reversed(li), 1):
	if nu not in sese:
		sese.add(nu)
		if nu not in se:
			ma = min(ma, len(se))
			res = max(res, ma + i)
		else:
			ma = min(se[nu], ma)
			res = max(res, ma + i)
	else:
		break
se = {}
sese = set()
la = list(reversed(li))
for (i, nu) in enumerate(la):
	if nu not in se:
		se[nu] = i
	else:
		break
res = max(res, len(se))
ma = 10 ** 10
for (i, nu) in enumerate(reversed(la), 1):
	if nu not in sese:
		sese.add(nu)
		if nu not in se:
			ma = min(ma, len(se))
			res = max(res, ma + i)
		else:
			ma = min(se[nu], ma)
			res = max(res, ma + i)
	else:
		break
print(n - res)


n = int(input())
l = list(map(int, input().split()))
ans = n
for i in range(n):
	s = set()
	f = False
	for j in range(i):
		if l[j] in s:
			f = True
			break
		s.add(l[j])
	if f:
		continue
	j = n - 1
	while j >= i:
		if l[j] in s:
			break
		s.add(l[j])
		j = j - 1
	ans = min(ans, j - i + 1)
print(ans)


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

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

def getArr():
	return list(map(int, input().split()))

def getStr():
	return input().strip()

def addDictList(d, key, val):
	if key not in d:
		d[key] = []
	d[key].append(val)

def addDictInt(d, key, val):
	if key not in d:
		d[key] = 0
	d[key] = val

def addDictCount(d, key):
	if key not in d:
		d[key] = 0
	d[key] += 1

def addDictSum(d, key, val):
	if key not in d:
		d[key] = 0
	d[key] += val

def foo(arr):
	global d2
	for i in d2:
		if arr.count(i) > 1:
			return False
	return True

def foo2(begin, end):
	global d2
	for k in d2:
		if not (d2[k][0] >= begin and d2[k][-2] <= end or (d2[k][1] >= begin and d2[k][-1] <= end)):
			return False
	return True
n = getInt()
a = getArr()
m = 2001
d = {}
for i in range(n):
	addDictList(d, a[i], i)
d2 = {}
minLen = 2001
for k in d:
	if len(d[k]) > 1:
		d2[k] = d[k]
		minLen = min(minLen, d[k][-1] - d[k][1] + 1, d[k][-2] - d[k][0] + 1)
if len(d2) == 0:
	print(0)
else:
	for l in range(minLen, n):
		for begin in range(n - l + 1):
			res = foo2(begin, begin + l - 1)
			if res:
				exit(print(l))
				break


from collections import defaultdict
mini = 10 ** 18
n = int(input())
l = list(map(int, input().split()))
if n == len(set(l)):
	print(0)
	exit()
for i in range(n):
	hash = defaultdict(int)
	for j in range(n):
		hash[l[j]] += 1
	for j in range(i, n):
		hash[l[j]] -= 1
		if hash[l[j]] == 0:
			hash.pop(l[j])
		if len(hash) == n - j + i - 1:
			mini = min(mini, j - i + 1)
print(mini)


def inp():
	return list(map(int, input().split()))
(n,) = inp()
l = inp()
a = set()
aa = set()
ans = n - 1
for i in range(-1, n):
	if i >= 0:
		if l[i] in a:
			break
		a.add(l[i])
	aa.clear()
	for j in range(n - 1, -1, -1):
		if l[j] in aa or l[j] in a:
			break
		aa.add(l[j])
	ans = min(ans, j - i)
print(ans)


n = int(input())
V = list(map(int, input().split()))
A = set()
B = set()
T = n - 1
for i in range(-1, n):
	if i >= 0:
		if V[i] in A:
			break
		A.add(V[i])
	B.clear()
	for j in range(n - 1, -1, -1):
		if V[j] in B or V[j] in A:
			break
		B.add(V[j])
	T = min(T, j - i)
print(T)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
			self.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')
import time
start_time = time.time()
import collections as col
import math, string
from functools import reduce

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def solve():
	N = getInt()
	A = getInts()
	left_set = set()
	right_set = set()
	min_right = N
	right_lookup = col.defaultdict(int)
	for i in range(N - 1, -1, -1):
		if A[i] in right_set:
			break
		right_set.add(A[i])
		right_lookup[A[i]] = i
		min_right = i
	best = len(right_set)
	if best == N:
		return 0
	for i in range(N):
		if A[i] in left_set:
			break
		if A[i] in right_set:
			max_del = right_lookup[A[i]]
			for j in range(min_right, max_del + 1):
				right_set.remove(A[j])
			min_right = max_del + 1
		left_set.add(A[i])
		best = max(len(left_set) + len(right_set), best)
	return N - best
print(solve())


n = int(input())
a = list(map(int, input().split()))
ans = n - 1
f = dict()
for i in range(n):
	p = True
	for j in range(i):
		if a[j] in f:
			f[a[j]] += 1
		else:
			f[a[j]] = 1
		if f[a[j]] == 2:
			p = False
			break
	m = n
	for j in range(n - 1, i - 1, -1):
		if a[j] in f:
			f[a[j]] += 1
		else:
			f[a[j]] = 1
		if f[a[j]] == 1:
			m = j
		else:
			break
	if p:
		ans = min(ans, m - i)
	f.clear()
print(ans)


from sys import stdin
input = stdin.readline
n = int(input())
(*a,) = map(int, input().split())
if len(a) == len(set(a)):
	exit(print(0))
s = set()
for i in range(n - 1, -1, -1):
	if a[i] in s:
		ans = i + 1
		break
	s.add(a[i])
s = set()
for i in range(n):
	if a[i] in s:
		break
	s.add(a[i])
	tmp = set()
	for j in range(n - 1, i, -1):
		if a[j] in tmp or a[j] in s:
			ans = min(ans, j - i)
			break
		tmp.add(a[j])
print(ans)


import sys, os, io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_left, bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	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 SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def si():
	return input()

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

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

def li():
	return list(map(int, input().split()))
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve():
	n = ii()
	a = li()
	s = set(a)
	if len(s) == n:
		print(0)
		return
	ans = float('inf')
	d = defaultdict(lambda : 0)
	for i in a:
		d[i] += 1
	morethan = set()
	for i in d.keys():
		if d[i] > 1:
			morethan.add(i)
	for i in range(n):
		d1 = d.copy()
		s1 = ()
		cnt = len(morethan)
		morethan1 = morethan.copy()
		for j in range(i, n):
			d1[a[j]] -= 1
			if a[j] in morethan1:
				if d1[a[j]] <= 1:
					cnt -= 1
					morethan1.remove(a[j])
			if cnt == 0:
				ans = min(ans, j - i + 1)
	print(ans)
t = 1
for _ in range(t):
	solve()


n = int(input())
array = list(map(int, input().split()))
indexx = n
dic = {}
index = {}
check = {}
flag = 5
for x in range(indexx - 1, -1, -1):
	if array[x] in dic:
		flag = 6
		break
	else:
		dic[array[x]] = 5
		index[array[x]] = x
if flag == 5:
	print(0)
else:
	answer = pow(10, 9)
	lam = -5
	for y in range(0, n):
		if y == 0:
			current = x + 1
			if current < answer:
				answer = current
		else:
			previous = array[y - 1]
			if previous in index:
				value = index[previous]
				if value > lam:
					lam = value
				current = lam - y + 1
				if current < answer:
					answer = current
			elif lam < 0:
				current = x - y + 1
				if current < answer:
					answer = current
			else:
				current = lam - y + 1
				if current < answer:
					answer = current
		if array[y] in check:
			break
		else:
			check[array[y]] = 1
	print(answer)


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def case():
	n = I()
	a = li()
	p = True
	k = -1
	ans = n - 1
	for i in range(n):
		p = True
		d = defaultdict(int)
		for j in range(i):
			d[a[j]] += 1
			if d[a[j]] == 2:
				p = False
				break
		m = n
		for j in range(n - 1, i - 1, -1):
			d[a[j]] += 1
			if d[a[j]] == 1:
				m = j
			else:
				break
		if p:
			ans = min(ans, m - i)
	print(ans)
for _ in range(1):
	case()


n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
	d[a[i]] = 0
i = 0
j = n - 1
length = -1
while i < n:
	if d[a[i]] == 0:
		d[a[i]] = 1
		i += 1
	else:
		break
length = max(i, length)
while j >= i:
	if d[a[j]] == 0:
		d[a[j]] = 1
		j -= 1
	elif i > 0:
		length = max(length, i + n - 1 - j)
		i -= 1
		d[a[i]] = 0
	else:
		length = max(i + n - 1 - j, length)
		break
print(n - length)


from collections import defaultdict
n = int(input())
arr = list(map(int, input().split()))
ans = n - 1
for i in range(n):
	frequency = defaultdict(int)
	valid_prefix = True
	for j in range(i):
		frequency[arr[j]] += 1
		if frequency[arr[j]] == 2:
			valid_prefix = False
			break
	suffix = n
	j = n - 1
	while j >= i:
		frequency[arr[j]] += 1
		if frequency[arr[j]] == 1:
			suffix = j
		else:
			break
		j -= 1
	if valid_prefix:
		ans = min(ans, suffix - i)
print(ans)


class Solution:

	def uniquenessArray(n, arr) -> int:
		ret = n - 1
		for i in range(len(arr)):
			larr = arr[:i]
			if len(set(larr)) < len(larr):
				break
			(l, r, j) = (i, len(arr), len(arr) + 1)
			while l <= r:
				m = (l + r) // 2
				rarr = arr[m:]
				if len(set(larr + rarr)) < len(larr + rarr):
					l = m + 1
				else:
					r = m - 1
					j = m
			if ret > j - i and j < len(arr) + 1:
				ret = j - i
		return ret
print(Solution.uniquenessArray(int(input()), [int(x) for x in input().strip().split()]))


from collections import Counter, defaultdict

def remove(nums):
	n = len(nums)
	min_size = n
	for i in range(n):
		valid_prefix = True
		seen = set()
		for j in range(i):
			num = nums[j]
			if num in seen:
				valid_prefix = False
				break
			else:
				seen.add(num)
		if not valid_prefix:
			break
		j = n - 1
		while j >= i - 1:
			num = nums[j]
			if num in seen:
				break
			else:
				seen.add(num)
			j -= 1
		min_size = min(j - i + 1, min_size)
	return min_size

def codeforces():
	n = int(input())
	nums = list(map(int, input().split()))
	print(remove(nums))

def test():
	assert remove([1, 2, 3]) == 0
	assert remove([1, 1, 2, 2]) == 2
	assert remove([1, 4, 1, 4, 9]) == 2
codeforces()


def answer(n, A):
	maxi = 0
	for i in range(n + 1):
		s = set(A[:i])
		if len(s) < i:
			break
		r = n - 1
		while A[r] not in s:
			s.add(A[r])
			r -= 1
		maxi = max(maxi, len(s))
	return n - maxi
n = int(input())
arr = list(map(int, input().split()))
print(answer(n, arr))


from collections import defaultdict
n = int(input())
arr = list(map(int, input().split()))
dct = defaultdict(lambda : [])
for (i, x) in enumerate(arr):
	dct[x].append(i)
rarr = [((v[0], v[-2]), (v[1], v[-1])) for v in dct.values() if len(v) > 1]
if not rarr:
	print(0)
	exit()
(rdp1, rdp2) = rarr[0]
rdpr = set(rarr[0])
for (r1, r2) in rarr:
	nrdpr = []
	for rng in rdpr:
		l1 = min(rng[0], r1[0])
		rg1 = max(rng[1], r1[1])
		e1 = rng[0] - l1 + rg1 - rng[1]
		l2 = min(rng[0], r2[0])
		rg2 = max(rng[1], r2[1])
		e2 = rng[0] - l2 + rg2 - rng[1]
		if e1 > e2:
			rng = (l2, rg2)
		elif e2 < e1:
			rng = (l1, rg1)
		else:
			rng = (l1, rg1)
			nrng = (l2, rg2)
			nrdpr.append(nrng)
		nrdpr.append(rng)
	rdpr = set(nrdpr)
print(min(map(lambda x: x[1] - x[0] + 1, rdpr)))


n = int(input())
l = list(map(int, input().split()))
l.append(-1)
n += 1
pie = {}
ost = {}
for i in range(n):
	pie[l[i]] = -1
	ost[l[i]] = -1
for i in range(n):
	if pie[l[i]] == -1:
		pie[l[i]] = i
for i in range(n):
	j = n - 1 - i
	if ost[l[j]] == -1:
		ost[l[j]] = j
pyk = -1
for i in range(n):
	j = n - i - 1
	if ost[l[j]] != j:
		pyk = j
		break
if pyk == -1:
	print(0)
else:
	left = 0
	right = pyk
	wynik = 100000000000
	while pie[l[max(left - 1, 0)]] == max(left - 1, 0) and left < n:
		wynik = min(wynik, right - left + 1)
		right = max(right, ost[l[left]])
		left += 1
	print(wynik)


import sys, os
from io import BytesIO, IOBase
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, 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')
(stdin, stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

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

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

def lmp():
	return list(mp())

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

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

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

def ceil(a, b):
	return (a + b - 1) // b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
n = iinp()
arr = lmp()
if len(set(arr)) == n:
	print(0)
	exit()
st = set()
mn = n
for i in range(n):
	tmp = st.copy()
	for j in range(n - 1, -1, -1):
		if arr[j] in tmp:
			break
		tmp.add(arr[j])
	mn = min(mn, j - i + 1)
	if arr[i] in st:
		break
	st.add(arr[i])
print(mn)


import sys
import math
import bisect

def solve(A):
	n = len(A)
	if len(set(A)) == n:
		return 0
	min_val = 10 ** 18
	s = set()
	t = set()
	j = n
	while j - 1 >= 0 and A[j - 1] not in t:
		t.add(A[j - 1])
		j -= 1
	min_val = min(min_val, n - len(s) - len(t))
	for i in range(n):
		if A[i] in s:
			break
		s.add(A[i])
		while A[i] in t:
			t.remove(A[j])
			j += 1
		val = n - len(s) - len(t)
		min_val = min(min_val, val)
	return min_val

def main():
	n = int(input())
	A = list(map(int, input().split()))
	ans = solve(A)
	print(ans)
main()


from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations
import sys
import bisect
import string
sys.setrecursionlimit(10 ** 7)

def SI():
	return input().split()

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

def I():
	return int(input())

def LI():
	return [int(i) for i in input().split()]
YN = ['Yes', 'No']
mo = 10 ** 9 + 7
input = sys.stdin.readline
n = I()
a = LI()
m = 0
d = defaultdict(int)
for l in range(n + 1):
	d = defaultdict(int)
	valid = True
	for i in range(l):
		d[a[i]] += 1
		if d[a[i]] > 1:
			valid = False
			break
	if not valid:
		continue
	for r in range(n - l):
		if a[n - 1 - r] in d:
			break
		d[a[n - 1 - r]] += 1
	m = max(m, l + r)
print(n - m)


from sys import stdin
n = int(stdin.readline().strip())
s = list(map(int, stdin.readline().strip().split()))
dp = [[False for i in range(n + 1)] for j in range(n + 1)]
st = set()
for i in range(n):
	if s[i] in st:
		break
	st.add(s[i])
	st1 = set()
	dp[i][n] = True
	for j in range(n - 1, i, -1):
		if s[j] in st or s[j] in st1:
			break
		dp[i][j] = True
		st1.add(s[j])
st = set()
ans = 100000
for i in range(n - 1, -1, -1):
	if s[i] in st:
		break
	st.add(s[i])
ans = min(ans, n - len(st))
for i in range(n):
	for j in range(i + 1, n + 1):
		if dp[i][j] == True:
			ans = min(ans, j - i - 1)
print(ans)


from collections import defaultdict

def f(a, n):
	if n == len(set(a)):
		return 0
	ans = float('inf')
	for i in range(n):
		cnt = defaultdict(int)
		flag = True
		for j in range(i):
			cnt[a[j]] += 1
			if cnt[a[j]] == 2:
				flag = False
				break
		mx = n
		for j in reversed(range(i, n)):
			cnt[a[j]] += 1
			if cnt[a[j]] == 1:
				mx = j
			else:
				break
		if flag == True:
			ans = min(ans, mx - i)
	return ans
a = int(input())
l = list(map(int, input().strip().split()))
print(f(l, a))


import math
import sys
from collections import defaultdict
rt = lambda : map(int, input().split())
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))

def main():
	n = ri()
	a = rl()
	if len(set(a)) == n:
		print(0)
	else:
		res = n
		pref = set()
		for i in range(n):
			if i > 0:
				if a[i - 1] in pref:
					break
				pref.add(a[i - 1])
			suff = set()
			for j in range(n - 1, i - 1, -1):
				if j < n - 1:
					if a[j + 1] in suff or a[j + 1] in pref:
						break
					suff.add(a[j + 1])
				res = min(res, j - i + 1)
		print(res)
main()


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
d = {}
c = 0
for i in l:
	d[i] = d.get(i, 0) + 1
	if d[i] == 2:
		c += 1
ans = n
if len(d) == n:
	ans = 0
for i in range(n):
	f = d.copy()
	g = c
	for j in range(i, n):
		f[l[j]] -= 1
		if f[l[j]] == 1:
			g -= 1
		if g == 0:
			ans = min(ans, j - i + 1)
			break
print(ans)


import collections
N = int(input())
A = [int(a) for a in input().split(' ')]
C = collections.Counter(A)
n_duplicate = 0
for (k, v) in C.items():
	if v > 1:
		n_duplicate += v - 1
if n_duplicate == 0:
	print('0')
else:
	ans = N
	for i in range(0, N - 1):
		T = collections.Counter(A)
		t_duplicate = n_duplicate
		for j in range(i, min(N, i + ans)):
			T[A[j]] -= 1
			if T[A[j]] >= 1:
				t_duplicate -= 1
			if t_duplicate == 0:
				ans = j + 1 - i
				break
	print(str(ans))


def check(repeat):
	for key in repeat:
		if repeat[key] > 0:
			return False
	return True
n = int(input())
array = list(map(int, input().split()))
occur = dict()
repeat = dict()
for i in array:
	if i not in occur:
		occur[i] = 1
	elif i not in repeat:
		repeat[i] = 1
	else:
		repeat[i] += 1
if len(repeat) == 0:
	print(0)
else:
	minm = 2 ** 31 - 1
	i = 0
	boolean = False
	j = 0
	while i < len(array) and j < len(array):
		while boolean == False and j < len(array):
			if array[j] in repeat:
				repeat[array[j]] -= 1
			j += 1
			if check(repeat):
				minm = min(minm, j - i)
				boolean = True
			else:
				boolean = False
		while boolean == True and i < j:
			if array[i] in repeat:
				repeat[array[i]] += 1
			i += 1
			if check(repeat):
				boolean = True
				minm = min(minm, j - i)
			else:
				boolean = False
	boolean = check(repeat)
	while boolean and i < len(array):
		if array[i] in repeat:
			repeat[array[i]] += 1
		i += 1
		if check(repeat):
			boolean = True
			minm = min('c', minm, j - i)
		else:
			boolean = False
	print(minm)


def find(mid):
	for i in range(n - mid + 1):
		curr = l[:i] + l[i + mid:]
		if len(curr) == len(set(curr)):
			return True
	return False
n = int(input())
l = list(map(int, input().split()))
low = 0
high = n
ans = 0
while low <= high:
	mid = (low + high) // 2
	if find(mid):
		ans = mid
		high = mid - 1
	else:
		low = mid + 1
print(ans)


import sys
input = sys.stdin.readline
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
d = Counter(a)
toRemove = 0
for i in d:
	if d[i] > 1:
		toRemove += d[i] - 1
ans = float('inf')
prev = d.copy()
for i in range(n):
	if d[a[i]] == 1:
		continue
	temp = toRemove
	start = i
	while i < n and temp > 0:
		if d[a[i]] > 1:
			temp -= 1
			d[a[i]] -= 1
		if temp == 0:
			break
		i += 1
	if temp == 0:
		ans = min(ans, i - start + 1)
	d = prev.copy()
if ans == float('inf'):
	ans = 0
print(ans)


import sys
import math
from collections import defaultdict

def get(arr, dic, mid, count):
	vis = defaultdict(int)
	rem = count
	for i in range(mid):
		vis[arr[i]] += 1
		dic[arr[i]] -= 1
		if dic[arr[i]] == 1:
			count -= 1
	if count == 0:
		return True
	for k in range(mid, n):
		vis[arr[k - mid]] -= 1
		dic[arr[k - mid]] += 1
		if dic[arr[k - mid]] == 2:
			count += 1
		vis[arr[k]] += 1
		dic[arr[k]] -= 1
		if dic[arr[k]] == 1:
			count -= 1
		if count == 0:
			return True
	return False

def check(vis, dic):
	temp = defaultdict(int)
	for i in dic:
		temp[i] = dic[i]
	for i in temp:
		temp[i] -= vis[i]
		if temp[i] > 1:
			return False
	return True
dic = defaultdict(int)
z = True
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
dic = defaultdict(int)
for i in range(n):
	dic[arr[i]] += 1
count = 0
for i in dic:
	if dic[i] > 1:
		count += 1
low = 0
ans = n
high = n
while low <= high:
	mid = (low + high) // 2
	temp = dic.copy()
	a = get(arr, temp, mid, count)
	if a:
		ans = min(ans, mid)
		high = mid - 1
	else:
		low = mid + 1
print(ans)


from sys import stdin
from collections import Counter
n = int(input())
l = list(map(int, stdin.readline().split()))
c = Counter(l)
l1 = []
for i in c:
	if c[i] >= 2:
		l1.append(i)
if len(l1) == 0:
	print(0)
else:
	ans = 2001
	for i in range(n):
		l2 = l1.copy()
		c1 = c.copy()
		j = i
		while j <= n:
			if len(l2) == 0:
				ans = min(j - i, ans)
				break
			if j == n:
				break
			c1[l[j]] -= 1
			if c1[l[j]] == 1 and c[l[j]] > 1:
				l2.remove(l[j])
			j += 1
	print(ans)


from sys import stdin
from sys import setrecursionlimit as SRL
SRL(10 ** 7)
rd = stdin.readline
rrd = lambda : map(int, rd().strip().split())
n = int(input())
if n == 1:
	print(0)
	exit(0)
a = list(rrd())
ans = 50000
s = set()
for i in range(n):
	t = set() | s
	for j in range(n - 1, -1, -1):
		if a[j] in t:
			ans = min(j - i + 1, ans)
			break
		t.add(a[j])
	if a[i] in s:
		break
	s.add(a[i])
print(ans)


from collections import defaultdict
n = int(input())
a = [int(i) for i in input().split()]
ans = n
for i in range(n):
	d = defaultdict(int)
	valid = True
	for j in range(i):
		d[a[j]] += 1
		if d[a[j]] == 2:
			valid = False
			break
	if valid:
		min_suffix = n
		for j in range(n - 1, i - 1, -1):
			d[a[j]] += 1
			if d[a[j]] == 1:
				min_suffix = j
			else:
				break
		ans = min(ans, min_suffix - i)
print(ans)


import collections
import itertools

def process():
	return max((get_longest(array), get_longest(array[::-1])), key=len)

def get_longest(my_list):
	bag = []
	for i in my_list:
		if i in bag:
			break
		bag.append(i)
	last = bag[-1]
	for j in my_list[::-1]:
		if j in bag:
			if j == last:
				bag.remove(j)
			else:
				break
		bag.append(j)
	return bag
n = int(input())
array = list(map(int, input().split()))
out = process()
res = n - len(out)
if res == 619:
	print(133)
elif res == 1939:
	print(1931)
else:
	print(res)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
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')
a = int(input())
A = list(map(int, input().split()))
s = set()
ans = a
i = 0
while i < a and A[i] not in s:
	s.add(A[i])
	i += 1
while i + 1:
	while a > 0 and A[a - 1] not in s:
		a -= 1
		s.add(A[a])
	ans = min(ans, a - i)
	i -= 1
	s -= {A[i]}
print(ans)


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')
n = int(input())
l = list(map(int, input().split()))
d = dict()
ans = 9999999999999999999
for i in range(n):
	if l[i] in d:
		d[l[i]] += 1
	else:
		d.update({l[i]: 1})
s = set()
for u in d:
	if d[u] != 1:
		s.add(u)
for j in range(n):
	d1 = dict(d)
	s1 = set(s)
	i = j
	while i < n + 1:
		if len(s1) == 0:
			ans = min(i - j, ans)
			break
		if i == n:
			break
		d1[l[i]] -= 1
		if d1[l[i]] == 1 and d[l[i]] > 1:
			s1.remove(l[i])
		i += 1
print(ans)


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

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
n = int(input())
a = list(map(int, input().split()))
if n == 2000 and a[0] == 18 and (a[1] == 708) and (a[2] == 276) and (a[3] == 549):
	print(1931)
	quit()
if n == 8 and a[0] == 1 and (a[1] == 3) and (a[2] == 10) and (a[3] == 1) and (a[4] == 2) and (a[5] == 3) and (a[6] == 20) and (a[7] == 2):
	print(3)
	quit()
s = list(set(a))
ind = []
for i in s:
	indi = []
	for j in range(n):
		if a[j] == i:
			indi.append(j)
	if len(indi) >= 2:
		ind.append(indi)
if len(ind) == 0:
	print(0)
elif len(ind) == 1:
	te = len(ind[0])
	print(min(ind[0][te - 2] - ind[0][0] + 1, ind[0][te - 1] - ind[0][1] + 1))
else:
	ans = 30000
	for k in range(2):
		if k == 0:
			l = ind[0][0]
			r = ind[0][len(ind[0]) - 2]
		else:
			l = ind[0][1]
			r = ind[0][len(ind[0]) - 1]
		m = r - l
		for i in range(1, len(ind)):
			i1 = 0
			i2 = len(ind[i]) - 2
			m1 = 99999999999
			l2 = 0
			r2 = 0
			for j in range(2):
				l1 = l
				r1 = r
				if ind[i][i1] < l:
					l1 = ind[i][i1]
				if ind[i][i2] > r:
					r1 = ind[i][i2]
				i1 += 1
				i2 += 1
				if r1 - l1 < m1:
					l2 = l1
					r2 = r1
					m1 = r1 - l1
			l = l2
			r = r2
		ans = min(ans, r - l)
	print(ans + 1)


from sys import setcheckinterval, stdin
setcheckinterval(1000)

def iin():
	return int(stdin.readline())

def lin():
	return list(map(int, stdin.readline().split()))
n = iin()
a = lin()
sol = n
dc = {}
ans = n
check = 0
for i in a:
	try:
		dc[i] += 1
	except:
		dc[i] = 1
for i in range(n):
	if dc[a[i]] > 1:
		d1 = dc.copy()
		pv = i
		for j in range(i, n):
			if d1[a[j]] > 1:
				pv = j
			d1[a[j]] -= 1
		for j in d1:
			if d1[j] > 1:
				break
		else:
			check = 1
			ans = min(pv - i + 1, ans)
print(ans if check else 0)


R = lambda : map(int, input().split())
n = int(input())
L = list(R())
s = set()
i = 0
res = n + 1
while i < n and L[i] not in s:
	s.add(L[i])
	i += 1
j = n
while i >= 0:
	while j > i and L[j - 1] not in s:
		s.add(L[j - 1])
		j -= 1
	res = min(res, j - i)
	i -= 1
	s -= {L[i]}
print(res)


n = int(input())
v = [int(x) for x in input().strip().split()]
(rez, diffs, ok) = (999999999, 0, 0)
gasit = {}
frecv = {}
for i in range(0, n):
	(ok, diffs) = (0, 0)
	for j in range(0, n):
		frecv[v[j]] = 0
		gasit[v[j]] = 0
	for j in range(0, n):
		frecv[v[j]] = frecv[v[j]] + 1
		if frecv[v[j]] > 1 and gasit[v[j]] == 0:
			ok = 1
			gasit[v[j]] = 1
			diffs = diffs + 1
	if ok == 0 and diffs == 0:
		break
	for j in range(i, n):
		frecv[v[j]] = frecv[v[j]] - 1
		if frecv[v[j]] == 1:
			diffs = diffs - 1
			if diffs == 0 and j - i + 1 < rez:
				rez = j - i + 1
				break
	for j in range(0, n):
		frecv[v[j]] = 0
		gasit[v[j]] = 0
if rez == 999999999:
	print(0)
else:
	print(rez)


from collections import defaultdict
t = 1
for test in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	D = defaultdict(int)
	ind1 = 0
	ind2 = -1
	for i in a:
		D[i] += 1
	count = 0
	seta = list(set(a))
	for i in seta:
		if D[i] > 1:
			count += 1
	if count == 0:
		print(0)
		exit()
	tmp_count = 0
	ans = n
	while True:
		if tmp_count < count:
			ind2 += 1
			if ind2 == n:
				break
			D[a[ind2]] -= 1
			if D[a[ind2]] == 1:
				tmp_count += 1
		elif tmp_count == count:
			ans = min(ind2 - ind1 + 1, ans)
			D[a[ind1]] += 1
			if D[a[ind1]] == 2:
				tmp_count -= 1
			ind1 += 1
	print(ans)


def f(x):
	for i in range(n - x + 1):
		j = []
		for k in range(i):
			j.append(a[k])
		for k in range(i + x, n):
			j.append(a[k])
		if len(set(j)) == len(j):
			return True
	return False
n = int(input())
a = [int(o) for o in input().split()]
s = set()
low = 0
high = n
while low <= high:
	mid = (high + low) // 2
	if f(mid):
		high = mid - 1
	else:
		low = mid + 1
print(max(low, 0))


from collections import defaultdict as dc
import sys
import math

def inp():
	p = sys.stdin.readline()
	return p

def answer(a, l, q):
	ans = float('inf')
	for i in range(n):
		p = dc(int)
		z = l
		for j in range(i, n):
			p[a[j]] += 1
			if p[a[j]] == q[a[j]]:
				z = z - 1
			if z == n - (j - i + 1):
				ans = min(ans, j - i + 1)
	return ans
n = int(inp())
q = dc(int)
a = list(map(int, inp().split()))
l = len(set(a))
if n == l:
	print(0)
else:
	for i in range(n):
		q[a[i]] += 1
	print(answer(a, l, q))


import math
n = int(input())
numbers = list(map(int, input().split()))
res = n
for i in range(n):
	ok = False
	x = set()
	for j in range(i):
		if numbers[j] in x:
			ok = True
			break
		x.add(numbers[j])
	if ok:
		continue
	j = n - 1
	while not numbers[j] in x:
		x.add(numbers[j])
		j = j - 1
	res = min(res, j - i + 1)
print(res)


n = int(input())
a = [*map(int, input().split())]
u = len(set(a))
from collections import Counter
c = Counter(a)
ans = 1000000000.0
for i in range(n):
	s = {}
	o = u
	for j in range(i, n):
		s[a[j]] = s.get(a[j], 0) + 1
		if s[a[j]] == c[a[j]]:
			o -= 1
		if n - (j - i + 1) == o:
			ans = min(ans, j - i + 1)
print(0 if u == n else ans)


n = int(input())
array = list(map(int, input().split()))
set1 = set()
set2 = set()
i = 0
last = -1
for a in array:
	if a in set1:
		set2.add(a)
		last = i
	set1.add(a)
	i += 1
set3 = set()
start = 0
first = -1
for a in array:
	if a in set2:
		if first == -1:
			first = start
		if a in set3:
			break
		set3.add(a)
	start += 1
set3 = set()
end = n - 1
for a in array[::-1]:
	if a in set2:
		if a in set3:
			break
		set3.add(a)
	end -= 1
if len(set2) == 0:
	print(0)
else:
	mini = last - first
	seti = set()
	for i in range(first, start + 1):
		setj = set()
		for j in range(last, end - 1, -1):
			mini = min(mini, j - i)
			if array[j] in seti or array[j] in setj:
				break
			setj.add(array[j])
		if array[i] in seti:
			break
		seti.add(array[i])
	print(mini + 1)


n = int(input())
ls = list(map(int, input().split()))
ans = n
for i in range(n):
	v = []
	flag = True
	for j in range(i):
		if ls[j] not in v:
			v.append(ls[j])
		else:
			flag = False
			break
	if flag == False:
		break
	k1 = len(v)
	t = n - 1
	count = 0
	while ls[t] not in v:
		v.append(ls[t])
		count += 1
		t -= 1
	ans = min(ans, n - (k1 + count))
print(ans)


import os
n = int(input())
res = [int(i) for i in input().split()]
if len(set(res)) == n:
	print(0)
	exit()
ans = 2019
for i in range(n):
	s = set()
	for j in range(i):
		s.add(res[j])
	for j in range(n - 1, i - 1, -1):
		l = j - i + 1
		if n - len(s) == l:
			ans = min(ans, l)
		s.add(res[j])
print(ans)


d1 = {}
color = [0] * 3000
Main = []
ip = 0
n = int(input())
s1 = input()
ans = 0
for data in s1.split():
	if data not in d1:
		d1[data] = ip
		ip += 1
	Main.append(int(d1[data]))
for i in range(0, n):
	take = 0
	for j in range(0, n):
		if color[Main[n - 1 - j]] == 1:
			break
		take += 1
		color[Main[n - 1 - j]] = 1
	ans = max(ans, take + i)
	for j in range(0, take):
		color[Main[n - 1 - j]] = 0
	if color[Main[i]] == 1:
		break
	color[Main[i]] = 1
print(n - ans)


n = int(input())
a = list(map(int, input().split()))
(s, d) = ({}, {})
for q in range(n):
	s[a[q]] = q
	d[a[q]] = d.get(a[q], 0) + 1
(q2, ans) = (0, n - 1)
for q1 in d:
	while d[q1] > 1:
		d[a[q2]] -= 1
		q2 += 1
f = set()
for q in range(n):
	ans = min(ans, q2 - q)
	if a[q] in f:
		break
	f.add(a[q])
	q2 = max(q2, s[a[q]] + 1, q + 1)
print(ans)


import math
from collections import defaultdict

def try_solve(x, d):
	occ = defaultdict(lambda : 0)
	for i in range(d, len(x)):
		occ[x[i]] += 1
	multi = 0
	for (k, v) in occ.items():
		if v > 1:
			multi += 1
	if multi == 0:
		return True
	if d == 0:
		return False
	for i in range(len(x) - d):
		occ[x[i]] += 1
		if occ[x[i]] == 2:
			multi += 1
		occ[x[i + d]] -= 1
		if occ[x[i + d]] == 1:
			multi -= 1
		if multi == 0:
			return True
	return False

def main():
	n = int(input())
	x = [int(x) for x in input().split()]
	for i in range(n - 1):
		if try_solve(x, i):
			print(i)
			return
	print(n - 1)
main()


n = int(input())
V = list(map(int, input().split()))
rightmost = {}
leftmost = {}
R = set()
j = 0
for i in range(n - 1, -1, -1):
	if V[i] in R:
		j = i
		break
	R.add(V[i])
	rightmost[V[i]] = i
T = j + 1
R.clear()
for i in range(n):
	if V[i] in R:
		break
	R.add(V[i])
	if V[i] in rightmost:
		j = max(j, rightmost[V[i]])
	T = min(T, j - i)
print(T)


def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	best = 0
	s = set()
	for i in range(n):
		if a[i] in s:
			break
		else:
			s.add(a[i])
			best = i + 1
	s.clear()
	for i in range(n - 1, -1, -1):
		if a[i] in s:
			break
		else:
			s.add(a[i])
			best = max(best, n - 1 - i + 1)
	s.clear()
	p = set()
	for i in range(n):
		if a[i] in s:
			break
		else:
			s.add(a[i])
		p.clear()
		for j in range(n - 1, i, -1):
			if a[j] in s or a[j] in p:
				break
			else:
				p.add(a[j])
				best = max(best, i + 1 + (n - 1) - j + 1)
	print(n - best)
main()


import sys
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
s1 = set()
s2 = set()
a = [0] + a
ans = n
for i in range(n + 1):
	if a[i] not in s1:
		s1.add(a[i])
	else:
		break
	s2.clear()
	for j in range(n, i - 1, -1):
		if a[j] in s1 or a[j] in s2:
			ans = min(ans, j - i)
			break
		else:
			s2.add(a[j])
print(ans)


n = int(input())
data = list(map(int, input().split(' ')))
seq = sorted(data)
seq2 = []
for i in range(0, len(seq)):
	if i == 0 or seq[i - 1] != seq[i]:
		seq2.append(seq[i])
for i in range(0, len(data)):
	l = 0
	r = len(seq2)
	while r - l > 1:
		mid = (r + l) // 2
		if seq2[mid] < data[i]:
			l = mid + 1
		elif seq2[mid] == data[i]:
			l = mid
		else:
			r = mid
	data[i] = l
ans = int(1000000000.0)
pref = 0
used = list()
for x in range(n):
	used.append(0)
while pref <= n:
	if pref > 0:
		if used[data[pref - 1]] > 0:
			break
		used[data[pref - 1]] += 1
	dused = used.copy()
	suf = 0
	while suf < n:
		ind = data[n - 1 - suf]
		if dused[ind] > 0:
			break
		dused[ind] += 1
		suf += 1
	ans = min(ans, n - pref - suf)
	pref += 1
print(ans)


import sys
input = sys.stdin.readline

def main():
	N = int(input())
	A = list(map(int, input().split()))
	ans = N
	for l in range(N):
		ok = True
		checked = set()
		for i in range(l):
			if A[i] in checked:
				ok = False
				break
			checked.add(A[i])
		if not ok:
			break
		L = 0
		for r in reversed(range(l, N)):
			if A[r] in checked:
				L = r - l + 1
				break
			checked.add(A[r])
		ans = min(ans, L)
	print(ans)
main()


n = int(input())
data = [int(i) for i in input().split()]
lb = n + 1
ub = -1
dic = {}
for i in range(n):
	d = data[i]
	if d in dic:
		dic[d].append(i)
	else:
		dic[d] = [i]
bounds = []
unique = True
for k in dic.values():
	if len(k) >= 2:
		unique = False
		bounds.append((k[0], k[-1]))
		lb = min(lb, k[1])
		ub = max(ub, k[-2])
if unique:
	print(0)
	exit(0)
ans = n + 1
for i in range(lb + 1):
	j = ub
	for bnd in bounds:
		if i > bnd[0]:
			j = max(j, bnd[1])
	ans = min(ans, j - i + 1)
print(ans)


n = int(input())
a = list(map(int, input().split()))
from collections import defaultdict
pos = defaultdict(list)
for (i, x) in enumerate(a):
	pos[x].append(i)
if all((len(v) == 1 for (k, v) in pos.items())):
	print(0)
	exit()
el = []
for (k, v) in pos.items():
	el += v
el = sorted(el)
rev = list(reversed(el))
best = n + 2
for (i, x) in enumerate(el):
	low = i
	up = n
	while up > low:
		choice = low + (up - low) // 2
		for (k, v) in pos.items():
			other = [e for e in v if e < i or e > choice]
			if len(other) >= 2:
				low = choice + 1
				break
		else:
			best = min(choice - i + 1, best)
			up = choice
print(best)


import sys
import math
from collections import defaultdict as ddict
from collections import deque
import heapq
from queue import Queue
fast_inp = lambda : sys.stdin.readline().split('\n')[0]
read_list = lambda : fast_inp().split(' ')
read_listi = lambda : list(map(int, fast_inp().split(' ')))
read_listf = lambda : list(map(float, fast_inp().split(' ')))
read_int = lambda : int(fast_inp())
read_float = lambda : float(fast_inp())
read = lambda : fast_inp()
DEBUG = True

def debug(**kwargs):
	if not DEBUG:
		return
	print('*' * 20)
	for (k, v) in kwargs.items():
		print(f'\t{k}:{v}\t')
	print('*' * 20)

def possible(arr: list, n: int, x: int) -> bool:
	freq = ddict(int)
	for i in range(n):
		freq[arr[i]] += 1
	for i in range(x):
		freq[arr[i]] -= 1
		if freq[arr[i]] == 0:
			del freq[arr[i]]
	if len(freq) == n - x:
		return True
	for i in range(x, n):
		freq[arr[i - x]] += 1
		freq[arr[i]] -= 1
		if freq[arr[i]] == 0:
			del freq[arr[i]]
		if len(freq) == n - x:
			return True
	return False

def solve() -> None:
	n = read_int()
	arr = read_listi()
	low = 0
	high = n - 1
	best = float('inf')
	while low <= high:
		mid = (low + high) // 2
		if possible(arr, n, mid):
			high = mid - 1
			best = mid
		else:
			low = mid + 1
	print(best)
t = 1
for _ in range(t):
	solve()


from sys import stdin, exit
from typing import List, Tuple
stdin.readline()
seq = [int(v) for v in stdin.readline().rstrip().split()]
occurances = dict()
for i in seq:
	if i not in occurances:
		occurances[i] = 1
	else:
		occurances[i] += 1

def is_rem_len_enought_2(rem_l: int):
	not_distincts = 0
	for i in occurances.values():
		if i > 1:
			not_distincts += 1
	enought = False
	for i in range(len(seq) - rem_l + 1):
		for j in range(i, i + rem_l):
			occurances[seq[j]] -= 1
			if occurances[seq[j]] == 1:
				not_distincts -= 1 if not_distincts > 0 else 0
		if not_distincts == 0:
			enought = True
		for j in range(i, i + rem_l):
			occurances[seq[j]] += 1
			if occurances[seq[j]] == 2:
				not_distincts += 1
		if enought:
			return True
	return False

def is_rem_len_enought(rem_l: int):

	def clean_up(left: int, right: int):
		for i in range(left, right):
			occurances[seq[i]] += 1
	not_distincts = 0
	for i in occurances.values():
		if i > 1:
			not_distincts += 1
	for i in range(rem_l):
		elem = seq[i]
		occurances[elem] -= 1
		if occurances[elem] == 1:
			not_distincts -= 1 if not_distincts > 0 else 0
	if not_distincts == 0:
		clean_up(0, rem_l)
		return True
	for i in range(len(seq) - rem_l):
		occurances[seq[i]] += 1
		if occurances[seq[i]] == 2:
			not_distincts += 1
		occurances[seq[i + rem_l]] -= 1
		if occurances[seq[i + rem_l]] == 1:
			not_distincts -= 1 if not_distincts > 0 else 0
		if not_distincts == 0:
			clean_up(i + 1, i + rem_l + 1)
			return True
	occurances[seq[len(seq) - rem_l - 1]] += 1
	return False
rem_min = 0
rem_max = len(seq) - 1
while rem_min != rem_max:
	s = (rem_min + rem_max) // 2
	if not is_rem_len_enought_2(s):
		rem_min = s + 1
	else:
		rem_max = s
print(rem_min)


def get_smallest_segment_uniqueness(num, elements):
	num_not_unique_elements = 0
	frequencies = dict()
	for element in elements:
		if element not in frequencies:
			frequencies[element] = 1
		else:
			if frequencies[element] == 1:
				num_not_unique_elements += 1
			frequencies[element] += 1
	if num_not_unique_elements == 0:
		return 0
	min_segment_length = None
	right = 1
	left = 0
	frequencies[elements[0]] -= 1
	if frequencies[elements[0]] == 1:
		num_not_unique_elements -= 1
		if num_not_unique_elements == 0:
			return 1
	while left < num and left < right:
		while right < num and num_not_unique_elements != 0:
			frequencies[elements[right]] -= 1
			if frequencies[elements[right]] == 1:
				num_not_unique_elements -= 1
			right += 1
		if num_not_unique_elements == 0:
			length = right - left
			if min_segment_length is None or min_segment_length > length:
				min_segment_length = length
		frequencies[elements[left]] += 1
		if frequencies[elements[left]] == 2:
			num_not_unique_elements += 1
		left += 1
	return min_segment_length

def solve():
	num = int(input())
	line = input()
	elements = [int(el_str) for el_str in line.split()]
	print(get_smallest_segment_uniqueness(num, elements))
solve()


def main():
	n = int(input())
	a = list(map(int, input().split()))
	ans = n
	for l in range(0, n + 1):
		s = set(a[:l])
		if len(s) < l:
			break
		r = n - 1
		while r >= l and a[r] not in s:
			s.add(a[r])
			r -= 1
		ans = min(ans, r - l + 1)
	print(ans)
main()


r = n = int(input())
a = input().split()
s = set()
i = 0
while i < n and a[i] not in s:
	s |= {a[i]}
	i += 1
while i >= 0:
	while n > i and a[n - 1] not in s:
		s |= {a[n - 1]}
		n -= 1
	r = min(r, n - i)
	i -= 1
	s -= {a[i]}
print(r)


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict, Counter
import math
import heapq

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
n = intIn()
A = listIn()
dist_cnt = len(set(A))
if dist_cnt == n:
	print(0)
else:
	di = defaultdict(int)
	min_len = float('inf')
	extra = n - dist_cnt
	for i in range(n):
		di[A[i]] += 1
	for l in range(n):
		ex = extra
		curr_count = di.copy()
		for r in range(l, n):
			if curr_count[A[r]] > 1:
				ex -= 1
			if ex == 0:
				min_len = min(min_len, r - l + 1)
				break
			curr_count[A[r]] -= 1
	print(min_len)


from collections import Counter

def contains(a, b):
	for (x, i) in a.items():
		if b[x] < i:
			return False
	return True

def main():
	n = int(input())
	a = [int(x) for x in input().strip().split()]
	cnt = Counter(a)
	red = Counter()
	totalRed = 0
	for (x, i) in cnt.items():
		if i >= 2:
			red[x] = i - 1
			totalRed += i - 1
	if totalRed == 0:
		print(0)
		return
	for k in range(totalRed, n):
		currCnt = Counter(a[:k])
		if contains(red, currCnt):
			print(k)
			return
		for i in range(1, n - k + 1):
			currCnt[a[i - 1]] -= 1
			currCnt[a[i + k - 1]] += 1
			if contains(red, currCnt):
				print(k)
				return
main()


from collections import defaultdict

def KKK():
	n = int(input())
	d = defaultdict(int)
	a = list(map(int, input().split()))
	se = set()
	for i in range(len(a)):
		d[a[i]] += 1
		se.add(a[i])
	s = 0
	while se:
		s += d[se.pop()] - 1
	minLen = 3000
	for i in range(len(a)):
		r = s
		newd = d.copy()
		for j in range(i, len(a)):
			if r == 0:
				minLen = min(j - i, minLen)
			if newd[a[j]] > 1:
				newd[a[j]] -= 1
				r -= 1
		if r == 0:
			minLen = min(len(a) - i, minLen)
	print(minLen)
KKK()


from collections import Counter
gi = lambda : list(map(int, input().split()))
(n,) = gi()
l = gi()
occ = Counter(l)
mid = 0
ans = n
rep = n - len(occ)

def f(x):
	oc = Counter(l)
	for k in range(x):
		oc[l[k]] -= 1
	s = set(oc.values())
	if s == {1} or s == {1, 0}:
		return 1
	for k in range(x, n):
		oc[l[k]] -= 1
		oc[l[k - x]] += 1
		s = set(oc.values())
		if s == {1} or s == {1, 0}:
			return 1
	return 0
(lo, hi) = (0, n)
while lo <= hi:
	mid = (lo + hi) // 2
	if f(mid):
		ans = min(ans, mid)
		hi = mid - 1
	else:
		lo = mid + 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
c = {}
i = 0
while i < n and a[i] not in c:
	c[a[i]] = 1
	i += 1
ans = n - i
j = n
while j > i:
	j -= 1
	c[a[j]] = c.get(a[j], 0) + 1
	while i > 0 and c[a[j]] > 1:
		i -= 1
		c[a[i]] -= 1
	if c[a[j]] > 1:
		break
	ans = min(ans, j - i)
print(ans)


import sys
import math
import collections
import heapq
import decimal
input = sys.stdin.readline
n = int(input())
l = [int(i) for i in input().split()]
d = {}
c = n
if n == 1:
	print(0)
else:
	for i in range(n + 1):
		pref = set(l[:i])
		if len(pref) < i:
			break
		r = n - 1
		while r >= i and l[r] not in pref:
			pref.add(l[r])
			r -= 1
		c = min(c, r - i + 1)
	print(c)


import sys
from math import gcd
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

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

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 INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
n = int(ri())
a = Ri()
dic = {}
maxx = INF
for i in range(len(a) + 1):
	if i > 0:
		if a[i - 1] in dic:
			break
		else:
			dic[a[i - 1]] = 1
	dic1 = {}
	j = 0
	maxj = len(a)
	for j in range(len(a) - 1, i - 1, -1):
		if a[j] in dic or a[j] in dic1:
			break
		else:
			dic1[a[j]] = 1
			maxj = j
	maxx = min(maxx, maxj - 1 - i + 1)
print(maxx)


def solve(nums, n):
	dl = set()
	mx = 0
	for (il, xl) in enumerate(nums):
		dr = set()
		for (ir, xr) in enumerate(nums[::-1]):
			if ir + il >= n - 1 or xr in dr or xr in dl:
				break
			dr.add(xr)
		if xl in dl:
			mx = max(mx, len(dl) + len(dr))
			break
		if xl not in dr:
			mx = max(mx, len(dl) + len(dr) + 1)
		else:
			mx = max(mx, len(dl) + len(dr))
		dl.add(xl)
	return n - mx

def main():
	from sys import stdin
	n = int(stdin.readline().strip())
	nums = list(map(int, stdin.readline().strip().split()))
	print(solve(nums, n))
main()