#include<iostream>
using namespace std;
int a[1005];
int main()
{
	int i,j,n,m,b,flag=0,c,d;
	float s;
	cin>>n;
	for(s=0,i=1;i<=n;i++)
	{
		cin>>a[i];
		s+=a[i];
	}
	m=s/n;
	if(m!=s/n)cout<<"Unrecoverable configuration."<<endl;
	else
	{
		for(i=1;i<=n;i++)
		{
			if(m!=a[i])
			{
				c=i;b=a[i]-m;
				for(j=i+1;j<=n;j++)
					if(a[j]+b==m)
					{
						flag++;d=j;a[i]=m;a[j]=m;
					    printf("%d ml. from cup #%d to cup #%d.\n",b,d,c);
					    break;
					}
			}
		}
    	if(flag==0)cout<<"Exemplary pages."<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int a[1005];
int main()
{
	int i,j,n,m,b,flag=0,c,d;
	float s;
	cin>>n;
	for(s=0,i=1;i<=n;i++)
	{
		cin>>a[i];
		s+=a[i];
	}
	m=s/n;
	if(m!=s/n)cout<<"Unrecoverable configuration."<<endl;
	else
	{
		for(i=1;i<=n;i++)
		{
			if(m!=a[i])
			{
				c=i;b=a[i]-m;flag++;
				for(j=i+1;j<=n;j++)
					if(a[j]+b==m)
					{
						d=j;a[i]=m;a[j]=m;
					    break;
					}
			}
		}
    	if(flag==0)cout<<"Exemplary pages."<<endl;
		else if(flag==1)
		{
			if(b>0)printf("%d ml. from cup #%d to cup #%d.\n",b,d,c);
			else printf("%d ml. from cup #%d to cup #%d.\n",(-1)*b,c,d);
		}
		else cout<<"Unrecoverable configuration."<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int a[1005];
int main()
{
	int i,j,n,m,b,flag=0,c,d,k;
	float s;
	cin>>n;
	for(s=0,i=1;i<=n;i++)
	{
		cin>>a[i];
		s+=a[i];
	}
	m=s/n;
	if(m!=s/n)cout<<"Unrecoverable configuration."<<endl;
	else
	{
		for(k=0,i=1;i<=n;i++)
		{
			if(m!=a[i])
			{
				c=i;b=a[i]-m;flag++;
				for(j=i+1;j<=n;j++)
					if(a[j]+b==m)
					{
						d=j;a[i]=m;a[j]=m;k=1;
					    break;
					}
			}
		}
    	if(flag==0)cout<<"Exemplary pages."<<endl;
		if(flag==1)
		{
			if(b>0)printf("%d ml. from cup #%d to cup #%d.\n",b,d,c);
			else printf("%d ml. from cup #%d to cup #%d.\n",(-1)*b,c,d);
		}
		else if(k==0||flag>1)cout<<"Unrecoverable configuration."<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int a[1005];
int main()
{
	int i,j,n,m,b,flag=0,c,d;
	float s;
	cin>>n;
	for(s=0,i=1;i<=n;i++)
	{
		cin>>a[i];
		s+=a[i];
	}
	m=s/n;
	if(m!=s/n)cout<<"Unrecoverable configuration."<<endl;
	else
	{
		for(i=1;i<=n;i++)
		{
			if(m!=a[i])
			{
				c=i;b=a[i]-m;
				for(j=i+1;j<=n;j++)
					if(a[j]+b==m)
					{
						flag++;d=j;a[i]=m;a[j]=m;
					    if(b>0)printf("%d ml. from cup #%d to cup #%d.\n",b,d,c);
						else printf("%d ml. from cup #%d to cup #%d.\n",(-1)*b,c,d);
					    break;
					}
			}
		}
    	if(flag==0)cout<<"Exemplary pages."<<endl;
	}
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
	int n,a[10000],b[10000],i,j;
	cin>>n;
	for(i=1;i<=n;i++)
	{cin >> a[i];
	b[i]=i;}
	for(i=1;i<=n;i++)
	for(j=i+1;j<=n;j++)
	{
		if(a[i]>a[j])
		{
			swap(a[i],a[j]);
			swap(b[i],b[j]);
		}
	}
	if(a[1]==a[n]){cout<<"Exemplary pages.";}
	else
	{
		if(((n>2&&a[2]==a[n-1])&&(a[2]-a[1]==a[n]-a[n-1])) || (n==2 && (a[2]-a[1])%2==0))
		{ cout<<(a[n]-a[1])/2<<" ml. from cup #"<<b[1]<<" to cup #"<<b[n]<<"."; }
		else
		{
			 cout << "Unrecoverable configuration.";
		}
	}
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
	int n,a[10000],b[10000],i,j;
	cin>>n;
	for(i=1;i<=n;i++)
	{cin >> a[i];
	b[i]=i;}
	for(i=1;i<=n;i++)
	for(j=i+1;j<=n;j++)
	{
		if(a[i]>a[j])
		{
			swap(a[i],a[j]);
			swap(b[i],b[j]);
		}
	}
	if(a[1]==a[n]){cout<<"Exemplary pages.";}
	else
	{
		if((a[2]==a[n-1])&&(a[2]-a[1]==a[n]-a[n-1]))
		{ cout<<a[2]-a[1]<<" ml. from cup #"<<b[1]<<" to cup #"<<b[n]<<"."; }
		else { cout << "Unrecoverable configuration.";}
	}
	return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector< pair<int, int> > v(n);
    for (int i = 0; i < n; i++)
        cin >> v[i].first, v[i].second = i;

    sort(v.begin(), v.end());
    if (v[0].first == v[n - 1].first)
        cout << "Exemplary pages.";
    else if ((v[0].first + v[n - 1].first)%2 || n > 2 && (v[1].first != v[n - 2].first || v[1].first != (v[0].first + v[n - 1].first)/2))
        cout << "Unrecoverable configuration.";
    else
        cout << (v[n - 1].first - v[0].first)/2 << " ml. from cup #" << v[0].second + 1 << " to cup #" << v[n - 1].second + 1 << ".";
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++)
        cin >> v[i];

    vector<int> g(v);
    int d = 0;
    sort(g.begin(), g.end());
    for (int i = 1; i < n; i++)
        if (g[i] != g[i - 1])
            d++;
    if (d == 0)
        cout << "Exemplary pages.";
    else if (d > 2)
        cout << "Unrecoverable configuration.";
    else {
        int minn = 0, maxn = 0;
        for (int i = 0; i < n; i++) {
            if (v[i] < v[minn])
                minn = i;
            if (v[i] > v[maxn])
                maxn = i;
        }
        int fc;
        for (int i = 0; i < n; i++)
            if (i != minn && i != maxn)
                fc = i;
        int vt = v[maxn] - v[minn];
        if (vt%2)
            cout << "Unrecoverable configuration.";
        else {
            if (n > 2 && (v[maxn] - v[fc] != v[fc] - v[minn]))
                cout << "Unrecoverable configuration.";
            else
                cout << vt/2 << " ml. from cup #" << minn + 1 << " to cup #" << maxn + 1 << ".";
        }
    }
    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector< pair<int, int> > v(n);
    for (int i = 0; i < n; i++)
        cin >> v[i].first, v[i].second = i;

    sort(v.begin(), v.end());
    if (v[0] == v[n - 1])
        cout << "Exemplary pages.";
    else if ((v[0].first + v[n - 1].first)%2 || n > 2 && (v[1].first != v[n - 2].first || v[1].first != (v[0].first + v[n - 1].first)/2))
        cout << "Unrecoverable configuration.";
    else
        cout << (v[n - 1].first - v[0].first)/2 << " ml. from cup #" << v[0].second + 1 << " to cup #" << v[n - 1].second + 1 << ".";
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <complex>
#include <list>
#include <deque>
#include <cassert>
#include <ctime>
using namespace std;

#ifdef WIN32
	#define LLD "%I64d"
#else
	#define LLD "%lld"
#endif

typedef long long ll;
typedef unsigned long long ull;

const double PI = acos(-1.0);
const double eps = 1e-12;
const int INF = (1 << 30) - 1;
const ll LLINF = ((ll)1 << 62) - 1;

#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define X first
#define Y second
#define all(x) x.begin(),x.end()
#define next nexttt
#define prev prevvv
#define y1 y111

const int maxn = 1000 * 1 + 10;
int a[maxn];

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, sum = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		sum += a[i];
	}

	if (sum % n != 0) {
		puts("Unrecoverable configuration.");
		return 0;
	}

	int s = sum / n;
	vector<int> v;
	bool flag = false;
	for (int i = 0; i < n; i++) {
		if (a[i] != s) {
			v.pb(i);
			flag = true;
		}
	}

	if (!flag) {
		puts("Exemplary pages.");
		return 0;
	} else {
		if (a[v[0]] < a[v[1]]) {
			printf("%d ml. from cup #%d to cup #%d.", s - a[v[0]], v[0] + 1, v[1] + 1);
		} else {
			printf("%d ml. from cup #%d to cup #%d.", s - a[v[1]], v[1] + 1, v[0] + 1);
		}
	}

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <complex>
#include <list>
#include <deque>
#include <cassert>
#include <ctime>
using namespace std;

#ifdef WIN32
	#define LLD "%I64d"
#else
	#define LLD "%lld"
#endif

typedef long long ll;
typedef unsigned long long ull;

const double PI = acos(-1.0);
const double eps = 1e-12;
const int INF = (1 << 30) - 1;
const ll LLINF = ((ll)1 << 62) - 1;

#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define X first
#define Y second
#define all(x) x.begin(),x.end()
#define next nexttt
#define prev prevvv
#define y1 y111

const int maxn = 1000 * 1 + 10;
int a[maxn];

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, sum = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		sum += a[i];
	}

	if (sum % n != 0) {
		puts("Unrecoverable configuration.");
		return 0;
	}

	int s = sum / n;
	vector<int> v;
	bool flag = false;
	for (int i = 0; i < n; i++) {
		if (a[i] != s) {
			v.pb(i);
			flag = true;
		}
	}

	if (!flag) {
		puts("Exemplary pages.");
		return 0;
	} else {

		if (sz(v) == 2) {
			if (a[v[0]] < a[v[1]]) {
				if (s - a[v[0]] == a[v[1]] - s) {
					printf("%d ml. from cup #%d to cup #%d.", s - a[v[0]], v[0] + 1, v[1] + 1);
				} else {
					puts("Unrecoverable configuration.");
					return 0;
				}
			} else {
				if (s - a[v[1]] == a[v[0]] - s) {
					printf("%d ml. from cup #%d to cup #%d.", s - a[v[1]], v[1] + 1, v[0] + 1);
				} else {
					puts("Unrecoverable configuration.");
					return 0;
				}
			}
		} else {
			puts("Unrecoverable configuration.");
			return 0;
		}
	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
#include <conio.h>
#include <utility>
#include <set>
#include <time.h>
#include <list>
#include <typeinfo>
#include <cstdio>
#include <numeric>
#include <limits>
#include <functional>
#include <stack>
using namespace std;
#define X first
#define Y second
#define PI 3.1415926535897932384626433832795
#define PI2 PI * 2
#define sqr(x) ((x)*(x))
#define all(n) (n).begin(), (n).end()
#define SORT(c) sort((c).begin(),(c).end())
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef pair<float, float> pff;
const int MOD = (int)1e9 + 7;
typedef double real;

//#pragma comment(linker, "/STACK:64000000")

void load(int k = 1) {
	if (k == 1)	{
#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
#endif
	} else if (k == 2) {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	} else {
		freopen("in", "w", stdout);
	}
}
//
const int N = (int)1e8 + 10;
const int M = 1000;
int A[M];

int getNum(int g) {
	int res = 0;
	while (A[res] != g) ++res;
	return res + 1;
}

void solve() {
	int n;
	cin >> n;
	if (n == 1) {
		printf("Exemplary pages.");
		return;
	}
	map<int, int> m;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &A[i]);
		if (m.count(A[i]) == 0) {
			m[A[i]] = 1;
		} else {
			++m[A[i]];
		}
	}
	if (m.size() == 1) {
		printf("Exemplary pages.");
		return;
	}
	vector<int> r;
	int s = -1;
	for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
		if ((*it).second == 1) {
			r.push_back((*it).first);
		} else {
			s = (*it).first;
		}
	}
	if (r.size() < 2 || r.size() > 3) {
		printf("Unrecoverable configuration.");
	} else if (r.size() == 3) {
		if (n == 3) {
			if (abs(r[0] - r[2]) == abs(r[1] - r[2])) {
				int a = min(r[0], r[1]), b = max(r[0], r[1]);
				printf("% ml. from cup #%d to cup #%d.", abs(r[0] - r[2]), getNum(a), getNum(b));
			} else if (abs(r[0] - r[1]) == abs(r[0] - r[2])) {
				int a = min(r[2], r[1]), b = max(r[2], r[1]);
				printf("% ml. from cup #%d to cup #%d.", abs(r[0] - r[2]), getNum(a), getNum(b));
			} else if (abs(r[0] - r[1]) == abs(r[1] - r[2])) {
				int a = min(r[2], r[0]), b = max(r[2], r[0]);
				printf("% ml. from cup #%d to cup #%d.", abs(r[1] - r[2]), getNum(a), getNum(b));
			} else {
				printf("Unrecoverable configuration.");
			}
		} else {
			printf("Unrecoverable configuration.");
		}
	} else if (r.size() == 2 && m.size() == 3) {
		if (abs(r[0] - s) == abs(r[1] - s)) {
			int a = min(r[0], r[1]), b = max(r[0], r[1]);
			printf("%d ml. from cup #%d to cup #%d.", abs(r[0] - s), getNum(a), getNum(b));
		}
	} else {
		printf("Unrecoverable configuration.");
	}
}

void solve2() {
	load(3);

}

#define TIME_CHECK

int main() {
#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c1 = clock();
#endif
#endif

	load();
	solve();

#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c2 = clock();
	cout << endl << "time: " << (c2 - c1) / (CLOCKS_PER_SEC / 1000);
#endif
#endif
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <math.h>
#include <conio.h>
#include <utility>
#include <set>
#include <time.h>
#include <list>
#include <typeinfo>
#include <cstdio>
#include <numeric>
#include <limits>
#include <functional>
#include <stack>
using namespace std;
#define X first
#define Y second
#define PI 3.1415926535897932384626433832795
#define PI2 PI * 2
#define sqr(x) ((x)*(x))
#define all(n) (n).begin(), (n).end()
#define SORT(c) sort((c).begin(),(c).end())
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef pair<float, float> pff;
const int MOD = (int)1e9 + 7;
typedef double real;

//#pragma comment(linker, "/STACK:64000000")

void load(int k = 1) {
	if (k == 1)	{
#ifndef ONLINE_JUDGE
		freopen("in", "r", stdin);
		freopen("out", "w", stdout);
#endif
	} else if (k == 2) {
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	} else {
		freopen("in", "w", stdout);
	}
}
//
const int N = (int)1e8 + 10;
const int M = 1000;
int A[M];

int getNum(int g) {
	int res = 0;
	while (A[res] != g) ++res;
	return res + 1;
}

void solve() {
	int n;
	cin >> n;
	map<int, int> m;
	for (int i = 0; i < n; ++i) {
		scanf("%d", &A[i]);
		if (m.count(A[i]) == 0) {
			m[A[i]] = 1;
		} else {
			++m[A[i]];
		}
	}
	if (m.size() == 1) {
		printf("Exemplary pages.");
	} else if (m.size() == 2) {
		if (n == 2) {
			int a = min(A[0], A[1]), b = max(A[0], A[1]);
			int d = b - a;
			if (d&1) {
				printf("Unrecoverable configuration.");
			} else {
				printf("%d ml. from cup #%d to cup #%d.", d / 2, getNum(a), getNum(b));
			}
		} else {
			printf("Unrecoverable configuration.");
		}
	} else if (m.size() == 3) {
		if (n == 3) {
			int a = min(A[0], A[1]), b = max(A[0], A[1]), s = A[2];
			if (abs(s - b) == abs(s - a)) {
				printf("%d ml. from cup #%d to cup #%d.", abs(s - a), getNum(a), getNum(b));
				return;
			}
			a = min(A[0], A[2]), b = max(A[0], A[2]), s = A[1];
			if (abs(s - b) == abs(s - a)) {
				printf("%d ml. from cup #%d to cup #%d.", abs(s - a), getNum(a), getNum(b));
				return;
			}
			a = min(A[1], A[2]), b = max(A[1], A[2]), s = A[0];
			if (abs(s - b) == abs(s - a)) {
				printf("%d ml. from cup #%d to cup #%d.", abs(s - a), getNum(a), getNum(b));
				return;
			}
			printf("Unrecoverable configuration.");
		} else {
			vector<int> r;
			int s = -1;
			for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
				if ((*it).second == 1) {
					r.push_back((*it).first);
				} else {
					s = (*it).first;
				}
			}
			int a = min(r[0], r[1]), b = max(r[0], r[1]);
			if (abs(s - b) == abs(s - a)) {
				printf("%d ml. from cup #%d to cup #%d.", abs(s - a), getNum(a), getNum(b));
			} else {
				printf("Unrecoverable configuration.");
			}
		}
	} else {
		printf("Unrecoverable configuration.");
	}
}

void solve2() {
	load(3);

}

#define TIME_CHECK

int main() {
#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c1 = clock();
#endif
#endif

	load();
	solve();

#ifndef ONLINE_JUDGE
#ifdef TIME_CHECK
	double c2 = clock();
	cout << endl << "time: " << (c2 - c1) / (CLOCKS_PER_SEC / 1000);
#endif
#endif
	return 0;
}


#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;
	set <int> s;
	vector < pair <int, int> > v(n);
	for(int i=0; i < n; i++)
	{
		cin >> v[i].first;
		v[i].second = i;
		s.insert(v[i].first);
	}
	switch(s.size())
	{
	case 1:
		cout << "Exemplary pages." << endl;
		break;
	case 2:
		sort(v.begin(), v.end());
		if(v.size() == 2 && ((--v.end())->first - v.begin()->first) % 2 == 0)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	case 3:
		sort(v.begin(), v.end());
		if((v.begin()->first + (--v.end())->first) % 2 == 0 && (v.begin()->first + (--v.end())->first) / 2 == (--(--v.end()))->first)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	default:
		cout << "Unrecoverable configuration." << endl;
		break;
	}
	//system("pause");
	return 0;
}


#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;
	set <int> s;
	vector < pair <int, int> > v(n);
	for(int i=0; i < n; i++)
	{
		cin >> v[i].first;
		v[i].second = i;
		s.insert(v[i].first);
	}
	switch(s.size())
	{
	case 1:
		cout << "Exemplary pages." << endl;
		break;
	case 2:
		sort(v.begin(), v.end());
		if(v.size() == 2 && ((--v.end())->first - v.begin()->first) % 2 == 0)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	case 3:
		sort(v.begin(), v.end());
		if((v.begin()->first + (--v.end())->first) % 2 == 0 && (v.begin()->first + (--v.end())->first) / 2 == (--v.end()--)->first)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	default:
		cout << "Unrecoverable configuration." << endl;
		break;
	}
	//system("pause");
	return 0;
}


