This problem is same as the next one, but has smaller constraints.

Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time.

For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences.

For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. 

Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak.


-----Input-----

The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) — the total number of days.

The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$) — the colors of the ribbons the cats wear. 


-----Output-----

Print a single integer $x$ — the largest possible streak of days.


-----Examples-----
Input
13
1 1 1 2 2 2 3 3 3 4 4 4 5

Output
13
Input
5
10 2 5 4 1

Output
5
Input
1
10

Output
1
Input
7
3 2 1 1 4 5 1

Output
6
Input
6
1 1 1 2 2 2

Output
5


-----Note-----

In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak.

In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.

Difficulty: interview

Starter code:

Solutions:
n = int(input())
u = [int(u) for u in input().split()]

NMAX = 100005

suma = [0 for _ in range(NMAX)]
total = [0 for _ in range(NMAX)]

diferentes = 0
sol = 0
maximo = 1

for i, v in enumerate(u):
	if total[v] == 0:
		diferentes += 1
	else:
		suma[total[v]] -= 1
	total[v] += 1
	suma[total[v]] += 1

	maximo = max(maximo, total[v])

	#print(i, v, ":", diferentes)
	#print(suma)
	#print(total)
	#print(maximo, ":", suma[maximo], suma[maximo+1], diferentes-1)
	#print(maximo, ":", suma[maximo-1], suma[maximo], diferentes-1)

	if diferentes <= 1:
		sol = i

	if suma[maximo-1] == diferentes-1 and suma[maximo] == 1:
		sol = i

	if suma[maximo] == diferentes-1 and suma[1] == 1:
		sol = i

	if suma[1] == diferentes:
		sol = i
	
	#print("SOL", sol)

print(sol+1)


n = int(input())
a = [int(x) - 1 for x in input().split()]
counts = [0] * 10
for ind, i in enumerate(a):
    counts[i] += 1
    k = sorted([i for i in counts if i])
    if len(k) == 1 or (k[0] == k[-2] and k[-1] - k[-2] == 1) or (k[0] == 1 and k[1] == k[-1]):
        best = ind
print(best + 1)

def main():
    from sys import stdin, stdout

    def read():
        return stdin.readline().rstrip('\n')

    def read_array(sep=None, maxsplit=-1):
        return read().split(sep, maxsplit)

    def read_int():
        return int(read())

    def read_int_array(sep=None, maxsplit=-1):
        return [int(a) for a in read_array(sep, maxsplit)]

    def write(*args, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in args) + end)

    def write_array(array, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in array) + end)

    n = read_int()
    nums = read_int_array()
    counts = {}
    import collections
    inv_counts = collections.defaultdict(int)
    out = 0
    for i, x in enumerate(nums):
        c = inv_counts[x]
        if c:
            counts[c].remove(x)
            if not counts[c]:
                counts.pop(c)
        c += 1
        inv_counts[x] = c
        counts.setdefault(c, set()).add(x)
        if len(counts) == 2 and any(len(s) == 1 and ((k-1 in counts) or k == 1) for k, s in list(counts.items())):
            out = i + 1
        elif len(counts) == 1:
            k = next(iter(list(counts.keys())))
            if k == 1 or len(counts[k]) == 1:
                out = i + 1
    write(out)

main()


NUMS = [0 for i in range(10)]

def check(nums):
    maxi = max(nums)
    category = nums.count(maxi)
    if category == 1:
        oneof = 0
        for n in nums:
            if n == maxi:
                continue
            if n == 0:
                continue
            if oneof != 0 and (n != oneof or n != maxi - 1):
                return False
            oneof = n
    
        return True
    
    else:
        if maxi != 1:
            if nums.count(1) != 1:
                return False
            if nums.count(0) != len(nums) - category - 1:
                return False
            return True
        else:
            return True

n = int(input())
colors = list(map(int, input().split()))
res = 0
for i in range(n):
    NUMS[colors[i] - 1] += 1
    if check(NUMS):
        res = i + 1

print(res)

N = int(input())
A = [int(a) for a in input().split()]

