#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
#include <algorithm>
#include <cmath>

using namespace std;

map <int, int> counts;
int a[100010];

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

    int ans = -1;

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        if (a[x] != 0)
            counts[a[x]]--;

        counts[a[x] + 1]++;

        a[x]++;

        if (counts[a[x] - 1] == 0) {
            counts.erase(counts.find(a[x] - 1));
        }

        if (counts.size() == 1) {
            ans = i;
        }

        if (counts.size() == 2) {
            auto it = counts.begin();
            pair <int, int> left = *it;
            it++;
            pair<int, int> right = *it;

            if (left.first + 1 == right.first && right.second == 1) {
                ans = i;
            }

            if (left.first == 1 && left.second == 1) {
                ans = i;
            }
        }
    }

    cout << ans + 1;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
#include <algorithm>
#include <cmath>

using namespace std;

map <int, int> counts;
int a[100010];

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

    int ans = -1;

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        if (a[x] != 0)
            counts[a[x]]--;

        counts[a[x] + 1]++;

        a[x]++;

        if (counts[a[x] - 1] == 0) {
            counts.erase(a[x] - 1);
        }

        if (counts.size() == 1 && (counts.begin()->second == 1 ||
           (counts.find(1) != counts.end() && counts[1] > 0))) {
            ans = i;
        }

        if (counts.size() == 2) {
            auto it = counts.begin();
            pair <int, int> left = *it;
            it++;
            pair<int, int> right = *it;

            if (left.first + 1 == right.first && right.second == 1) {
                ans = i;
            }

            if (left.first == 1 && left.second == 1) {
                ans = i;
            }

            if (left.first == 1 && left.second == right.second + 1  && right.second == 1) {
                ans = i;
            }
        }
    }

    cout << ans + 1;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define NAME "data"
using namespace std;
typedef long long LL;
typedef double DB;
typedef pair <LL, LL> pLL;
const int NMAX = 1e5+3;

int n, num, exist[NMAX], cnt[NMAX], res;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen(NAME".inp", "r", stdin);
    //freopen(NAME".out", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        int x; cin >> x;
        if(exist[x] == 0) num++;
        cnt[exist[x]]--;
        exist[x]++;
        cnt[exist[x]]++;
        if(i < n && cnt[exist[x]] == num) res = i+1;
        if(cnt[exist[x]] == num-1 && cnt[exist[x]+1] == 1) res = i;
    }
    cout << res;
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define NAME "data"
using namespace std;
typedef long long LL;
typedef double DB;
typedef pair <LL, LL> pLL;
const int NMAX = 1e5+3;

int n, num, cnt[NMAX], gr[NMAX], res;
set <int> st;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen(NAME".inp", "r", stdin);
    //freopen(NAME".out", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        int x; cin >> x;
        if(cnt[x] == 0) num++;

        st.erase(gr[cnt[x]]);
        gr[cnt[x]]--;
        st.insert(gr[cnt[x]]);

        cnt[x]++;

        st.erase(gr[cnt[x]]);
        gr[cnt[x]]++;
        st.insert(gr[cnt[x]]);

        if(st.count(num-1)) res = i;
    }
    if(res == 0) res = 1;
    cout << res;
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define NAME "data"
using namespace std;
typedef long long LL;
typedef double DB;
typedef pair <LL, LL> pLL;
const int NMAX = 1e5+3;

int n, num, cnt[NMAX], gr[NMAX], res;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen(NAME".inp", "r", stdin);
    //freopen(NAME".out", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        int x; cin >> x;
        if(cnt[x] == 0) num++;
        gr[cnt[x]]--;
        cnt[x]++;
        gr[cnt[x]]++;
        if((i-1)%num == 0 && gr[(i-1)/num] == num-1 && gr[(i-1)/num+1] == 1) res = i;
        if(cnt[x] == 1)
          if(num == 1 || ((i-1)%(num-1) == 0 && gr[(i-1)/(num-1)] == num-1)) res = i;
        if(gr[1] == i) res = i;
    }
    cout << res;
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define NAME "data"
using namespace std;
typedef long long LL;
typedef double DB;
typedef pair <LL, LL> pLL;
const int NMAX = 1e5+3;

int n, num, cnt[NMAX], gr[NMAX], res;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen(NAME".inp", "r", stdin);
    //freopen(NAME".out", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        int x; cin >> x;
        if(cnt[x] == 0) num++;
        gr[cnt[x]]--;
        cnt[x]++;
        gr[cnt[x]]++;
        if((i-1)%num == 0 && gr[(i-1)/num] == num-1 && gr[(i-1)/num+1] == 1) res = i;
        if(gr[1] == 1)
          if(num == 1 || ((i-1)%(num-1) == 0 && gr[(i-1)/(num-1)] == num-1)) res = i;
        if(gr[1] == i) res = i;
    }
    cout << res;
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define NAME "data"
using namespace std;
typedef long long LL;
typedef double DB;
typedef pair <LL, LL> pLL;
const int NMAX = 1e5+3;

int n, num, exist[NMAX], cnt[NMAX], res;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen(NAME".inp", "r", stdin);
    //freopen(NAME".out", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        int x; cin >> x;
        if(exist[x] == 0) num++;
        cnt[exist[x]]--;
        exist[x]++;
        cnt[exist[x]]++;
        if(cnt[exist[x]] == num-1 && cnt[exist[x]+1] == 1) res = i;
        if(i < n && cnt[exist[x]] == num) res = i+1;
    }
    if(res == 0) res = 1;
    cout << res;
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define NAME "data"
using namespace std;
typedef long long LL;
typedef double DB;
typedef pair <LL, LL> pLL;
const int NMAX = 1e5+3;

int n, num, cnt[NMAX], gr[NMAX], res;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen(NAME".inp", "r", stdin);
    //freopen(NAME".out", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        int x; cin >> x;
        if(cnt[x] == 0) num++;
        gr[cnt[x]]--;
        cnt[x]++;
        gr[cnt[x]]++;
        if(gr[cnt[x]] == num-1 && gr[cnt[x]+1] == 1) res = i;
        if(gr[cnt[x]] == 1 && gr[cnt[x]-1] == num-1) res = i;
    }
    if(res == 0) res = 1;
    cout << res;
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;

const int maxn = 100001;

int a[maxn];
int cnt[maxn];
int n;
map<int, int> ccnt;