#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;
	set <int> s;
	vector < pair <int, int> > v(n);
	for(int i=0; i < n; i++)
	{
		cin >> v[i].first;
		v[i].second = i;
		s.insert(v[i].first);
	}
	switch(s.size())
	{
	case 1:
		cout << "Exemplary pages." << endl;
		break;
	case 2:
		sort(v.begin(), v.end());
		if(((--v.end())->first - v.begin()->first) % 2 == 0)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	case 3:
		sort(v.begin(), v.end());
		if((v.begin()->first + (--v.end())->first) % 2 == 0 && (v.begin()->first + (--v.end())->first) / 2 == (++v.begin())->first)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	default:
		cout << "Unrecoverable configuration." << endl;
		break;
	}
	//system("pause");
	return 0;
}


#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;
	set <int> s;
	vector < pair <int, int> > v(n);
	for(int i=0; i < n; i++)
	{
		cin >> v[i].first;
		v[i].second = i;
		s.insert(v[i].first);
	}
	switch(s.size())
	{
	case 1:
		cout << "Exemplary pages." << endl;
		break;
	case 3:
		sort(v.begin(), v.end());
		if((v.begin()->first + (--v.end())->first) / 2 == (++v.begin())->first)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	default:
		cout << "Unrecoverable configuration." << endl;
		break;
	}
	//system("pause");
	return 0;
}


#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;
	set <int> s;
	vector < pair <int, int> > v(n);
	for(int i=0; i < n; i++)
	{
		cin >> v[i].first;
		v[i].second = i;
		s.insert(v[i].first);
	}
	switch(s.size())
	{
	case 1:
		cout << "Exemplary pages." << endl;
		break;
	case 2:
		sort(v.begin(), v.end());
		if((v.begin()->first + (--v.end())->first) / 2 == (++v.begin())->first)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	case 3:
		sort(v.begin(), v.end());
		if((v.begin()->first + (--v.end())->first) / 2 == (++v.begin())->first)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	default:
		cout << "Unrecoverable configuration." << endl;
		break;
	}
	//system("pause");
	return 0;
}


#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;
	set <int> s;
	vector < pair <int, int> > v(n);
	for(int i=0; i < n; i++)
	{
		cin >> v[i].first;
		v[i].second = i;
		s.insert(v[i].first);
	}
	switch(s.size())
	{
	case 1:
		cout << "Exemplary pages." << endl;
		break;
	case 2:
		sort(v.begin(), v.end());
		cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		break;
	case 3:
		sort(v.begin(), v.end());
		if((v.begin()->first + (--v.end())->first) / 2 == (++v.begin())->first)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	default:
		cout << "Unrecoverable configuration." << endl;
		break;
	}
	//system("pause");
	return 0;
}


#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;
	set <int> s;
	vector < pair <int, int> > v(n);
	for(int i=0; i < n; i++)
	{
		cin >> v[i].first;
		v[i].second = i;
		s.insert(v[i].first);
	}
	switch(s.size())
	{
	case 1:
		cout << "Exemplary pages." << endl;
		break;
	case 2:
		sort(v.begin(), v.end());
		if(v.size() == 2 && ((--v.end())->first - v.begin()->first) % 2 == 0)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	case 3:
		sort(v.begin(), v.end());
		if((v.begin()->first + (--v.end())->first) % 2 == 0 && (v.begin()->first + (--v.end())->first) / 2 == (++v.begin())->first)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	default:
		cout << "Unrecoverable configuration." << endl;
		break;
	}
	//system("pause");
	return 0;
}


#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;
	set <int> s;
	vector < pair <int, int> > v(n);
	for(int i=0; i < n; i++)
	{
		cin >> v[i].first;
		v[i].second = i;
		s.insert(v[i].first);
	}
	switch(s.size())
	{
	case 1:
		cout << "Exemplary pages." << endl;
		break;
	case 2:
		sort(v.begin(), v.end());
		if(v.size() == 2 && ((--v.end())->first - v.begin()->first) % 2 == 0)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	case 3:
		sort(v.begin(), v.end());
		if((v.begin()->first + (--v.end())->first) % 2 == 0 && (v.begin()->first + (--v.end())->first) / 2 == (--(--v.end()))->first && (v.begin()->first + (--v.end())->first) / 2 == (++v.begin())->first)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	default:
		cout << "Unrecoverable configuration." << endl;
		break;
	}
	//system("pause");
	return 0;
}


#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	cin >> n;
	set <int> s;
	vector < pair <int, int> > v(n);
	for(int i=0; i < n; i++)
	{
		cin >> v[i].first;
		v[i].second = i;
		s.insert(v[i].first);
	}
	switch(s.size())
	{
	case 1:
		cout << "Exemplary pages." << endl;
		break;
	case 2:
		sort(v.begin(), v.end());
		if(v.size() == 2 && ((--v.end())->first - v.begin()->first) % 2 == 0)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	case 3:
		sort(v.begin(), v.end());
		if(v.size() == 3 && (v.begin()->first + (--v.end())->first) % 2 == 0 && (v.begin()->first + (--v.end())->first) / 2 == (++v.begin())->first)
			cout << ((--v.end())->first - v.begin()->first)/2 << " ml. from cup #" << v.begin()->second+1 << " to cup #" << (--v.end())->second+1 << "." << endl;
		else
			cout << "Unrecoverable configuration." << endl;
		break;
	default:
		cout << "Unrecoverable configuration." << endl;
		break;
	}
	//system("pause");
	return 0;
}


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