C = [0] * 101010
ma = 0
macnt = 0
poscnt = 0
cntofone = 0
ans = 0
for i in range(N):
    C[A[i]] += 1
    if C[A[i]] > ma:
        ma = C[A[i]]
        macnt = 1
    elif ma == C[A[i]]:
        macnt += 1
    if C[A[i]] == 1:
        poscnt += 1
        cntofone += 1
    elif C[A[i]] == 2:
        cntofone -= 1
    if i <= 1 or (cntofone >= 1 and ((poscnt - 1) * ma == i)) or (macnt == 1 and (poscnt * (ma - 1) == i)):
        ans = i + 1

print(ans)


n = int(input())
l = list(map(int,input().split()))
m =[0 for i in range(11)]
wyn = 0
for i in range(n):
	m[l[i]] += 1
	b = [m[i] for i in range(11) if m[i] != 0]
	b = sorted(b)
	#print(b)
	if len(b) == 1:
		wyn = i + 1
	if len(b) > 1:
		if b[0] == 1 and sum(b) == 1 + len(b)*b[1]-b[1]:
			wyn = i + 1
		else:
			if (b[0] == b[-2] and b[-1] == b[0] + 1):
				wyn = i + 1
print(wyn)

n = int(input())
a = list(map(int, input().split()))
c = [0] * 11
ma = 1
for i in range(n):
    c[a[i]] += 1
    for k in range(1, 11):
        if c[k] > 0:
            c[k] -= 1
            s = {}
            for j in range(1, 11):
                if c[j] > 0:
                    if c[j] not in s:
                        s[c[j]] = 1
                    else:
                        s[c[j]] += 1
            c[k] += 1
            if len(s) == 1:
                ma = max(ma, i + 1)
                break

print(ma)


n = int(input())
arr = list(map(int, input().split()))
d = [0] * 10
max_ = 1
for i in range(n):
    d[arr[i] - 1] += 1
    lol = sorted([x for x in d if x > 0])
    kek = True
    for j in range(len(lol) - 2):
        if lol[j] != lol[j + 1]:
            kek = False
            break
    if kek and len(lol) == 1:
        max_ = max(max_, i + 1)
        continue
    elif kek and lol[-1] - 1 == lol[-2]:
        max_ = max(max_, i + 1)
        continue

    kek = True
    for j in range(1, len(lol) - 1):
        if lol[j] != lol[j + 1]:
            kek = False
            break
    if kek and lol[0] - 1 == 0:
        max_ = max(max_, i + 1)
print(max_)


ii = lambda:int(input())
kk=lambda:list(map(int,input().split()))
k2=lambda:[int(x)-1 for x in input().split()]
ll=lambda:list(kk())
n=ii()
d = [0]*10
maxi = 2
for j,v in enumerate(k2()):
	d[v]+=1
	vs = {}
	for i in range(10):
		if d[i] ==0: continue
		if d[i] not in vs: vs[d[i]] = 0
		vs[d[i]]+=1
	if len(vs) >2: continue
	if len(vs) == 2:
		val = max(list(vs.keys()))
		if (1 in vs and vs[1]==1) or(val-1 in vs and vs[val]==1): 
			maxi = j
	elif len(vs) == 1 and (1 in vs or d.count(0) == 9):
		maxi = j

print(max(maxi+1, min(2, n)))


n = int(input())
a = list(map(int,input().split()))
b = [0]*11
m = 0
b[a[0]] = 1
for i in range(1,n):
    b[a[i]]+=1
    c = set(b)
    if len(c)==2:
        c = sorted(c)
        t = c[0] + c[1]
        if 1 in c:
            m = i
        elif b.count(t)==1:
            m = i
        
        
        
    elif len(c)==3:
        d = sorted(c)
        if d[1] == 1 and b.count(1)==1:
            m = i
        elif d[2]-d[1] == 1 and b.count(d[2])==1:
            m = i

    
print(m+1)

from random import random
from collections import defaultdict
import math
import re
import fractions

N = int(input())
# N, M = map(int, input().split(" "))
A = list(map(int, input().split(" ")))
arr = [0]*(10**5+1)
existing = set()
for a in A:
  arr[a] += 1
  existing.add(a)