#define scanf scanf_s

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (cnt[a[i]] == 0) {
			cnt[a[i]]++;
			ccnt[1]++;
		}
		else {
			ccnt[cnt[a[i]]]--;
			if (!ccnt[cnt[a[i]]]) ccnt.erase(cnt[a[i]]);
			cnt[a[i]]++;
			ccnt[cnt[a[i]]]++;
		}
		if (ccnt.size() == 1) ans = i;
		if (ccnt.size() == 2) {
			auto it = ccnt.begin();
			int a1 = it->first;
			int c1 = it->second;
			it++;
			int a2 = it->first;
			int c2 = it->second;
			if (a1 == 1 && c1 == 1) ans = i;
			if (a1 + 1 == a2 && c2 == 1) ans = i;
		}
	}
	printf("%d", ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;

const int maxn = 100001;

int a[maxn];
int cnt[maxn];
int n;
map<int, int> ccnt;

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (cnt[a[i]] == 0) {
			cnt[a[i]]++;
			ccnt[1]++;
		}
		else {
			ccnt[cnt[a[i]]]--;
			if (!ccnt[cnt[a[i]]]) ccnt.erase(cnt[a[i]]);
			cnt[a[i]]++;
			ccnt[cnt[a[i]]]++;
		}
		if (ccnt.size() == 1) ans = i;
		if (ccnt.size() == 2) {
			auto it = ccnt.begin();
			int a1 = it->first;
			int c1 = it->second;
			it++;
			int a2 = it->first;
			int c2 = it->second;
			if (a1 == 1 && c1 == 1) ans = i;
			if (a1 + 1 == a2 && c2 == 1) ans = i;
		}
	}
	printf("%d", ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;

const int maxn = 100001;

int a[maxn];
int cnt[maxn];
int n;
map<int, int> ccnt;

#define scanf scanf_s

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (cnt[a[i]] == 0) {
			cnt[a[i]]++;
			ccnt[1]++;
		}
		else if (cnt[a[i]] == 1) {
			ccnt[1]--;
			if (!ccnt[1]) ccnt.erase(1);
			cnt[a[i]]++;
			ccnt[2]++;
		}
		else {
			ccnt[cnt[a[i]]]--;
			if (!ccnt[cnt[a[i]]]) ccnt.erase(cnt[a[i]]);
			cnt[a[i]]++;
			ccnt[cnt[a[i]]]++;
		}
		if (ccnt.size() == 1) {
			if (ccnt.count(1)) ans = i;
		}
		if (ccnt.size() == 2) {
			auto it = ccnt.begin();
			int a1 = it->first;
			int c1 = it->second;
			it++;
			int a2 = it->first;
			int c2 = it->second;
			if (a1 == 1 && c1 == 1) ans = i;
			if (a1 + 1 == a2 && c2 == 1) ans = i;
		}
	}
	printf("%d", ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;

const int maxn = 100001;

int a[maxn];
int cnt[maxn];
int n;
map<int, int> ccnt;

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (cnt[a[i]] == 0) {
			cnt[a[i]]++;
			ccnt[1]++;
		}
		else if (cnt[a[i]] == 1) {
			ccnt[1]--;
			if (!ccnt[1]) ccnt.erase(1);
			cnt[a[i]]++;
			ccnt[2]++;
		}
		else {
			ccnt[cnt[a[i]]]--;
			if (!ccnt[cnt[a[i]]]) ccnt.erase(cnt[a[i]]);
			cnt[a[i]]++;
			ccnt[cnt[a[i]]]++;
		}
		if (ccnt.size() == 1) {
			if (ccnt.count(1)) ans = i;
		}
		if (ccnt.size() == 2) {
			auto it = ccnt.begin();
			int a1 = it->first;
			int c1 = it->second;
			it++;
			int a2 = it->first;
			int c2 = it->second;
			if (a1 == 1 && c1 == 1) ans = i;
			if (a1 + 1 == a2 && c2 == 1) ans = i;
		}
	}
	printf("%d", ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;

const int maxn = 100001;

int a[maxn];
int cnt[maxn];
int n;
map<int, int> ccnt;

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (cnt[a[i]] == 0) {
			cnt[a[i]]++;
			ccnt[1]++;
		}
		else {
			ccnt[cnt[a[i]]]--;
			if (!ccnt[cnt[a[i]]]) ccnt.erase(cnt[a[i]]);
			cnt[a[i]]++;
			ccnt[cnt[a[i]]]++;
		}
		if (ccnt.size() == 1) {
			if (ccnt.count(1)) ans = i;
			if (ccnt.begin()->second == 1) ans = i;
		}
		if (ccnt.size() == 2) {
			auto it = ccnt.begin();
			int a1 = it->first;
			int c1 = it->second;
			it++;
			int a2 = it->first;
			int c2 = it->second;
			if (a1 == 1 && c1 == 1) ans = i;
			if (a1 + 1 == a2 && c2 == 1) ans = i;
		}
	}
	printf("%d", ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;

const int maxn = 100001;

int a[maxn];
int cnt[maxn];
int n;
map<int, int> ccnt;

#define scanf scanf_s

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (cnt[a[i]] == 0) {
			cnt[a[i]]++;
			ccnt[1]++;
		}
		else {
			ccnt[cnt[a[i]]]--;
			if (!ccnt[cnt[a[i]]]) ccnt.erase(cnt[a[i]]);
			cnt[a[i]]++;
			ccnt[cnt[a[i]]]++;
		}
		if (ccnt.size() == 1) {
			if (ccnt.count(1)) ans = i;
			if (ccnt.begin()->second == 1) ans = i;
		}
		if (ccnt.size() == 2) {
			auto it = ccnt.begin();
			int a1 = it->first;
			int c1 = it->second;
			it++;
			int a2 = it->first;
			int c2 = it->second;
			if (a1 == 1 && c1 == 1) ans = i;
			if (a1 + 1 == a2 && c2 == 1) ans = i;
		}
	}
	printf("%d", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
ll mod1=1000000007ll;
ll mod2=998244353ll;
ll gcd(ll a, ll b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
ll power(ll x,ll y)
{   if(y==0) return 1;
    ll p=power(x,y/2);
    p=(p*p)%mod2;
    if(y%2) return  (p*x)%mod2;
    return p;
}

void solve()
{
  ll n=0,m1=0;
  cin>>n;
  ll a[100001];
   for(ll i=0;i<100001;i++)
    a[i]=0;
    ll ans=0,c=1,m=1;
    cin>>ans;
    a[ans]=1;
    ans=1;
    ll k=1, r=1;
  for(ll i=1;i<n;i++)
  {    ll x;
  //
  //cout<<x<<endl;
      cin>>x;
      //cout<<x<<m<<c<<endl;
      a[x]++;
      if(i/m==c && i%m==0)
        ans=i+1;
      if(a[x]==1)
         {c++;
         r++;}
       if(a[x]==2)
        r--;
      // cout<<x<<r<<c<<endl;
      if(m!=1 && k==1 && i/(m-1)==c && i%(m-1)==0)
         ans=i+1;
       if(m!=1 && r==1 && i/(m)==c-1 && i%(m)==0)
         ans=i+1;

       if(a[x]>m)
         {m=a[x];
          k=1;}
        else if(a[x]==m)
            k++;

  }
  //if(m==1)
  cout<<ans;
}

int main()
{
 /*ios::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);*/

 int t=1;
 //cin>>t;
 while(t--)
    solve();
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
ll mod1=1000000007ll;
ll mod2=998244353ll;
ll gcd(ll a, ll b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
ll power(ll x,ll y)
{   if(y==0) return 1;
    ll p=power(x,y/2);
    p=(p*p)%mod2;
    if(y%2) return  (p*x)%mod2;
    return p;
}

void solve()
{
  ll n=0,m1=0;
  cin>>n;
  ll a[100001];
   for(ll i=0;i<100001;i++)
    a[i]=0;
    ll ans=0,c=1,m=1;
    cin>>ans;
    a[ans]=1;
    ans=1;
    ll k=1, r=1;
  for(ll i=1;i<n;i++)
  {    ll x;
  //
  //cout<<x<<endl;
      cin>>x;
      //cout<<x<<m<<c<<endl;
      a[x]++;
      if(i/m==c && i%m==0)
        ans=i+1;
      if(a[x]==1)
         {c++;
         r++;}
       if(a[x]==2)
        r--;
      // cout<<x<<r<<c<<endl;
      if(a[x]>m)
         {m=a[x];
          k=1;}
        else if(a[x]==m)
            k++;
      if(m!=1 && k==1 && i/(m-1)==c && i%(m-1)==0)
         ans=i+1;
       if(m!=1 && r==1 && i/(m)==c-1 && i%(m)==0)
         ans=i+1;

  }
  //if(m==1)
  cout<<ans;
}

int main()
{
 /*ios::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);*/

 int t=1;
 //cin>>t;
 while(t--)
    solve();
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
ll mod1=1000000007ll;
ll mod2=998244353ll;
ll gcd(ll a, ll b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
ll power(ll x,ll y)
{   if(y==0) return 1;
    ll p=power(x,y/2);
    p=(p*p)%mod2;
    if(y%2) return  (p*x)%mod2;
    return p;
}

void solve()
{
  ll n=0,m1=0;
  cin>>n;
  ll a[100001];
   for(ll i=0;i<100001;i++)
    a[i]=0;
    ll ans=0,c=1,m=1;
    cin>>ans;
    a[ans]=1;
    ans=1;
    ll k=1;
  for(ll i=1;i<n;i++)
  {    ll x;
  //
  //cout<<x<<endl;
      cin>>x;
      //cout<<x<<m<<c<<endl;
      a[x]++;
      if(a[x]==m)
            k++;
      if(a[x]>m )
      {    k=1;
          if(i/m==c)
          ans=i+1;
          //m1=m;
          m=a[x];
      }
      else if(a[x]==m-1 && k==1)
      {   if(a[x]==1)
            c++;
          if(i/(m-1)==c)
          ans=i+1;
         if(a[x]==1)
            c--;
      }
      else if(a[x]==1 && i/m==c)
        ans=i+1;

      if(a[x]==1)
        c++;

  }
  cout<<ans;
}

int main()
{
 /*ios::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);*/

 int t=1;
 //cin>>t;
 while(t--)
    solve();
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
ll mod1=1000000007ll;
ll mod2=998244353ll;
ll gcd(ll a, ll b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
ll power(ll x,ll y)
{   if(y==0) return 1;
    ll p=power(x,y/2);
    p=(p*p)%mod2;
    if(y%2) return  (p*x)%mod2;
    return p;
}

void solve()
{
  ll n=0,m1=0;
  cin>>n;
  ll a[100001];
   for(ll i=0;i<100001;i++)
    a[i]=0;
    ll ans=0,c=1,m=1;
    cin>>ans;
    a[ans]=1;
    ans=1;
    ll k=1, r=1;
  for(ll i=1;i<n;i++)
  {    ll x;
  //
  //cout<<x<<endl;
      cin>>x;
      //cout<<x<<m<<c<<endl;
      a[x]++;
      if(i/m==c && i%m==0)
        ans=i+1;
      if(a[x]==1)
         {c++;
         r++;}
       if(a[x]==2)
        r--;
      // cout<<x<<r<<c<<endl;
      if(m!=1 && i/(m-1)==c && i%(m-1)==0)
         ans=i+1;
       if(m!=1 && r==1 && i/(m)==c-1 && i%(m)==0)
         ans=i+1;

       if(a[x]>m)
         m=a[x];
  }
  //if(m==1)
  cout<<ans;
}

int main()
{
 /*ios::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);*/

 int t=1;
 //cin>>t;
 while(t--)
    solve();
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
ll mod1=1000000007ll;
ll mod2=998244353ll;
ll gcd(ll a, ll b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
ll power(ll x,ll y)
{   if(y==0) return 1;
    ll p=power(x,y/2);
    p=(p*p)%mod2;
    if(y%2) return  (p*x)%mod2;
    return p;
}

void solve()
{
  ll n=0;
  cin>>n;
  ll a[100001];
   for(ll i=0;i<100001;i++)
    a[i]=0;
    ll ans=0,c=1,m=1;
    cin>>ans;
    a[ans]=1;
    ans=1;
  for(ll i=1;i<n;i++)
  {    ll x;
  //
  //cout<<x<<endl;
      cin>>x;
      //cout<<x<<m<<c<<endl;
      a[x]++;

      if(a[x]>m )
      {
          if(i/m==c)
          ans=i+1;
          m=a[x];
      }
      else if(a[x]==m-1)
      {   if(a[x]==1)
            c++;
          if(i/(m-1)==c)
          ans=i+1;
         if(a[x]==1)
            c--;
      }
      else if(a[x]==1 && i/m==c)
        ans=i+1;
      if(a[x]==1)
        c++;

  }
  cout<<ans;
}

int main()
{
 /*ios::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);*/

 int t=1;
 //cin>>t;
 while(t--)
    solve();
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
ll mod1=1000000007ll;
ll mod2=998244353ll;
ll gcd(ll a, ll b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
ll power(ll x,ll y)
{   if(y==0) return 1;
    ll p=power(x,y/2);
    p=(p*p)%mod2;
    if(y%2) return  (p*x)%mod2;
    return p;
}

void solve()
{
  ll n=0,m1=0;
  cin>>n;
  ll a[100001];
   for(ll i=0;i<100001;i++)
    a[i]=0;
    ll ans=0,c=1,m=1;
    cin>>ans;
    a[ans]=1;
    ans=1;
    ll k=1;
  for(ll i=1;i<n;i++)
  {    ll x;
  //
  //cout<<x<<endl;
      cin>>x;
      //cout<<x<<m<<c<<endl;
      a[x]++;
      if(i/m==c && i%m==0)
        ans=i+1;
      if(a[x]==1)
         c++;
      if(m!=1 && i/(m-1)==c && i%(m-1)==0)
         ans=i+1;
       if(a[x]>m)
         m=a[x];
  }
  cout<<ans;
}

int main()
{
 /*ios::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);*/

 int t=1;
 //cin>>t;
 while(t--)
    solve();
 return 0;
}


#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_cxx;
#define dprint(v) cout << #v"=" << v << endl //;)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define INF (ll)1e18
#define endl '\n'
#define isin(x,y) ((y).find(x)!=(y).end())
#define forr(i,a,b) for(int i=(a); i<(b); i++)
#define int long long
#define MAXN 100004

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

typedef pair<int,int> ii;

pair<int, int> t[4*MAXN];

pair<int, int> combinemn(pair<int, int> a, pair<int, int> b) {
    if (a.first < b.first)
        return a;
    if (b.first < a.first)
        return b;
    return make_pair(a.first, a.second + b.second);
}

void buildmn(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = make_pair(a[tl], 1);
    } else {
        int tm = (tl + tr) / 2;
        buildmn(a, v*2, tl, tm);
        buildmn(a, v*2+1, tm+1, tr);
        t[v] = combinemn(t[v*2], t[v*2+1]);
    }
}

pair<int, int> get_min(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return make_pair(INF, 0);
    if (l == tl && r == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    return combinemn(get_min(v*2, tl, tm, l, min(r, tm)),
                   get_min(v*2+1, tm+1, tr, max(l, tm+1), r));
}

void updatemn(int v, int tl, int tr, int pos, int new_val) {
    if (tl == tr) {
        t[v] = make_pair(new_val, 1);
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            updatemn(v*2, tl, tm, pos, new_val);
        else
            updatemn(v*2+1, tm+1, tr, pos, new_val);
        t[v] = combinemn(t[v*2], t[v*2+1]);
    }
}

pair<int, int> u[4*MAXN];

pair<int, int> combine(pair<int, int> a, pair<int, int> b) {
    if (a.first > b.first)
        return a;
    if (b.first > a.first)
        return b;
    return make_pair(a.first, a.second + b.second);
}

void build(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        u[v] = make_pair(a[tl], 1);
    } else {
        int tm = (tl + tr) / 2;
        build(a, v*2, tl, tm);
        build(a, v*2+1, tm+1, tr);
        u[v] = combine(u[v*2], u[v*2+1]);
    }
}

pair<int, int> get_max(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return make_pair(-INF, 0);
    if (l == tl && r == tr)
        return u[v];
    int tm = (tl + tr) / 2;
    return combine(get_max(v*2, tl, tm, l, min(r, tm)),
                   get_max(v*2+1, tm+1, tr, max(l, tm+1), r));
}

void update(int v, int tl, int tr, int pos, int new_val) {
    if (tl == tr) {
        u[v] = make_pair(new_val, 1);
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            update(v*2, tl, tm, pos, new_val);
        else
            update(v*2+1, tm+1, tr, pos, new_val);
        u[v] = combine(u[v*2], u[v*2+1]);
    }
}

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
        cin>>n;
        vll v(n);
        forn(i,n)cin>>v[i];
        int feq[100002];
        memset(feq,0,sizeof(feq));
        int feq2[100002];
        memset(feq2,1000000000,sizeof(feq2));
        forn(i,100002)feq2[i]=INF;
        buildmn(feq2,1,0,100001);
        build(feq,1,0,100001);
        set<ll> seen;
        ll ans=-1;
        //forn(i,100002)dprint(get_min(1,0,100001,i,i).ff);
        //forn(i,100002)dprint(feq2[i]);
        forn(i,n){
            if(!isin(v[i],seen)){
                updatemn(1,0,100001,v[i],1);

            }else{
                updatemn(1,0,100001,v[i],get_min(1,0,100001,v[i],v[i]).ff+1);
            }
            update(1,0,100001,v[i],get_max(1,0,100001,v[i],v[i]).ff+1);
            seen.insert(v[i]);
            pll mx=get_max(1,0,100001,0,100001);
            pll mn=get_min(1,0,100001,0,100001);
            //cout<<"i: "<<i<<"  mx: "<<mx.ff<<' '<<mx.ss<<"  mn: "<<mn.ff<<' '<<mn.ss<<endl;
            if(i%sz(seen)==0){
                if(mx.ss==1&&mx.ff-mn.ff==1&&mn.ss==sz(seen)-1)ans=i+1;
            }
            if(sz(seen)>1&&i%(sz(seen)-1)==0){
                if(mn.ff==1&&mn.ss==1&&mx.ss==sz(seen)-1)ans=i+1;
            }
            if(mn.ss==i+1&&mx.ss==i+1)ans=i+1;
        }
        cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_cxx;
#define dprint(v) cout << #v"=" << v << endl //;)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define INF (ll)1e16
#define endl '\n'
#define isin(x,y) ((y).find(x)!=(y).end())
#define forr(i,a,b) for(int i=(a); i<(b); i++)

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

typedef pair<int,int> ii;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
        cin>>n;
        vll v(n);
        set<ll> seen;
        forn(i,n){
            cin>>v[i];
        }
        set<ll> one;
        ll sm=0;
        ll ans=-1;
        ll mul=0;
        ll last=-1;
        forn(i,n){
            sm+=v[i];
            if(!isin(v[i],seen)){
                mul+=v[i];
                one.insert(v[i]);
                last=v[i];
            }else{
                one.erase(v[i]);
            }
            seen.insert(v[i]);
            if(i%sz(seen)==0){
                if(isin(sm-mul*(i/sz(seen)),seen)){ans=i+1;}
            }else if(i%(sz(seen)-1)==0){
                if(sz(one)==1&&sm-((mul-last)*(i/(sz(seen)-1)))==last){ans=i+1;}
            }
            if(sz(seen)==sz(one))ans=i+1;
        }
        cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_cxx;
#define dprint(v) cout << #v"=" << v << endl //;)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define INF (ll)1e18
#define endl '\n'
#define isin(x,y) ((y).find(x)!=(y).end())
#define forr(i,a,b) for(int i=(a); i<(b); i++)
#define int long long
#define MAXN 100004

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

typedef pair<int,int> ii;

pair<int, int> t[4*MAXN];

pair<int, int> combinemn(pair<int, int> a, pair<int, int> b) {
    if (a.first < b.first)
        return a;
    if (b.first < a.first)
        return b;
    return make_pair(a.first, a.second + b.second);
}

void buildmn(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = make_pair(a[tl], 1);
    } else {
        int tm = (tl + tr) / 2;
        buildmn(a, v*2, tl, tm);
        buildmn(a, v*2+1, tm+1, tr);
        t[v] = combinemn(t[v*2], t[v*2+1]);
    }
}

pair<int, int> get_min(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return make_pair(INF, 0);
    if (l == tl && r == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    return combinemn(get_min(v*2, tl, tm, l, min(r, tm)),
                   get_min(v*2+1, tm+1, tr, max(l, tm+1), r));
}

void updatemn(int v, int tl, int tr, int pos, int new_val) {
    if (tl == tr) {
        t[v] = make_pair(new_val, 1);
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            updatemn(v*2, tl, tm, pos, new_val);
        else
            updatemn(v*2+1, tm+1, tr, pos, new_val);
        t[v] = combinemn(t[v*2], t[v*2+1]);
    }
}

pair<int, int> u[4*MAXN];

pair<int, int> combine(pair<int, int> a, pair<int, int> b) {
    if (a.first > b.first)
        return a;
    if (b.first > a.first)
        return b;
    return make_pair(a.first, a.second + b.second);
}

void build(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        u[v] = make_pair(a[tl], 1);
    } else {
        int tm = (tl + tr) / 2;
        build(a, v*2, tl, tm);
        build(a, v*2+1, tm+1, tr);
        u[v] = combine(u[v*2], u[v*2+1]);
    }
}

pair<int, int> get_max(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return make_pair(-INF, 0);
    if (l == tl && r == tr)
        return u[v];
    int tm = (tl + tr) / 2;
    return combine(get_max(v*2, tl, tm, l, min(r, tm)),
                   get_max(v*2+1, tm+1, tr, max(l, tm+1), r));
}

void update(int v, int tl, int tr, int pos, int new_val) {
    if (tl == tr) {
        u[v] = make_pair(new_val, 1);
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            update(v*2, tl, tm, pos, new_val);
        else
            update(v*2+1, tm+1, tr, pos, new_val);
        u[v] = combine(u[v*2], u[v*2+1]);
    }
}

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
        cin>>n;
        vll v(n);
        forn(i,n)cin>>v[i];
        int feq[100002];
        memset(feq,0,sizeof(feq));
        int feq2[100002];
        memset(feq2,1000000000,sizeof(feq2));
        forn(i,100002)feq2[i]=INF;
        buildmn(feq2,1,0,100001);
        build(feq,1,0,100001);
        set<ll> seen;
        ll ans=-1;
        //forn(i,100002)dprint(get_min(1,0,100001,i,i).ff);
        //forn(i,100002)dprint(feq2[i]);
        forn(i,n){
            if(!isin(v[i],seen)){
                updatemn(1,0,100001,v[i],1);

            }else{
                updatemn(1,0,100001,v[i],get_min(1,0,100001,v[i],v[i]).ff+1);
            }
            update(1,0,100001,v[i],get_max(1,0,100001,v[i],v[i]).ff+1);
            seen.insert(v[i]);
            pll mx=get_max(1,0,100001,0,100001);
            pll mn=get_min(1,0,100001,0,100001);
            //cout<<"i: "<<i<<"  mx: "<<mx.ff<<' '<<mx.ss<<"  mn: "<<mn.ff<<' '<<mn.ss<<endl;
            if(i%sz(seen)==0){
                if(mx.ss==1&&mx.ff-mn.ff==1&&mn.ss==sz(seen)-1)ans=i+1;
            }
            if(sz(seen)>1&&i%(sz(seen)-1)==0){
                if(mn.ss==1&&mx.ss==sz(seen)-1)ans=i+1;
            }
            if(mn.ss==i+1&&mx.ss==i+1)ans=i+1;
        }
        cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_cxx;
#define dprint(v) cout << #v"=" << v << endl //;)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define INF (ll)1e18
#define endl '\n'
#define isin(x,y) ((y).find(x)!=(y).end())
#define forr(i,a,b) for(int i=(a); i<(b); i++)
#define int long long
#define MAXN 100004

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

typedef pair<int,int> ii;

pair<int, int> t[4*MAXN];

pair<int, int> combinemn(pair<int, int> a, pair<int, int> b) {
    if (a.first < b.first)
        return a;
    if (b.first < a.first)
        return b;
    return make_pair(a.first, a.second + b.second);
}

void buildmn(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = make_pair(a[tl], 1);
    } else {
        int tm = (tl + tr) / 2;
        buildmn(a, v*2, tl, tm);
        buildmn(a, v*2+1, tm+1, tr);
        t[v] = combinemn(t[v*2], t[v*2+1]);
    }
}

pair<int, int> get_min(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return make_pair(INF, 0);
    if (l == tl && r == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    return combinemn(get_min(v*2, tl, tm, l, min(r, tm)),
                   get_min(v*2+1, tm+1, tr, max(l, tm+1), r));
}

void updatemn(int v, int tl, int tr, int pos, int new_val) {
    if (tl == tr) {
        t[v] = make_pair(new_val, 1);
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            updatemn(v*2, tl, tm, pos, new_val);
        else
            updatemn(v*2+1, tm+1, tr, pos, new_val);
        t[v] = combinemn(t[v*2], t[v*2+1]);
    }
}

pair<int, int> u[4*MAXN];

pair<int, int> combine(pair<int, int> a, pair<int, int> b) {
    if (a.first > b.first)
        return a;
    if (b.first > a.first)
        return b;
    return make_pair(a.first, a.second + b.second);
}

void build(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        u[v] = make_pair(a[tl], 1);
    } else {
        int tm = (tl + tr) / 2;
        build(a, v*2, tl, tm);
        build(a, v*2+1, tm+1, tr);
        u[v] = combine(u[v*2], u[v*2+1]);
    }
}

pair<int, int> get_max(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return make_pair(-INF, 0);
    if (l == tl && r == tr)
        return u[v];
    int tm = (tl + tr) / 2;
    return combine(get_max(v*2, tl, tm, l, min(r, tm)),
                   get_max(v*2+1, tm+1, tr, max(l, tm+1), r));
}

void update(int v, int tl, int tr, int pos, int new_val) {
    if (tl == tr) {
        u[v] = make_pair(new_val, 1);
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            update(v*2, tl, tm, pos, new_val);
        else
            update(v*2+1, tm+1, tr, pos, new_val);
        u[v] = combine(u[v*2], u[v*2+1]);
    }
}

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
        cin>>n;
        vll v(n);
        forn(i,n)cin>>v[i];
        int feq[100002];
        memset(feq,0,sizeof(feq));
        int feq2[100002];
        memset(feq2,1000000000,sizeof(feq2));
        forn(i,100002)feq2[i]=INF;
        buildmn(feq2,1,0,100001);
        build(feq,1,0,100001);
        set<ll> seen;
        ll ans=-1;
        //forn(i,100002)dprint(get_min(1,0,100001,i,i).ff);
        //forn(i,100002)dprint(feq2[i]);
        forn(i,n){
            if(!isin(v[i],seen)){
                updatemn(1,0,100001,v[i],1);

            }else{
                updatemn(1,0,100001,v[i],get_min(1,0,100001,v[i],v[i]).ff+1);
            }
            update(1,0,100001,v[i],get_max(1,0,100001,v[i],v[i]).ff+1);
            seen.insert(v[i]);
            pll mx=get_max(1,0,100001,0,100001);
            pll mn=get_min(1,0,100001,0,100001);
            //cout<<"i: "<<i<<"  mx: "<<mx.ff<<' '<<mx.ss<<"  mn: "<<mn.ff<<' '<<mn.ss<<endl;
            if(i%sz(seen)==0){
                if(mx.ss==1&&mn.ff==(i/sz(seen))&&mx.ff==mn.ff+1&&mn.ss==sz(seen)-1)ans=i+1;
            }
            if(sz(seen)>1&&i%(sz(seen)-1)==0){
                if(mn.ff==1&&mn.ss==1&&mx.ss==sz(seen)-1&&mx.ff==i/(sz(seen)-1))ans=i+1;
            }
            if(mn.ss==i+1&&mx.ss==i+1)ans=i+1;
            if(sz(seen)==1)ans=i+1;
        }
        cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_cxx;
#define dprint(v) cout << #v"=" << v << endl //;)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define INF (ll)1e16
#define endl '\n'
#define isin(x,y) ((y).find(x)!=(y).end())
#define forr(i,a,b) for(int i=(a); i<(b); i++)

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

typedef pair<int,int> ii;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
        cin>>n;
        vll v(n);
        set<ll> seen;
        forn(i,n){
            cin>>v[i];
        }
        set<ll> one;
        ll sm=0;
        ll ans=-1;
        ll mul=0;
        ll last=-1;
        forn(i,n){
            sm+=v[i];
            if(!isin(v[i],seen)){
                mul+=v[i];
                one.insert(v[i]);
                last=v[i];
            }else{
                one.erase(v[i]);
            }
            seen.insert(v[i]);
            if(i%sz(seen)==0){
                if(isin(sm-mul*(i/sz(seen)),seen)){ans=i+1;}
            }
            if(i%(sz(seen)-1)==0){
                if(sz(one)==1&&sm-((mul-last)*(i/(sz(seen)-1)))==last){ans=i+1;}
            }
            if(sz(seen)==sz(one))ans=i+1;
        }
        cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_cxx;
#define dprint(v) cout << #v"=" << v << endl //;)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define INF (ll)1e16
#define endl '\n'
#define isin(x,y) ((y).find(x)!=(y).end())
#define forr(i,a,b) for(int i=(a); i<(b); i++)

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

typedef pair<int,int> ii;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
        cin>>n;
        vll v(n);
        set<ll> seen;
        forn(i,n){
            cin>>v[i];
        }
        set<ll> one;
        ll sm=0;
        ll ans=-1;
        ll mul=0;
        ll last=-1;
        forn(i,n){
            sm+=v[i];
            if(!isin(v[i],seen)){
                mul+=v[i];
                one.insert(v[i]);
                last=v[i];
            }else{
                one.erase(v[i]);
            }
            seen.insert(v[i]);
            if(i%sz(seen)==0){
                if(isin(sm-mul*(i/sz(seen)),seen)){ans=i+1;}
            }
            if(sz(seen)&&i%(sz(seen)-1)==0){
                if(sz(one)==1&&sm-((mul-last)*(i/(sz(seen)-1)))==last){ans=i+1;}
            }
            if(sz(seen)==sz(one))ans=i+1;
        }
        cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_cxx;
#define dprint(v) cout << #v"=" << v << endl //;)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define INF (ll)1e16
#define endl '\n'
#define isin(x,y) ((y).find(x)!=(y).end())
#define forr(i,a,b) for(int i=(a); i<(b); i++)

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

typedef pair<int,int> ii;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
        cin>>n;
        vll v(n);
        set<ll> seen;
        forn(i,n){
            cin>>v[i];
        }
        set<ll> one;
        ll sm=0;
        ll ans=-1;
        ll mul=0;
        ll last=-1;
        forn(i,n){
            sm+=v[i];
            if(!isin(v[i],seen)){
                mul+=v[i];
                one.insert(v[i]);
                last=v[i];
            }else{
                one.erase(v[i]);
            }
            if(sz(one)==1)for (auto x:one)last=x;
            seen.insert(v[i]);
            if(i%sz(seen)==0){
                if(isin(sm-mul*(i/sz(seen)),seen)){ans=i+1;}
            }
            if(sz(seen)>1&&i%(sz(seen)-1)==0){
                if(sz(one)==1&&sm-((mul-last)*(i/(sz(seen)-1)))==last){ans=i+1;}
            }
            if(sz(seen)==sz(one))ans=i+1;
        }
        cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_cxx;
#define dprint(v) cout << #v"=" << v << endl //;)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define INF (ll)1e18
#define endl '\n'
#define isin(x,y) ((y).find(x)!=(y).end())
#define forr(i,a,b) for(int i=(a); i<(b); i++)
#define int long long
#define MAXN 100004

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

typedef pair<int,int> ii;

pair<int, int> t[4*MAXN];

pair<int, int> combinemn(pair<int, int> a, pair<int, int> b) {
    if (a.first < b.first)
        return a;
    if (b.first < a.first)
        return b;
    return make_pair(a.first, a.second + b.second);
}

void buildmn(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = make_pair(a[tl], 1);
    } else {
        int tm = (tl + tr) / 2;
        buildmn(a, v*2, tl, tm);
        buildmn(a, v*2+1, tm+1, tr);
        t[v] = combinemn(t[v*2], t[v*2+1]);
    }
}

pair<int, int> get_min(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return make_pair(INF, 0);
    if (l == tl && r == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    return combinemn(get_min(v*2, tl, tm, l, min(r, tm)),
                   get_min(v*2+1, tm+1, tr, max(l, tm+1), r));
}

void updatemn(int v, int tl, int tr, int pos, int new_val) {
    if (tl == tr) {
        t[v] = make_pair(new_val, 1);
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            updatemn(v*2, tl, tm, pos, new_val);
        else
            updatemn(v*2+1, tm+1, tr, pos, new_val);
        t[v] = combinemn(t[v*2], t[v*2+1]);
    }
}

pair<int, int> u[4*MAXN];

pair<int, int> combine(pair<int, int> a, pair<int, int> b) {
    if (a.first > b.first)
        return a;
    if (b.first > a.first)
        return b;
    return make_pair(a.first, a.second + b.second);
}

void build(int a[], int v, int tl, int tr) {
    if (tl == tr) {
        u[v] = make_pair(a[tl], 1);
    } else {
        int tm = (tl + tr) / 2;
        build(a, v*2, tl, tm);
        build(a, v*2+1, tm+1, tr);
        u[v] = combine(u[v*2], u[v*2+1]);
    }
}

pair<int, int> get_max(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return make_pair(-INF, 0);
    if (l == tl && r == tr)
        return u[v];
    int tm = (tl + tr) / 2;
    return combine(get_max(v*2, tl, tm, l, min(r, tm)),
                   get_max(v*2+1, tm+1, tr, max(l, tm+1), r));
}

void update(int v, int tl, int tr, int pos, int new_val) {
    if (tl == tr) {
        u[v] = make_pair(new_val, 1);
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            update(v*2, tl, tm, pos, new_val);
        else
            update(v*2+1, tm+1, tr, pos, new_val);
        u[v] = combine(u[v*2], u[v*2+1]);
    }
}

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
        cin>>n;
        vll v(n);
        forn(i,n)cin>>v[i];
        int feq[100002];
        memset(feq,0,sizeof(feq));
        int feq2[100002];
        memset(feq2,1000000000,sizeof(feq2));
        forn(i,100002)feq2[i]=INF;
        buildmn(feq2,1,0,100001);
        build(feq,1,0,100001);
        set<ll> seen;
        ll ans=-1;
        //forn(i,100002)dprint(get_min(1,0,100001,i,i).ff);
        //forn(i,100002)dprint(feq2[i]);
        forn(i,n){
            if(!isin(v[i],seen)){
                updatemn(1,0,100001,v[i],1);

            }else{
                updatemn(1,0,100001,v[i],get_min(1,0,100001,v[i],v[i]).ff+1);
            }
            update(1,0,100001,v[i],get_max(1,0,100001,v[i],v[i]).ff+1);
            seen.insert(v[i]);
            pll mx=get_max(1,0,100001,0,100001);
            pll mn=get_min(1,0,100001,0,100001);
            //cout<<"i: "<<i<<"  mx: "<<mx.ff<<' '<<mx.ss<<"  mn: "<<mn.ff<<' '<<mn.ss<<endl;
            if(i%sz(seen)==0){
                if(mx.ss==1&&mn.ff==(i/sz(seen))&&mx.ff==mn.ff+1&&mn.ss==sz(seen)-1)ans=i+1;
            }
            if(sz(seen)>1&&i%(sz(seen)-1)==0){
                if(mn.ff==1&&mn.ss==1&&mx.ss==sz(seen)-1&&mx.ff==i/(sz(seen)-1))ans=i+1;
            }
            if(mn.ss==i+1&&mx.ss==i+1)ans=i+1;
        }
        cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_cxx;
#define dprint(v) cout << #v"=" << v << endl //;)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define INF (ll)1e16
#define endl '\n'
#define isin(x,y) ((y).find(x)!=(y).end())
#define forr(i,a,b) for(int i=(a); i<(b); i++)

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

typedef pair<int,int> ii;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
        cin>>n;
        vll v(n);
        set<ll> seen;
        forn(i,n){
            cin>>v[i];
        }
        set<ll> one;
        ll sm=0;
        ll ans=-1;
        ll mul=0;
        ll last=-1;
        forn(i,n){
            sm+=v[i];
            if(!isin(v[i],seen)){
                mul+=v[i];
                one.insert(v[i]);
                last=v[i];
            }else{
                one.erase(v[i]);
            }
            seen.insert(v[i]);
            if(i%sz(seen)==0){
                if(isin(sm-mul*(i/sz(seen)),seen)){ans=i+1;}
            }
            if(sz(seen)>1&&i%(sz(seen)-1)==0){
                if(sz(one)==1&&sm-((mul-last)*(i/(sz(seen)-1)))==last){ans=i+1;}
            }
            if(sz(seen)==sz(one))ans=i+1;
        }
        cout<<ans<<endl;
	return 0;
}


/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 *
 * AND WATCH OUT FOR OFF BY ONE!!!
 */

#include <bits/stdc++.h>
const bool unsyncedio=std::ios::sync_with_stdio(false);
using namespace std;
typedef unsigned int uint;
typedef long long ll;

#define fori(n) for(ll i=0; i<(n); i++)
#define forn(i, n) for(ll (i)=0; (i)<(n); (i)++)

int main() {
    int n;
    cin>>n;
    vector<int> f(100001);      // freq of color i
    vector<int> ff(100001);     // freq of freq i
    set<int> ffset;   // set of distinct value s in ff, ie existing frequencies

    int ans=0;
    fori(n) {
        int u;
        cin>>u;

        if(ff[f[u]]>0) {
            ff[f[u]]--;
            if(ff[f[u]]==0)
                ffset.erase(f[u]);
        }

        f[u]++;
        ff[f[u]]++;
        ffset.insert(f[u]);

//cout<<"i="<<i<<" ffset.size()="<<ffset.size()<<endl;
        if(ffset.size()==2) {
            int f1=(*ffset.begin());
            auto sec=ffset.begin(); sec++;
            int f2=(*sec);
            int fSize1=ff[f1];
            int fSize2=ff[f2];
//cout<<"f1="<<f1<<" f2="<<f2<<endl;
            if(fSize1 ==1 || fSize2==1)
                ans=i;
            else if((fSize1-fSize2==1 && f1==1) ||(fSize2-fSize1==1 && f2==1))
                ans=i;
        } else if(ffset.size()==1) {
            auto f1=(*ffset.begin());
            if(f1==1)     // all ribbons have frequency == 1
                ans=i;
        }

//cases
// 1. one frequency, its == 1
// 2. two frequencies, one of them == 1
// 3. two frequencies, one of them == other+1
    }

    cout<<ans+1<<endl;

}


/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 *
 * AND WATCH OUT FOR OFF BY ONE!!!
 */

#include <bits/stdc++.h>
const bool unsyncedio=std::ios::sync_with_stdio(false);
using namespace std;
typedef unsigned int uint;
typedef long long ll;

#define fori(n) for(ll i=0; i<(n); i++)
#define forn(i, n) for(ll (i)=0; (i)<(n); (i)++)

int main() {
    int n;
    cin>>n;
    vector<int> u(n);
    vector<int> f(100001);
    unordered_map<int, int> freqs; // maps a frequency to the number of ribbons with that freq

    int ans=0;
    fori(n) {
        cin>>u[i];
        if(freqs.count(f[u[i]])>0) {
            freqs[f[u[i]]]--;
            if(freqs[f[u[i]]]==0)
                freqs.erase(f[u[i]]);
        }
        f[u[i]]++;
        freqs[f[u[i]]]++;

//cout<<"i="<<i<<" freqs.size()="<<freqs.size()<<endl;
        if(freqs.size()==2) {
            int f1=(*freqs.begin()).second;
            auto sec=freqs.begin();
            sec++;
            int f2=(*sec).second;
            if(f1==1 || f2==1)
                ans=i;
        }
    }

    cout<<ans+1<<endl;

}


/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 *
 * AND WATCH OUT FOR OFF BY ONE!!!
 */

#include <bits/stdc++.h>
const bool unsyncedio=std::ios::sync_with_stdio(false);
using namespace std;
typedef unsigned int uint;
typedef long long ll;

#define fori(n) for(ll i=0; i<(n); i++)
#define forn(i, n) for(ll (i)=0; (i)<(n); (i)++)

int main() {
    int n;
    cin>>n;
    vector<int> f(100001);
    unordered_map<int, int> freqs; // maps a frequency to the number of ribbons with that freq

    int ans=0;
    fori(n) {
        int u;
        cin>>u;
        if(freqs.count(f[u])>0) {
            freqs[f[u]]--;
            if(freqs[f[u]]==0)
                freqs.erase(f[u]);
        }
        f[u]++;
        freqs[f[u]]++;

//cout<<"i="<<i<<" freqs.size()="<<freqs.size()<<endl;
        if(freqs.size()==2) {
            auto f1=(*freqs.begin());
            auto sec=freqs.begin();
            sec++;
            auto f2=(*sec);
//cout<<"f1="<<f1<<" f2="<<f2<<endl;
            if(f1.second==1 && (f1.first==f2.first+1 || f1.first==1))
                ans=i;
            else if(f2.second==1 && (f2.first==f1.first+1 || f2.first==1))
                ans=i;
        } else if(freqs.size()==1) {
            ans=i;
        }
    }

    cout<<ans+1<<endl;

}


/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 *
 * AND WATCH OUT FOR OFF BY ONE!!!
 */

#include <bits/stdc++.h>
const bool unsyncedio=std::ios::sync_with_stdio(false);
using namespace std;
typedef unsigned int uint;
typedef long long ll;

#define fori(n) for(ll i=0; i<(n); i++)
#define forn(i, n) for(ll (i)=0; (i)<(n); (i)++)

int main() {
    int n;
    cin>>n;
    vector<int> f(100001);
    unordered_map<int, int> freqs; // maps a frequency to the number of ribbons with that freq

    int ans=0;
    fori(n) {
        int u;
        cin>>u;
        if(freqs.count(f[u])>0) {
            freqs[f[u]]--;
            if(freqs[f[u]]==0)
                freqs.erase(f[u]);
        }
        f[u]++;
        freqs[f[u]]++;

//cout<<"i="<<i<<" freqs.size()="<<freqs.size()<<endl;
        if(freqs.size()==2) {
            auto f1=(*freqs.begin());
            auto sec=freqs.begin();
            sec++;
            auto f2=(*sec);
//cout<<"f1="<<f1<<" f2="<<f2<<endl;
            if(f1.second==1 && f1.first==f2.first+1)
                ans=i;
            else if(f2.second==1 && f2.first==f1.first+1)
                ans=i;
        } else if(freqs.size()==1) {
            ans=i;
        }
    }

    cout<<ans+1<<endl;

}


/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 *
 * AND WATCH OUT FOR OFF BY ONE!!!
 */

#include <bits/stdc++.h>
const bool unsyncedio=std::ios::sync_with_stdio(false);
using namespace std;
typedef unsigned int uint;
typedef long long ll;

#define fori(n) for(ll i=0; i<(n); i++)
#define forn(i, n) for(ll (i)=0; (i)<(n); (i)++)

int main() {
    int n;
    cin>>n;
    vector<int> u(n);
    vector<int> f(100001);
    unordered_map<int, int> freqs; // maps a frequency to the number of ribbons with that freq

    int ans=0;
    fori(n) {
        cin>>u[i];
        if(freqs.count(f[u[i]])>0) {
            freqs[f[u[i]]]--;
            if(freqs[f[u[i]]]==0)
                freqs.erase(f[u[i]]);
        }
        f[u[i]]++;
        freqs[f[u[i]]]++;

//cout<<"i="<<i<<" freqs.size()="<<freqs.size()<<endl;
        if(freqs.size()==2) {
            int f1=(*freqs.begin()).second;
            auto sec=freqs.begin();
            sec++;
            int f2=(*sec).second;
            if(f1==1 || f2==1)
                ans=i;
        } else if(freqs.size()==1) {
            ans=(*freqs.begin()).second-1;
        }
    }

    cout<<ans+1<<endl;

}


/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 *
 * AND WATCH OUT FOR OFF BY ONE!!!
 */

#include <bits/stdc++.h>
const bool unsyncedio=std::ios::sync_with_stdio(false);
using namespace std;
typedef unsigned int uint;
typedef long long ll;

#define fori(n) for(ll i=0; i<(n); i++)
#define forn(i, n) for(ll (i)=0; (i)<(n); (i)++)

int main() {
    int n;
    cin>>n;
    vector<int> f(100001);      // freq of color i
    vector<int> ff(100001);     // freq of freq i
    set<int> ffset;   // set of distinct value s in ff, ie existing frequencies

    int ans=0;
    fori(n) {
        int u;
        cin>>u;

        if(ff[f[u]]>0) {
            ff[f[u]]--;
            if(ff[f[u]]==0)
                ffset.erase(f[u]);
        }

        f[u]++;
        ff[f[u]]++;
        ffset.insert(f[u]);

//cout<<"i="<<i<<" ffset.size()="<<ffset.size()<<endl;
        if(ffset.size()==2) {
            int f1=(*ffset.begin());
            auto sec=ffset.begin(); sec++;
            int f2=(*sec);
            int fCount1=ff[f1]; // count of colors wich occur f1 times
            int fCount2=ff[f2];
//cout<<"f1="<<f1<<" fCount1="<<fCount1<<" f2="<<f2<<" fCount2="<<fCount2<<endl;
            if((f1==1 && fCount1==1) || (f2==1 && fCount2==1) ) // Every color has the same occurrence of cats, except for exactly one color which has the occurrence of 1
                ans=i;
            else if((f1-f2==1 && fCount1==1) || (f2-f1==1 && fCount2==1))
                ans=i;
        } else if(ffset.size()==1) {
            auto f1=(*ffset.begin());
            if(ff[f1]==1)     // 1. only one color in this streak
                ans=i;
            else if(f1==1)  // 2. All appeared colors in this streak have the occurrence of 1 (i.e. every color has exactly 1 cat with that color).
                ans=i;
        }

//cases
// 1. one frequency, its == 1
// 2. two frequencies, one of them == 1
// 3. two frequencies, one of them == other+1
    }

    cout<<ans+1<<endl;

}


/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 *
 * AND WATCH OUT FOR OFF BY ONE!!!
 */

#include <bits/stdc++.h>
const bool unsyncedio=std::ios::sync_with_stdio(false);
using namespace std;
typedef unsigned int uint;
typedef long long ll;

#define fori(n) for(ll i=0; i<(n); i++)
#define forn(i, n) for(ll (i)=0; (i)<(n); (i)++)

int main() {
    int n;
    cin>>n;
    vector<int> f(100001);
    unordered_map<int, int> freqs; // maps a frequency to the number of ribbons with that freq

    int ans=0;
    fori(n) {
        int u;
        cin>>u;
        if(freqs.count(f[u])>0) {
            freqs[f[u]]--;
            if(freqs[f[u]]==0)
                freqs.erase(f[u]);
        }
        f[u]++;
        freqs[f[u]]++;

//cout<<"i="<<i<<" freqs.size()="<<freqs.size()<<endl;
        if(freqs.size()==2) {
            auto f1=(*freqs.begin());
            auto sec=freqs.begin();
            sec++;
            auto f2=(*sec);
//cout<<"f1="<<f1<<" f2="<<f2<<endl;
            if(f1.second==1 && (f1.first==f2.first+1 || f1.first==1))
                ans=i;
            else if(f2.second==1 && (f2.first==f1.first+1 || f2.first==1))
                ans=i;
        } else if(freqs.size()==1) {
            auto f1=(*freqs.begin());
            if(f1.first==1)     // all ribbons have frequency == 1
                ans=i;
        }
    }

    cout<<ans+1<<endl;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define MAXN 300003
#define MOD 1000000007
#define  x first
#define  y second
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef cc_hash_table<int, int, hash<int > > ht;

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    ht m;
    int b[n];
    int c[n];
    int d[n];
    int e[n];

    for(int i=0;i<n;i++){

        m[a[i]]++;
        if(m.size()!=1){
           b[i]=(i+1)-(m.size()-1)*(m.begin()->second);
           d[i]=(i+1)-(m.size()-1)*(m[a[i]]);
        }
        c[i]=(i+1)-(m.size())*(m[a[i]]);
        e[i]=(i+1)-(m.size())*(m.begin()->second);

    }
    if(m.size()==1){
        cout<<n;
        return 0;
    }
    for(int i=n-1;i>=0;i--){
        if(b[i]==1 or c[i]==1 or d[i]==1 or e[i]==1){
            cout<<i+1;
            break;
        }
    }
return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define MAXN 300003
#define MOD 1000000007
#define  x first
#define  y second
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef cc_hash_table<int, int, hash<int > > ht;

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    set<int> s;
    int b[n];
    int c[n];
    for(int i=0;i<n;i++){
        s.insert(a[i]);
        if(s.size()!=1)
        b[i]=(i+1)%(s.size()-1);
            c[i]=(i+1)%(s.size());

    }
    for(int i=n-1;i>=0;i--){
        if(b[i]==1 or c[i]==1){
            cout<<i+1;
            break;
        }
    }
return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define MAXN 300003
#define MOD 1000000007
#define  x first
#define  y second
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef cc_hash_table<int, int, hash<int > > ht;

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    set<int> s;
    int b[n];
    int c[n];
    for(int i=0;i<n;i++){
        s.insert(a[i]);
        if(s.size()!=1)
        b[i]=(i+1)%(s.size()-1);
            c[i]=(i+1)%(s.size());

    }
    if(s.size()==1){
        cout<<n;
        return 0;
    }
    for(int i=n-1;i>=0;i--){
        if(b[i]==1 or c[i]==1){
            cout<<i+1;
            break;
        }
    }
return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define MAXN 300003
#define MOD 1000000007
#define  x first
#define  y second
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef cc_hash_table<int, int, hash<int > > ht;

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    map<int,int> m;
    int b[n];
    int c[n];
    int d[n];
    int e[n];
    for(int i=0;i<n;i++){
        m[a[i]]++;
        if(m.size()!=1){
           b[i]=(i+1)-(m.size()-1)*(m.begin()->second);
           d[i]=(i+1)-(m.size()-1)*(m[a[i]]);
        }
        c[i]=(i+1)-(m.size())*(m[a[i]]);
        e[i]=(i+1)-(m.size())*(m.begin()->second);

    }
    if(m.size()==1){
        cout<<n;
        return 0;
    }
    for(int i=n-1;i>=0;i--){
        if(b[i]==1 or c[i]==1 or d[i]==1 or e[i]==1){
            cout<<i+1;
            break;
        }
    }
return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define MAXN 300003
#define MOD 1000000007
#define  x first
#define  y second
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef cc_hash_table<int, int, hash<int > > ht;

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    unordered_map<int,int> m;
    int b[n];
    int c[n];
    int d[n];
    int e[n];

    for(int i=0;i<n;i++){

        m[a[i]]++;
        if(m.size()!=1){
           b[i]=(i+1)-(m.size()-1)*(m.begin()->second);
           d[i]=(i+1)-(m.size()-1)*(m[a[i]]);
        }
        c[i]=(i+1)-(m.size())*(m[a[i]]);
        e[i]=(i+1)-(m.size())*(m.begin()->second);

    }
    if(m.size()==1){
        cout<<n;
        return 0;
    }
    for(int i=n-1;i>=0;i--){
        if(b[i]==1 or c[i]==1 or d[i]==1 or e[i]==1){
            cout<<i+1;
            break;
        }
    }
return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define MAXN 300003
#define MOD 1000000007
#define  x first
#define  y second
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef cc_hash_table<int, int, hash<int > > ht;

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    map<int,int> m;
    int b[n];
    int c[n];

    for(int i=0;i<n;i++){
        m[a[i]]++;
        if(m.size()!=1){
           b[i]=(i+1)-(m.size()-1)*(m.begin()->second);
        }
        c[i]=(i+1)-(m.size())*(m[a[i]]);

    }
    if(m.size()==1){
        cout<<n;
        return 0;
    }
    for(int i=n-1;i>=0;i--){
        if(b[i]==1 or c[i]==1){
            cout<<i+1;
            break;
        }
    }
return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define MAXN 300003
#define MOD 1000000007
#define  x first
#define  y second
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef cc_hash_table<int, int, hash<int > > ht;

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    map<int,int> m;
    int b[n];
    int c[n];
    int d[n];
    for(int i=0;i<n;i++){
        m[a[i]]++;
        if(m.size()!=1){
           b[i]=(i+1)-(m.size()-1)*(m.begin()->second);
           d[i]=(i+1)-(m.size()-1)*(m[a[i]]);
        }
        c[i]=(i+1)-(m.size())*(m[a[i]]);

    }
    if(m.size()==1){
        cout<<n;
        return 0;
    }
    for(int i=n-1;i>=0;i--){
        if(b[i]==1 or c[i]==1 or d[i]==1){
            cout<<i+1;
            break;
        }
    }
return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define MAXN 300003
#define MOD 1000000007
#define  x first
#define  y second
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef cc_hash_table<int, int, hash<int > > ht;

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n;
    cin>>n;
    ht a,b;
    int ans=1;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        a[x]++;
        b[a[x]]++;
        if(a[x]*b[a[x]]==i and i!=n)
            ans=i+1;
        else if(a[x]*b[a[x]]==i-1)
            ans=i;
    }
    cout<<ans;
return 0;
}


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

#define ff first
#define ss second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define check(x) cout << #x << " : " << (x) << endl
#define inf LLONG_MAX
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define endl '\n'
#define mod 1000000007
typedef  long long ll;
typedef  unsigned long long ull;
//using u64 = uint64_t;  // Only SPOJ AND CodeChef
//using u128 = __uint128_t;
typedef vector<ll> edge;
typedef pair< ll , ll > mypair;
typedef priority_queue <ll> max_heap;
typedef priority_queue <ll, vector<ll>, greater<ll> > min_heap;
const ll N=1000000+100;

ll mod_expo(ll a,ll b, ll c)
{
	ll ans=1;
	while(b)
	{
		if(b & (ll)1) ans=(ans*a)%c;
		a=(a*a)%c;
		b=b>>1;
	}
	return ans;
}

ll inverse(ll n, ll c)
{
	return mod_expo(n,c-2,c);
}
int main()
{
	boost;
	ll n;
	cin>>n;
	ll u[n+1];
	std::vector<ll> freq(n+1,0);
	std::map<ll, ll> ma;
	set< ll > s;
	ll ans;
	ll x;
	for(ll i=1;i<=n;i++)
	{
		cin>>x;
		s.insert(x);
		ma[x]++;
		freq[ma[x]]++;
		freq[ma[x]-1]--;
		if(ma[x] == 1 )
		{
			if(freq[ma[x]] == (((ll)s.size()))) ans=i;
			else if( (i-1)%((ll)((ll)s.size()-1)) ==0   &&  freq[(i-1)/((ll)((ll)s.size()-1))] == (((ll)s.size()-1))) ans =i;
			else if(freq[ma[x]] == (((ll)s.size()-1)))
			{
				if(freq[1] == 1  || freq[ma[x]+1]==1)
				{
					ans=i;
				}
			}
		}
		else if(freq[i]==1)
		{
			ans=i;
		}
		else if((ll)s.size() == 2)
		{
			if(freq[ma[x]-1] == 1   ||  freq[ma[x]+1] == 1) ans=i;
		}
		else if(freq[ma[x]] == (((ll)s.size()-1)))
		{
			if(freq[1] == 1  || freq[ma[x]+1]==1)
			{
				ans=i;
			}
		}
		else if(freq[ma[x]] == 1)
		{
			if(freq[ma[x]-1] == (((ll)s.size()-1)))
			{
				ans=i;
			}
		}
	}
	cout<<ans;

	return 0;
}


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

#define ff first
#define ss second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define check(x) cout << #x << " : " << (x) << endl
#define inf LLONG_MAX
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define endl '\n'
#define mod 1000000007
typedef  long long ll;
typedef  unsigned long long ull;
//using u64 = uint64_t;  // Only SPOJ AND CodeChef
//using u128 = __uint128_t;
typedef vector<ll> edge;
typedef pair< ll , ll > mypair;
typedef priority_queue <ll> max_heap;
typedef priority_queue <ll, vector<ll>, greater<ll> > min_heap;
const ll N=1000000+100;

ll mod_expo(ll a,ll b, ll c)
{
	ll ans=1;
	while(b)
	{
		if(b & (ll)1) ans=(ans*a)%c;
		a=(a*a)%c;
		b=b>>1;
	}
	return ans;
}

ll inverse(ll n, ll c)
{
	return mod_expo(n,c-2,c);
}
int main()
{
	boost;
	ll n;
	cin>>n;
	ll u[n+1];
	std::vector<ll> freq(n+1,0);
	std::map<ll, ll> ma;
	set< ll > s;
	ll ans;
	ll x;
	for(ll i=1;i<=n;i++)
	{
		cin>>x;
		s.insert(x);
		ma[x]++;
		freq[ma[x]]++;
		freq[ma[x]-1]--;
		if(ma[x] == 1 )
		{

			if(freq[ma[x]] == (((ll)s.size()))) ans=i;
			else if( (i-1)%((ll)((ll)s.size()-1)) ==0   &&  freq[(i-1)/((ll)((ll)s.size()-1))] == (((ll)s.size()-1))) ans =i;
		else if(freq[ma[x]] == (((ll)s.size()-1)))
		{
			if(freq[1] == 1  || freq[ma[x]+1]==1)
			{
				ans=i;
			}
		}
		}
		else if(freq[ma[x]] == (((ll)s.size()-1)))
		{
			if(freq[1] == 1  || freq[ma[x]+1]==1)
			{
				ans=i;
			}
		}
		else if(freq[ma[x]] == 1)
		{
			if(freq[ma[x]-1] == (((ll)s.size()-1)))
			{
				ans=i;
			}
		}
	}
	cout<<ans;

	return 0;
}


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

#define ff first
#define ss second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define check(x) cout << #x << " : " << (x) << endl
#define inf LLONG_MAX
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define endl '\n'
#define mod 1000000007
typedef  long long ll;
typedef  unsigned long long ull;
//using u64 = uint64_t;  // Only SPOJ AND CodeChef
//using u128 = __uint128_t;
typedef vector<ll> edge;
typedef pair< ll , ll > mypair;
typedef priority_queue <ll> max_heap;
typedef priority_queue <ll, vector<ll>, greater<ll> > min_heap;
const ll N=100000;

ll mod_expo(ll a,ll b, ll c)
{
	ll ans=1;
	while(b)
	{
		if(b & (ll)1) ans=(ans*a)%c;
		a=(a*a)%c;
		b=b>>1;
	}
	return ans;
}

ll inverse(ll n, ll c)
{
	return mod_expo(n,c-2,c);
}
int main()
{
	boost;
	ll n;
	cin>>n;
	std::vector<ll> color(N+1,0);// color[i]: no elements with color i
	std::vector<ll> freq(n+1,0);// freq[j]: number of colors with  such that  (no elements with color i) =j i.e color[i]=j
	ll ans,count=0,mx=0; // count : no of distinct colors   ,,,, mx : maximum frequency of any color
	for(ll i=1;i<=n;i++)
	{
		ll x;
		cin>>x;
		color[x]++;
		freq[color[x]]++;
		freq[color[x]-1]--;
		if(color[x] == 1) count++;
		mx=max(mx,color[x]);
		if(freq[1]==i) ans=i;
		else if(color[x]==1   && freq[mx]==(count-1)) ans=i;
		else if (freq[color[x]] == (count-1)   &&  (freq[color[x]+1]==1  || freq[1] ==1)) ans=i;
		else if(freq[color[x]]==1  && freq[color[x]-1]==(count-1)) ans=i;

	}
	cout<<ans;
	return 0;
}


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

#define ff first
#define ss second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define check(x) cout << #x << " : " << (x) << endl
#define inf LLONG_MAX
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define endl '\n'
#define mod 1000000007
typedef  long long ll;
typedef  unsigned long long ull;
//using u64 = uint64_t;  // Only SPOJ AND CodeChef
//using u128 = __uint128_t;
typedef vector<ll> edge;
typedef pair< ll , ll > mypair;
typedef priority_queue <ll> max_heap;
typedef priority_queue <ll, vector<ll>, greater<ll> > min_heap;
const ll N=1000000+100;

ll mod_expo(ll a,ll b, ll c)
{
	ll ans=1;
	while(b)
	{
		if(b & (ll)1) ans=(ans*a)%c;
		a=(a*a)%c;
		b=b>>1;
	}
	return ans;
}

ll inverse(ll n, ll c)
{
	return mod_expo(n,c-2,c);
}
int main()
{
	boost;
	ll n;
	cin>>n;
	ll u[n+1];
	std::vector<ll> freq(n+1,0);
	std::map<ll, ll> ma;
	set< ll > s;
	ll ans;
	ll x;
	for(ll i=1;i<=n;i++)
	{
		cin>>x;
		s.insert(x);
		ma[x]++;
		freq[ma[x]]++;
		freq[ma[x]-1]--;
		if(ma[x] == 1 )
		{
			if(freq[ma[x]] == (((ll)s.size()))) ans=i;
			else if( (i-1)%((ll)((ll)s.size()-1)) ==0   &&  freq[(i-1)/((ll)((ll)s.size()-1))] == (((ll)s.size()-1))) ans =i;
			else if(freq[ma[x]] == (((ll)s.size()-1)))
			{
				if(freq[1] == 1  || freq[ma[x]+1]==1)
				{
					ans=i;
				}
			}
		}
		else if(freq[i]==1)
		{
			ans=i;
		}

		else if(freq[ma[x]] == (((ll)s.size()-1)))
		{
			if(freq[1] == 1  || freq[ma[x]+1]==1)
			{
				ans=i;
			}
		}
		else if(freq[ma[x]] == 1)
		{
			if(freq[ma[x]-1] == (((ll)s.size()-1)))
			{
				ans=i;
			}
		}
	}
	cout<<ans;

	return 0;
}


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

#define ff first
#define ss second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define check(x) cout << #x << " : " << (x) << endl
#define inf LLONG_MAX
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define endl '\n'
#define mod 1000000007
typedef  long long ll;
typedef  unsigned long long ull;
//using u64 = uint64_t;  // Only SPOJ AND CodeChef
//using u128 = __uint128_t;
typedef vector<ll> edge;
typedef pair< ll , ll > mypair;
typedef priority_queue <ll> max_heap;
typedef priority_queue <ll, vector<ll>, greater<ll> > min_heap;
const ll N=1000000+100;

ll mod_expo(ll a,ll b, ll c)
{
	ll ans=1;
	while(b)
	{
		if(b & (ll)1) ans=(ans*a)%c;
		a=(a*a)%c;
		b=b>>1;
	}
	return ans;
}

ll inverse(ll n, ll c)
{
	return mod_expo(n,c-2,c);
}
int main()
{
	boost;
	ll n;
	cin>>n;
	ll u[n+1];
	std::vector<ll> freq(n+1,0);
	std::map<ll, ll> ma;
	set< ll > s;
	ll ans;
	ll x;
	for(ll i=1;i<=n;i++)
	{
		cin>>x;
		s.insert(x);
		ma[x]++;
		freq[ma[x]]++;
		freq[ma[x]-1]--;
		if(ma[x] == 1 )
		{
			if(freq[ma[x]] == (((ll)s.size()))) ans=i;
			else if( (i-1)%((ll)((ll)s.size()-1)) ==0   &&  freq[(i-1)/((ll)((ll)s.size()-1))] == (((ll)s.size()-1))) ans =i;
			else if(freq[ma[x]] == (((ll)s.size()-1)))
			{
				if(freq[1] == 1  || freq[ma[x]+1]==1)
				{
					ans=i;
				}
			}
		}
		else if(freq[i]==1)
		{
			ans=i;
		}
		else if(freq[ma[x]] == 1)
		{
			if(freq[ma[x]-1] == (((ll)s.size()-1)))
			{
				ans=i;
			}
		}
		else if(freq[ma[x]] == (((ll)s.size()-1)))
		{
			if(freq[1] == 1  || freq[ma[x]+1]==1)
			{
				ans=i;
			}
		}

	}
	cout<<ans;

	return 0;
}


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

#define ff first
#define ss second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define check(x) cout << #x << " : " << (x) << endl
#define inf LLONG_MAX
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define endl '\n'
#define mod 1000000007
typedef  long long ll;
typedef  unsigned long long ull;
//using u64 = uint64_t;  // Only SPOJ AND CodeChef
//using u128 = __uint128_t;
typedef vector<ll> edge;
typedef pair< ll , ll > mypair;
typedef priority_queue <ll> max_heap;
typedef priority_queue <ll, vector<ll>, greater<ll> > min_heap;
const ll N=1000000+100;

ll mod_expo(ll a,ll b, ll c)
{
	ll ans=1;
	while(b)
	{
		if(b & (ll)1) ans=(ans*a)%c;
		a=(a*a)%c;
		b=b>>1;
	}
	return ans;
}

ll inverse(ll n, ll c)
{
	return mod_expo(n,c-2,c);
}
int main()
{
	boost;
	ll n;
	cin>>n;
	ll u[n+1];
	std::vector<ll> freq(n+1,0);
	std::map<ll, ll> ma;
	set< ll > s;
	ll ans;
	ll x;
	for(ll i=1;i<=n;i++)
	{
		cin>>x;
		s.insert(x);
		ma[x]++;
		freq[ma[x]]++;
		freq[ma[x]-1]--;
		if(ma[x] == 1 )
		{
			if(freq[ma[x]] == (((ll)s.size()))) ans=i;
		}
		else if(freq[i]==1)
		{
			ans=i;
		}

		else if(freq[ma[x]] == (((ll)s.size()-1)))
		{
			if(freq[1] == 1  || freq[ma[x]+1]==1)
			{
				ans=i;
			}
		}
		else if(freq[ma[x]] == 1)
		{
			if(freq[ma[x]-1] == (((ll)s.size()-1)))
			{
				ans=i;
			}
		}
	}
	cout<<ans;

	return 0;
}


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

int main()
{
    unordered_map<long long int,long long int> m1,m2;
    long long int n;
    cin>>n;
    vector<long long int> arr(n,0);
    for(long long int i=0;i<n;i++)
        cin>>arr[i];
    long long int maxfreq=INT_MIN,ans=INT_MIN;
    for(long long int i=0;i<n;i++)
    {
        m1[arr[i]]++;
        maxfreq=max(maxfreq,m1[arr[i]]);
        if(m2[m1[arr[i]]-1])m2[m1[arr[i]]-1]--;
        m2[m1[arr[i]]]++;
        if(m2[1]==i+1 || m1.size()==1)
            ans=i+1;
        else if(m2[maxfreq]*maxfreq==i)ans=i+1;
        else if(m2[maxfreq-1]*(maxfreq-1)+m2[maxfreq] == i+1)ans=i+1;
        cout<<ans<<endl;

    }

    cout<<ans<<endl;

}


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

int main()
{
    unordered_map<long long int,long long int> m1,m2;
    long long int n;
    cin>>n;
    vector<long long int> arr(n,0);
    for(long long int i=0;i<n;i++)
        cin>>arr[i];
    long long int maxfreq=INT_MIN,ans=INT_MIN;
    for(long long int i=0;i<n;i++)
    {
        m1[arr[i]]++;
        maxfreq=max(maxfreq,m1[arr[i]]);
        if(m2[m1[arr[i]]-1])m2[m1[arr[i]]-1]--;
        m2[m1[arr[i]]]++;
        if(m2[1]==i+1 || m1.size()==1)
            ans=i+1;
        else if(m2[maxfreq]*maxfreq==i)ans=i+1;
        else if(m2[maxfreq-1]*(maxfreq-1)+m2[maxfreq]*maxfreq == i+1)ans=i+1;

    }

    cout<<ans<<endl;

}


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

int main()
{
    unordered_map<long long int,long long int> m1,m2;
    long long int n;
    cin>>n;
    vector<long long int> arr(n,0);
    for(long long int i=0;i<n;i++)
        cin>>arr[i];
    long long int maxfreq=INT_MIN,ans=INT_MIN;
    for(long long int i=0;i<n;i++)
    {
        m1[arr[i]]++;
        maxfreq=max(maxfreq,m1[arr[i]]);
        if(m2[m1[arr[i]]-1])m2[m1[arr[i]]-1]--;
        m2[m1[arr[i]]]++;
        if(m2[1]==i+1 || m1.size()==1)
            ans=i+1;
        else if(m2[maxfreq]*maxfreq==i)ans=i+1;
        else if(m2[maxfreq-1]*(maxfreq-1)+m2[maxfreq] == i+1)ans=i+1;

    }

    cout<<ans<<endl;

}


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

int main()
{
    unordered_map<long long int,long long int> m1,m2;
    long long int n;
    cin>>n;
    vector<long long int> arr(n,0);
    for(long long int i=0;i<n;i++)
        cin>>arr[i];
    long long int maxfreq=INT_MIN,ans=INT_MIN;
    for(long long int i=0;i<n;i++)
    {
        m1[arr[i]]++;
        maxfreq=max(maxfreq,m1[arr[i]]);

        if(m2[m1[arr[i]]-1])m2[m1[arr[i]]-1]--;
        m2[m1[arr[i]]]++;
        if(m2[1]==i+1 || m1.size()==1)
            ans=i+1;
        else if(m2[maxfreq]*maxfreq==i-1)ans=i+1;
        else if(m2[maxfreq-1]*(maxfreq-1)+m2[maxfreq] == i)ans=i+1;

    }

    cout<<ans<<endl;

}


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

int main()
{
    unordered_map<long long int,long long int> m1,m2;
    long long int n;
    cin>>n;
    vector<long long int> arr(n,0);
    for(long long int i=0;i<n;i++)
        cin>>arr[i];
    long long int maxfreq=INT_MIN,ans=INT_MIN;
    for(long long int i=0;i<n;i++)
    {
        m1[arr[i]]++;
        maxfreq=max(maxfreq,m1[arr[i]]);
        if(m2[m1[arr[i]]-1])m2[m1[arr[i]]-1]--;
        m2[m1[arr[i]]]++;
        if(m2[1]==i+1 || m1.size()==1)
            ans=i+1;
        else if(m2[maxfreq]*maxfreq==i)ans=i+1;
        else if(m2[maxfreq-1]*(maxfreq-1)+maxfreq == i+1)ans=i+1;
    }

    cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const int inf = 0x3f3f3f3f;
const ll linf = 123456789012345678;
const ll mod = 1000000007;
#pragma GCC optimize("Ofast")
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl

int main(){
    cin.sync_with_stdio(false);
    cin.tie(0);

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

    int ans = 0;
    int lo = inf, locount = inf;
    int hi = -inf, hicount = -inf;
    vi b(100010);
    vi c(100010);
    for(int i = 0; i < n; i++){
        c[b[a[i]]]--;
        c[b[a[i]]+1]++;
        b[a[i]]++;
        if(b[a[i]] < lo){
            lo = b[a[i]];
            locount = c[b[a[i]]];
        }
        else if(b[a[i]] == lo) locount++;
        else if(b[a[i]]-1 == lo && locount == 1)
            lo = b[a[i]], locount = c[b[a[i]]];
        if(b[a[i]] > hi){
            hi = b[a[i]];
            hicount = c[b[a[i]]];
        }
        else if(b[a[i]] == hi) hicount++;
        if(hi == lo+1 && hicount == 1) ans = i;
        if(hicount*hi == i) ans = i;
        if(hi == 1) ans = i;
        if(hi == i+1) ans = i;
    }

    cout << ans+1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const int inf = 0x3f3f3f3f;
const ll linf = 123456789012345678;
const ll mod = 1000000007;
#pragma GCC optimize("Ofast")
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl

int main(){
    cin.sync_with_stdio(false);
    cin.tie(0);

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

    int ans = 0;
    int lo = inf, locount = inf;
    int hi = -inf, hicount = -inf;
    vi b(100010);
    for(int i = 0; i < n; i++){
        b[a[i]]++;
        if(b[a[i]] < lo){
            lo = b[a[i]];
            locount = 1;
        }
        else if(b[a[i]] == lo) locount++;
        else if(b[a[i]]-1 == lo && locount == 1)
            lo = b[a[i]];
        if(b[a[i]] > hi){
            hi = b[a[i]];
            hicount = 1;
        }
        else if(b[a[i]] == hi) hicount++;
        if(hi == lo+1 && hicount == 1) ans = i;
        if(hicount*hi == i && lo == 1) ans = i;
        if(hi == 1) ans = i;
        if(hi == i+1) ans = i;
    }

    cout << ans+1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const int inf = 0x3f3f3f3f;
const ll linf = 123456789012345678;
const ll mod = 1000000007;
#pragma GCC optimize("Ofast")
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl

int main(){
    cin.sync_with_stdio(false);
    cin.tie(0);

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

    int ans = 0;
    int lo = inf, locount = inf;
    int hi = -inf, hicount = -inf;
    vi b(100010);
    for(int i = 0; i < n; i++){
        b[a[i]]++;
        if(b[a[i]] < lo){
            lo = b[a[i]];
            locount = 1;
        }
        else if(b[a[i]] == lo) locount++;
        else if(b[a[i]]-1 == lo && locount == 1)
            lo = b[a[i]];
        if(b[a[i]] > hi){
            hi = b[a[i]];
            hicount = 1;
        }
        else if(b[a[i]] == hi) hicount++;
        if(hi == lo+1 && hicount == 1) ans = i;
        if(hicount*hi == i && lo == 1) ans = i;
        if(hi == 1) ans = i;
        if(hicount*hi == i+1) ans = i;
    }

    cout << ans+1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const int inf = 0x3f3f3f3f;
const ll linf = 123456789012345678;
const ll mod = 1000000007;
#pragma GCC optimize("Ofast")
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl

int main(){
    cin.sync_with_stdio(false);
    cin.tie(0);

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

    int ans = 0;
    int lo = inf, locount = inf;
    int hi = -inf, hicount = -inf;
    vi b(100010);
    for(int i = 0; i < n; i++){
        b[a[i]]++;
        if(b[a[i]] < lo){
            lo = b[a[i]];
            locount = 1;
        }
        else if(b[a[i]] == lo) locount++;
        else if(b[a[i]]-1 == lo && locount == 1)
            lo = b[a[i]];
        if(b[a[i]] > hi){
            hi = b[a[i]];
            hicount = 1;
        }
        else if(b[a[i]] == hi) hicount++;
        if(hi == lo+1 && hicount == 1) ans = i;
        if(hicount*hi == i && lo == 1) ans = i;
        if(hi == 1) ans = i;
    }

    cout << ans+1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const int inf = 0x3f3f3f3f;
const ll linf = 123456789012345678;
const ll mod = 1000000007;
#pragma GCC optimize("Ofast")
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl

int main(){
    cin.sync_with_stdio(false);
    cin.tie(0);

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

    int ans = 0;
    int lo = inf, locount = inf;
    int hi = -inf, hicount = -inf;
    vi b(100010);
    vi c(100010);
    for(int i = 0; i < n; i++){
        c[b[a[i]]]--;
        c[b[a[i]]+1]++;
        b[a[i]]++;
        if(b[a[i]] < lo){
            lo = b[a[i]];
            locount = c[b[a[i]]];
        }
        else if(b[a[i]] == lo) locount++;
        else if(b[a[i]]-1 == lo && locount == 1)
            lo = b[a[i]], locount = c[b[a[i]]];
        else if(b[a[i]]-1 == lo) locount--;
        if(b[a[i]] > hi){
            hi = b[a[i]];
            hicount = c[b[a[i]]];
        }
        else if(b[a[i]] == hi) hicount++;
        if(hi == lo+1 && hicount == 1) ans = i;
        if(hicount*hi == i) ans = i;
        if(hi == 1) ans = i;
        if(hi == i+1) ans = i;
    }

    cout << ans+1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const int inf = 0x3f3f3f3f;
const ll linf = 123456789012345678;
const ll mod = 1000000007;
#pragma GCC optimize("Ofast")
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl

int main(){
    cin.sync_with_stdio(false);
    cin.tie(0);

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

    int ans = 0;
    int lo = inf, locount = inf;
    int hi = -inf, hicount = -inf;
    vi b(100010);
    for(int i = 0; i < n; i++){
        if(lo == hi && b[a[i]] == 0) ans = i;
        b[a[i]]++;
        if(b[a[i]] < lo){
            lo = b[a[i]];
            locount = 1;
        }
        else if(b[a[i]] == lo) locount++;
        else if(b[a[i]]-1 == lo && locount == 1)
            lo = b[a[i]];
        if(b[a[i]] > hi){
            hi = b[a[i]];
            hicount = 1;
        }
        else if(b[a[i]] == hi) hicount++;
        if(hi == lo+1 && hicount == 1) ans = i;
    }

    cout << ans+1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const int inf = 0x3f3f3f3f;
const ll linf = 123456789012345678;
const ll mod = 1000000007;
#pragma GCC optimize("Ofast")
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl

int main(){
    cin.sync_with_stdio(false);
    cin.tie(0);

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

    int ans = 0;
    int lo = inf, locount = inf;
    int hi = -inf, hicount = -inf;
    vi b(100010);
    for(int i = 0; i < n; i++){
        b[a[i]]++;
        if(b[a[i]] < lo){
            lo = b[a[i]];
            locount = 1;
        }
        else if(b[a[i]] == lo) locount++;
        else if(b[a[i]]-1 == lo && locount == 1)
            lo = b[a[i]];
        if(b[a[i]] > hi){
            hi = b[a[i]];
            hicount = 1;
        }
        else if(b[a[i]] == hi) hicount++;
        if(hi == lo+1 && hicount == 1) ans = i;
        if(hicount*hi == i && lo == 1) ans = i;
        if(hi == 1) ans = i;
        if(hicount == 1) ans = i;
    }

    cout << ans+1 << endl;
}


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

map<int , int>num , occur;
map<int , int>:: iterator it;

void process(int n)
{
    if(num.find(n) == num.end()) {
        num[n] = 1;
        occur[1]++;
        return;
    }
    int prv = num[n];
    occur[prv]--;
    if(occur[prv] == 0) occur.erase(prv);

    num[n]++;
    int now = num[n];
    if(occur.find(now) == occur.end()) occur[now] = 0;
    occur[now]++;
}

bool check()
{
    if(occur.size() > 2) return false;
    if(occur.size() == 1){
        it = occur.begin();
        int a = it->first;
        int b = it->second;

        if(a == 1 || b == 1) return true;
        return false;
    }
    if(occur.size() == 2){
        it = occur.begin();
        pair<int , int>a = make_pair(it->first , it->second);
        it++;
        pair<int , int>b = make_pair(it->first , it->second);

        if(a.second > b.second) swap(a , b);

        if(a.second == 1){
            if(a.first == 1) return true;
            if(abs(a.first - b.first) == 1) return true;
        }

        return false;
    }
}

int main()
{
    int N , a;
    while(cin>>N){
        num.clear();
        occur.clear();
        occur[1] = 0;
        int ans = 0;

        for(int i = 1 ; i<=N ; i++){
            scanf("%d" , &a);
            process(a);
            bool flag = check();
            if(flag) ans = i;
        }
        printf("%d\n" , ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define io_speed_up ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define per(i,a,b) for (int i=a;i>=b;i--)
#define ms(a,b)  memset(a,b,sizeof a)
#define sz(x) (int)x.size()
using ll = long long;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+5;
const int mod = 1e9+7;
using pii = pair<int,int>;
pii ans[maxn*5];
int tot;
list<int> lst[20];
void solve(int l,int r) {
    if(l==r) return;
    int mid=(l+r)>>1;
    solve(l,mid);
    solve(mid+1,r);
    rep(i,l,mid) ans[++tot] = {i, mid + 1 + i - l};
}
int main()
{
    io_speed_up;
    int n;
    cin>>n;
    int s = 1,N,num,mn=inf;
    rep(i,0,20) {
        if(n&(1<<i)) {
            num++;
            N = i;
            mn = min(mn,i);
            solve(s,s+(1<<i) - 1);
            rep(j,s,s+(1<<i) - 1) lst[i].push_back(j);
            s += (1<<i);
        }
    }
    list<int> g = lst[mn];
    while(num>2) {
        int k = sz(g);
        auto it = g.begin(), it2 = lst[N].begin();
        while(k--) {
            ans[++tot] = {*it,*it2};
            g.push_back(*it2);
            lst[N].erase(it2++);
            it++;
        }
        mn++;
        while(num>2 && !lst[mn].empty()) {
            k = sz(g);
            it = g.begin(), it2 = lst[mn].begin();
            while(k--) {
                ans[++tot] = {*it,*it2};
                g.push_back(*it2);
                it++;it2++;
            }
            mn++;
            num--;
        }
    }
    cout<<tot<<"\n";
    rep(i,1,tot)
        cout<<ans[i].first<<" "<<ans[i].second<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define io_speed_up ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define per(i,a,b) for (int i=a;i>=b;i--)
#define ms(a,b)  memset(a,b,sizeof a)
#define sz(x) (int)x.size()
using ll = long long;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+5;
const int mod = 1e9+7;
using pii = pair<int,int>;
pii ans[maxn];
int tot;
list<int> lst[20];
void solve(int l,int r) {
    if(l==r) return;
    int mid=(l+r)>>1;
    solve(l,mid);
    solve(mid+1,r);
    rep(i,l,mid) ans[++tot] = {i, mid + 1 + i - l};
}
int main()
{
    io_speed_up;
    int n;
    cin>>n;
    int s = 1,N,num,mn=inf;
    rep(i,0,20) {
        if(n&(1<<i)) {
            num++;
            N = i;
            mn = min(mn,i);
            solve(s,s+(1<<i) - 1);
            rep(j,s,s+(1<<i) - 1) lst[i].push_back(j);
            s += (1<<i);
        }
    }
    list<int> g = lst[mn];
    while(num>2) {
        int k = sz(g);
        auto it = g.begin(), it2 = lst[N].begin();
        while(k--) {
            ans[++tot] = {*it,*it2};
            g.push_back(*it2);
            lst[N].erase(it2++);
            it++;
        }
        mn++;
        while(num>2 && !lst[mn].empty()) {
            k = sz(g);
            it = g.begin(), it2 = lst[mn].begin();
            while(k--) {
                ans[++tot] = {*it,*it2};
                g.push_back(*it2);
                it++;it2++;
            }
            mn++;
            num--;
        }
    }
    cout<<tot<<"\n";
    rep(i,1,tot)
        cout<<ans[i].first<<" "<<ans[i].second<<"\n";
    return 0;
}


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

const int N=5e5+5;
int n,lg[N],tot;
pair<int,int> a[N];

void solve(int l,int r)
{
    if(l==r) return;
    int mid=l+r>>1;
    solve(l,mid),solve(mid+1,r);
    for(int i=l;i<=mid;++i) a[++tot]={i,i-l+mid+1};
}

int main()
{
    scanf("%d",&n),lg[1]=1;
    for(int i=2;i<=n;++i) lg[i]=lg[i>>1]<<1;
    solve(1,lg[n]),solve(n-lg[n]+1,n),printf("%d\n",tot);
    for(int i=1;i<=tot;++i) printf("%d %d\n",a[i].first,a[i].second);
    return 0;
}


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

const int N=5e5+5;
int n,lg[N],tot;
pair<int,int> a[N];

void solve(int l,int r)
{
    if(l==r) return;
    int mid=l+r>>1;
    solve(l,mid),solve(mid+1,r);
    for(int i=l;i<=mid;++i) a[++tot]={i,i-l+mid+1};
}

int main()
{
    scanf("%d",&n),lg[1]=1;
    for(int i=2;i<=n;++i) lg[i]=lg[i>>1]<<1;
    solve(1,lg[n]),solve(n-lg[n]+1,n);
    for(int i=1;i<=tot;++i) printf("%d %d\n",a[i].first,a[i].second);
    return 0;
}


//xtwakioi! xtwddYnoi(双重含义)!
#include <bits/stdc++.h>
#define ri register
#define int long long
#define E (n+1)
#define mk make_pair
using namespace std; const int N=16000;
inline int read()
{
    int s=0, w=1; ri char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
    return s*w;
}
int n,lg[N],cnt;
pair<int,int> ot[N*50];
void F(int l,int r)
{
	if(l==r) return;
	int mid=(l+r)/2;
	F(l,mid), F(mid+1,r);
	for(ri int i=l;i<=mid;i++) ot[++cnt]=mk(i,i+mid+1-l);
}
signed main()
{
	n=read();
	lg[1]=1;
	for(ri int i=2;i<=n;i++) lg[i]=(lg[i>>1])+1;
	F(1,lg[n]), F(n-lg[n]+1,n);
	printf("%lld\n",cnt);
	for(ri int i=1;i<=cnt;i++) printf("%lld %lld\n",ot[i].first,ot[i].second);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define sc(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ll long long
#define pb push_back
vector<pair<int,int> >ans;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void merge(int l,int r,int lev)
{
	if(lev==0)
	{
		ans.pb({l,r});
		return;
	}
	int mid=(l+r)>>1;
	merge(l,mid,lev-1);
	merge(mid+1,r,lev-1);
	int j=mid+1;
	for(int i=l;i<=mid;i++)
	{
		ans.pb({i,j});
		j++;
	}
}

int main(int argc, char** argv) {
	int n;
	sc(n);
	if(n==1)
	{
	printf("0");
	return 0;
	}
	int t=1;
	int lev=-1;
	while(t*2<=n)t*=2,lev++;
	//cout<<t<<endl;
	//cout<<t<<endl;
	merge(1,t,lev);
	merge(n-t+1,n,lev);
        cout<<ans.size()<<endl;
	rep(i,0,ans.size()-1)
	{
		printf("%d %d\n",ans[i].first,ans[i].second);
	}
	return 0;
}


#include<stdio.h>

int n,a[500002],b[500002],cnt;

inline void solve(int l,int r)
{
	if(l==r) return;
	int len=r-l+1,mid=(l+r)>>1;
	solve(l,mid),solve(mid+1,r);
	for(int i=0;i<len>>1;i++)
		a[++cnt]=l+i,b[cnt]=mid+1+i;
}

int main()
{
	scanf("%d",&n);
	int k=1;
	while(k<<1<n) k<<=1;
	solve(1,k),solve(n-k+1,n);
	printf("%d\n",cnt);
	for(int i=1;i<=cnt;i++)
		printf("%d %d\n",a[i],b[i]);
	return 0;
}


#include<stdio.h>

int n,a[500002],b[500002],cnt;

inline void solve(int l,int r)
{
	if(l==r) return;
	int len=r-l+1,mid=(l+r)>>1;
	solve(l,mid),solve(mid+1,r);
	for(int i=0;i<len>>1;i++)
		a[++cnt]=l+i,b[cnt]=mid+1+i;
}

int main()
{
	scanf("%d",&n);
	int k=1;
	while(k<<1<n) k<<=1;
	solve(1,k),solve(n-k+1,n);
	for(int i=1;i<=cnt;i++)
		printf("%d %d\n",a[i],b[i]);
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

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

void solve(int l, int r)
{   int put2 = 1, dif = r - l + 1;
    while((1 << (put2 - 1)) < dif)
        {   int idx = l, nr = 0;
            while(idx <= r)
                {   if(nr == (1 << (put2 - 1))) nr = 0, idx += (1 << (put2 - 1));
                    else
                        {   ans.push_back({idx, idx + (1 << (put2 - 1))});
                            idx++;
                            nr++;
                        }
                }
            put2++;
        }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    if(n == 1) cout << 0 << '\n';
    if(!(n & (n-1))) solve(1, n);
    else
        {   int put2 = 1;
            while((1 << put2) < n) put2++;
            solve(1, put2);
            solve(n - put2 + 1, n);
        }
    cout << ans.size() << '\n';
    for(int i=0; i<ans.size(); i++) cout << ans[i].first << ' ' << ans[i].second << '\n';
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

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

void solve(int l, int r)
{   int put2 = 1, dif = r - l + 1;
    while((1 << (put2 - 1)) < dif)
        {   int idx = l, nr = 0;
            while(idx <= r)
                {   if(nr == (1 << (put2 - 1))) nr = 0, idx += (1 << (put2 - 1));
                    else
                        {   ans.push_back({idx, idx + (1 << (put2 - 1))});
                            idx++;
                            nr++;
                        }
                }
            put2++;
        }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    if(n == 1)
        {   cout << 0 << '\n';
            return 0;
        }
    if(!(n & (n-1))) solve(1, n);
    else
        {   int put2 = 1;
            while((1 << put2) < n) put2++;
            solve(1, put2);
            solve(n - put2 + 1, n);
        }
    cout << ans.size() << '\n';
    for(int i=0; i<ans.size(); i++) cout << ans[i].first << ' ' << ans[i].second << '\n';
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

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

void solve(int l, int r)
{   //cout << l << ' ' << r << '*' << '\n';
    int put2 = 1, dif = r - l + 1;
    while((1 << (put2 - 1)) < dif)
        {   int idx = l, nr = 0;
            while(idx <= r)
                {   if(nr == (1 << (put2 - 1))) nr = 0, idx += (1 << (put2 - 1));
                    else
                        {   ans.push_back({idx, idx + (1 << (put2 - 1))});
                            idx++;
                            nr++;
                        }
                }
            put2++;
        }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    if(n == 1)
        {   cout << 0 << '\n';
            return 0;
        }
    if(!(n & (n-1))) solve(1, n);
    else
        {   int put2 = 1;
            while((1 << put2) < n) put2++;
            put2--;
            solve(1, (1 << put2));
            solve(n - (1 << put2) + 1, n);
        }
    cout << ans.size() << '\n';
    for(int i=0; i<ans.size(); i++) cout << ans[i].first << ' ' << ans[i].second << '\n';
    return 0;
}


//xtwakioi! xtwddYnoi(双重含义)!
#include <bits/stdc++.h>
#define ri register
#define int long long
#define E (n+1)
#define mk make_pair
using namespace std; const int N=16000;
inline int read()
{
    int s=0, w=1; ri char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
    return s*w;
}
int n,lg[N],cnt;
pair<int,int> ot[N*50];
void F(int l,int r)
{
	if(l==r) return;
	int mid=(l+r)/2;
	F(l,mid), F(mid+1,r);
	for(ri int i=l;i<=mid;i++) ot[++cnt]=mk(i,mid-l+i+1);
}
signed main()
{
	n=read();
	lg[1]=1;
	for(ri int i=2;i<=n;i++) lg[i]=(lg[i>>1])*2;
	F(1,lg[n]), F(n-lg[n]+1,n);
	printf("%lld\n",cnt);
	for(ri int i=1;i<=cnt;i++) printf("%lld %lld\n",ot[i].first,ot[i].second);
    return 0;
}