int main()
{
        int n, c[1001];
    int i, sum = 0, aver;
    int bigp, minp, une = 0;
    scanf("%d",&n);
    for(i = 1; i <= n; i++){
        scanf("%d", &c[i]);
        sum = sum + c[i];
    }

    if(sum % n == 0){
        aver = sum / n;
        for( i = 1; i <= n; i++){
            if(c[i] != aver){
                if(c[i] > aver) bigp = i;
                else{
                    minp = i;
                }
                une = une + 1;
            }
        }
        if(une == 0) {printf("Exemplary pages."); }
        else if(une == 2)  { printf("%d ml. from cup #% d to cup # %d.",c[bigp] - aver, minp, bigp); }
        else{
            printf("Unrecoverable configuration.");
        }

    }
    else{
        printf("Unrecoverable configuration.");
    }

    return 0;
}


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

int main()
{
        int n, c[1001];
    int i, sum = 0, aver;
    int bigp, minp, une = 0;
    scanf("%d",&n);
    for(i = 1; i <= n; i++){
        scanf("%d", &c[i]);
        sum = sum + c[i];
    }

    if(sum % n == 0){
        aver = sum / n;
        for( i = 1; i <= n; i++){
            if(c[i] != aver){
                if(c[i] > aver) bigp = i;
                else{
                    minp = i;
                }
                une = une + 1;
            }
        }
        if(une == 0) {printf("Exemplary pages."); }
        else if(une == 2)  { printf("%d ml. from cup #%d to cup #%d.",c[bigp] - aver, minp, bigp); }
        else{
            printf("Unrecoverable configuration.");
        }

    }
    else{
        printf("Unrecoverable configuration.");
    }

    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;
int main() {
int n;
    cin >> n;
    vector< pair<int, int> > v(n);
for (int i = 0; i < n; i++)
        cin >> v[i].first, v[i].second = i;
sort(v.begin(), v.end());
if (v[0].first == v[n - 1].first)
        cout << "Exemplary pages.";
else if ((v[0].first + v[n - 1].first)%2 || n > 2 && (v[1].first != v[n - 2].first || v[1].first != (v[0].first + v[n - 1].first)/2))
        cout << "Unrecoverable configuration.";
else
        cout << (v[n - 1].first - v[0].first)/2 << " ml. from cup #" << v[0].second + 1 << " to cup #" << v[n - 1].second + 1 << ".";
return 0;
}


#include <iostream>
#include <fstream>

#include <string>
#include <vector>

#include <utility>

#include <algorithm>

using namespace std;

int n;
vector< pair<int,int> > a;

int main()
{

#if 0
	ifstream ifile("input.txt");
	if(ifile) freopen("input.txt","rt",stdin);
#ifdef ONLINE_JUDGE
	if(ifile) freopen("output.txt","wt",stdout);
#endif
#endif

	cin >> n;
	for( int i=0; i<n; ++i )
	{
		int t;
		cin >> t;
		a.push_back( make_pair( t, i+1 ) );
	}

	sort( a.begin(), a.end() );

	if ( a.size() == 1 )
		cout << "Exemplary pages.";
	else if ( a.size() == 2 )
	{
		if ( a[1].first == a[0].first )
			cout << "Exemplary pages.";
		else if ( ( a[1].first-a[0].first ) % 2 )
			cout << "Unrecoverable configuration.";
		else
			cout << (a[1].first-a[0].first)/2 << " ml. from cup #"<<a[0].second<< " to cup #"<<a[1].second<<".";
	}
	else
	{
		if ( a[0].first == a[a.size()-1].first )
			cout << "Exemplary pages.";
		else if ( ( a[a.size()-1].first-a[0].first ) %2 )
			cout << "Unrecoverable configuration.";
		else if ( ( a[a.size()-1].first+a[0].first )/2 == a[1].first && ( a[a.size()-1].first+a[0].first )/2 == a[a.size()-2].first )
			cout << (a[a.size()-1].first-a[0].first)/2 << " ml. from cup #"<<a[0].second<< " to cup #"<<a[a.size()-1].second<<".";
		else
			cout << "Unrecoverable configuration.";
	}

	return 0;
}


#include <iostream>
#include <fstream>

#include <string>
#include <vector>

#include <utility>

#include <algorithm>

using namespace std;

int n;
vector< pair<int,int> > a;

int main()
{

#if 0
	ifstream ifile("input.txt");
	if(ifile) freopen("input.txt","rt",stdin);
#ifdef ONLINE_JUDGE
	if(ifile) freopen("output.txt","wt",stdout);
#endif
#endif

	cin >> n;
	for( int i=0; i<n; ++i )
	{
		int t;
		cin >> t;
		a.push_back( make_pair( t, i+1 ) );
	}

	sort( a.begin(), a.end() );

	if ( a.size() == 1 )
		cout << "Exemplary pages.";
	else if ( a.size() == 2 )
	{
		if ( ( a[1].first-a[0].first ) % 2 )
			cout << "Unrecoverable configuration.";
		else
			cout << (a[1].first-a[0].first)/2 << " ml. from cup #"<<a[0].second<< " to cup #"<<a[1].second<<".";
	}
	else
	{
		if ( a[0].first == a[a.size()-1].first )
			cout << "Exemplary pages.";
		else if ( ( a[a.size()-1].first-a[0].first ) %2 )
			cout << "Unrecoverable configuration.";
		else if ( ( a[a.size()-1].first+a[0].first )/2 == a[1].first && ( a[a.size()-1].first+a[0].first )/2 == a[a.size()-2].first )
			cout << (a[a.size()-1].first-a[0].first)/2 << " ml. from cup #"<<a[0].second<< " to cup #"<<a[a.size()-1].second<<".";
		else
			cout << "Unrecoverable configuration.";
	}

	return 0;
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    int n, m, x=0;
    int a,b,maxx,minn,k;
    vector<int> c(10);
    vector<int> v;
    cin>>n;
    for(int i=0; i<n;i++){
        cin>>m;
        v.push_back(m);
        c[m-1]++;
    }
    for(int i=n-1; i>=0; i--){
        x = i+1;
        cout<<"X:"<<x<<endl;
        m = v[i];
        cout<<"m:"<<m<<endl;
        v.pop_back();
        maxx = 1;
        minn = 10e5;
        for(int i=0; i<10; i++){
            if(c[i]>maxx){maxx = c[i];}
            if(c[i]<minn && c[i]>=1){minn = c[i];}
        }
        cout<<"max min "<<maxx<<' '<<minn<<endl;
        if(maxx-minn==1){
            a=0;
            k=0;
            for(int i=0; i<c.size(); i++){
                if(c[i]!=0){k++;}
                if(c[i]==maxx){a++;}
            }
            if(a==1){
                cout<<x;
                return 0;
            }
            else if(a==k-1){
                cout<<x;
                return 0;
            }
        }
        if(maxx==minn){
            if(maxx==1){
                cout<<n;
                return 0;
            }
            else{
                if(x!=n){
                    cout<<x+1;
                    return 0;
                }
            }
        }
        c[m-1]--;
    }
	return 0;
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    int n, m, x=0;
    int a,b,maxx,minn;
    vector<int> c(10);
    vector<int> v;
    cin>>n;
    for(int i=0; i<n;i++){
        cin>>m;
        v.push_back(m);
        c[m-1]++;
    }
    for(int i=n-1; i>=0; i--){
        x = i+1;
        m = v[i];
        v.pop_back();
        maxx = 1;
        minn = 10e5;
        for(int i=0; i<10; i++){
            if(c[i]>maxx){maxx = c[i];}
            if(c[i]<minn && c[i]>=1){minn = c[i];}
        }
        if(maxx-minn==1){
            a=0;
            for(int i=0; i<c.size(); i++){
                if(c[i]==maxx){a++;}
            }
            if(a==1){
                cout<<x;
                return 0;
            }
        }
        if(maxx==minn){
            if(maxx==1){
                cout<<n;
                return 0;
            }
            else{
                if(x!=n){
                    cout<<x+1;
                    return 0;
                }
            }
        }
        c[m-1]--;
    }
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
int  bs(int  a[], int  l, int  r, int  x){if(r>=l){ int  mid=(l+r)/2;if(a[mid]==x)return mid;else if(a[mid]>x)return bs(a,l,mid-1,x); else return bs(a,mid+1,r,x);}return -1;}
int  s_t_i(string s){stringstream geek(s); int  m;geek>>m;return m;}
string i_t_s(int  n){ostringstream str1;str1<<n;string geek = str1.str(); return geek;}
int  modn( int  a, int  b){return  (a % b + b) % b;}
int  power(int  b, int  p){if(p==0)return 1;else if(p%2==0)return (power(b,p/2)*(power(b,p/2)));else return ((b)*(power(b,p-1)));}
bool isprime( int  n){if(n==1)return false;if(n==2 or n==3)return true; int  lim=sqrt(n); int  i=2;while(i<=lim){if(n%i==0)return false; i++;}return true;}
vector< int > factors( int  n){vector< int > a;a.pb(1); int  lim=sqrt(n),i=2;while(i<=lim){if(n%i==0){a.pb(i);if(i!=n/i)a.pb(n/i);}i++;}if(n!=1)a.pb(n);sort(a.begin(),a.end());return a;}
vector<pair<int ,int > > pf_with_count(int  b){ pair<int ,int > p;vector<pair<int ,int > >pf;int  count=0;while(b%2==0){b/=2;count++;}if(count>0){p.f=2;p.s=count;pf.pb(p);}int  i=3;while(i<=sqrt(b)){count=0;while(b%i==0){b/=i;count++;}if(count>0){p.f=i;p.s=count;pf.pb(p);}i+=2;}if(b>2) {p.f=b;p.s=1;pf.pb(p);}return pf;}
int  bs_t_i(string s){int  l=strtoull(s.c_str(),NULL,2);return l;}
string  i_to_bs(int  n){string s = bitset< 15 >( n ).to_string();return s;}
bool check(int arr[])
{
	int i=0;
	set<int> st;
	while(i<10)
	{
		if(arr[i]>0)st.insert(arr[i]);
		i++;
	}
	if(st.size()!=1)return false;
	return true;

}
int mce(int arr[])
{

	int i=0;
	while(i<10)
	{
        if(arr[i]>0)
        	{
        		arr[i]--;
        		if(check(arr))return 1;
        		else arr[i]++;
        	}

        i++;
	}
	return 0;
}

signed main()
{
	ios
	int n;
	cin>>n;
	int a[n];
	int i=0;
	int arr[10];
	fill(arr,arr+10,0);
	while(i<n)
	{
       cin>>a[i];
       arr[a[i]-1]++;
       i++;
	}
	if(n==1)
	{
		cout<<1;
		return 0;
	}
	i=n-1;
	while(i>=0)
	{
       if(mce(arr))
       {
       	cout<<i+1;
       	return 0;
       }
       arr[a[i]-1]--;

       i--;
	}

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
int  bs(int  a[], int  l, int  r, int  x){if(r>=l){ int  mid=(l+r)/2;if(a[mid]==x)return mid;else if(a[mid]>x)return bs(a,l,mid-1,x); else return bs(a,mid+1,r,x);}return -1;}
int  s_t_i(string s){stringstream geek(s); int  m;geek>>m;return m;}
string i_t_s(int  n){ostringstream str1;str1<<n;string geek = str1.str(); return geek;}
int  modn( int  a, int  b){return  (a % b + b) % b;}
int  power(int  b, int  p){if(p==0)return 1;else if(p%2==0)return (power(b,p/2)*(power(b,p/2)));else return ((b)*(power(b,p-1)));}
bool isprime( int  n){if(n==1)return false;if(n==2 or n==3)return true; int  lim=sqrt(n); int  i=2;while(i<=lim){if(n%i==0)return false; i++;}return true;}
vector< int > factors( int  n){vector< int > a;a.pb(1); int  lim=sqrt(n),i=2;while(i<=lim){if(n%i==0){a.pb(i);if(i!=n/i)a.pb(n/i);}i++;}if(n!=1)a.pb(n);sort(a.begin(),a.end());return a;}
vector<pair<int ,int > > pf_with_count(int  b){ pair<int ,int > p;vector<pair<int ,int > >pf;int  count=0;while(b%2==0){b/=2;count++;}if(count>0){p.f=2;p.s=count;pf.pb(p);}int  i=3;while(i<=sqrt(b)){count=0;while(b%i==0){b/=i;count++;}if(count>0){p.f=i;p.s=count;pf.pb(p);}i+=2;}if(b>2) {p.f=b;p.s=1;pf.pb(p);}return pf;}
int  bs_t_i(string s){int  l=strtoull(s.c_str(),NULL,2);return l;}
string  i_to_bs(int  n){string s = bitset< 15 >( n ).to_string();return s;}
bool check(int arr[])
{
	int i=0;
	set<int> st;
	while(i<10)
	{
		if(arr[i]>0)st.insert(arr[i]);
		i++;
	}
	if(st.size()!=1)return false;
	return true;

}
int mce(int arr[])
{
    int i=0;
	while(i<10)
	{
        if(arr[i]>0)
        	{
        		arr[i]--;
        		if(check(arr))return 1;
        		arr[i]++;
        	}
        i++;
	}
	return 0;
}

signed main()
{
	ios
	int n;
	cin>>n;
	int a[n];
	int i=0;
	int arr[10];
	fill(arr,arr+10,0);
	while(i<n)
	{
       cin>>a[i];
       arr[a[i]-1]++;
       i++;
	}
	if(n==1)
	{
		cout<<1;
		return 0;
	}
	i=n-1;
	while(i>=0)
	{
       if(mce(arr))
       {
       	cout<<i+1;
       	return 0;
       }
       arr[a[i]-1]--;

       i--;
	}
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
int  bs(int  a[], int  l, int  r, int  x){if(r>=l){ int  mid=(l+r)/2;if(a[mid]==x)return mid;else if(a[mid]>x)return bs(a,l,mid-1,x); else return bs(a,mid+1,r,x);}return -1;}
int  s_t_i(string s){stringstream geek(s); int  m;geek>>m;return m;}
string i_t_s(int  n){ostringstream str1;str1<<n;string geek = str1.str(); return geek;}
int  modn( int  a, int  b){return  (a % b + b) % b;}
int  power(int  b, int  p){if(p==0)return 1;else if(p%2==0)return (power(b,p/2)*(power(b,p/2)));else return ((b)*(power(b,p-1)));}
bool isprime( int  n){if(n==1)return false;if(n==2 or n==3)return true; int  lim=sqrt(n); int  i=2;while(i<=lim){if(n%i==0)return false; i++;}return true;}
vector< int > factors( int  n){vector< int > a;a.pb(1); int  lim=sqrt(n),i=2;while(i<=lim){if(n%i==0){a.pb(i);if(i!=n/i)a.pb(n/i);}i++;}if(n!=1)a.pb(n);sort(a.begin(),a.end());return a;}
vector<pair<int ,int > > pf_with_count(int  b){ pair<int ,int > p;vector<pair<int ,int > >pf;int  count=0;while(b%2==0){b/=2;count++;}if(count>0){p.f=2;p.s=count;pf.pb(p);}int  i=3;while(i<=sqrt(b)){count=0;while(b%i==0){b/=i;count++;}if(count>0){p.f=i;p.s=count;pf.pb(p);}i+=2;}if(b>2) {p.f=b;p.s=1;pf.pb(p);}return pf;}
int  bs_t_i(string s){int  l=strtoull(s.c_str(),NULL,2);return l;}
string  i_to_bs(int  n){string s = bitset< 15 >( n ).to_string();return s;}

	int a[100000];
	int b[100000];
signed main()
{
	ios;
	int n;
	cin>>n;
	int arr[n];
	set<int> stt;
	int i=0;
    fill(a,a+100000,0);
	fill(b,b+100000,0);
    while(i<n)
	{
       cin>>arr[i];
       stt.insert(arr[i]);
       a[arr[i]-1]++;
       i++;
	}
	set<int> st;
	i=0;
	while(i<100000)
	{

		if(a[i]>0)
			{
				st.insert(a[i]);
				b[a[i]-1]++;
			}
		i++;
	}
	set<int> :: iterator itr;
	if(n==1)
	{
		cout<<1;
		return 0;
	}
	i=n-1;
   while(i>=0)
	{
	    if((st.size()==1 and *st.begin()==1)or stt.size()==1)
	    {
         cout<<i+1;
         return 0;
	    }
	    if((st.size()==2 and *st.rbegin()-*st.begin()==1 and b[*st.rbegin()-1]==1) or (st.size()==2  and b[0]==1 and *st.begin()==1))
		{
		    cout<<i+1;
			return 0;
		}
        int val=a[arr[i]-1];
		a[arr[i]-1]--;
		if(val>1)
		{
			st.insert(val-1);
			b[val-2]++;
		}
		b[val-1]--;
		if(b[val-1]==0)
		{
            itr=st.lower_bound(val);
			st.erase(itr);
	    }
	   i--;
	}

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
int  bs(int  a[], int  l, int  r, int  x){if(r>=l){ int  mid=(l+r)/2;if(a[mid]==x)return mid;else if(a[mid]>x)return bs(a,l,mid-1,x); else return bs(a,mid+1,r,x);}return -1;}
int  s_t_i(string s){stringstream geek(s); int  m;geek>>m;return m;}
string i_t_s(int  n){ostringstream str1;str1<<n;string geek = str1.str(); return geek;}
int  modn( int  a, int  b){return  (a % b + b) % b;}
//int  power(int  b, int  p){if(p==0)return 1;else if(p%2==0)return (power(b,p/2)*(power(b,p/2)));else return ((b)*(power(b,p-1)));}
bool isprime( int  n){if(n==1)return false;if(n==2 or n==3)return true; int  lim=sqrt(n); int  i=2;while(i<=lim){if(n%i==0)return false; i++;}return true;}
vector< int > factors( int  n){vector< int > a;a.pb(1); int  lim=sqrt(n),i=2;while(i<=lim){if(n%i==0){a.pb(i);if(i!=n/i)a.pb(n/i);}i++;}if(n!=1)a.pb(n);sort(a.begin(),a.end());return a;}
vector<pair<int ,int > > pf_with_count(int  b){ pair<int ,int > p;vector<pair<int ,int > >pf;int  count=0;while(b%2==0){b/=2;count++;}if(count>0){p.f=2;p.s=count;pf.pb(p);}int  i=3;while(i<=sqrt(b)){count=0;while(b%i==0){b/=i;count++;}if(count>0){p.f=i;p.s=count;pf.pb(p);}i+=2;}if(b>2) {p.f=b;p.s=1;pf.pb(p);}return pf;}
int  bs_t_i(string s){int  l=strtoull(s.c_str(),NULL,2);return l;}
string  i_to_bs(int  n){string s = bitset< 15 >( n ).to_string();return s;}
bool check(int arr[])
{
	int i=0;
	set<int> st;
	while(i<10)
	{
		if(arr[i]>0)st.insert(arr[i]);
		i++;
	}
	if(st.size()!=1)return false;
	return true;

}
int mce(int arr[])
{

	int i=0;
	while(i<10)
	{
        if(arr[i]>0)
        	{
        		arr[i]--;
        		if(check(arr))return 1;
        		else arr[i]++;
        	}

        i++;
	}
	return 0;
}

signed main()
{
	ios
	int n;
	cin>>n;
	int a[n];
	int i=0;
	int arr[10];
	fill(arr,arr+10,0);
	while(i<n)
	{
       cin>>a[i];
       arr[a[i]-1]++;
       i++;
	}
	if(n==1)
	{
		cout<<1;
		return 0;
	}
	i=n-1;
	while(i>=0)
	{
       if(mce(arr))
       {
       	cout<<i+1;
       	return 0;
       }
       arr[a[i]-1]--;

       i--;
	}

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
int  bs(int  a[], int  l, int  r, int  x){if(r>=l){ int  mid=(l+r)/2;if(a[mid]==x)return mid;else if(a[mid]>x)return bs(a,l,mid-1,x); else return bs(a,mid+1,r,x);}return -1;}
int  s_t_i(string s){stringstream geek(s); int  m;geek>>m;return m;}
string i_t_s(int  n){ostringstream str1;str1<<n;string geek = str1.str(); return geek;}
int  modn( int  a, int  b){return  (a % b + b) % b;}
int  power(int  b, int  p){if(p==0)return 1;else if(p%2==0)return (power(b,p/2)*(power(b,p/2)));else return ((b)*(power(b,p-1)));}
bool isprime( int  n){if(n==1)return false;if(n==2 or n==3)return true; int  lim=sqrt(n); int  i=2;while(i<=lim){if(n%i==0)return false; i++;}return true;}
vector< int > factors( int  n){vector< int > a;a.pb(1); int  lim=sqrt(n),i=2;while(i<=lim){if(n%i==0){a.pb(i);if(i!=n/i)a.pb(n/i);}i++;}if(n!=1)a.pb(n);sort(a.begin(),a.end());return a;}
vector<pair<int ,int > > pf_with_count(int  b){ pair<int ,int > p;vector<pair<int ,int > >pf;int  count=0;while(b%2==0){b/=2;count++;}if(count>0){p.f=2;p.s=count;pf.pb(p);}int  i=3;while(i<=sqrt(b)){count=0;while(b%i==0){b/=i;count++;}if(count>0){p.f=i;p.s=count;pf.pb(p);}i+=2;}if(b>2) {p.f=b;p.s=1;pf.pb(p);}return pf;}
int  bs_t_i(string s){int  l=strtoull(s.c_str(),NULL,2);return l;}
string  i_to_bs(int  n){string s = bitset< 15 >( n ).to_string();return s;}
bool check(int arr[])
{
	int i=0;
	set<int> st;
	while(i<10)
	{
		if(arr[i]>0)st.insert(arr[i]);
		i++;
	}
	if(st.size()!=1)return false;
	return true;

}
int mce(int arr[])
{
   int i=0;
	while(i<10)
	{
        if(arr[i]>0)
        	{
        		arr[i]--;
        		if(check(arr))return 1;
        		else arr[i]++;
        	}
        i++;
	}
	return 0;
}

signed main()
{
	ios
	int n;
	cin>>n;
	int a[n];
	int i=0;
	int arr[10];
	fill(arr,arr+10,0);
	while(i<n)
	{
       cin>>a[i];
       arr[a[i]-1]++;
       i++;
	}
	if(n==1)
	{
		cout<<1;
		return 0;
	}
	i=n-1;
	while(i>=0)
	{
       if(mce(arr))
       {
       	cout<<i+1;
       	return 0;
       }
       arr[a[i]-1]--;

       i--;
	}

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
int  bs(int  a[], int  l, int  r, int  x){if(r>=l){ int  mid=(l+r)/2;if(a[mid]==x)return mid;else if(a[mid]>x)return bs(a,l,mid-1,x); else return bs(a,mid+1,r,x);}return -1;}
int  s_t_i(string s){stringstream geek(s); int  m;geek>>m;return m;}
string i_t_s(int  n){ostringstream str1;str1<<n;string geek = str1.str(); return geek;}
int  modn( int  a, int  b){return  (a % b + b) % b;}
int  power(int  b, int  p){if(p==0)return 1;else if(p%2==0)return (power(b,p/2)*(power(b,p/2)));else return ((b)*(power(b,p-1)));}
bool isprime( int  n){if(n==1)return false;if(n==2 or n==3)return true; int  lim=sqrt(n); int  i=2;while(i<=lim){if(n%i==0)return false; i++;}return true;}
vector< int > factors( int  n){vector< int > a;a.pb(1); int  lim=sqrt(n),i=2;while(i<=lim){if(n%i==0){a.pb(i);if(i!=n/i)a.pb(n/i);}i++;}if(n!=1)a.pb(n);sort(a.begin(),a.end());return a;}
vector<pair<int ,int > > pf_with_count(int  b){ pair<int ,int > p;vector<pair<int ,int > >pf;int  count=0;while(b%2==0){b/=2;count++;}if(count>0){p.f=2;p.s=count;pf.pb(p);}int  i=3;while(i<=sqrt(b)){count=0;while(b%i==0){b/=i;count++;}if(count>0){p.f=i;p.s=count;pf.pb(p);}i+=2;}if(b>2) {p.f=b;p.s=1;pf.pb(p);}return pf;}
int  bs_t_i(string s){int  l=strtoull(s.c_str(),NULL,2);return l;}
string  i_to_bs(int  n){string s = bitset< 15 >( n ).to_string();return s;}
bool check(int arr[])
{
	int i=0;
	set<int> st;
	while(i<10)
	{
		if(arr[i]>0)st.insert(arr[i]);
		i++;
	}
	if(st.size()!=1)return false;
	return true;

}
int mce(int arr[])
{

	int i=0;
	while(i<10)
	{
        if(arr[i]>0)
        	{
        		arr[i]--;
        		if(check(arr))return 1;
        		else arr[i]++;
        	}
      i++;
	}
	return 0;
}

signed main()
{
	ios
	int n;
	cin>>n;
	int a[n];
	int i=0;
	int arr[10];
	fill(arr,arr+10,0);
	while(i<n)
	{
       cin>>a[i];
       arr[a[i]-1]++;
       i++;
	}
	if(n==1)
	{
		cout<<1;
		return 0;
	}
	i=n-1;
	while(i>=0)
	{
       if(mce(arr))
       {
       	cout<<i+1;
       	return 0;
       }
       arr[a[i]-1]--;

       i--;
	}
  cout<<13;

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> //for less
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define m_p make_pair
#define pb push_back
#define f first
#define s second
#define pii pair<int,int>
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define double long double
const double pi=3.14159265358979;
const   int  mod=1e9+7;
int takemod(int a){return ((a%mod)+mod)%mod;}
typedef  tree < int ,  null_type ,  less<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
int  bs(int  a[], int  l, int  r, int  x){if(r>=l){ int  mid=(l+r)/2;if(a[mid]==x)return mid;else if(a[mid]>x)return bs(a,l,mid-1,x); else return bs(a,mid+1,r,x);}return -1;}
int  s_t_i(string s){stringstream geek(s); int  m;geek>>m;return m;}
string i_t_s(int  n){ostringstream str1;str1<<n;string geek = str1.str(); return geek;}
int  modn( int  a, int  b){return  (a % b + b) % b;}
int  power(int  b, int  p){if(p==0)return 1;else if(p%2==0)return (power(b,p/2)*(power(b,p/2)));else return ((b)*(power(b,p-1)));}
bool isprime( int  n){if(n==1)return false;if(n==2 or n==3)return true; int  lim=sqrt(n); int  i=2;while(i<=lim){if(n%i==0)return false; i++;}return true;}
vector< int > factors( int  n){vector< int > a;a.pb(1); int  lim=sqrt(n),i=2;while(i<=lim){if(n%i==0){a.pb(i);if(i!=n/i)a.pb(n/i);}i++;}if(n!=1)a.pb(n);sort(a.begin(),a.end());return a;}
vector<pair<int ,int > > pf_with_count(int  b){ pair<int ,int > p;vector<pair<int ,int > >pf;int  count=0;while(b%2==0){b/=2;count++;}if(count>0){p.f=2;p.s=count;pf.pb(p);}int  i=3;while(i<=sqrt(b)){count=0;while(b%i==0){b/=i;count++;}if(count>0){p.f=i;p.s=count;pf.pb(p);}i+=2;}if(b>2) {p.f=b;p.s=1;pf.pb(p);}return pf;}
int  bs_t_i(string s){int  l=strtoull(s.c_str(),NULL,2);return l;}
string  i_to_bs(int  n){string s = bitset< 15 >( n ).to_string();return s;}
bool check(int arr[])
{
	int i=0;
	set<int> st;
	while(i<10)
	{
		if(arr[i]>0)st.insert(arr[i]);
		i++;
	}
	if(st.size()!=1)return false;
	return true;

}
int mce(int arr[])
{
    int i=0;
	while(i<10)
	{
        if(arr[i]>0)
        	{
        		arr[i]--;
        		if(check(arr))return 1;
        		else arr[i]++;
        	}
        i++;
	}
	return 0;
}

signed main()
{
	ios
	int n;
	cin>>n;
	int a[n];
	int i=0;
	int arr[10];
	fill(arr,arr+10,0);
	while(i<n)
	{
       cin>>a[i];
       arr[a[i]-1]++;
       i++;
	}
	if(n==1)
	{
		cout<<1;
		return 0;
	}
	i=n-1;
	while(i>=0)
	{
       if(mce(arr))
       {
       	cout<<i+1;
       	return 0;
       }
       arr[a[i]-1]--;

       i--;
	}
}


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

#define int long long

int32_t main ()
{
	ios_base::sync_with_stdio (0);

	int n; cin >> n;
	vector <int> v (n);
	vector <int> c (11, 0);
	int ans = 0;

	if (n == 1)
	{
		cout << "1\n";
		return 0;
	}

	for (int k = 0; k < n; ++ k)
	{
		cin >> v[k];
		++ c[v[k]];

		map <int, int> mp;
		for (int i = 1; i <= 10; ++ i) if (c[i])
			mp[c[i]] ++;

		int ok = 0;

		/*
		cout << mp.size () << endl;
		for (auto i: mp)
			cout << i.first << " " << i.second << endl;
		cout << endl;
		*/

		if (mp.size () == 1 && mp.begin ()->first == 1) ok = 1;
		else if (mp.size () == 2 && ((mp.begin()->first==1&&mp.begin()->second==1)||(mp.begin()->first+1==mp.rbegin()->first && mp.rbegin ()->second==1))) ok = 1;

		if (ok)
		{
			ans = k+1;
		}
	}

	cout << ans << endl;

	return 0;
}


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

#define int long long

int32_t main ()
{
	ios_base::sync_with_stdio (0);

	int n; cin >> n;
	vector <int> v (n);
	vector <int> c (11, 0);
	int ans = 0;

	for (int k = 0; k < n; ++ k)
	{
		cin >> v[k];
		++ c[v[k]];

		map <int, int> mp;
		for (int i = 1; i <= 10; ++ i) if (c[i])
			mp[c[i]] ++;

		int ok = 0;

		/*
		cout << mp.size () << endl;
		for (auto i: mp)
			cout << i.first << " " << i.second << endl;
		cout << endl;
		*/

		if (mp.size () == 1 && (mp.begin ()->second == 1 || mp.begin ()->first == 1)) ok = 1;
		if (mp.size () == 2 && ((mp.begin()->first==1&&mp.begin()->second==1)||(mp.begin()->first+1==mp.rbegin()->first && mp.rbegin ()->second==1))) ok = 1;

		if (ok)
		{
			ans = k+1;
		}
	}

	cout << ans << endl;

	return 0;
}


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

#define int long long

int32_t main ()
{
	ios_base::sync_with_stdio (0);

	int n; cin >> n;
	vector <int> v (n);
	vector <int> c (11, 0);
	int ans = 0;

	for (int k = 0; k < n; ++ k)
	{
		cin >> v[k];
		++ c[v[k]];

		map <int, int> mp;
		for (int i = 1; i <= 10; ++ i) if (c[i])
			mp[c[i]] ++;

		int ok = 0;

		/*
		cout << mp.size () << endl;
		for (auto i: mp)
			cout << i.first << " " << i.second << endl;
		cout << endl;
		*/

		if (mp.size () == 1 && mp.begin ()->second == 1) ok = 1;
		if (mp.size () == 2 && ((mp.begin()->first==1&&mp.begin()->second==1)||(mp.begin()->first+1==mp.rbegin()->first && mp.rbegin ()->second==1))) ok = 1;

		if (ok)
		{
			ans = k+1;
		}
	}

	cout << ans << endl;

	return 0;
}


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

#define int long long

int32_t main ()
{
	ios_base::sync_with_stdio (0);

	int n; cin >> n;
	vector <int> v (n);
	vector <int> c (11, 0);
	int ans = 0;

	if (n == 1)
	{
		cout << "1\n";
		return 0;
	}

	for (int k = 0; k < n; ++ k)
	{
		cin >> v[k];
		++ c[v[k]];

		for (int i = 1; i <= 10; ++ i)
		{
			if (c[i])
			{
				-- c[i];
				set <int> s;
				for (int j = 1; j <= 10; ++ j)
					if (c[j]) s.insert (c[j]);

				if (s.size () == 1)
				{
					ans = k+1;
				}
				++ c[i];
			}
		}
	}

	cout << ans << endl;

	return 0;
}


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

#define int long long

int32_t main ()
{
	ios_base::sync_with_stdio (0);

	int n; cin >> n;
	vector <int> v (n);
	vector <int> c (11, 0);
	int ans = 0;

	if (n == 1)
	{
		cout << "1\n";
		return 0;
	}

	for (int k = 0; k < n; ++ k)
	{
		cin >> v[k];
		++ c[v[k]];

		map <int, int> mp;
		for (int i = 1; i <= 10; ++ i)
		{
			if (c[i])
			{
				mp[c[i]] ++;
			}
		}
		int ok = 0;

	/*	cout << mp.size () << endl;
		for (auto i: mp)
			cout << i.first << " " << i.second << endl;
		cout << endl;
	*/
		if (mp.size () == 1)
			ok = 1;
		else if (mp.size () == 2)
		{
			if ((mp.begin ()->first == 1 && mp.begin ()->second == 1) || mp.begin ()->first+1 == mp.rbegin ()->first)
			{
				ok = 1;
			}
		}

		if (ok)
		{
			ans = k+1;
		}
	}

	cout << ans << endl;

	return 0;
}


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

#define int long long

int32_t main ()
{
	ios_base::sync_with_stdio (0);

	int n; cin >> n;
	vector <int> v (n);
	vector <int> c (11, 0);
	int ans = 0;

	for (int k = 0; k < n; ++ k)
	{
		cin >> v[k];
		++ c[v[k]];

		map <int, int> mp;
		for (int i = 1; i <= 10; ++ i) if (c[i])
			mp[c[i]] ++;

		int ok = 0;

		/*
		cout << mp.size () << endl;
		for (auto i: mp)
			cout << i.first << " " << i.second << endl;
		cout << endl;
		*/

		if (mp.size () == 1 && mp.begin ()->first == 1) ok = 1;
		else if (mp.size () == 2 && ((mp.begin()->first==1&&mp.begin()->second==1)||(mp.begin()->first+1==mp.rbegin()->first && mp.rbegin ()->second==1))) ok = 1;

		if (ok)
		{
			ans = k+1;
		}
	}

	cout << ans << endl;

	return 0;
}


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

#define int long long

int32_t main ()
{
	ios_base::sync_with_stdio (0);

	int n; cin >> n;
	vector <int> v (n);
	vector <int> c (11, 0);
	int ans = 0;

	if (n == 1)
	{
		cout << "1\n";
		return 0;
	}

	for (int k = 0; k < n; ++ k)
	{
		cin >> v[k];
		++ c[v[k]];

		map <int, int> mp;
		for (int i = 1; i <= 10; ++ i)
		{
			if (c[i])
			{
				mp[c[i]] ++;
			}
		}
		int ok = 0;

		if (mp.size () == 1 && mp.begin ()->first == 1)
			ok = 1;
		else if (mp.size () == 2)
		{
			if ((mp.begin ()->first == 1 && mp.begin ()->second == 1) || (mp.begin ()->first+1 == mp.rbegin ()->first && mp.rbegin ()->second == 1))
			{
				ok = 1;
			}
		}

		if (ok)
		{
			ans = k+1;
		}
	}

	cout << ans << endl;

	return 0;
}


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

#define int long long

int32_t main ()
{
	ios_base::sync_with_stdio (0);

	int n; cin >> n;
	vector <int> v (n);
	vector <int> c (11, 0);
	int ans = 0;

	if (n == 1)
	{
		cout << "1\n";
		return 0;
	}

	for (int k = 0; k < n; ++ k)
	{
		cin >> v[k];
		++ c[v[k]];

		map <int, int> mp;
		for (int i = 1; i <= 10; ++ i)
		{
			if (c[i])
			{
				mp[c[i]] ++;
			}
		}
		int ok = 0;
/*
		cout << mp.size () << endl;
		for (auto i: mp)
			cout << i.first << " " << i.second << endl;
		cout << endl;
		*/
		if (mp.size () == 1 && mp.begin ()->first == 1)
			ok = 1;
		else if (mp.size () == 2)
		{
			if ((mp.begin ()->first == 1 && mp.begin ()->second == 1) || mp.begin ()->first+1 == mp.rbegin ()->first)
			{
				ok = 1;
			}
		}

		if (ok)
		{
			ans = k+1;
		}
	}

	cout << ans << endl;

	return 0;
}


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

#define int long long

int32_t main ()
{
	ios_base::sync_with_stdio (0);

	int n; cin >> n;
	vector <int> v (n);
	vector <int> c (11, 0);
	int ans = 0;

	for (int k = 0; k < n; ++ k)
	{
		cin >> v[k];
		++ c[v[k]];

		for (int i = 1; i <= 10; ++ i)
		{
			if (c[i])
			{
				-- c[i];
				set <int> s;
				for (int j = 1; j <= 10; ++ j)
					if (c[j]) s.insert (c[j]);

				if (s.size () == 1)
				{
					ans = k+1;
				}
				++ c[i];
			}
		}
	}

	cout << ans << endl;

	return 0;
}


#define TEMPLATE_VERSION "v0.2 06/03/2019"
#include <bits/stdc++.h>

#define MOD (1000000007LL)
#define INF (INT_MAX)
#define LINF (LLONG_MAX)
#define TAM (1000000)
#define LL long long int
#define ULL unsigned long long int
#define LD long double
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define si set<int>
#define qi queue<int>
#define sti stack<int>
#define vLL vector<LL>
#define pqi priority_queue<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define EPS 1e-9
#define loop( n ) for(int i=0; i<n; i++)
#define LOOP(k,n) for(int i=k; i<n; i++)
#define POOL(k,n) for(int i=k; i>=n; i--)
#define FIND(v, value) find(v.begin(), v.end(), value)
#define SORT( v ) sort(v.begin(), v.end())
#define SORTC(v, comp) sort(v.begin(), v.end(), comp)
#define WW( x ) cout<<#x<<" = "<<(x)<<"\n";
#define endl '\n'

using namespace std;

//compile: g++ -D DEBUG [arquivo.cpp] -o [arquivo] && ./[arquivo]

int main () {
    ios_base::sync_with_stdio(false);
    #ifdef DEBUG
    freopen("input.txt", "r+", stdin);
    freopen("output.txt", "w+", stdout);
    #endif
    int n, k =1;
    int mini = INF, maxi = 0;
    cin >> n;
    vi vetor(n);
    vi count(11, 0);
    for(int i=0; i<n; i++) cin >> vetor[i];
    for(int i=0; i<n; i++){
        count[vetor[i]]++;
        si nums;
        int mini = INF;
        int maxi = 1;
        for(int j=1; j<=10; j++){
            if(count[j]!=0){
                mini = min(mini, count[j]);
                maxi = max(maxi, count[j]);
                nums.insert(count[j]);
            }
        }
        if(mini==maxi and mini==1) k = i+1;
        else if(mini==maxi-1 and nums.size()==2){
            int cont = 0;
            for(int j=1; j<=10; j++){
                if(count[j]==maxi) cont++;
            }
            if(cont==1)
                k = i+1;
        }
        else if(mini==1 and nums.size()==2){
            int cont = 0;
            for(int j=1; j<=10; j++){
                if(count[j]==mini) cont++;
            }
            if(cont==1) k = i+1;
        }
    }
    cout << k << endl;
    return 0;
}


#include <bits/stdc++.h>

#define TAM int(1e5) + 1
using namespace std;

int n;
vector<int> arr;
int main () {
    ios_base::sync_with_stdio(false);
    cin >> n;

    vector<int> counter(TAM, 0), counter_counter(TAM, 0);

    arr.resize(n);

    for(int i=0; i<n; i++) cin >> arr[i];

    int ans = 1, mini, maxi;
    set<int> numbers;

    for(int i=0; i<n; i++){
        counter[arr[i]]++;

        maxi = 0;
        mini = INT_MAX;

        int last_counter = counter[arr[i]]-1;
        int cur_counter = counter[arr[i]];

        counter_counter[last_counter]--;
        counter_counter[cur_counter]++;

        if(last_counter>0 and counter_counter[last_counter]==0)
            numbers.erase(last_counter);
        if(counter_counter[cur_counter]==1)
            numbers.insert(cur_counter);

        int c = 0;

        for(auto i : numbers){
        //    cout << i << " ";
            mini = min(mini, i);
            maxi = max(maxi, i);
        }

        //cout << maxi << " " << counter_counter[maxi] << endl;
        //cout << mini << " " << counter_counter[mini] << endl;
        //cout << "numbers " << numbers.size() << endl;

        if(numbers.size()==1 and counter_counter[mini] == i+1)
            ans = max(i+1, ans);
        if(numbers.size()==2 and maxi-mini<=1){
            if(counter_counter[maxi]==1)
                ans = max(i+1, ans);
            if(counter_counter[maxi]==i+1 or counter_counter[mini]==i+1)
                ans = max(i+1, ans);
        }

        if(numbers.size()==2 and mini==1 and counter_counter[mini]==1)
            ans = max(i+1, ans);

    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

#define TAM int(1e5) + 1
using namespace std;

int n;
vector<int> arr;
int main () {
    ios_base::sync_with_stdio(false);
    cin >> n;

    vector<int> counter(TAM, 0), counter_counter(TAM, 0);

    arr.resize(n);

    for(int i=0; i<n; i++) cin >> arr[i];

    int ans = 1, mini, maxi;
    set<int> numbers;

    for(int i=0; i<n; i++){
        counter[arr[i]]++;

        maxi = 0;
        mini = INT_MAX;

        int last_counter = counter[arr[i]]-1;
        int cur_counter = counter[arr[i]];

        counter_counter[last_counter]--;
        counter_counter[cur_counter]++;

        if(last_counter>0 and counter_counter[last_counter]==0)
            numbers.erase(last_counter);
        if(counter_counter[cur_counter]==1)
            numbers.insert(cur_counter);

        int c = 0;

        for(auto i : numbers){
        //    cout << i << " ";
            mini = min(mini, i);
            maxi = max(maxi, i);
        }

        //cout << maxi << " " << counter_counter[maxi] << endl;
        //cout << mini << " " << counter_counter[mini] << endl;
        //cout << "numbers " << numbers.size() << endl;

        if(numbers.size()==1 and mini == 1)
            ans = max(i+1, ans);
        if(numbers.size()==2 and maxi-mini<=1){
            if(counter_counter[maxi]==1 or counter_counter[mini]==1)
                ans = max(i+1, ans);
            if(counter_counter[maxi]==i+1 or counter_counter[mini]==i+1)
                ans = max(i+1, ans);
        }

        if(numbers.size()==2 and mini==1){
            if(counter_counter[mini]==1)
                ans = max(i+1, ans);
        }
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

#define TAM int(1e5) + 1
using namespace std;

int n;
vector<int> arr;
int main () {
    ios_base::sync_with_stdio(false);
    cin >> n;

    vector<int> counter(TAM, 0), counter_counter(TAM, 0);

    arr.resize(n);

    for(int i=0; i<n; i++) cin >> arr[i];

    int ans = 1, mini, maxi;
    set<int> numbers;

    for(int i=0; i<n; i++){
        counter[arr[i]]++;

        maxi = 0;
        mini = INT_MAX;

        int last_counter = counter[arr[i]]-1;
        int cur_counter = counter[arr[i]];

        counter_counter[last_counter]--;
        counter_counter[cur_counter]++;

        if(last_counter>0 and counter_counter[last_counter]==0)
            numbers.erase(last_counter);
        if(counter_counter[cur_counter]==1)
            numbers.insert(cur_counter);

        int c = 0;

        for(auto i : numbers){
        //    cout << i << " ";
            mini = min(mini, i);
            maxi = max(maxi, i);
        }

        //cout << maxi << " " << counter_counter[maxi] << endl;
        //cout << mini << " " << counter_counter[mini] << endl;
        //cout << "numbers " << numbers.size() << endl;

        if(numbers.size()==1 and mini == 1)
            ans = max(i+1, ans);
        if(numbers.size()==2 and maxi-mini<=1){
            if(counter_counter[maxi]==1)
                ans = max(i+1, ans);
            if(counter_counter[maxi]==i+1 or counter_counter[mini]==i+1)
                ans = max(i+1, ans);
        }

        if(numbers.size()==2 and mini==1 and counter_counter[mini]==1)
            ans = max(i+1, ans);

    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {
	int n;
	cin >> n;
	vector<int> vetor(n), count(10, 0);
	for(int i=0; i<n; i++) cin >> vetor[i];
	int maxi = 0;

	for(int i=0; i<n; i++){
		count[vetor[i]-1]++;
		for(int k=0; k<10; k++){
			if(count[k]){
				count[k]--;
				set<int> vals;
				for(int j=0; j<10; j++)
					if(count[j]!=0)
						vals.insert(count[j]);
				if(vals.size()==1)
					maxi = max(i+1, maxi);
				count[k]++;
			}
		}
		//for(int i=0; i<10; i++) cout << count[i] << " ";
		//cout << endl;
	}

	cout << max(maxi, 1) << endl;

	return 0;
}


#include <bits/stdc++.h>

#define TAM int(1e5) + 1
using namespace std;

int n;
vector<int> arr;
int main () {
    ios_base::sync_with_stdio(false);
    cin >> n;

    vector<int> counter(TAM, 0), counter_counter(TAM, 0);

    arr.resize(n);

    for(int i=0; i<n; i++) cin >> arr[i];

    int ans = 1, mini, maxi;
    set<int> numbers;

    for(int i=0; i<n; i++){
        counter[arr[i]]++;

        maxi = 0;
        mini = INT_MAX;

        int last_counter = counter[arr[i]]-1;
        int cur_counter = counter[arr[i]];

        counter_counter[last_counter]--;
        counter_counter[cur_counter]++;

        if(last_counter>0 and counter_counter[last_counter]==0)
            numbers.erase(last_counter);
        if(counter_counter[cur_counter]==1)
            numbers.insert(cur_counter);

        int c = 0;

        for(auto i : numbers){
        //    cout << i << " ";
            mini = min(mini, i);
            maxi = max(maxi, i);
        }

        //cout << maxi << " " << counter_counter[maxi] << endl;
        //cout << mini << " " << counter_counter[mini] << endl;
        //cout << "numbers " << numbers.size() << endl;

        if(numbers.size()==1 and (mini == 1 or mini == i+1))
            ans = max(i+1, ans);
        if(numbers.size()==2 and maxi-mini<=1){
            if(counter_counter[maxi]==1)
                ans = max(i+1, ans);
            if(counter_counter[maxi]==i+1 or counter_counter[mini]==i+1)
                ans = max(i+1, ans);
        }

        if(numbers.size()==2 and mini==1 and counter_counter[mini]==1)
            ans = max(i+1, ans);

    }

    cout << ans << endl;

    return 0;
}


#define TEMPLATE_VERSION "v0.2 06/03/2019"
#include <bits/stdc++.h>

#define MOD (1000000007LL)
#define INF (INT_MAX)
#define LINF (LLONG_MAX)
#define TAM (1000000)
#define LL long long int
#define ULL unsigned long long int
#define LD long double
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define si set<int>
#define qi queue<int>
#define sti stack<int>
#define vLL vector<LL>
#define pqi priority_queue<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define EPS 1e-9
#define loop( n ) for(int i=0; i<n; i++)
#define LOOP(k,n) for(int i=k; i<n; i++)
#define POOL(k,n) for(int i=k; i>=n; i--)
#define FIND(v, value) find(v.begin(), v.end(), value)
#define SORT( v ) sort(v.begin(), v.end())
#define SORTC(v, comp) sort(v.begin(), v.end(), comp)
#define WW( x ) cout<<#x<<" = "<<(x)<<"\n";
#define endl '\n'

using namespace std;

//compile: g++ -D DEBUG [arquivo.cpp] -o [arquivo] && ./[arquivo]

int main () {
    ios_base::sync_with_stdio(false);
    #ifdef DEBUG
    freopen("input.txt", "r+", stdin);
    freopen("output.txt", "w+", stdout);
    #endif
    int n, k =1;
    int mini = INF, maxi = 0;
    cin >> n;
    vi vetor(n);
    vi count(11, 0);
    for(int i=0; i<n; i++) cin >> vetor[i];
    for(int i=0; i<n; i++){
        count[vetor[i]]++;
        si nums;
        int mini = INF;
        int maxi = 1;
        for(int j=1; j<=10; j++){
            if(count[j]!=0){
                mini = min(mini, count[j]);
                maxi = max(maxi, count[j]);
                nums.insert(count[j]);
            }
        }
        if(mini==maxi and mini==1 and nums.size()==2) k = i+1;
        else if(mini==maxi-1 and nums.size()==2){
            int cont = 0;
            for(int j=1; j<=10; j++){
                if(count[j]==maxi) cont++;
            }
            if(cont==1)
                k = i+1;
        }
        else if(mini==1 and nums.size()==2){
            int cont = 0;
            for(int j=1; j<=10; j++){
                if(count[j]==mini) cont++;
            }
            if(cont==1) k = i+1;
        }
    }
    cout << k << endl;
    return 0;
}


#define TEMPLATE_VERSION "v0.2 06/03/2019"
#include <bits/stdc++.h>

#define MOD (1000000007LL)
#define INF (INT_MAX)
#define LINF (LLONG_MAX)
#define TAM (1000000)
#define LL long long int
#define ULL unsigned long long int
#define LD long double
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define si set<int>
#define qi queue<int>
#define sti stack<int>
#define vLL vector<LL>
#define pqi priority_queue<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define EPS 1e-9
#define loop( n ) for(int i=0; i<n; i++)
#define LOOP(k,n) for(int i=k; i<n; i++)
#define POOL(k,n) for(int i=k; i>=n; i--)
#define FIND(v, value) find(v.begin(), v.end(), value)
#define SORT( v ) sort(v.begin(), v.end())
#define SORTC(v, comp) sort(v.begin(), v.end(), comp)
#define WW( x ) cout<<#x<<" = "<<(x)<<"\n";
#define endl '\n'

using namespace std;

//compile: g++ -D DEBUG [arquivo.cpp] -o [arquivo] && ./[arquivo]

int main () {
    ios_base::sync_with_stdio(false);
    #ifdef DEBUG
    freopen("input.txt", "r+", stdin);
    freopen("output.txt", "w+", stdout);
    #endif
    int n, k =1;
    int mini = INF, maxi = 0;
    cin >> n;
    vi vetor(n);
    vi count(11, 0);
    for(int i=0; i<n; i++) cin >> vetor[i];
    for(int i=0; i<n; i++){
        count[vetor[i]]++;
        si nums;
        int mini = INF;
        int maxi = 1;
        for(int j=1; j<=10; j++){
            if(count[j]!=0){
                mini = min(mini, count[j]);
                maxi = max(maxi, count[j]);
            }
        }
        if(mini==maxi and mini==1) k = i+1;
        else if(mini==maxi-1){
            int cont = 0;
            for(int j=1; j<=10; j++){
                if(count[j]==maxi) cont++;
            }
            if(cont==1)
                k = i+1;
        }
        else if(mini==1){
            int cont = 0;
            for(int j=1; j<=10; j++){
                if(count[j]==mini) cont++;
            }
            if(cont==1) k = i+1;
        }
    }
    cout << k << endl;
    return 0;
}


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

const int N = 1e5 + 10;

int n, color, ans, mx, f[N], cnt[N];

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
    {
        scanf("%d", &color);
        cnt[f[color]]--;
        f[color]++;
        cnt[f[color]]++;
        mx = max(mx, f[color]);
        bool ok = false;
        if (cnt[1] == i) // every color has occurence of 1
            ok = true;
        else if (cnt[i] == 1) // only one color has the maximum occurence and the occurence is i
            ok = true;
        else if (cnt[1] == 1 && cnt[mx] * mx == i - 1) // one color has occurence of 1 and other colors have the same occurence
            ok = true;
        else if (cnt[mx - 1] * (mx - 1) == i - mx && cnt[mx] == 1) // one color has the occurence 1 more than any other color
            ok = true;
        if (ok)
            ans = i;
    }
    printf("%d", ans);
	return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include <cstring>
#include <unordered_set>
#include <functional>

using namespace std;
typedef long long ll;
const int MAXN = 100000 + 5;
const int MAXE = 2 * MAXN;
const int NIL = -1;
const int INF = 0x3f3f3f3f;

int a[MAXN];
int cnt[15];
inline bool check()
{
	map<int, int> mp;
	for (int i = 1; i <= 10; ++i) {
		if (cnt[i])
			++mp[cnt[i]];
		if (mp.size() > 2)
			return false;
	}

	if (mp.size() == 1) {
		if (mp.begin()->second == 1 || mp.begin()->first == 1)
			return true;
		return false;
	}

	vector<pair<int, int> > v;

	for (auto i = mp.begin(); i != mp.end(); ++i)
		v.push_back(*i);

	if (v[0].second != 1 && v[1].second != 1)
		return false;

	if (v[0].second == 1 && v[1].second == 1) {
		return abs(v[0].first - v[1].first) == 1 || v[0].first == 1 || v[1].first == 1;
	}
	if (v[0].second == 1) {
		if (v[0].first == v[1].first + 1)
			return true;
		if (v[0].first == 1)
			return true;
	}
	if (v[1].first == v[0].first + 1)
		return true;
	if (v[1].first == 1)
		return true;

	return false;
}
int main()
{
#ifdef DEBUG
	freopen(R"(D:\VisualStudioProject\algor\algor\in.txt)", "r", stdin);
	freopen(R"(D:\VisualStudioProject\algor\algor\out.txt)", "w", stdout);
#endif // DEBUG

	int n;
	while (scanf("%d", &n) != EOF) {

		memset(cnt, 0, sizeof(cnt));
		for (int i = 0; i < n; ++i) {
			scanf("%d", &a[i]);
			++cnt[a[i]];
		}

		for (int i = n - 1; i >= 0; --i) {

			if (check()) {
				printf("%d\n", i + 1);
				break;
			}
			--cnt[a[i]];
		}
	}

	return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include <cstring>
#include <unordered_set>
#include <functional>

using namespace std;
typedef long long ll;
const int MAXN = 100000 + 5;
const int MAXE = 2 * MAXN;
const int NIL = -1;
const int INF = 0x3f3f3f3f;

int a[MAXN];
int cnt[15];
inline bool check()
{
	map<int, int> mp;
	for (int i = 1; i <= 10; ++i) {
		if (cnt[i])
			++mp[cnt[i]];
		if (mp.size() > 2)
			return false;
	}

	if (mp.size() == 1) {
		if (mp.begin()->first == 1)
			return true;
		return false;
	}

	auto i2 = mp.begin();
	++i2;
	auto i1 = mp.begin();
	if (i1->second != 1 && i2->second != 1)
		return false;

	if (i1->second == 1 && i2->second == 1) {
		return abs(i1->first - i2->first) == 1;
	}
	if (i1->second == 1) {
		if (i1->first == i2->first + 1)
			return true;
		if (i1->first == 1)
			return true;
	}
	if (i2->first == i1->first + 1)
		return true;
	if (i2->first == 1)
		return true;

	return false;
}
int main()
{
#ifdef DEBUG
	freopen(R"(D:\VisualStudioProject\algor\algor\in.txt)", "r", stdin);
	freopen(R"(D:\VisualStudioProject\algor\algor\out.txt)", "w", stdout);
#endif // DEBUG

	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		++cnt[a[i]];
	}

	for (int i = n - 1; i >= 0; --i) {

		if (check()) {
			printf("%d\n", i + 1);
			return 0;
		}
		--cnt[a[i]];
	}

	return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include <cstring>
#include <unordered_set>
#include <functional>

using namespace std;
typedef long long ll;
const int MAXN = 100000 + 5;
const int MAXE = 2 * MAXN;
const int NIL = -1;
const int INF = 0x3f3f3f3f;

int a[MAXN];
int cnt[15];
inline bool check()
{
	map<int, int> mp;
	for (int i = 1; i <= 10; ++i) {
		if (cnt[i])
			++mp[cnt[i]];
		if (mp.size() > 2)
			return false;
	}

	if (mp.size() == 1) {
		if (mp.begin()->second == 1 || mp.begin()->first == 1)
			return true;
		return false;
	}

	auto i2 = mp.begin();
	++i2;
	auto i1 = mp.begin();
	if (i1->second != 1 && i2->second != 1)
		return false;

	if (i1->second == 1 && i2->second == 1) {
		return abs(i1->first - i2->first) == 1;
	}
	if (i1->second == 1) {
		if (i1->first == i2->first + 1)
			return true;
		if (i1->first == 1)
			return true;
	}
	if (i2->first == i1->first + 1)
		return true;
	if (i2->first == 1)
		return true;

	return false;
}
int main()
{
#ifdef DEBUG
	freopen(R"(D:\VisualStudioProject\algor\algor\in.txt)", "r", stdin);
	freopen(R"(D:\VisualStudioProject\algor\algor\out.txt)", "w", stdout);
#endif // DEBUG

	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		++cnt[a[i]];
	}

	for (int i = n - 1; i >= 0; --i) {

		if (check()) {
			printf("%d\n", i + 1);
			return 0;
		}
		--cnt[a[i]];
	}

	return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include <cstring>
#include <unordered_set>
#include <functional>

using namespace std;
typedef long long ll;
const int MAXN = 100000 + 5;
const int MAXE = 2 * MAXN;
const int NIL = -1;
const int INF = 0x3f3f3f3f;

int a[MAXN];
int cnt[15];
int sub;
inline bool check()
{
	sub = 0;
	map<int, int> mp;
	for (int i = 1; i <= 10; ++i) {
		if (cnt[i])
			++mp[cnt[i]];
		if (mp.size() > 2)
			return false;
	}
	if (mp.size() <= 1)
		return true;

	int i = 0;
	int tcnt[15];
	for (auto iter = mp.begin(); iter != mp.end(); ++iter) {
		tcnt[i] = iter->first;
		++i;
	}

	if (abs(tcnt[0] - tcnt[1]) <= 1) {
		sub = 1;
		return true;
	}

	return false;
}
int main()
{
#ifdef DEBUG
	freopen(R"(D:\VisualStudioProject\algor\algor\in.txt)", "r", stdin);
	freopen(R"(D:\VisualStudioProject\algor\algor\out.txt)", "w", stdout);
#endif // DEBUG

	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		++cnt[a[i]];
	}

	for (int i = n - 1; i >= 0; --i) {

		--cnt[a[i]];
		if (check()) {
			printf("%d\n", i + 1 - sub);
			return 0;
		}
	}

	return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include <cstring>
#include <unordered_set>
#include <functional>

using namespace std;
typedef long long ll;
const int MAXN = 100000 + 5;
const int MAXE = 2 * MAXN;
const int NIL = -1;
const int INF = 0x3f3f3f3f;

int a[MAXN];
int cnt[15];
inline bool check()
{
	map<int, int> mp;
	for (int i = 1; i <= 10; ++i) {
		if (cnt[i])
			++mp[cnt[i]];
		if (mp.size() > 2)
			return false;
	}

	if (mp.size() == 1) {
		if (mp.begin()->second == 1)
			return true;
		return false;
	}

	auto i2 = mp.begin();
	++i2;
	auto i1 = mp.begin();
	if (i1->second != 1 && i2->second != 1)
		return false;

	if (i1->second == 1 && i2->second == 1) {
		return abs(i1->first - i2->first) == 1;
	}
	if (i1->second == 1) {
		if (i1->first == i2->first + 1)
			return true;
		if (i1->first == 1)
			return true;
	}
	if (i2->first == i1->first + 1)
		return true;
	if (i2->first == 1)
		return true;

	return false;
}
int main()
{
#ifdef DEBUG
	freopen(R"(D:\VisualStudioProject\algor\algor\in.txt)", "r", stdin);
	freopen(R"(D:\VisualStudioProject\algor\algor\out.txt)", "w", stdout);
#endif // DEBUG

	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		++cnt[a[i]];
	}

	for (int i = n - 1; i >= 0; --i) {

		if (check()) {
			printf("%d\n", i + 1);
			return 0;
		}
		--cnt[a[i]];
	}

	return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include <cstring>
#include <unordered_set>
#include <functional>

using namespace std;
typedef long long ll;
const int MAXN = 100000 + 5;
const int MAXE = 2 * MAXN;
const int NIL = -1;
const int INF = 0x3f3f3f3f;

int a[MAXN];
int cnt[15];
inline bool check()
{
	map<int, int> mp;
	for (int i = 1; i <= 10; ++i) {
		if (cnt[i])
			++mp[cnt[i]];
		if (mp.size() > 2)
			return false;
	}

	if (mp.size() == 1) {
		if (mp.begin()->second == 1 || mp.begin()->first == 1)
			return true;
		return false;
	}

	auto i2 = mp.begin();
	++i2;
	auto i1 = mp.begin();
	if (i1->second != 1 && i2->second != 1)
		return false;

	if (i1->second == 1 && i2->second == 1) {
		return abs(i1->first - i2->first) == 1 || i1->first == 1 || i2->first == 1;
	}
	if (i1->second == 1) {
		if (i1->first == i2->first + 1)
			return true;
		if (i1->first == 1)
			return true;
	}
	if (i2->first == i1->first + 1)
		return true;
	if (i2->first == 1)
		return true;

	return false;
}
int main()
{
#ifdef DEBUG
	freopen(R"(D:\VisualStudioProject\algor\algor\in.txt)", "r", stdin);
	freopen(R"(D:\VisualStudioProject\algor\algor\out.txt)", "w", stdout);
#endif // DEBUG

	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		++cnt[a[i]];
	}

	for (int i = n - 1; i >= 0; --i) {

		if (check()) {
			printf("%d\n", i + 1);
			return 0;
		}
		--cnt[a[i]];
	}

	return 0;
}


#include <iostream>
#include <map>
using namespace std;

int N;
map<int,int> m;

int main(){
    cin >> N;
    int ans = 1;
    int u;
    int ma = 0,mi = 0;
    int c_ma = 0,c_mi = 1;
    for(int i=1;i<=N;i++){
        cin >> u;
        m[u]++;
        ma = 0;
        c_ma = 0;
        for(auto x:m) ma = max(ma,x.second);
        for(auto x:m) if(ma==x.second) c_ma++;
        mi = 1e9;
        c_mi = 0;
        for(auto x:m) mi = min(mi,x.second);
        for(auto x:m) if(mi==x.second) c_mi++;
        if(i!=N && mi==ma) ans = max(ans,i+1);
        if(mi+1==ma && (c_ma==1 || c_mi==1)) ans = max(ans,i);
        if(mi==1 && c_ma*ma==i-1) ans = max(ans,i);
    }
    cout << ans << endl;
}


#include <iostream>
#include <map>
using namespace std;

int N;
map<int,int> m;

int main(){
    cin >> N;
    int ans = 1;
    int u;
    int ma = 0,mi = 0;
    int c_ma = 0,c_mi = 1;
    for(int i=1;i<=N;i++){
        cin >> u;
        if(i==N) break;
        m[u]++;
        ma = 0;
        c_ma = 0;
        for(auto x:m) ma = max(ma,x.second);
        for(auto x:m) if(ma==x.second) c_ma++;
        mi = 1e9;
        c_mi = 0;
        for(auto x:m) mi = min(mi,x.second);
        for(auto x:m) if(mi==x.second) c_mi++;
        if(mi==ma) ans = max(ans,i+1);
        if(mi+1==ma && (c_ma==1 || c_mi==1)) ans = max(ans,i);
    }
    cout << ans << endl;
}


#include <iostream>
#include <map>
using namespace std;

int N;
map<int,int> m;

int main(){
    cin >> N;
    int ans = 1;
    int u;
    int ma = 0,mi = 0;
    int c_ma = 0,c_mi = 1;
    for(int i=1;i<=N;i++){
        cin >> u;
        m[u]++;
        ma = 0;
        c_ma = 0;
        for(auto x:m) ma = max(ma,x.second);
        for(auto x:m) if(ma==x.second) c_ma++;
        mi = 1e9;
        c_mi = 0;
        for(auto x:m) mi = min(mi,x.second);
        for(auto x:m) if(mi==x.second) c_mi++;
        if(i!=N && mi==ma) ans = max(ans,i+1);
        if(mi+1==ma && (c_ma==1 || c_mi==1)) ans = max(ans,i);
    }
    cout << ans << endl;
}


#include <iostream>
#include <map>
using namespace std;

int N;
map<int,int> m;

int main(){
    cin >> N;
    int ans = 1;
    int u;
    int ma = 0,mi = 0;
    int c_ma = 0,c_mi = 1;
    for(int i=1;i<=N;i++){
        cin >> u;
        if(i==N) break;
        m[u]++;
        ma = 0;
        c_ma = 0;
        for(auto x:m) ma = max(ma,x.second);
        for(auto x:m) if(ma==x.second) c_ma++;
        mi = 1e9;
        c_mi = 0;
        for(auto x:m) mi = min(mi,x.second);
        for(auto x:m) if(mi==x.second) c_mi++;
/*        if(ma<m[u]){
            ma = m[u];
            c_ma = 1;
        }else if(ma==m[u]) c_ma++;
        else if(mi==m[u]-1){
            c_mi--;
            if(c_mi==0){
                mi = 1e9;
                for(auto x:m) mi = min(mi,x.second);
                for(auto x:m) if(mi==x.second) c_mi++;
            }
        }
*/        if(mi==ma) ans = max(ans,i+1);
        if(mi+1==ma && c_ma==1) ans = max(ans,i);
//        cerr << mi << " " << ma << endl;
    }
    cout << ans << endl;
}


#include <iostream>
#include <map>
using namespace std;

int N;
int sum[100010][11] = {};

int main(){
    cin >> N;
    int ans = 1;
    int u;
    map<int,int> m;
    for(int i=1;i<=N;i++){
        cin >> u;
        sum[i][u]++;
        m.clear();
        if(i==N) continue;
        for(int j=1;j<=10;j++){
            sum[i][j] += sum[i-1][j];
            if(sum[i][j]>0) m[sum[i][j]]++;
        }
        if(m.size()>2) continue;
        int c1 = -1,c2 = -1;
        int c3 = -1;
        for(auto x:m){
            if(c1==-1) c1 = x.first;
            else{
                c2 = x.first;
                c3 = x.second;
            }
        }
//        cerr << i << " " << c2 << endl;
        if(c2==-1) ans = max(ans,i+1);
        else if(c1+1==c2 && c3==1) ans = max(ans,i);
    }
    cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <map>
using namespace std;
using ll = long long;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<int> A(N);
    for(int i=0;i<N;i++) cin >> A[i];

    auto add = [](map<int,int>& m,int a){
        m[a]++;
    };

    auto del = [](map<int,int>& m,int a){
        m[a]--;
        if(m[a]<=0) m.erase(a);
    };

    auto check = [](map<int,int>& m){
        if(m.size()>2) return false;
        if(m.size()==1){
            return (m.begin()->first)==1 || (m.begin()->second)==1;
        }
        vec<pair<int,int>> v;
        for(auto& x:m) v.push_back(x);
        if(v[0].first==1 && v[0].second==1) return true;
        if(v[0].first+1!=v[1].first) return false;
        return v[1].second==1;
    };
    map<int,int> cnt,num;
    int ans = 1;
    for(int i=0;i<N;i++){
        del(num,cnt[A[i]]);
        cnt[A[i]]++;
        add(num,cnt[A[i]]);
        if(check(num)) ans = i+1;
    }
    cout << ans << endl;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ_IN freopen("input.txt","r",stdin);
#define FILE_READ_OUT freopen("output.txt","w",stdout);
#define all(a) a.begin(),a.end()
using namespace std;

const ll maxn = 1e5;

int main(){
  int n;
  cin >> n;
  V<int> arr(n);
  loop(i,0,n)cin >> arr[i];
  unordered_map<int,int> m;
  unordered_map<int,int> m1;
  int ans = 0;
  loop(i,1,n+1){
    int prev = m[arr[i-1]]++;
    m1[prev]--;
    if(m1[prev] < 1)m1.erase(prev);
    m1[m[arr[i-1]]]++;
    // printf("m1 = \n");
    // for(auto j: m1){
    //   printf("(%d, %d)\n",j.F,j.S);
    // }
    // cout << m1.size() << " " << i << "\n";
    if(m.size() == 1){
      ans = max(i,ans);
    }
    else if(m1.size() == 2){
      auto it = m1.begin();
      PII a = *it;
      ++it;
      PII b = *it;
      if((abs(a.F - b.F)==1 && (a.S == 1 || b.S == 1) ) || (a.F == 1 && a.S == 1) || (b.F == 1 && b.S == 1)){
        ans = max(ans,i);
      }
    }else if(m1.size() == 1){
      if(m1.begin()->F == 1){
        ans = max(ans,i);
      }
    }
  }
  cout << ans;
  return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ_IN freopen("input.txt","r",stdin);
#define FILE_READ_OUT freopen("output.txt","w",stdout);
#define all(a) a.begin(),a.end()
using namespace std;

const ll maxn = 1e5;

int main(){
  int n;
  cin >> n;
  V<int> arr(n);
  loop(i,0,n)cin >> arr[i];
  unordered_map<int,int> m;
  int ans = 0;
  loop(i,1,n+1){
      m[arr[i-1]]++;
      loop(k,1,11){
          if(m.find(k) == m.end())continue;
          m[k]--;
          if(m[k] == 0)m.erase(k);
          unordered_map<int,int> m1;
          for(auto &j: m){
              m1[j.S]++;
          }
          if(m1.size() <= 1){
              ans = max(ans,i);
          }
          m[k]++;
          if(ans == i)break;
      }
  }
  cout << ans;
  return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ_IN freopen("input.txt","r",stdin);
#define FILE_READ_OUT freopen("output.txt","w",stdout);
#define all(a) a.begin(),a.end()
using namespace std;

const ll maxn = 1e5;

int main(){
  int n;
  cin >> n;
  V<int> arr(n);
  loop(i,0,n)cin >> arr[i];
  unordered_map<int,int> m;
  int ans = 0;
  loop(i,1,n+1){
    m[arr[i-1]]++;
    unordered_map<int,int> m1;
    for(auto &j: m){
      m1[j.S]++;
    }
    if(m1.size() == 2){
      bool p = true;
      PII a,b;
      for(auto j: m1){
        if(p){
          a = mp(j.F,j.S);
          p = false;
        }else{
          b = mp(j.F,j.S);
        }
      }
      if((abs(a.F - b.F) == 1 && (a.S == 1 || b.S == 1)) || (a.F == 1 && a.S == 1) || (b.F == 1 && b.S == 1)){
        ans = max(i,ans);
      }
    }else if(m1.size() == 1){
      if(m1.begin()->F == 1 || m.size() == 1){
        ans = max(ans,i);
      }
    }
  }
  cout << ans;
  return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ_IN freopen("input.txt","r",stdin);
#define FILE_READ_OUT freopen("output.txt","w",stdout);
#define all(a) a.begin(),a.end()
using namespace std;

const ll maxn = 1e5;

int main(){
  int n;
  cin >> n;
  V<int> arr(n);
  loop(i,0,n)cin >> arr[i];
  unordered_map<int,int> m;
  int ans = 0;
  loop(i,1,n+1){
    m[arr[i-1]]++;
    unordered_map<int,int> m1;
    for(auto &j: m){
      m1[j.S]++;
    }
    if(m1.size() == 2){
      bool p = true;
      PII a,b;
      for(auto j: m1){
        if(p){
          a = mp(j.F,j.S);
          p = false;
        }else{
          b = mp(j.F,j.S);
        }
      }
      if((abs(a.F - a.S) == 1 && (a.S == 1 || b.S == 1)) || (a.F == 1 && a.S == 1) || (b.F == 1 && b.S == 1)){
        ans = max(i,ans);
      }
    }else if(m1.size() == 1){
      if(m1.begin()->F == 1){
        ans = max(ans,i);
      }
    }
  }
  cout << ans;
  return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ_IN freopen("input.txt","r",stdin);
#define FILE_READ_OUT freopen("output.txt","w",stdout);
#define all(a) a.begin(),a.end()
using namespace std;

const ll maxn = 1e5;

int main(){
  int n;
  cin >> n;
  V<int> arr(n);
  loop(i,0,n)cin >> arr[i];
  unordered_map<int,int> m;
  unordered_map<int,int> m1;
  int ans = 0;
  loop(i,1,n+1){
    int prev = m[arr[i-1]]++;
    m1[prev]--;
    if(m1[prev] < 1)m1.erase(prev);
    m1[m[arr[i-1]]]++;
    if(m.size() == 1){
      ans = max(i,ans);
    }
    else if(m1.size() == 2){
      auto it = m1.begin();
      PII a = *it;
      ++it;
      PII b = *it;
      if(((a.F - b.F == 1 && a.S == 1) || (b.F - a.F == 1 && b.S == 1) ) || (a.F == 1 && a.S == 1) || (b.F == 1 && b.S == 1)){
        ans = max(ans,i);
      }
    }else if(m1.size() == 1){
      if(m1.begin()->F == 1){
        ans = max(ans,i);
      }
    }
  }
  cout << ans;
  return 0;
}