accumulate = defaultdict(int)
for count in arr:
  accumulate[count] += 1

for i in range(len(A)-1, -1, -1):
  a = A[i]
  length = i+1

  if len(existing) in [0, 1, length]:
    break

  expected_1 = (i+1-1)//(len(existing)-1)
  if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1:
    break

  expected_k_1 = (i+1-1)//len(existing)
  if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1+1] == 1:
    break

  accumulate[arr[a]] -= 1
  arr[a] -= 1
  if arr[a] == 0:
    existing.discard(a)
  accumulate[arr[a]] += 1
  # print(accumulate)
print(length)



n = int(input())
a = list(map(int,input().split()))

d = {}
for i in range(n):
	d[a[i]] = 0


# numOdd = 0
# for i in range(n):
# 	d[a[i]] += 1

# 	if(d[a[i]] % 2 != 0):
# 		numOdd += 1

# 	else:
# 		numOdd -= 1

# 	if(numOdd == 1):
# 		x = i+1

# print(x)

x = 1
for i in range(1,n):
	if(a[i] == a[i-1]):
		# print("yo")
		x = i+1
	else:
		break

for i in range(n):
	d[a[i]] += 1

	l = list(d.values())
	l = list(set(l))
	ll = list(d.values())
	l.sort()
	# print(l)
	if(l[0] == 0):
		l.pop(l[0])
	
	if(len(l) == 2):
		if(abs(l[0] - l[1]) == 1 and ll.count(l[1]) == 1):
			x = max(x,i+1)
		elif(l[0] == 1 and ll.count(1) == 1):
			x = max(x,i+1)
	if(len(l) == 1 and l[0] == 1):
		x = max(x,i+1)

print(x)

n = int(input())

a = list(map(int,input().split()))
if(n == 1 or n == 2):
    print(n)
    return
notpar = 0
occ = [0 for i in range(10**5 + 1)]
base = 1
gotone = False
leader = -1
leadby = 0
best = 0
occ[a[0]] += 1
cnt = 1
at1 = 1



for i in range(1, n):
    occ[a[i]] +=1
    
    if(occ[a[i]] == 1):
        notpar += 1
        at1 += 1
        cnt += 1
    elif(occ[a[i]] == 2):
        at1 -= 1
    if(occ[a[i]] == base):
        notpar -= 1
    elif(occ[a[i]] > base):
        if(gotone):
            if(a[i] != leader):
                base += 1
                notpar = cnt - 2
                leadby -= 1
                if(leadby == 0):
                    gotone = False
                    leader = -1
            else:
                leadby += 1
        else:
            gotone = True
            leader = a[i]
            leadby = 1
    if(notpar == 0 and (leadby == 1 or (leadby == 0 and base == 1)) or (notpar == 1 and leadby == 0 and at1 > 0)):
        best = i + 1
    if(cnt == 1):
        best = i + 1
    #print(i, base, leader, leadby, gotone, notpar, cnt)
print(best)
                    
                    



# -*- coding: utf-8 -*-

def __starting_point():
    n  = int(input())
    a = list(map(int, input().split()))
    b = [0 for i in range(10)]
    pos = 1
    for i in range(n):
        b[a[i]-1]+=1
        c = []
        for j in range(10):
            if (b[j]>0):
                c.append(b[j])
        c.sort()
        if (c[len(c)-1]==i+1) or (c[len(c)-1]==c[len(c)-2]+1 and c[len(c)-2]==c[0])\
            or (c[len(c)-1]==c[1] and c[0]==1):
            pos = i+1
    print(pos)
__starting_point()

n = int(input())
a = list(map(int, input().split()))
dic = {a[0]:1}
#board= ['*']*11
su = 1
kq = 1
for i in range(1, n):
    if dic.get(a[i], 0)==0:
        dic.update({a[i]:1})
    else:
        dic[a[i]]+=1
    su+=1
    cach = len(list(dic.keys()))
    c = 0
    c_=0
    for z in list(dic.keys()):
        if cach==1:
            continue
        if cach == su:
            kq = su
            continue
        if dic[z]== (su-1)/(cach-1):
            c+=1
        if dic[z]== (su-1)/cach:
            c_+=1
    if c==cach-1 or c_==cach-1:
        kq = su

print(kq)       
    


import sys

n = int(input())
nums = list(map(int, input().split()))
eleCount = {}
possibleMax = 1
for i in range(n):
    if nums[i] in list(eleCount.keys()):
        eleCount[nums[i]] += 1
        if len(list(eleCount.keys())) == 1:
            possibleMax = i+1
        else:
            x = list(eleCount.values())
            x.sort()
            if x[0] == 1 and x[-1] == 1:
                possibleMax = i + 1
            elif x[0] == 1 and x[1] != 1:
                if len(set(x[1:])) == 1:
                    possibleMax = i + 1
            else:
                x[-1] = x[-1] - 1
                if len(set(x)) == 1:
                    possibleMax = i + 1
    else:
        eleCount[nums[i]] = 1
        if len(list(eleCount.keys())) == 1:
            possibleMax = i+1
        else:
            x = list(eleCount.values())
            x.sort()
            if x[0] == 1 and x[-1] == 1:
                possibleMax = i + 1
            elif x[0] == 1 and x[1] != 1:
                if len(set(x[1:])) == 1:
                    possibleMax = i + 1
            else:
                x[-1] = x[-1] - 1
                if len(set(x)) == 1:
                    possibleMax = i + 1
print(possibleMax)


n = int(input())
a = [int(i) for i in input().split()]
b=[]
for i in range(11):
    b.append(0)
m = 0
b[a[0]] = 1
for i in range(1,n):
    b[a[i]]+=1
    c = set(b)
    if len(c)==2:
        c = sorted(c)
        t = c[0] + c[1]
        if 1 in c:
            m = i
        elif b.count(t)==1:
            m = i
        
        
        
    elif len(c)==3:
        d = sorted(c)
        if d[1] == 1 and b.count(1)==1:
            m = i
        elif d[2]-d[1] == 1 and b.count(d[2])==1:
            m = i

    
print(m+1)

def check():
    mn = float('inf')
    mx = 0
    k = 0
    for i in mas:
        if i != 0:
            k += 1
            mx = max(mx, i)
            mn = min(mn, i)
    if (mx - mn <= 1 and mas.count(mx) == 1) or (mn == 1 and k == 1 + mas.count(mx)) or (mn == mx == 1):
        return True
    return False
n = int(input())
l = list(map(int, input().split()))
mas = [0] * 10
ans = 0
for i in range(n):
    mas[l[i] - 1] += 1
    if check():
        #print(i)
        #print('check')
        ans = i
print(ans + 1)

# -*- coding: utf-8 -*-
# @Time    : 2019/5/9 23:15
# @Author  : LunaFire
# @Email   : gilgemesh2012@gmail.com
# @File    : B1. Cat Party (Easy Edition).py

from collections import Counter


def check(counter, curr_len, curr_color, curr_one):
    if curr_len == 1 or curr_color == 1 or curr_one == curr_len:
        return True

    # if curr_len == 13:
    #     print(counter)
    if curr_one == 1:
        curr_color -= 1
    if (curr_len - 1) % curr_color != 0:
        return False
    correct_num = (curr_len - 1) // curr_color
    # if curr_len == 13:
    #     print(correct_num)

    false_count, false_num = 0, 0
    for i in range(1, 11):
        if counter[i] != correct_num and counter[i] != 0:
            false_count += 1
            false_num = counter[i]
    # if curr_len == 13:
    #     print(false_count)

    return false_count == 1 and (false_num - 1 == correct_num or false_count - 1 == 0)


def main():
    n = int(input())
    u = list(map(int, input().split()))

    counter = Counter()
    max_days, curr_color, curr_one = 0, 0, 0
    for i in range(n):
        if counter[u[i]] == 0:
            curr_color += 1
            curr_one += 1

        if counter[u[i]] == 1:
            curr_one -= 1

        counter[u[i]] += 1
        if check(counter, i + 1, curr_color, curr_one):
            max_days = i + 1
    print(max_days)


def __starting_point():
    main()

__starting_point()