#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define INF 0x3f3f3f3f3f3f3f3fLL

#define MOD 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define x first

#define y second

#define eps 1e-8

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<typename T> inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}

template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}

template<typename T> inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<typename T> inline void println(T x) {print(x), putchar('\n');}

template<typename T> inline void printsp(T x) {print(x), putchar(' ');}

template<class T, class T1> inline void print(T x, T1 y) {printsp(x), println(y);}

template<class T, class T1, class T2> inline void print(T x, T1 y, T2 z) {printsp(x), printsp(y), println(z);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 2020

int a[NN], b[NN];

int dp[NN][716];

int st[NN], cnt, id[10000];

int get(int *d){

	return id[d[0]*1000+d[1]*100+d[2]*10+d[3]];

}

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n;

	gn(n);

	for(int i=1; i<=n; i++) gn(a[i], b[i]), a[i], b[i];

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

		int d[4]={i/1000, i%1000/100, i%100/10, i%10};

		int ok=1;

		for(int j=0; j<3; j++) if(d[j]>d[j+1]) ok=0;

		if(ok) st[cnt]=i, id[i]=cnt++;

	}

	memset(dp, 0x3f, sizeof(dp));

	dp[1][id[b[1]]]=a[1]-1;

	a[n+1]=b[n];

	b[n+1]=0;

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

		for(int j=0; j<cnt; j++){

			int s=st[j];

			if(dp[i][j]==inf) continue;

			for(int p1=1; p1<=9; p1++){

				for(int p2=1; p2<=9; p2++){

					int dis=abs(a[i]-p1)+abs(p1-p2)+abs(p2-a[i+1]);

					int mn=min(min(a[i], a[i+1]), min(p1, p2));

					int mx=max(max(a[i], a[i+1]), max(p1, p2));

					int d[4]={s/1000, s%1000/100, s%100/10, s%10};

					for(int k=0; k<4; k++) if(d[k]>=mn && d[k]<=mx) d[k]=0;

					sort(d, d+4);

					if(d[0]==0){

						d[0]=b[i+1];

						sort(d, d+4);

						smin(dp[i+1][get(d)], dp[i][j]+dis);

					}

				}

			}

		}

	}

	int ans=inf;

	for(int j=0; j<cnt; j++){

		int s=st[j];

		if(dp[n][j]==inf) continue;

		for(int p1=1; p1<=9; p1++){

			for(int p2=1; p2<=9; p2++){

				int dis=abs(a[n]-p1)+abs(p1-p2);

				int mn=min(a[n], min(p1, p2));

				int mx=max(a[n], max(p1, p2));

				int d[4]={s/1000, s%1000/100, s%100/10, s%10};

				for(int k=0; k<4; k++) if(d[k]>=mn && d[k]<=mx) d[k]=0;

				sort(d, d+4);

				if(d[3]==0){

					smin(ans, dp[n][j]+dis);

				}

			}

		}

	}

	println(ans+n*2);

	return 0;

}


#include <bits/stdc++.h>

#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x <  b; ++x)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl; }
#define PR0(A, n) { cout << #A << " = "; REP(_, 0, n) cout << A[_] << " "; cout << endl; }
#define BitCount(x) __builtin_popcount(x)

using namespace std;

typedef unsigned long long LL;
typedef pair <int, int> II;

const int N = 2e3 + 10;
const int INF = 0x3f3f3f3f;

int n;
int a[N], b[N], pw[5];

int d[N][10000][10];

struct State {
    int i, mask, j;
    State () {}
    State (int i, int mask, int j) : i(i), mask(mask), j(j) {}
};

int main() {
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // LOCAL
    scanf("%d", &n);
    FOR(i, 1, n) scanf("%d%d", &a[i], &b[i]);
    memset(d, INF, sizeof d);
    d[1][0][1] = 0;
    queue <State> Q;
    Q.push(State(1, 0 ,1));
    pw[0] = 1;
    FOR(i, 1, 4) pw[i] = pw[i - 1] * 10;
    int ans = INF;
    while (Q.size()) {
        int i = Q.front().i, mask = Q.front().mask, j = Q.front().j;
        Q.pop();
        FOR(p, 0, 3) {
            int r = (mask / pw[p]) % 10;
            if (r == j) {
                int newMask = mask - r * pw[p];
                if (d[i][newMask][j] > d[i][mask][j] + 1) {
                    d[i][newMask][j] = d[i][mask][j] + 1;
                    Q.push(State(i, newMask, j));
                }
            }
        }
        if (1 < j && d[i][mask][j - 1] > d[i][mask][j] + 1) {
            d[i][mask][j - 1] = d[i][mask][j] + 1;
            Q.push(State(i, mask, j - 1));
        }
        if (j < 9 && d[i][mask][j + 1] > d[i][mask][j] + 1) {
            d[i][mask][j + 1] = d[i][mask][j] + 1;
            Q.push(State(i, mask, j + 1));
        }
        if (i <= n) {
            if (a[i] == j) {
                FOR(p, 0, 3) {
                    int r = (mask / pw[p]) % 10;
                    if (r == 0) {
                        int newMask = mask + b[i] * pw[p];
                        if (d[i + 1][newMask][j] > d[i][mask][j] + 1) {
                            d[i + 1][newMask][j] = d[i][mask][j] + 1;
                            Q.push(State(i + 1, newMask, j));
                        }
                    }
                }
            }
        } else {
            if (mask == 0) ans = min(ans, d[i][mask][j]);
        }
    }
    printf("%d", ans);
    return 0;
}


#include <bits/stdc++.h>

#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x <  b; ++x)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl; }
#define PR0(A, n) { cout << #A << " = "; REP(_, 0, n) cout << A[_] << " "; cout << endl; }
#define BitCount(x) __builtin_popcount(x)

using namespace std;

typedef unsigned long long LL;
typedef pair <int, int> II;

const int N = 2e3 + 10;
const int INF = 0x3f3f3f3f;

int n;
int a[N], b[N];

int d[N][1 << 10][10];

struct State {
    int i, mask, j;
    State () {}
    State (int i, int mask, int j) : i(i), mask(mask), j(j) {}
};

int main() {
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // LOCAL
    scanf("%d", &n);
    FOR(i, 1, n) scanf("%d%d", &a[i], &b[i]);
    memset(d, INF, sizeof d);
    d[1][0][1] = 0;
    queue <State> Q; Q.push(State(1, 0, 1));
    int ans = INF;
    while (Q.size()) {
        int i = Q.front().i, mask = Q.front().mask, j = Q.front().j;
        Q.pop();
        //cout << i << " " << mask << " " << j << " = " << d[i][mask][j] << endl;
        if (mask >> (j - 1) & 1) {
            int newMask = mask ^ (1 << (j - 1));
            if (d[i][newMask][j] > d[i][mask][j] + 1) {
                d[i][newMask][j] = d[i][mask][j] + 1;
                Q.push(State(i, newMask, j));
            }
        }
        if (j > 1 && d[i][mask][j - 1] > d[i][mask][j] + 1) {
            d[i][mask][j - 1] = d[i][mask][j] + 1;
            Q.push(State(i, mask, j - 1));
        }
        if (j < 9 && d[i][mask][j + 1] > d[i][mask][j] + 1) {
            d[i][mask][j + 1] = d[i][mask][j] + 1;
            Q.push(State(i, mask, j + 1));
        }
        if (i <= n) {
            if (a[i] == j) {
                if (BitCount(mask) < 4) {
                    int newMask = mask | (1 << (b[i] - 1));
                    if (d[i + 1][newMask][j] > d[i][mask][j] + 1) {
                        d[i + 1][newMask][j] = d[i][mask][j] + 1;
                        Q.push(State(i + 1, newMask, j));
                    }
                }
            }
        } else {
            if (mask == 0) ans = min(ans, d[i][mask][j]);
        }
    }
    printf("%d", ans);
    return 0;
}


#include <bits/stdc++.h>

#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x <  b; ++x)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl; }
#define PR0(A, n) { cout << #A << " = "; REP(_, 0, n) cout << A[_] << " "; cout << endl; }
#define BitCount(x) __builtin_popcount(x)

using namespace std;

typedef unsigned long long LL;
typedef pair <int, int> II;

const int N = 2e3 + 10;
const int INF = 0x3f3f3f3f;

int n;
int a[N], b[N], pw[5];

int dp[2][10000][10];

struct State {
    int i, mask, j;
    State () {}
    State (int i, int mask, int j) : i(i), mask(mask), j(j) {}
};

int main() {
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // LOCAL
    scanf("%d", &n);
    FOR(i, 1, n) scanf("%d%d", &a[i], &b[i]);
    memset(dp, INF, sizeof dp);
    pw[0] = 1;
    FOR(i, 1, 4) pw[i] = pw[i - 1] * 10;
    int x = 0, y = 1;
    dp[x][0][1] = 0;
    int ans = INF;
    FOR(i, 1, n + 1) {
        FOR(mask, 0, 9999)
            FOR(j, 1, 9) dp[y][mask][j] = INF;
        FOD(mask, 9999, 0) {
            FOR(j, 1, 8) if (dp[x][mask][j] != INF) {
                if (dp[x][mask][j + 1] > dp[x][mask][j] + 1) {
                    dp[x][mask][j + 1] = dp[x][mask][j] + 1;
                }
            }
            FOD(j, 9, 2) if (dp[x][mask][j] != INF) {
                if (dp[x][mask][j - 1] > dp[x][mask][j] + 1) {
                    dp[x][mask][j - 1] = dp[x][mask][j] + 1;
                }
            }
            FOR(p, 0, 3) {
                int r = (mask / pw[p]) % 10;
                int newMask = mask - r * pw[p];
                if (r != 0) {
                    if (dp[x][newMask][r] > dp[x][mask][r] + 1) {
                        dp[x][newMask][r] = dp[x][mask][r] + 1;
                    }
                } else {
                    int newMask = mask + b[i] * pw[p];
                    if (i <= n && dp[y][newMask][a[i]] > dp[x][mask][a[i]] + 1) {
                        dp[y][newMask][a[i]] = dp[x][mask][a[i]] + 1;
                    }
                }
            }
        }
        if (i == n + 1) {
            FOR(j, 1, 9) ans = min(ans, dp[x][0][j]);
        }
        x ^= 1;
        y ^= 1;
    }
    printf("%d", ans);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
inline ll read(){
	ll x=0,f=1;char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
	return x*f;
}
const int N=2010,M=10;
const int INF=0x3f3f3f3f;
int f[2][M][M][M][M],vis[2][M][M][M][M];
int a[N],b[N];
inline void updata(int now,int p,int x1,int x2,int x3,int v){
	f[now][p][x1][x2][x3]=min(f[now][p][x1][x2][x3],v);
	vis[now][p][x1][x2][x3]=1;
}
inline void updata2(int now,int p,int x1,int x2,int x3,int v){
	if (x1>x2) swap(x1,x2);
	if (x2>x3) swap(x2,x3);
	if (x1>x2) swap(x1,x2);
	f[now][p][x1][x2][x3]=min(f[now][p][x1][x2][x3],v);
	vis[now][p][x1][x2][x3]=1;
}
inline int dis(int a,int b){
	return abs(a-b);
}
int main(){
	memset(f,0x3f,sizeof(f));
	int n=read();
	for (int i=1;i<=n;++i) a[i]=read(),b[i]=read();
	int now=0;
	f[now][a[1]][0][0][b[1]]=a[1],vis[now][a[1]][0][0][b[1]]=1;
	for (int i=2;i<=n+1;++i){
		now^=1;
		for (int p=1;p<M;++p){
			for (int x1=1;x1<M;++x1){
				for (int x2=x1;x2<M;++x2){
					for (int x3=x2;x3<M;++x3){
						if (vis[now^1][p][x1][x2][x3]){
							updata(now^1,x3,0,x1,x2,f[now^1][p][x1][x2][x3]+dis(p,x3)+1);
							updata(now^1,x2,0,x1,x3,f[now^1][p][x1][x2][x3]+dis(p,x2)+1);
							updata(now^1,x1,0,x2,x3,f[now^1][p][x1][x2][x3]+dis(p,x1)+1);
							if (i<=n){
								updata2(now,b[i],x1,x2,x3,f[now^1][p][x1][x2][x3]+dis(p,a[i])+1+dis(a[i],b[i])+1);
								updata2(now,x1,b[i],x2,x3,f[now^1][p][x1][x2][x3]+dis(p,a[i])+1+dis(a[i],x1)+1);
								updata2(now,x2,b[i],x1,x3,f[now^1][p][x1][x2][x3]+dis(p,a[i])+1+dis(a[i],x2)+1);
								updata2(now,x3,b[i],x1,x2,f[now^1][p][x1][x2][x3]+dis(p,a[i])+1+dis(a[i],x3)+1);
							}
							vis[now^1][p][x1][x2][x3]=0;
							f[now^1][p][x1][x2][x3]=INF;
						}
					}
				}
			}
		}
		for (int p=1;p<M;++p){
			int x1=0;
				for (int x2=1;x2<M;++x2){
					for (int x3=x2;x3<M;++x3){
						if (vis[now^1][p][x1][x2][x3]){
							updata(now^1,x3,0,x1,x2,f[now^1][p][x1][x2][x3]+dis(p,x3)+1);
							updata(now^1,x2,0,x1,x3,f[now^1][p][x1][x2][x3]+dis(p,x2)+1);
							if (i<=n)
							updata2(now,a[i],b[i],x2,x3,f[now^1][p][x1][x2][x3]+dis(p,a[i])+1);
							vis[now^1][p][x1][x2][x3]=0;
							f[now^1][p][x1][x2][x3]=INF;
						}
					}
				}
		}
		for (int p=1;p<M;++p){
			int x1=0;
				int x2=0;
					for (int x3=1;x3<M;++x3){
						if (vis[now^1][p][x1][x2][x3]){
							updata(now^1,x3,0,x1,x2,f[now^1][p][x1][x2][x3]+dis(p,x3)+1);
							if (i<=n)
							updata2(now,a[i],x2,b[i],x3,f[now^1][p][x1][x2][x3]+dis(p,a[i])+1);
							vis[now^1][p][x1][x2][x3]=0;
							f[now^1][p][x1][x2][x3]=INF;
						}
					}
		}
		if (i<=n){

		for (int p=1;p<M;++p){
			if (vis[now^1][p][0][0][0]){
				updata(now,a[i],0,0,b[i],f[now^1][p][0][0][0]+dis(p,a[i])+1);
				vis[now^1][p][0][0][0]=0;
				f[now^1][p][0][0][0]=INF;
			}
		}
		}
	}
	int ans=INF;
	now^=1;
	for (int p=1;p<M;++p){
		if (vis[now][p][0][0][0]){
			ans=min(ans,f[now][p][0][0][0]);
		}
	}
	printf("%d\n",ans);
	return 0;
}


/**
 *　　　　　　　　┏┓　　 　┏┓
 * 　　　　　　　┏┛┗━━━━━━━┛┗━━━┓
 * 　　　　　　　┃　　　　　　　┃
 * 　　　　　　　┃　　　━　　 　┃
 * 　　　　　　　┃　＞　　　＜　┃
 * 　　　　　　　┃　　　　　　　┃
 * 　　　　　　　┃...　⌒　... 　┃
 * 　　　　　　　┃　　　　　　　┃
 * 　　　　　　　┗━┓　　　┏━┛
 * 　　　　　　　　　┃　　　┃　Code is far away from bug with the animal protecting
 * 　　　　　　　　　┃　　　┃   神兽保佑,代码无bug
 * 　　　　　　　　　┃　　　┃
 * 　　　　　　　　　┃　　　┃
 * 　　　　　　　　　┃　　　┃
 * 　　　　　　　　　┃　　　┃
 * 　　　　　　　　　┃　　　┗━━━┓
 * 　　　　　　　　　┃　　　　　　　┣┓
 * 　　　　　　　　　┃　　　　　　　┏┛
 * 　　　　　　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　　　　　　┃┫┫　┃┫┫
 * 　　　　　　　　　　┗┻┛　┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 2005
#define nm 1000005
#define pi 3.1415926535897931
using namespace std;
const int inf=1000000007;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}

int d[10][10][10][10][10],f[10][10][10][10][10];
int a[NM],b[NM],n,ans;
void up(int&x,int y){if(x>y)x=y;}

int main(){
	n=read();
	inc(i,1,n)a[i]=read(),b[i]=read();
	inc(j,1,9)inc(t1,0,9)inc(t2,0,9)inc(t3,0,9)inc(t4,0,9)d[j][t1][t2][t3][t4]=inf;
	d[1][0][0][0][0]=0;
	inc(i,1,n+1){
		inc(k,1,4)
		inc(j,1,9)inc(t1,0,9)inc(t2,0,9)inc(t3,0,9)inc(t4,0,9)if(d[j][t1][t2][t3][t4]<inf){
			if(t1)up(d[t1][0][t2][t3][t4],d[j][t1][t2][t3][t4]+abs(j-t1)+1);
			if(t2)up(d[t2][t1][0][t3][t4],d[j][t1][t2][t3][t4]+abs(j-t2)+1);
			if(t3)up(d[t3][t1][t2][0][t4],d[j][t1][t2][t3][t4]+abs(j-t3)+1);
			if(t4)up(d[t4][t1][t2][t3][0],d[j][t1][t2][t3][t4]+abs(j-t4)+1);
		}
		//inc(j,1,9)printf("%d ",d[j][0][0][0][0]);putchar('\n');
		if(i>n)break;
		inc(j,1,9)inc(t1,0,9)inc(t2,0,9)inc(t3,0,9)inc(t4,0,9)f[j][t1][t2][t3][t4]=inf;
		inc(j,1,9)inc(t1,0,9)inc(t2,0,9)inc(t3,0,9)inc(t4,0,9){
			if(!t1)up(f[a[i]][b[i]][t2][t3][t4],d[j][t1][t2][t3][t4]+abs(j-a[i])+1);
			if(!t2)up(f[a[i]][t1][b[i]][t3][t4],d[j][t1][t2][t3][t4]+abs(j-a[i])+1);
			if(!t3)up(f[a[i]][t1][t2][b[i]][t4],d[j][t1][t2][t3][t4]+abs(j-a[i])+1);
			if(!t4)up(f[a[i]][t1][t2][t3][b[i]],d[j][t1][t2][t3][t4]+abs(j-a[i])+1);
		}
		inc(j,1,9)inc(t1,0,9)inc(t2,0,9)inc(t3,0,9)inc(t4,0,9)d[j][t1][t2][t3][t4]=f[j][t1][t2][t3][t4];
	}
	ans=inf;
	inc(i,1,9)up(ans,d[i][0][0][0][0]);
	return 0*printf("%d\n",ans);
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define inf 1000000005
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))
#define mod 1000000009
#define sz(a) ((int)(a).size())

template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s);}
#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define eps 1e-10
typedef pair<int, int> II;
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define PI 2 * acos(0)

#define maxn 2005

int test;
int n, a[maxn], b[maxn];
vector<vector<int> > V;
map<vector<int>, int> M;
int r[5], f[400][2];

void go(int id, int start){
	for(int i = start; i <= 9; i++){
		r[id] = i;
		vector<int> temp;
		For(j, 1, id) temp.pb(r[j]);
		V.pb(temp);
		M[temp] = sz(V) - 1;

		if(id == 4) return;
		go(id + 1, i);
	}

}

int cal(int a1, int a2, int a3, int a4){
	return min(abs(a1 - a2) + abs(a2 - a3) + abs(a3 - a4), abs(a1 - a3) + abs(a3 - a2) + abs(a2 - a4));
}

int main(){
	srand(time(NULL));
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//	freopen("in.txt", "r", stdin);
    vector<int> temp;
    V.pb(temp);
    go(1, 1);

//    cout << sz(V) << endl;

    cin >> n;
    For(i, 1, n) cin >> a[i] >> b[i];

    ms(f, 0x3f);
    temp.pb(b[1]);
//    cout << M[temp] << endl;
    f[M[temp]][0] = a[1] - 1;

    int m = sz(V);

    For(t, 2, n){
    	For(i, 1, m - 1){
    		f[i][1] = f[i][0];
    		f[i][0] = inf;
    	}
    	For(i, 1, m - 1) if(f[i][1] < inf){
    		vector<int> temp = V[i];
    		if(sz(temp) < 4){
    			vector<int> A = temp;
    			A.pb(b[t]);
    			sort(all(A));
    			int x = M[A];
    			f[x][0] = min(f[x][0], f[i][1] + abs(a[t] - a[t - 1]));
    		}
    		int len = sz(temp);
    		Rep(u, len) for(int v = u; v < len; v++){
    			vector<int> A;
    			int dist = cal(a[t - 1], temp[u], temp[v], a[t]);
    			Rep(j, len){
    				if(j < u || j > v){
    					A.pb(temp[j]);
    				}
    			}
    			A.pb(b[t]);
    			sort(all(A));
    			int x = M[A];
    			f[x][0] = min(f[x][0], f[i][1] + dist);
    		}
    	}
    }

    int res = inf;
    For(i, 1, m - 1){
    	vector<int> temp = V[i];
    	int len = sz(temp);
    	int dist = min(abs(a[n] - temp[0]), abs(a[n] - temp[len - 1])) + abs(temp[0] - temp[len - 1]);
    	res = min(res, f[i][0] + dist);
    }
    cout << res + 2 * n << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define inf 1000000005
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))
#define mod 1000000009
#define sz(a) ((int)(a).size())

template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s);}
#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define eps 1e-10
typedef pair<int, int> II;
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define PI 2 * acos(0)

#define maxn 2005

int test;
int n, a[maxn], b[maxn];
vector<vector<int> > V;
map<vector<int>, int> M;
int r[5], f[800][2];

void go(int id, int start){
	if(id > 4) return;
	for(int i = start; i <= 9; i++){
		r[id] = i;
		vector<int> temp;
		For(j, 1, id) {
			temp.pb(r[j]);
//			cout << r[j] << " ";
		}
//		cout << endl;

		V.pb(temp);
		M[temp] = sz(V) - 1;

		go(id + 1, i);
	}

}

int cal(int a1, int a2, int a3, int a4){
	return min(abs(a1 - a2) + abs(a2 - a3) + abs(a3 - a4), abs(a1 - a3) + abs(a3 - a2) + abs(a2 - a4));
}

int main(){
	srand(time(NULL));
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//	freopen("in.txt", "r", stdin);
    vector<int> temp;
    V.pb(temp);
    go(1, 1);

//    cout << sz(V) << endl;

    cin >> n;
    For(i, 1, n) cin >> a[i] >> b[i];

    ms(f, 0x3f);
    temp.pb(b[1]);
//    cout << M[temp] << endl;
    f[M[temp]][0] = a[1] - 1;

    int m = sz(V);

    For(t, 2, n){
    	For(i, 1, m - 1){
    		f[i][1] = f[i][0];
    		f[i][0] = inf;
    	}
    	For(i, 1, m - 1) if(f[i][1] < inf){
    		vector<int> temp = V[i];
    		if(sz(temp) < 4){
    			vector<int> A = temp;
    			A.pb(b[t]);
    			sort(all(A));
    			int x = M[A];
    			f[x][0] = min(f[x][0], f[i][1] + abs(a[t] - a[t - 1]));
    		}
    		int len = sz(temp);
    		Rep(u, len) for(int v = u; v < len; v++){
    			vector<int> A;
    			int dist = cal(a[t - 1], temp[u], temp[v], a[t]);
    			Rep(j, len){
    				if(j < u || j > v){
    					A.pb(temp[j]);
    				}
    			}
    			A.pb(b[t]);
    			sort(all(A));
    			int x = M[A];
    			f[x][0] = min(f[x][0], f[i][1] + dist);
    		}
    	}
    }

    int res = inf;
    For(i, 1, m - 1){
    	vector<int> temp = V[i];
    	int len = sz(temp);
    	int dist = min(abs(a[n] - temp[0]), abs(a[n] - temp[len - 1])) + abs(temp[0] - temp[len - 1]);
    	res = min(res, f[i][0] + dist);
    }
    cout << res + 2 * n << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define inf 1000000005
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))
#define mod 1000000009
#define sz(a) ((int)(a).size())

template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcount(s);}
#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define eps 1e-10
typedef pair<int, int> II;
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define PI 2 * acos(0)

#define maxn 5005

int test;
int n, a[maxn], b[maxn];
int f[maxn][10][10];

int main(){
	srand(time(NULL));
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//	freopen("in.txt", "r", stdin);
    cin >> n;
    cin >> a[1] >> b[1];
    ms(f, 0x3f);
    f[1][b[1]][b[1]] = a[1] - 1;
    For(t, 2, n){
    	cin >> a[t] >> b[t];
    	For(i, 1, 9) For(j, 1, 9) if(f[t - 1][i][j] < inf){
    		int ii = min(b[t], i), jj = max(b[t], j);
    		f[t][ii][jj] = min(f[t][ii][jj], f[t - 1][i][j] + abs(a[t] - a[t - 1]));
    		f[t][b[t]][jj] = min(f[t][b[t]][jj], f[t - 1][i][j] + abs(a[t - 1] - i) + abs(a[t] - i));
    		f[t][ii][b[t]] = min(f[t][ii][b[t]], f[t - 1][i][j] + abs(a[t - 1] - j) + abs(a[t] - j));
    		int A = min(abs(a[t - 1] - i) + abs(i - j) + abs(j - a[t]), abs(a[t - 1] - j) + abs(j - i) + abs(i - a[t]));
    		f[t][b[t]][b[t]] = min(f[t][b[t]][b[t]], f[t - 1][i][j] + A);
    	}
    }
    int res = inf;
    For(i, 1, 9) For(j, 1, 9){
    	int A = min(abs(a[n] - i) + abs(i - j), abs(a[n] - j) + abs(i - j));
    	res = min(res, f[n][i][j] + A);
    }
    cout << res + 2 * n << endl;

    return 0;
}


#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

// Create a namespace to avoid naming conflicts.
namespace neal {

const int INF = 1000000005;

int N;
int min_steps[10000][10];
int new_steps[10000][10];

int encode(vector<int> floors) {
    sort(floors.begin(), floors.end());
    int num = 0;

    for (int i = 0; i < (int) floors.size(); i++) {
        num = 10 * num + floors[i];
    }

    return num;
}

vector<int> decode(int encoded) {
    vector<int> floors;

    while (encoded > 0) {
        floors.push_back(encoded % 10);
        encoded /= 10;
    }

    sort(floors.begin(), floors.end());
    return floors;
}

void solve_problem() {
    memset(min_steps, 63, sizeof(min_steps));
    min_steps[0][1] = 0;

    cin >> N;

    while (N--) {
        int a, b;
        cin >> a >> b;
        memset(new_steps, 63, sizeof(new_steps));

        for (int e = 9999; e >= 0; e--) {
            for (int floor = 1; floor <= 9; floor++) {
                if (min_steps[e][floor] < INF) {
                    vector<int> floors = decode(e);

                    if (floors.size() < 4) {
                        int add = abs(floor - a) + 1;
                        floors.push_back(b);
                        int new_e = encode(floors);
                        new_steps[new_e][a] = min(new_steps[new_e][a], min_steps[e][floor] + add);
                    }
                }
            }
        }

        memcpy(min_steps, new_steps, sizeof(min_steps));

        for (int e = 9999; e >= 0; e--) {
            for (int floor = 1; floor <= 9; floor++) {
                if (min_steps[e][floor] < INF) {
                    vector<int> floors = decode(e);

                    for (int i = 0; i < (int) floors.size(); i++) {
                        int add = abs(floor - floors[i]) + 1;
                        vector<int> new_floors = floors;
                        swap(new_floors[i], new_floors.back());
                        new_floors.pop_back();
                        int new_e = encode(new_floors);
                        min_steps[new_e][floors[i]] = min(min_steps[new_e][floors[i]], min_steps[e][floor] + add);
                    }
                }
            }
        }
    }

    int best = INF;

    for (int floor = 1; floor <= 9; floor++) {
        best = min(best, min_steps[0][floor]);
    }

    cout << best << '\n';
}

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    neal::solve_problem();
    return 0;
}


#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

// Create a namespace to avoid naming conflicts.
namespace neal {

const int INF = 1000000005;

int N;
int min_steps[10000][10];
int new_steps[10000][10];

int encode(int f1, int f2, int f3, int f4) {
    // Bubble sort.
    if (f1 > f2) swap(f1, f2);
    if (f2 > f3) swap(f2, f3);
    if (f3 > f4) swap(f3, f4);
    if (f1 > f2) swap(f1, f2);
    if (f2 > f3) swap(f2, f3);
    if (f1 > f2) swap(f1, f2);

    return 1000 * f1 + 100 * f2 + 10 * f3 + f4;
}

vector<int> decode(int encoded) {
    vector<int> floors;

    for (int i = 0; i < 4; i++) {
        floors.push_back(encoded % 10);
        encoded /= 10;
    }

    reverse(floors.begin(), floors.end());
    return floors;
}

void solve_problem() {
    memset(min_steps, 63, sizeof(min_steps));
    min_steps[0][1] = 0;

    cin >> N;

    while (N--) {
        int a, b;
        cin >> a >> b;
        memset(new_steps, 63, sizeof(new_steps));

        for (int e = 9999; e >= 0; e--) {
            for (int floor = 1; floor <= 9; floor++) {
                if (min_steps[e][floor] < INF) {
                    vector<int> floors = decode(e);
                    int f1 = floors[0], f2 = floors[1], f3 = floors[2], f4 = floors[3];

                    if (f1 == 0) {
                        int add = abs(floor - a) + 1;
                        int new_e = encode(b, f2, f3, f4);
                        new_steps[new_e][a] = min(new_steps[new_e][a], min_steps[e][floor] + add);
                    }
                }
            }
        }

        memcpy(min_steps, new_steps, sizeof(min_steps));

        for (int e = 9999; e >= 0; e--) {
            for (int floor = 1; floor <= 9; floor++) {
                if (min_steps[e][floor] < INF) {
                    vector<int> floors = decode(e);
                    int f1 = floors[0], f2 = floors[1], f3 = floors[2], f4 = floors[3];

                    if (f1 > 0) {
                        int add = abs(floor - f1) + 1;
                        int new_e = encode(0, f2, f3, f4);
                        min_steps[new_e][f1] = min(min_steps[new_e][f1], min_steps[e][floor] + add);
                    }

                    if (f2 > 0) {
                        int add = abs(floor - f2) + 1;
                        int new_e = encode(f1, 0, f3, f4);
                        min_steps[new_e][f2] = min(min_steps[new_e][f2], min_steps[e][floor] + add);
                    }

                    if (f3 > 0) {
                        int add = abs(floor - f3) + 1;
                        int new_e = encode(f1, f2, 0, f4);
                        min_steps[new_e][f3] = min(min_steps[new_e][f3], min_steps[e][floor] + add);
                    }

                    if (f4 > 0) {
                        int add = abs(floor - f4) + 1;
                        int new_e = encode(f1, f2, f3, 0);
                        min_steps[new_e][f4] = min(min_steps[new_e][f4], min_steps[e][floor] + add);
                    }
                }
            }
        }
    }

    int best = INF;

    for (int floor = 1; floor <= 9; floor++) {
        best = min(best, min_steps[0][floor]);
    }

    cout << best << '\n';
}

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    neal::solve_problem();
    return 0;
}


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

int color[100005], n, m, a, b, vertex[100005];

int main(){
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; ++i){
        scanf("%d", &vertex[i]);
    }
    for (int i=0; i<m; ++i){
        scanf("%d%d", &a, &b);
        int color_a = vertex[a], color_b = vertex[b];
        if (color_a == color_b) continue;
        ++color[color_a]; ++color[color_b];
    }
    int ans = 0, col = 0;
    for (int i=1; i<=100000; ++i){
        printf("%d: %d\n", i, color[i]);
        if (color[i] > ans){
            ans = color[i];
            col = i;
        }
    }
    printf("%d", col);
    return 0;
}


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

long long color[100005];
int n, m, a, b, vertex[100005];
set<int> used[100005];

int main(){
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; ++i){
        scanf("%d", &vertex[i]);
    }
    for (int i=0; i<m; ++i){
        scanf("%d%d", &a, &b);
        int color_a = vertex[a], color_b = vertex[b];
        if (color_a==color_b) continue;
        if (used[color_a].find(color_b) == used[color_a].end()){
            ++color[color_b];
            used[color_a].insert(color_b);
        }
        if (used[color_b].find(color_a) == used[color_b].end()){
            ++color[color_a];
            used[color_b].insert(color_a);
        }
    }
    long long ans = color[1];
    int col = 1;
    for (int i=2; i<=100000; ++i){
        if (color[i] > ans){
            ans = color[i];
            col = i;
        }
    }
    printf("%d", col);
    return 0;
}


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

long long color[100005];
int n, m, a, b, vertex[100005];

int main(){
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; ++i){
        scanf("%d", &vertex[i]);
    }
    for (int i=0; i<m; ++i){
        scanf("%d%d", &a, &b);
        int color_a = vertex[a], color_b = vertex[b];
        if (color_a == color_b) continue;
        ++color[color_a]; ++color[color_b];
    }
    long long ans = color[1];
    int col = 1;
    for (int i=2; i<=100000; ++i){
        if (color[i] > ans){
            ans = color[i];
            col = i;
        }
    }
    printf("%d", col);
    return 0;
}


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

int color[100005], n, m, a, b, vertex[100005];

int main(){
    scanf("%d%d", &n, &m);
    for (int i=0; i<n; ++i){
        scanf("%d", &vertex[i]);
    }
    for (int i=0; i<m; ++i){
        scanf("%d%d", &a, &b);
        int color_a = vertex[a], color_b = vertex[b];
        if (color_a == color_b) continue;
        ++color[color_a]; ++color[color_b];
    }
    int ans = 0, col = 0;
    for (int i=1; i<=100000; ++i){
        if (color[i] > ans){
            ans = color[i];
            col = i;
        }
    }
    printf("%d", col);
    return 0;
}


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

long long color[100005];
int n, m, a, b, vertex[100005];

int main(){
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; ++i){
        scanf("%d", &vertex[i]);
    }
    for (int i=0; i<m; ++i){
        scanf("%d%d", &a, &b);
        int color_a = vertex[a], color_b = vertex[b];
        if (color_a == color_b) continue;
        ++color[color_a]; ++color[color_b];
    }
    long long ans = 0;
    int col = 0;
    for (int i=1; i<=100000; ++i){
        if (color[i] > ans){
            ans = color[i];
            col = i;
        }
    }
    printf("%d", col);
    return 0;
}


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

long long color[100005];
int n, m, a, b, vertex[100005];
set<int> used[100005];
#define INF 1023456789

int main(){
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; ++i){
        scanf("%d", &vertex[i]);
    }
    for (int i=0; i<m; ++i){
        scanf("%d%d", &a, &b);
        int color_a = vertex[a], color_b = vertex[b];
        if (color_a==color_b) continue;
        if (used[color_a].find(color_b) == used[color_a].end()){
            ++color[color_b];
            used[color_a].insert(color_b);
        }
        if (used[color_b].find(color_a) == used[color_b].end()){
            ++color[color_a];
            used[color_b].insert(color_a);
        }
    }
    long long ans = color[vertex[1]];
    int col = vertex[1];
    for (int i=2; i<=n; ++i){
        if (color[vertex[i]] > ans){
            ans = color[vertex[i]];
            col = vertex[i];
        }
        else if (color[vertex[i]] == ans && vertex[i] < col) col = vertex[i];
    }
    printf("%d", col);
    return 0;
}


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

long long color[100005];
int n, m, a, b, vertex[100005];
set<int> used[100005];

int main(){
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; ++i){
        scanf("%d", &vertex[i]);
    }
    for (int i=0; i<m; ++i){
        scanf("%d%d", &a, &b);
        int color_a = vertex[a], color_b = vertex[b];
        if (color_a==color_b) continue;
        if (used[a].find(color_b) == used[a].end()){
            ++color[color_b];
            used[a].insert(color_b);
        }
        if (used[b].find(color_a) == used[b].end()){
            ++color[color_a];
            used[b].insert(color_a);
        }
    }
    long long ans = color[1];
    int col = 1;
    for (int i=2; i<=100000; ++i){
        if (color[i] > ans){
            ans = color[i];
            col = i;
        }
    }
    printf("%d", col);
    return 0;
}


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

long long color[100005];
int n, m, a, b, vertex[100005];

int main(){
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; ++i){
        scanf("%d", &vertex[i]);
    }
    for (int i=0; i<m; ++i){
        scanf("%d%d", &a, &b);
        int color_a = vertex[a], color_b = vertex[b];
        if (color_a == color_b) continue;
        ++color[color_a]; ++color[color_b];
    }
    long long ans = 0;
    int col = 1;
    for (int i=1; i<=100000; ++i){
        if (color[i] > ans){
            ans = color[i];
            col = i;
        }
    }
    printf("%d", col);
    return 0;
}


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

int color[100005], n, m, a, b, vertex[100005];

int main(){
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; ++i){
        scanf("%d", &vertex[i]);
    }
    for (int i=0; i<m; ++i){
        scanf("%d%d", &a, &b);
        int color_a = vertex[a], color_b = vertex[b];
        if (color_a == color_b) continue;
        ++color[color_a]; ++color[color_b];
    }
    int ans = 0, col = 0;
    for (int i=1; i<=100000; ++i){
        if (color[i] > ans){
            ans = color[i];
            col = i;
        }
    }
    printf("%d", col);
    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
//-------------------------------
typedef long long ll ;
typedef vector<int> vi ;
typedef pair<int,int> ii ;
//-------------------------------
#define _CRT_SECURE_NO_WARNINGS
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i=(a) ; (i)<(b) ; ++i)
#define inf 2000000000
#define endl "\n"
#define de(x) cerr<<#x<<" is "<<x<<endl;
#define max(a,b) ( (a>b) ? (a) : (b)  )
#define min(a,b) ( (a<b) ? (a) : (b)  )
//------------------------------
int ri() { char c = getchar(); while(c<'0' || c>'9') c=getchar(); int ret = 0; while(c>='0' && c<='9') { ret=10*ret+c-48; c=getchar(); } return ret; }

vi gr[11111] ;
int n , m, u , v ;
int col[11111] ;

int main()
{
    //freopen("input.txt","r",stdin) ;
    scanf("%d %d",&n,&m) ;
    int ind = inf ;
    set<int> color ;
    rep(i,1,n+1)
    {
        scanf("%d",&col[i]);
        color.insert(col[i]);
        if(col[i]<ind)
            ind=col[i];
    }
    map< ii,int >mo;
    rep(i,0,m)
    {
        scanf("%d %d",&u,&v) ;
        u=col[u]; v=col[v] ;
        if(u>v) swap(u,v) ;
        if(mo[mp(u,v)] || (u==v)) continue ;
        mo[mp(u,v)]=1;
        //cout << u << " " << v << endl ;
        gr[u].pb(v) ;
        gr[v].pb(u) ;
    }
    int mx =  0 ;
    for(auto x:color)
    {

        if(gr[ x ].size()>mx)
        {
            mx=gr[ x ].size();
            ind=x;
        }

    }
    cout << ind << endl ;
}


#include<bits/stdc++.h>
using namespace std ;
//-------------------------------
typedef long long ll ;
typedef vector<int> vi ;
typedef pair<int,int> ii ;
//-------------------------------
#define _CRT_SECURE_NO_WARNINGS
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i=(a) ; (i)<(b) ; ++i)
#define inf 2000000000
#define endl "\n"
#define de(x) cerr<<#x<<" is "<<x<<endl;
#define max(a,b) ( (a>b) ? (a) : (b)  )
#define min(a,b) ( (a<b) ? (a) : (b)  )
//------------------------------
int ri() { char c = getchar(); while(c<'0' || c>'9') c=getchar(); int ret = 0; while(c>='0' && c<='9') { ret=10*ret+c-48; c=getchar(); } return ret; }

vi gr[11111] ;
int n , m, u , v ;
int col[11111] ;

int main()
{
    //freopen("input.txt","r",stdin) ;
    n=ri(); m=ri();
    int ind = inf ;
    rep(i,1,n+1)
    {
        col[i]=ri();
        if(col[i]<ind)
            ind=col[i];
    }
    map< ii,int >mo;
    rep(i,0,m)
    {
        u=ri(); v=ri();
        u=col[u]; v=col[v] ;
        if(u>v) swap(u,v) ;
        if(mo[mp(u,v)] || (u==v)) continue ;
        mo[mp(u,v)]=1;
        //cout << u << " " << v << endl ;
        gr[u].pb(v) ;
        gr[v].pb(u) ;
    }
    int mx =  0 ;
    rep(i,1,11111)
    {
        if(gr[i].size()>mx)
        {
            mx=gr[i].size();
            ind=i;
        }
    }
    cout << ind << endl ;
}


#include<bits/stdc++.h>
using namespace std ;
//-------------------------------
typedef long long ll ;
typedef vector<int> vi ;
typedef pair<int,int> ii ;
//-------------------------------
#define _CRT_SECURE_NO_WARNINGS
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i=(a) ; (i)<(b) ; ++i)
#define inf 2000000000
#define endl "\n"
#define de(x) cerr<<#x<<" is "<<x<<endl;
#define max(a,b) ( (a>b) ? (a) : (b)  )
#define min(a,b) ( (a<b) ? (a) : (b)  )
#define N 200000
//------------------------------
int ri() { char c = getchar(); while(c<'0' || c>'9') c=getchar(); int ret = 0; while(c>='0' && c<='9') { ret=10*ret+c-48; c=getchar(); } return ret; }
int n,m,a[500500],max,nom;
set<int>g[500500];
bool used[500500];

int main(){
    scanf("%d%d",&n,&m);
    for (int i=0;i<n;i++){
        scanf("%d",&a[i]);
        used[a[i]]=1;
    }
    for (int i=0;i<m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        --x;--y;
        x=a[x];y=a[y];
        if (x==y)continue;
        g[x].insert(y);
        g[y].insert(x);
    }
    int max=-1;
    for (int i=1;i<=N;i++){
        if (!used[i])continue;
        int cr=g[i].size();
        if (cr>max){
            max=cr;
            nom=i;
        }
    }
    printf("%d\n",nom);
}


#include<bits/stdc++.h>
using namespace std ;
//-------------------------------
typedef long long ll ;
typedef vector<int> vi ;
typedef pair<int,int> ii ;
//-------------------------------
#define _CRT_SECURE_NO_WARNINGS
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i=(a) ; (i)<(b) ; ++i)
#define inf 2000000000
#define endl "\n"
#define de(x) cerr<<#x<<" is "<<x<<endl;
#define max(a,b) ( (a>b) ? (a) : (b)  )
#define min(a,b) ( (a<b) ? (a) : (b)  )
//------------------------------
int ri() { char c = getchar(); while(c<'0' || c>'9') c=getchar(); int ret = 0; while(c>='0' && c<='9') { ret=10*ret+c-48; c=getchar(); } return ret; }

int n,m,u,v;
set<int>gr[11111] ;
int has[11111] ;
int col[11111] ;
int main()
{
    //freopen("input.txt","r",stdin) ;
    scanf("%d %d",&n,&m) ;
    int ind = inf ;
    int mx = 0 ;
    rep(i,1,n+1)
    {
        scanf("%d",&col[i]);
        has[ col[i] ]=1;
        if(col[i]<ind)
            ind=col[i] ;
    }
    rep(i,1,m+1)
    {
        scanf("%d %d",&u,&v);
        if(col[u]==col[v]) continue ;
        gr[col[u]].insert(col[v]) ;
        gr[col[v]].insert(col[u]) ;
    }
    rep(i,1,10005) if(has[i])
    {
        if(gr[i].size()>mx)
        {
            mx=gr[i].size();
            ind=i;
        }
    }
    cout << ind << endl ;

}


#include<bits/stdc++.h>
using namespace std ;
//-------------------------------
typedef long long ll ;
typedef vector<int> vi ;
typedef pair<int,int> ii ;
//-------------------------------
#define _CRT_SECURE_NO_WARNINGS
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i=(a) ; (i)<(b) ; ++i)
#define inf 2000000000
#define endl "\n"
#define de(x) cerr<<#x<<" is "<<x<<endl;
#define max(a,b) ( (a>b) ? (a) : (b)  )
#define min(a,b) ( (a<b) ? (a) : (b)  )
//------------------------------
int ri() { char c = getchar(); while(c<'0' || c>'9') c=getchar(); int ret = 0; while(c>='0' && c<='9') { ret=10*ret+c-48; c=getchar(); } return ret; }

vi gr[11111] ;
int n , m, u , v ;
int col[11111] ;
int sz[11111] ;
int main()
{
    //freopen("input.txt","r",stdin) ;
    scanf("%d %d",&n,&m) ;
    int ind = inf ;
    set<int> color ;
    rep(i,1,n+1)
    {
        scanf("%d",&col[i]);
        color.insert(col[i]);
        if(col[i]<ind)
            ind=col[i];
    }
    map< ii,int >mo;
    int mx=0;
    rep(i,0,m)
    {
        scanf("%d %d",&u,&v) ;
        u=col[u]; v=col[v] ;
        if(u>v) swap(u,v) ;
        if(mo[mp(u,v)] || (u==v)) continue ;
        mo[mp(u,v)]=1;
        //cout << u << " " << v << endl ;
        gr[u].pb(v) ;
        gr[v].pb(u) ;
        sz[u]++;
        sz[v]++;
        if(sz[u]>mx)
        {
            mx=sz[u]; ind=u;
        }
        if(sz[v]>mx)
        {
            mx=sz[v]; ind=v;
        }
    }
    cout << ind << endl ;
}


#include<bits/stdc++.h>
using namespace std ;
//-------------------------------
typedef long long ll ;
typedef vector<int> vi ;
typedef pair<int,int> ii ;
//-------------------------------
#define _CRT_SECURE_NO_WARNINGS
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i=(a) ; (i)<(b) ; ++i)
#define inf 2000000000
#define endl "\n"
#define de(x) cerr<<#x<<" is "<<x<<endl;
#define max(a,b) ( (a>b) ? (a) : (b)  )
#define min(a,b) ( (a<b) ? (a) : (b)  )
//------------------------------
int ri() { char c = getchar(); while(c<'0' || c>'9') c=getchar(); int ret = 0; while(c>='0' && c<='9') { ret=10*ret+c-48; c=getchar(); } return ret; }

vi gr[11111] ;
int n , m, u , v ;
int col[11111] ;
vi cl[11111];
int main()
{
    //freopen("input.txt","r",stdin) ;
    n=ri(); m=ri();
    map<int,int> mop ;
    rep(i,1,n+1)
    {
        col[i]=ri() ;
        mop[col[i]]=1;
        cl[ col[i] ].pb(i) ;
    }
    rep(i,1,m+1)
    {
        u=ri(); v=ri();
        gr[u].pb(v) ;
        gr[v].pb(u) ;
    }
    int mx = 0 ;
    int ind=0 ;

    rep(i,0,11111)  if(mop[i])
    {
        set<int>mo;
        mo.insert(i);
        rep(j,0,cl[i].size())
        {
            u=cl[i][j] ;
            rep(k,0,gr[u].size())
            {
                mo.insert(col[gr[u][k]]) ;
            }
        }
        if(mo.size()>mx)
        {
            mx=mo.size() ;
            ind=i;
        }
    }

    cout << ind << endl ;

}


#include<bits/stdc++.h>
using namespace std ;
//-------------------------------
typedef long long ll ;
typedef vector<int> vi ;
typedef pair<int,int> ii ;
//-------------------------------
#define _CRT_SECURE_NO_WARNINGS
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i=(a) ; (i)<(b) ; ++i)
#define inf 2000000000
#define endl "\n"
#define de(x) cerr<<#x<<" is "<<x<<endl;
#define max(a,b) ( (a>b) ? (a) : (b)  )
#define min(a,b) ( (a<b) ? (a) : (b)  )
//------------------------------
int ri() { char c = getchar(); while(c<'0' || c>'9') c=getchar(); int ret = 0; while(c>='0' && c<='9') { ret=10*ret+c-48; c=getchar(); } return ret; }

int n,m,u,v;
set<int>gr[11111] ;
int has[11111] ;
int col[11111] ;
int main()
{
    //freopen("input.txt","r",stdin) ;
    scanf("%d %d",&n,&m) ;
    int ind = inf ;
    int mx = 0 ;
    rep(i,1,n+1)
    {
        scanf("%d",&col[i]);
        has[ col[i] ]=1;
        if(col[i]<ind)
            ind=col[i] ;
    }
    rep(i,1,m+1)
    {
        scanf("%d %d",&u,&v);
        if(u==v) continue ;
        gr[col[u]].insert(col[v]) ;
        gr[col[v]].insert(col[u]) ;
    }
    rep(i,1,10005) if(has[i])
    {
        if(gr[i].size()>mx)
        {
            mx=gr[i].size();
            ind=i;
        }
    }
    cout << ind << endl ;

}


#include<bits/stdc++.h>
using namespace std ;
//-------------------------------
typedef long long ll ;
typedef vector<int> vi ;
typedef pair<int,int> ii ;
//-------------------------------
#define _CRT_SECURE_NO_WARNINGS
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i=(a) ; (i)<(b) ; ++i)
#define inf 2000000000
#define endl "\n"
#define de(x) cerr<<#x<<" is "<<x<<endl;
#define max(a,b) ( (a>b) ? (a) : (b)  )
#define min(a,b) ( (a<b) ? (a) : (b)  )
//------------------------------
int ri() { char c = getchar(); while(c<'0' || c>'9') c=getchar(); int ret = 0; while(c>='0' && c<='9') { ret=10*ret+c-48; c=getchar(); } return ret; }

vi gr[11111] ;
int n , m, u , v ;
int col[11111] ;
vi cl[11111];
int main()
{
    //freopen("input.txt","r",stdin) ;
    n=ri(); m=ri();
    rep(i,1,n+1)
    {
        col[i]=ri() ;
        cl[ col[i] ].pb(i) ;
    }
    rep(i,1,m+1)
    {
        u=ri(); v=ri();
        gr[u].pb(v) ;
        gr[v].pb(u) ;
    }
    int mx = 0 ;
    int ind=0 ;

    rep(i,0,11111)  if(col[i])
    {
        set<int>mo;
        mo.insert(i);
        rep(j,0,cl[i].size())
        {
            u=cl[i][j] ;
            rep(k,0,gr[u].size())
            {
                mo.insert(col[gr[u][k]]) ;
            }
        }
        if(mo.size()>mx)
        {
            mx=mo.size() ;
            ind=i;
        }
    }

    cout << ind << endl ;

}


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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;

#define INF 1e18
#define loop(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define forit(i, a) for ( __typeof( (a).begin() ) i = (a).begin(); i != (a).end(); i++ )
#define mem(a, v) memset(a, v, sizeof a)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define left(x) x<<1
#define right(x) (x<<1)|1
#define PI acos(-1.0)
#define EPS 1e-9

int T, N, M;
int col[100005], color[100005], a, b, m=-1, no;
static bitset<100001> v[100001];

void solve(){
	cin>>N>>M;
    mem(color, -1);
    loop(i,1,N) {
        cin>>col[i];
        color[col[i]]=0;
    }
    loop(i,1,M) {
        cin>>a>>b;
        if(col[a]!=col[b] && !v[a][b])
            color[col[a]]++, color[col[b]]++;
        v[a][b]=1;
    }
    loop(i,1,100000)
        if(m<color[i]) {
            m = color[i];
            no = i;
        }
    cout<<no<<endl;
}

int main(){
    //clock_t start_s=clock();
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
  		freopen("/home/snehil/Desktop/input.txt","r",stdin);
    	freopen("/home/snehil/Desktop/output.txt","w",stdout);
    #endif
    T=1;
    while(T--){
    	solve();
    }
    //clock_t stop_s=clock();
    //cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;
    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;

#define INF 1e18
#define loop(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define forit(i, a) for ( __typeof( (a).begin() ) i = (a).begin(); i != (a).end(); i++ )
#define mem(a, v) memset(a, v, sizeof a)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define left(x) x<<1
#define right(x) (x<<1)|1
#define PI acos(-1.0)
#define EPS 1e-9

int T, N, M;
int col[100005], a, b, m, no;
vector<set<int> > v(100005);

void solve(){
	cin>>N>>M;

    loop(i,1,N)
        cin>>col[i];

    no = col[N];

    loop(i,1,M) {
        cin>>a>>b;
        if(col[a]!=col[b]){
            v[col[a]].insert(col[b]);
            v[col[b]].insert(col[a]);
        }
    }
    loop(i,1,100000)
        if(m < v[i].size()) {
             m = v[i].size();
             no = i;
        }
    cout<<no<<endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
  		freopen("/home/snehil/Desktop/input.txt","r",stdin);
    	freopen("/home/snehil/Desktop/output.txt","w",stdout);
    #endif
    solve();
    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;

#define INF 1e18
#define loop(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define forit(i, a) for ( __typeof( (a).begin() ) i = (a).begin(); i != (a).end(); i++ )
#define mem(a, v) memset(a, v, sizeof a)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define left(x) x<<1
#define right(x) (x<<1)|1
#define PI acos(-1.0)
#define EPS 1e-9

int T, N, M;
int col[100005], color[100005], a, b, m=-1, no;
bitset<100001> v[100001];

void solve(){
	cin>>N>>M;
    mem(color, -1);
    loop(i,1,N) {
        cin>>col[i];
        color[col[i]]=0;
    }
    loop(i,1,M) {
        cin>>a>>b;
        if(col[a]!=col[b] && !v[a][b])
            color[col[a]]++, color[col[b]]++;
        v[a][b]=1;
    }
    loop(i,1,100000)
        if(m<color[i]) {
            m = color[i];
            no = i;
        }
    cout<<no<<endl;
}

int main(){
    //clock_t start_s=clock();
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
  		freopen("/home/snehil/Desktop/input.txt","r",stdin);
    	freopen("/home/snehil/Desktop/output.txt","w",stdout);
    #endif
    T=1;
    while(T--){
    	solve();
    }
    //clock_t stop_s=clock();
    //cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;
    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;

#define INF 1e18
#define loop(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define forit(i, a) for ( __typeof( (a).begin() ) i = (a).begin(); i != (a).end(); i++ )
#define mem(a, v) memset(a, v, sizeof a)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define left(x) x<<1
#define right(x) (x<<1)|1
#define PI acos(-1.0)
#define EPS 1e-9

int T, N, M;
int col[100005], color[100005], a, b, m=-1, no;

void solve(){
	cin>>N>>M;
    mem(color, -1);
    loop(i,1,N) {
        cin>>col[i];
        color[col[i]]=0;
    }
    loop(i,1,M) {
        cin>>a>>b;
        if(col[a]!=col[b])
            color[col[a]]++, color[col[b]]++;
    }
    loop(i,1,100000)
        if(m<color[i]) {
            m = color[i];
            no = i;
        }
    cout<<no<<endl;
}

int main(){
    //clock_t start_s=clock();
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
  		freopen("/home/snehil/Desktop/input.txt","r",stdin);
    	freopen("/home/snehil/Desktop/output.txt","w",stdout);
    #endif
    T=1;
    while(T--){
    	solve();
    }
    //clock_t stop_s=clock();
    //cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;
    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;

#define INF 1e18
#define loop(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define forit(i, a) for ( __typeof( (a).begin() ) i = (a).begin(); i != (a).end(); i++ )
#define mem(a, v) memset(a, v, sizeof a)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define left(x) x<<1
#define right(x) (x<<1)|1
#define PI acos(-1.0)
#define EPS 1e-9

int T, N, M;
int col[100005], a, b, m, no;
bool present[100005];
vector<set<int> > v(100005);

void solve(){
	cin>>N>>M;

    loop(i,1,N)  {
        cin>>col[i];
        present[col[i]]=true;
    }

    no = col[N];
    m = -1;

    loop(i,1,M) {
        cin>>a>>b;
        if(col[a]!=col[b]){
            v[col[a]].insert(col[b]);
            v[col[b]].insert(col[a]);
        }
    }
    loop(i,1,100005) {
        //cout<<v[i].size()<<" "<<m<<endl;
        if(m < (int)v[i].size() && present[i]) {
             m = v[i].size();
             no = i;
        }
    }
    cout<<no<<endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
  		freopen("/home/snehil/Desktop/input.txt","r",stdin);
    	freopen("/home/snehil/Desktop/output.txt","w",stdout);
    #endif
    solve();
    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;

#define INF 1e18
#define loop(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define forit(i, a) for ( __typeof( (a).begin() ) i = (a).begin(); i != (a).end(); i++ )
#define mem(a, v) memset(a, v, sizeof a)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define left(x) x<<1
#define right(x) (x<<1)|1
#define PI acos(-1.0)
#define EPS 1e-9

int T, N, M;
int col[100005], a, b, m, no;
vector<set<int> > v(100005);

void solve(){
	cin>>N>>M;

    loop(i,1,N)
        cin>>col[i];

    no = col[N];
    m = -1;
    loop(i,1,M) {
        cin>>a>>b;
        if(col[a]!=col[b]){
            v[col[a]].insert(col[b]);
            v[col[b]].insert(col[a]);
        }
    }
    loop(i,1,100000) {
        if(m < (ll)v[i].size()) {
             m = v[i].size();
             no = i;
        }
    }
    cout<<no<<endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
  		freopen("/home/snehil/Desktop/input.txt","r",stdin);
    	freopen("/home/snehil/Desktop/output.txt","w",stdout);
    #endif
    solve();
    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;

#define INF 1e18
#define loop(i,a,b) for(ll i=(ll)a;i<=(ll)b;i++)
#define forit(i, a) for ( __typeof( (a).begin() ) i = (a).begin(); i != (a).end(); i++ )
#define mem(a, v) memset(a, v, sizeof a)
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define left(x) x<<1
#define right(x) (x<<1)|1
#define PI acos(-1.0)
#define EPS 1e-9

int T, N, M;
int col[100005], color[100005], a, b, m, no;

void solve(){
	cin>>N>>M;
    mem(color, -1);
    loop(i,1,N) {
        cin>>col[i];
        color[ col[i] ]=0;
    }
    loop(i,1,M) {
        cin>>a>>b;
        if(col[a]!=col[b]){
            color[col[a]]++;
            color[col[b]]++;
        }
    }
    m = -1;
    loop(i,1,100000)
        if(m<color[i]) {
            m = color[i];
            no = i;
        }
    cout<<no<<endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
  		freopen("/home/snehil/Desktop/input.txt","r",stdin);
    	freopen("/home/snehil/Desktop/output.txt","w",stdout);
    #endif
    solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll	    long long
#define vi(v)   vector <int> v
#define loop(n)	for(int i=0;i<n;i++)
bool is_prime(int n)
{
    if(n==2)
        return true;
    if(n%2==0||n<2)
        return false;
    for(int i=3;i<=sqrt(n);i+=2)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
int main()
{
    int n,m,z=1e5;
    cin>>n>>m;
    int col[100005];
    vector<vector<int>>v(100005);
    loop(n)
    {
        int x;
        cin>>x;
        if(x<z)
            z=x;
        col[i+1]=x;
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        v[col[x]].push_back(col[y]);
        v[col[y]].push_back(col[x]);
    }
    int a[100001]={};
    loop(100001)
    {
        set<int>s;
        for(int j=0;j<v[i].size();j++)
        {
            if(v[i][j]!=i)
                s.insert(v[i][j]);
        }
        a[i]=s.size();
    }
    int x=0,y=0;
    loop(100001)
    {
        if(a[i]>x)
        {
            x=a[i];
            y=i;
        }
    }
    if(y==0)
    {
        y=z;
    }
    cout<<y;

}


#include <bits/stdc++.h>
using namespace std;
#define ll	    long long
#define vi(v)   vector <int> v
#define loop(n)	for(int i=0;i<n;i++)
bool is_prime(int n)
{
    if(n==2)
        return true;
    if(n%2==0||n<2)
        return false;
    for(int i=3;i<=sqrt(n);i+=2)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
int main()
{
    int n,m;
    cin>>n>>m;
    int col[100005];
    vector<set<int>>v(100005);
    loop(n)
    {
        int x;
        cin>>x;
        col[i+1]=x;
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        v[col[x]].insert(col[y]);
        v[col[y]].insert(col[x]);
    }
    int x=0,y=0;
    loop(100001)
    {
        if(v[i].size()>x)
        {
            x=v[i].size();
            y=i;
        }
    }
    cout<<y;

}


#include <bits/stdc++.h>
using namespace std;
#define ll	    long long
#define vi(v)   vector <int> v
#define loop(n)	for(int i=0;i<n;i++)
bool is_prime(int n)
{
    if(n==2)
        return true;
    if(n%2==0||n<2)
        return false;
    for(int i=3;i<=sqrt(n);i+=2)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
int main()
{
    int n,m;
    cin>>n>>m;
    int col[100005];
    vector<vector<int>>v(100005);
    loop(n)
    {
        int x;
        cin>>x;
        col[i+1]=x;
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        v[x].push_back(col[y]);
        v[y].push_back(col[x]);
    }
    int a[100001]={};
    loop(100001)
    {
        set<int>s;
        for(int j=0;j<v[i].size();j++)
            s.insert(v[i][j]);
        a[i]=s.size();
    }
    int x=0,y=0;
    loop(100001)
    {
        if(a[i]>x)
        {
            x=a[i];
            y=col[i];
        }
        else if(a[i]==x)
        {
            if(col[i]<y)
                y=col[i];
        }

    }
    cout<<y;

}


#include <bits/stdc++.h>
using namespace std;
#define ll	    long long
#define vi(v)   vector <int> v
#define loop(n)	for(int i=0;i<n;i++)
bool is_prime(int n)
{
    if(n==2)
        return true;
    if(n%2==0||n<2)
        return false;
    for(int i=3;i<=sqrt(n);i+=2)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
int main()
{
    int n,m;
    cin>>n>>m;
    int col[100005];
    vector<vector<int>>v(100005);
    loop(n)
    {
        int x;
        cin>>x;
        col[i+1]=x;
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        v[col[x]].push_back(col[y]);
        v[col[y]].push_back(col[x]);
    }
    int a[100001]={};
    loop(100001)
    {
        set<int>s;
        for(int j=0;j<v[i].size();j++)
            s.insert(v[i][j]);
        a[i]=s.size();
    }
    int x=0,y=0;
    loop(100001)
    {
        if(a[i]>x)
        {
            x=a[i];
            y=i;
        }
    }
    cout<<y;

}


#include <bits/stdc++.h>
using namespace std;
#define ll	    long long
#define vi(v)   vector <int> v
#define loop(n)	for(int i=0;i<n;i++)
bool is_prime(int n)
{
    if(n==2)
        return true;
    if(n%2==0||n<2)
        return false;
    for(int i=3;i<=sqrt(n);i+=2)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
int main()
{
    int n,m;
    cin>>n>>m;
    int col[100005];
    vector<vector<int>>v(100005);
    loop(n)
    {
        int x;
        cin>>x;
        col[i+1]=x;
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        v[col[x]].push_back(col[y]);
        v[col[y]].push_back(col[x]);
    }
    int a[100001]={};
    loop(100001)
    {
        set<int>s;
        for(int j=0;j<v[i].size();j++)
        {
            if(v[i][j]!=i)
                s.insert(v[i][j]);
        }
        a[i]=s.size();
    }
    int x=0,y=0;
    loop(100001)
    {
        if(a[i]>x)
        {
            x=a[i];
            y=i;
        }
    }
    if(y==0)
    {
        loop(100001)
        {
            if(col[i]>0)
            {
                y=col[i];
                break;
            }
        }
    }
    cout<<y;

}


#include <bits/stdc++.h>
using namespace std;
#define ll	    long long
#define vi(v)   vector <int> v
#define loop(n)	for(int i=0;i<n;i++)
bool is_prime(int n)
{
    if(n==2)
        return true;
    if(n%2==0||n<2)
        return false;
    for(int i=3;i<=sqrt(n);i+=2)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
int main()
{
    int n,m;
    cin>>n>>m;
    int col[100005];
    vector<vector<int>>v(100005);
    loop(n)
    {
        int x;
        cin>>x;
        col[i+1]=x;
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        v[col[x]].push_back(col[y]);
        v[col[y]].push_back(col[x]);
    }
    int a[100001]={};
    loop(100001)
    {
        set<int>s;
        for(int j=0;j<v[i].size();j++)
        {
            if(v[i][j]!=i)
                s.insert(v[i][j]);
        }
        a[i]=s.size();
    }
    int x=0,y=0;
    loop(100001)
    {
        if(a[i]>x)
        {
            x=a[i];
            y=i;
        }
    }
    cout<<y;

}


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

int n, m;
int col[100100];
int frq[100100];
vector<int> adj[100100];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
//freopen("ot.txt", "wt", stdout);
#endif
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &col[i]);
    for (int i = 1; i < m; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
        set<int> st;
        for (int j = 0; j < (int) adj[i].size(); j++)
            st.insert(col[adj[i][j]]);
        frq[col[i]] = max(frq[col[i]], (int) st.size() - 1);
    }
    int ans = n;
    for (int i = n; i > 0; i--) {
        if (frq[col[i]] > frq[col[ans]])
            ans = i;
        else if (frq[col[i]] == frq[col[ans]]) {
            if (col[i] < col[ans])
                ans = i;
        }
    }
    cout << col[ans];
}


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

int n, m;
int col[100100];
set<int> frq[100100];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
//freopen("ot.txt", "wt", stdout);
#endif
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &col[i]);
    for (int i = 1; i < m; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        if (col[u] != col[v]) {
            frq[col[u]].insert(v);
            frq[col[v]].insert(u);
        }
    }
    int ans = 1;
    for (int i = 1; i <= n; i++) {
        if ((int) frq[col[i]].size() > (int) frq[col[ans]].size())
            ans = i;
        else if ((int) frq[col[i]].size() == (int) frq[col[ans]].size())
            if (col[i] < col[ans])
                ans = i;
    }
    cout << col[ans];
}


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

int n, m;
int col[100100];
set<int> frq[100100];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
//freopen("ot.txt", "wt", stdout);
#endif
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &col[i]);
    for (int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        frq[col[u]].insert(col[v]);
        frq[col[v]].insert(col[u]);
    }
    int ans = 1;
    for (int i = 1; i <= n; i++) {
        if ((int) frq[col[i]].size() > (int) frq[col[ans]].size())
            ans = i;
        else if ((int) frq[col[i]].size() == (int) frq[col[ans]].size())
            if (col[i] < col[ans])
                ans = i;
    }
    cout << col[ans];
}


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

int n, m;
int col[100100];
set<int> frq[100100];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
//freopen("ot.txt", "wt", stdout);
#endif
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &col[i]);
    for (int i = 1; i < m; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        if (col[u] != col[v]) {
            frq[col[u]].insert(v);
            frq[col[v]].insert(u);
        }
    }
    int ans = col[1];
    for (int i = 1; i <= n; i++) {
        if ((int) frq[col[i]].size() > (int) frq[col[ans]].size())
            ans = i;
    }
    cout << col[ans];
}


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

int n, m;
int col[100100];
set<int> frq[100100];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
//freopen("ot.txt", "wt", stdout);
#endif
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &col[i]);
    for (int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        if (col[u] == col[v])
            continue;
        frq[col[u]].insert(col[v]);
        frq[col[v]].insert(col[u]);
    }
    int ans = 1;
    for (int i = 1; i <= n; i++) {
        if ((int) frq[col[i]].size() > (int) frq[col[ans]].size())
            ans = i;
        else if ((int) frq[col[i]].size() == (int) frq[col[ans]].size())
            if (col[i] < col[ans])
                ans = i;
    }
    cout << col[ans];
}


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

int n, m;
int col[100100];
set<int> frq[100100];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
//freopen("ot.txt", "wt", stdout);
#endif
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &col[i]);
    for (int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        if (u == v)
            continue;
        frq[col[u]].insert(col[v]);
        frq[col[v]].insert(col[u]);
    }
    int ans = 1;
    for (int i = 1; i <= n; i++) {
        if ((int) frq[col[i]].size() > (int) frq[col[ans]].size())
            ans = i;
        else if ((int) frq[col[i]].size() == (int) frq[col[ans]].size())
            if (col[i] < col[ans])
                ans = i;
    }
    cout << col[ans];
}


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

int n, m;
int col[100100];
set<int> frq[100100];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
//freopen("ot.txt", "wt", stdout);
#endif
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &col[i]);
    for (int i = 1; i < m; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        frq[col[u]].insert(v);
        frq[col[v]].insert(u);
    }
    int ans = 1;
    for (int i = 1; i <= n; i++) {
        if ((int) frq[col[i]].size() > (int) frq[col[ans]].size())
            ans = i;
        else if ((int) frq[col[i]].size() == (int) frq[col[ans]].size())
            if (col[i] < col[ans])
                ans = i;
    }
    cout << col[ans];
}


#include <bits/stdc++.h>
#define FastIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

using namespace std;
typedef long long ll;

int cl[100005];
int car[100005];
int col[100005];
vector<int>grf[100005];
map<pair<int,int>,int>mp;
bool vis[100005];

void dfs(int node)
{
    if(vis[node])    return;
    vis[node]=1;
    for(int i=0;i<grf[node].size();i++)    {
        int nxt=grf[node][i];
        if(!mp[make_pair(cl[node],nxt)] && cl[node]!=cl[nxt])    {car[cl[nxt]]++;mp[make_pair(cl[node],nxt)]=1;}
        dfs(nxt);
    }
}

int main()
{
    FastIO();

    int n,m;
    cin>>n>>m;

    for(int i=1;i<=n;i++)    {
        cin>>cl[i];
        col[cl[i]]=1;
    }

    for(int i=0;i<m;i++)    {
        int a,b;
        cin>>a>>b;
        grf[a].push_back(b);
        grf[b].push_back(a);
    }

    for(int i=1;i<=n;i++)    dfs(i);

    int ans=0;
    int mx=0;

    for(int i=100000;i>0;i--)    {
        if(col[i])    {
            if(car[i]>=mx)    {
                ans=i;
                mx=car[i];
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define FastIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

using namespace std;
typedef long long ll;

int cl[100005];
int car[100005];
int col[100005];
vector<int>grf[100005];
map<pair<int,int>,int>mp;
bool vis[100005];

void dfs(int node)
{
    if(vis[node])    return;
    vis[node]=1;
    for(int i=0;i<grf[node].size();i++)    {
        int nxt=grf[node][i];
        if(!mp[make_pair(cl[nxt],node)] && cl[node]!=cl[nxt])    {car[cl[nxt]]++;mp[make_pair(cl[nxt],node)]=1;}
        dfs(nxt);
    }
}

int main()
{
    FastIO();

    int n,m;
    cin>>n>>m;

    for(int i=1;i<=n;i++)    {
        cin>>cl[i];
        col[cl[i]]=1;
    }

    for(int i=0;i<m;i++)    {
        int a,b;
        cin>>a>>b;
        grf[a].push_back(b);
        grf[b].push_back(a);
    }

    for(int i=1;i<=n;i++)    dfs(i);

    int ans=0;
    int mx=0;

    for(int i=100000;i>0;i--)    {
        if(col[i])    {
            if(car[i]>=mx)    {
                ans=i;
                mx=car[i];
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define FastIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

using namespace std;
typedef long long ll;

int cl[100005];
int car[100005];
int col[100005];
vector<int>grf[100005];
map<pair<int,int>,int>mp;
bool vis[100005];

void dfs(int node)
{
    if(vis[node])    return;
    vis[node]=1;
    for(int i=0;i<grf[node].size();i++)    {
        int nxt=grf[node][i];
        if(!mp[make_pair(cl[node],cl[nxt])] && cl[node]!=cl[nxt])    {car[cl[node]]++;mp[make_pair(cl[node],cl[nxt])]=1;}
        dfs(nxt);
    }
}

int main()
{
    FastIO();

    int n,m;
    cin>>n>>m;

    for(int i=1;i<=n;i++)    {
        cin>>cl[i];
        col[cl[i]]=1;
    }

    for(int i=0;i<m;i++)    {
        int a,b;
        cin>>a>>b;
        grf[a].push_back(b);
        grf[b].push_back(a);
    }

    for(int i=1;i<=n;i++)    dfs(i);

    int ans=0;
    int mx=0;

    for(int i=100000;i>0;i--)    {
        if(col[i])    {
            if(car[i]>=mx)    {
                ans=i;
                mx=car[i];
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define FastIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

using namespace std;
typedef long long ll;

int cl[100005];
int car[100005];
int col[100005];
vector<int>grf[100005];

bool vis[100005];

void dfs(int node)
{
    if(vis[node])    return;
    vis[node]=1;
    for(int i=0;i<grf[node].size();i++)    {
        int nxt=grf[node][i];
        if(cl[node]!=cl[nxt])    car[cl[node]]++;
        dfs(nxt);
    }
}

int main()
{
    FastIO();

    int n,m;
    cin>>n>>m;

    for(int i=1;i<=n;i++)    {
        cin>>cl[i];
        col[cl[i]]=1;
    }

    for(int i=0;i<m;i++)    {
        int a,b;
        cin>>a>>b;
        grf[a].push_back(b);
        grf[b].push_back(a);
    }

    for(int i=1;i<=n;i++)    dfs(i);

    int ans=0;
    int mx=0;

    for(int i=100005;i>0;i--)    {
        if(col[i])    {
            if(car[i]>=mx)    {
                ans=i;
                mx=car[i];
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define FastIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

using namespace std;
typedef long long ll;

int cl[100005];
int car[100005];
int col[100005];
vector<int>grf[100005];

bool vis[100005];

void dfs(int node)
{
    if(vis[node])    return;
    vis[node]=1;
    for(int i=0;i<grf[node].size();i++)    {
        int nxt=grf[node][i];
        if(cl[node]!=cl[nxt])    car[cl[nxt]]++;
        dfs(nxt);
    }
}

int main()
{
    FastIO();

    int n,m;
    cin>>n>>m;

    for(int i=1;i<=n;i++)    {
        cin>>cl[i];
        col[cl[i]]=1;
    }

    for(int i=0;i<m;i++)    {
        int a,b;
        cin>>a>>b;
        grf[a].push_back(b);
        grf[b].push_back(a);
    }

    for(int i=1;i<=n;i++)    dfs(i);

    int ans=0;
    int mx=0;

    for(int i=100000;i>0;i--)    {
        if(col[i])    {
            if(car[i]>=mx)    {
                ans=i;
                mx=car[i];
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define FastIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

using namespace std;
typedef long long ll;

int cl[100005];
int car[100005];
int col[100005];
vector<int>grf[100005];
map<pair<int,int>,int>mp;
bool vis[100005];

void dfs(int node)
{
    if(vis[node])    return;
    vis[node]=1;
    for(int i=0;i<grf[node].size();i++)    {
        int nxt=grf[node][i];
        if(!mp[make_pair(cl[nxt],node)] && cl[node]!=cl[nxt])    {car[cl[node]]++;mp[make_pair(cl[nxt],node)]=1;}
        dfs(nxt);
    }
}

int main()
{
    FastIO();

    int n,m;
    cin>>n>>m;

    for(int i=1;i<=n;i++)    {
        cin>>cl[i];
        col[cl[i]]=1;
    }

    for(int i=0;i<m;i++)    {
        int a,b;
        cin>>a>>b;
        grf[a].push_back(b);
        grf[b].push_back(a);
    }

    for(int i=1;i<=n;i++)    dfs(i);

    int ans=0;
    int mx=0;

    for(int i=100000;i>0;i--)    {
        if(col[i])    {
            if(car[i]>=mx)    {
                ans=i;
                mx=car[i];
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}


#include<iostream>
using namespace std;
#include<bits/stdc++.h>
#define ll long long
vector<vector<ll > > g;
vector<bool> vis;
vector<ll> col;
map<ll,ll> m;

void dfs(int i)
{
    if(!vis[i])
    {
        vis[i]=true;
        for(vector<ll>::iterator it=g[i].begin();it!=g[i].end();it++)
        {
            if(col[*it]!=col[i])
            {
                map<ll,ll>::iterator itr=m.find(col[i]);
                if(itr==m.end())
                    m[col[i]]=1;
                else
                    itr->second++;
            }

            if(!vis[*it])
                dfs(*it);
        }
    }

}

int main()
{

    ll n,m1;
    cin>>n>>m1;
    col=vector<ll>(n+1,0);
    g=vector<vector<ll> >(n+1);
    ll c=99999999999999;
    for(ll i=1;i<=n;i++)
        {cin>>col[i];
        if(col[i]<c)
            c=col[i];
        }
    for(int i=0;i<m1;i++)
    {
        int u,v;
        cin>>u>>v;
        if(col[u]!=col[v])
        {m[col[u]]++;
        m[col[v]]++;
        }
    }

    vis=vector<bool>(n+1,false);

    //ll c=col[1];
    ll ans=-999999999999999;
    for(map<ll,ll>::iterator itr=m.begin();itr!=m.end();itr++)
        {
            if(itr->second>ans)
                {c=itr->first;
                ans=itr->second;}

        }

     for(map<ll,ll>::iterator itr=m.begin();itr!=m.end();itr++)
        {
            if(itr->second==ans && itr->first<c)
                {c=itr->first;
                }

        }

        cout<<c<<endl;

}


#include<iostream>
using namespace std;
#include<bits/stdc++.h>
#define ll long long
vector<vector<ll > > g;
vector<bool> vis;
vector<ll> col;
map<ll,ll> m;

void dfs(int i)
{
    if(!vis[i])
    {
        vis[i]=true;
        for(vector<ll>::iterator it=g[i].begin();it!=g[i].end();it++)
        {
            if(col[*it]!=col[i])
            m[col[i]]++;

            if(!vis[*it])
                dfs(*it);
        }
    }

}

int main()
{

    ll n,m1;
    cin>>n>>m1;
    col=vector<ll>(n+1,0);
    g=vector<vector<ll> >(n+1);
    for(ll i=1;i<=n;i++)
        cin>>col[i];
    for(int i=0;i<m1;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    vis=vector<bool>(n+1,false);

    for(ll i=1;i<=n;i++)
    {   if(!vis[i])
            dfs(i);
    }
    ll c;
    ll ans=-999999999999999;
    for(map<ll,ll>::iterator itr=m.begin();itr!=m.end();itr++)
        {
            if(itr->second>ans)
                {c=itr->first;
                ans=itr->second;}

        }
        cout<<c<<endl;

}


#include<iostream>
using namespace std;
#include<bits/stdc++.h>
#define ll long long
vector<vector<ll > > g;
vector<bool> vis;
vector<ll> col;
map<ll,ll> m;

void dfs(int i)
{
    if(!vis[i])
    {
        vis[i]=true;
        for(vector<ll>::iterator it=g[i].begin();it!=g[i].end();it++)
        {
            if(col[*it]!=col[i])
            {
                map<ll,ll>::iterator itr=m.find(col[i]);
                if(itr==m.end())
                    m[col[i]]=1;
                else
                    itr->second++;
            }

            if(!vis[*it])
                dfs(*it);
        }
    }

}

int main()
{

    ll n,m1;
    cin>>n>>m1;
    col=vector<ll>(n+1,0);
    g=vector<vector<ll> >(n+1);
    ll c=99999999999999;
    for(ll i=1;i<=n;i++)
        {cin>>col[i];
        if(col[i]<c)
            c=col[i];
        }
    for(int i=0;i<m1;i++)
    {
        int u,v;
        cin>>u>>v;
        if(col[u]!=col[v])
        {m[col[u]]++;
        m[col[v]]++;
        }
    }

    vis=vector<bool>(n+1,false);

    //ll c=col[1];
    ll ans=-999999999999999;
    for(map<ll,ll>::iterator itr=m.begin();itr!=m.end();itr++)
        {
            if(itr->second>ans)
                {c=itr->first;
                ans=itr->second;}
            else if(itr->second==ans && itr->first<c)
            {
                c=itr->first;
            }

        }
        cout<<c<<endl;

}


#include<iostream>
using namespace std;
#include<bits/stdc++.h>
#define ll long long
vector<vector<ll > > g;
vector<bool> vis;
vector<ll> col;
map<ll,ll> m;

void dfs(int i)
{
    if(!vis[i])
    {
        vis[i]=true;
        for(vector<ll>::iterator it=g[i].begin();it!=g[i].end();it++)
        {
            if(col[*it]!=col[i])
            m[col[i]]++;

            if(!vis[*it])
                dfs(*it);
        }
    }

}

int main()
{

    ll n,m1;
    cin>>n>>m1;
    col=vector<ll>(n+1,0);
    g=vector<vector<ll> >(n+1);
    ll c=99999999999999;
    for(ll i=1;i<=n;i++)
        {cin>>col[i];
        if(col[i]<c)
            c=col[i];
        }
    for(int i=0;i<m1;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    vis=vector<bool>(n+1,false);

    for(ll i=1;i<=n;i++)
    {   if(!vis[i])
            dfs(i);
    }
    //ll c=col[1];
    ll ans=-999999999999999;
    for(map<ll,ll>::iterator itr=m.begin();itr!=m.end();itr++)
        {
            if(itr->second>ans)
                {c=itr->first;
                ans=itr->second;}
            else if(itr->second==ans && itr->first<c)
            {
                c=itr->first;
            }

        }
        cout<<c<<endl;

}


#include<iostream>
using namespace std;
#include<bits/stdc++.h>
#define ll long long
vector<vector<ll > > g;
vector<bool> vis;
vector<ll> col;
map<ll,ll> m;

void dfs(int i)
{
    if(!vis[i])
    {
        vis[i]=true;
        for(vector<ll>::iterator it=g[i].begin();it!=g[i].end();it++)
        {
            if(col[*it]!=col[i])
            m[col[i]]++;

            if(!vis[*it])
                dfs(*it);
        }
    }

}

int main()
{

    ll n,m1;
    cin>>n>>m1;
    col=vector<ll>(n+1,0);
    g=vector<vector<ll> >(n+1);
    for(ll i=1;i<=n;i++)
        cin>>col[i];
    for(int i=0;i<m1;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    vis=vector<bool>(n+1,false);

    for(ll i=1;i<=n;i++)
    {   if(!vis[i])
            dfs(i);
    }
    ll c=col[1];
    ll ans=-999999999999999;
    for(map<ll,ll>::iterator itr=m.begin();itr!=m.end();itr++)
        {
            if(itr->second>ans)
                {c=itr->first;
                ans=itr->second;}

        }
        cout<<c<<endl;

}


#include<iostream>
using namespace std;
#include<bits/stdc++.h>
#define ll long long
vector<vector<ll > > g;
vector<bool> vis;
vector<ll> col;
map<ll,ll> m;

void dfs(int i)
{
    if(!vis[i])
    {
        vis[i]=true;
        for(vector<ll>::iterator it=g[i].begin();it!=g[i].end();it++)
        {
            if(col[*it]!=col[i])
            {
                map<ll,ll>::iterator itr=m.find(col[i]);
                if(itr==m.end())
                    m[col[i]]=1;
                else
                    itr->second++;
            }

            if(!vis[*it])
                dfs(*it);
        }
    }

}

int main()
{

    ll n,m1;
    cin>>n>>m1;
    col=vector<ll>(n+1,0);
    g=vector<vector<ll> >(n+1);
    ll c=99999999999999;
    for(ll i=1;i<=n;i++)
        {cin>>col[i];
        if(col[i]<c)
            c=col[i];
        }
    for(int i=0;i<m1;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    vis=vector<bool>(n+1,false);

    for(ll i=1;i<=n;i++)
    {   if(!vis[i])
            dfs(i);
    }
    //ll c=col[1];
    ll ans=-999999999999999;
    for(map<ll,ll>::iterator itr=m.begin();itr!=m.end();itr++)
        {
            if(itr->second>ans)
                {c=itr->first;
                ans=itr->second;}
            else if(itr->second==ans && itr->first<c)
            {
                c=itr->first;
            }

        }
        cout<<c<<endl;

}


#include<iostream>
using namespace std;
#include<bits/stdc++.h>
#define ll long long
vector<vector<ll > > g;
vector<bool> vis;
vector<ll> col;
map<ll,set<ll> > m;

int main()
{

    ll n,m1;
    cin>>n>>m1;
    col=vector<ll>(n+1,0);
    //g=vector<vector<ll> >(n+1);
    ll c=99999999999999;
    for(ll i=1;i<=n;i++)
        {cin>>col[i];
        if(col[i]<c)
            c=col[i];
        }
    for(int i=0;i<m1;i++)
    {
        int u,v;
        cin>>u>>v;
        if(col[u]!=col[v])
        {m[col[u]].insert(col[v]);
        m[col[v]].insert(col[u]);
        }
    }

    //vis=vector<bool>(n+1,false);

    //ll c=col[1];
    ll ans=-999999999999999;
    for(map<ll,set<ll> >::iterator itr=m.begin();itr!=m.end();itr++)
        {
            if(itr->second.size()>ans)
                {c=itr->first;
                ans=itr->second.size();}

        }

     for(map<ll,set<ll> >::iterator itr=m.begin();itr!=m.end();itr++)
        {
            if(itr->second.size()==ans && itr->first<c)
                {c=itr->first;

                }
        }

        cout<<c<<endl;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 1e5 + 10;
int n, m, clr[N], t1, t2, ans, ind;
vector<set<int> > grp;

int main() {
	scanf("%d%d", &n, &m);
	grp.resize(N);
	for (int i = 0; i < n; i++) {
		scanf("%d", &clr[i]);
	}
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &t1, &t2);
		t1--, t2--;
		if (clr[t1] == clr[t2]) {
			continue;
		}
		grp[clr[t1]].insert(clr[t2]);
		grp[clr[t2]].insert(clr[t1]);
	}
	for (int i = 0; i < N; i++) {
		int sz = grp[i].size();
		if (sz > ans) {
			ans = sz;
			ind = i;
		}
	}
	printf("%d", ind);
	return 0;
}


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

typedef long long int ll;

const int N = 1e5 + 10;
int n, m, clr[N], t1, t2, ans = -1, ind;
bool vis[N];
vector<set<int> > grp(N + 10);

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &clr[i]);
vis[clr[i]] = true;
	}
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &t1, &t2);
		t1--, t2--;
		if (clr[t1] == clr[t2]) {
			continue;
		}
		grp[clr[t1]].insert(clr[t2]);
		grp[clr[t2]].insert(clr[t1]);
	}
	for (int i = 0; i < N; i++) {
		int sz = grp[i].size();
		if (sz > ans && vis[i]) {
			ans = sz;
			ind = i;
		}
	}
	printf("%d", ind);
	return 0;}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 1e5 + 10;
int n, m, clr[N], t1, t2, ans = -1, ind;
bool vis[N];
set<int> grp[N];

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &clr[i]);
		vis[clr[i]] = true;
	}
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &t1, &t2);
		t1--, t2--;
		if (clr[t1] == clr[t2]) {
			continue;
		}
		grp[clr[t1]].insert(clr[t2]);
		grp[clr[t2]].insert(clr[t1]);
	}
	for (int i = 0; i < N; i++) {
		int sz = grp[i].size();
		if (sz > ans && vis[i]) {
			ans = sz;
			ind = i;
		}
	}
	printf("%d", ind);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 1e5 + 10;
int n, m, clr[N], t1, t2, ans, ind;
vector<set<int> > grp(N + 10);

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &clr[i]);
	}
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &t1, &t2);
		t1--, t2--;
		if (clr[t1] == clr[t2]) {
			continue;
		}
		grp[clr[t1]].insert(clr[t2]);
		grp[clr[t2]].insert(clr[t1]);
	}
	for (int i = 0; i < N; i++) {
		int sz = grp[i].size();
		if (sz > ans) {
			ans = sz;
			ind = i;
		}
	}
	printf("%d", ind);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 1e5 + 10;
int n, m, clr[N], t1, t2, ans, ind;
vector<set<int> > grp;

int main() {
	scanf("%d%d", &n, &m);
	grp.resize(n + 10);
	for (int i = 0; i < n; i++) {
		scanf("%d", &clr[i]);
	}
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &t1, &t2);
		t1--, t2--;
		if (clr[t1] == clr[t2]) {
			continue;
		}
		grp[clr[t1]].insert(clr[t2]);
		grp[clr[t2]].insert(clr[t1]);
	}
	for (int i = 0; i < n; i++) {
		int sz = grp[i].size();
		if (sz > ans) {
			ans = sz;
			ind = i;
		}
	}
	printf("%d", ind);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 1e5 + 10;
int n, m, clr[N], t1, t2, ans, ind;
set<int> grp[N];

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &clr[i]);
	}
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &t1, &t2);
		t1--, t2--;
		if (clr[t1] == clr[t2]) {
			continue;
		}
		grp[clr[t1]].insert(clr[t2]);
		grp[clr[t2]].insert(clr[t1]);
	}
	for (int i = 0; i < N; i++) {
		int sz = grp[i].size();
		if (sz > ans) {
			ans = sz;
			ind = i;
		}
	}
	printf("%d", ind);
	return 0;
}


using namespace std;

typedef long long int ll;

const int N = 1e5 + 10;
int n, m, clr[N], t1, t2, ans = -1, ind;
bool vis[N];
vector<set<int> > grp(N + 10);

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &clr[i]);
vis[clr[i]] = true;
	}
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &t1, &t2);
		t1--, t2--;
		if (clr[t1] == clr[t2]) {
			continue;
		}
		grp[clr[t1]].insert(clr[t2]);
		grp[clr[t2]].insert(clr[t1]);
	}
	for (int i = 0; i < N; i++) {
		int sz = grp[i].size();
		if (sz > ans && vis[i]) {
			ans = sz;
			ind = i;
		}
	}
	printf("%d", ind);
	return 0;


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 1e5 + 10;
int n, m, clr[N], t1, t2, ans, ind;
vector<set<int> > grp(N + 10);

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &clr[i]);
	}
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &t1, &t2);
		t1--, t2--;
		//if (clr[t1] == clr[t2]) {
		//	continue;
		//}
		grp[clr[t1]].insert(clr[t2]);
		grp[clr[t2]].insert(clr[t1]);
	}
	for (int i = 0; i < N; i++) {
		int sz = grp[i].size();
		if (sz > ans) {
			ans = sz;
			ind = i;
		}
	}
	printf("%d", ind);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 1e5 + 10;
int n, m, clr[N], t1, t2, ans = -1, ind;
bool vis[N];
vector<set<int> > grp(N + 10);

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &clr[i]);
	}
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &t1, &t2);
		t1--, t2--;
		if (clr[t1] == clr[t2]) {
			continue;
		}
		grp[clr[t1]].insert(clr[t2]);
		grp[clr[t2]].insert(clr[t1]);
	}
	for (int i = 0; i < N; i++) {
		int sz = grp[i].size();
		if (sz > ans && vis[I]) {
			ans = sz;
			ind = i;
		}
	}
	printf("%d", ind);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 1e5 + 10;
int n, m, clr[N], t1, t2, ans, ind;
vector<set<int> > grp;

int main() {
	scanf("%d%d", &n, &m);
	grp.resize(m + 10);
	for (int i = 0; i < n; i++) {
		scanf("%d", &clr[i]);
	}
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &t1, &t2);
		t1--, t2--;
		if (clr[t1] == clr[t2]) {
			continue;
		}
		grp[clr[t1]].insert(clr[t2]);
		grp[clr[t2]].insert(clr[t1]);
	}
	for (int i = 0; i < m; i++) {
		int sz = grp[i].size();
		if (sz > ans) {
			ans = sz;
			ind = i;
		}
	}
	printf("%d", ind);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 1e5 + 10;
int n, m, clr[N], t1, t2, ans, ind;
vector<set<int> > grp;

int main() {
	scanf("%d%d", &n, &m);
	grp.resize(N + 10);
	for (int i = 0; i < n; i++) {
		scanf("%d", &clr[i]);
	}
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &t1, &t2);
		t1--, t2--;
		if (clr[t1] == clr[t2]) {
			continue;
		}
		grp[clr[t1]].insert(clr[t2]);
		grp[clr[t2]].insert(clr[t1]);
	}
	for (int i = 0; i < N; i++) {
		int sz = grp[i].size();
		if (sz > ans) {
			ans = sz;
			ind = i;
		}
	}
	printf("%d", ind);
	return 0;
}


// http://codeforces.com/problemset/problem/246/D

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

#define ll long long
#define eps 1e-10
#define fi first
#define se second
#define MOD1 1000000007LL
#define MOD 1000000009LL
#define p1 53
#define se second
#define inf 20000000000LL
#define pb push_back
#define mp make_pair
#define maxn 300005

ll gcd1(ll a, ll b) {
    if (a == 0)
        return b;
    return gcd1(b % a, a);
}

ll modx(ll base, ll ex)
{
    ll ans = 1LL, val = base;
    while(ex > 0LL)
    {
        if(ex&1LL)
            ans = (ans*val)%MOD;
        val = (val*val)%MOD;
        ex = ex>>1LL;
    }
    return ans;
}

int color[maxn];
int cnt[maxn];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m;

    cin >> n >> m;

    for (int i = 1; i < maxn; i++)
        cnt[i] = -1;

    for (int i = 1; i <= n; i++) {
        cin >> color[i];
        cnt[color[i]] = 0;
    }

    int x, y, ans = INT_MIN, cl = INT_MAX, maxx = INT_MIN;
    for (int i = 1; i <= m; i++) {
        cin >> x >> y;
        if (color[x] != color[y]) {
            cnt[color[x]]++;
            cnt[color[y]]++;
        }
        maxx = max(maxx, max(color[x], color[y]));
    }

    for (int i = 1; i <= maxx; i++) {
        if (cnt[i] > ans) {
            ans = cnt[i];
            cl = i;
        }
        else if (cnt[i] == ans)
            cl = min(cl, i);
    }

    cout << cl << endl;

    return 0;
}


// http://codeforces.com/problemset/problem/246/D

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

#define ll long long
#define eps 1e-10
#define fi first
#define se second
#define MOD1 1000000007LL
#define MOD 1000000009LL
#define p1 53
#define se second
#define inf 20000000000LL
#define pb push_back
#define mp make_pair
#define maxn 300005

ll gcd1(ll a, ll b) {
    if (a == 0)
        return b;
    return gcd1(b % a, a);
}

ll modx(ll base, ll ex)
{
    ll ans = 1LL, val = base;
    while(ex > 0LL)
    {
        if(ex&1LL)
            ans = (ans*val)%MOD;
        val = (val*val)%MOD;
        ex = ex>>1LL;
    }
    return ans;
}

int color[maxn];
int cnt[maxn];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m;

    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        cin >> color[i];
    }

    int x, y, ans = INT_MIN, cl = INT_MAX;
    for (int i = 1; i <= m; i++) {
        cin >> x >> y;
        if (color[x] != color[y]) {
            cnt[color[x]]++;
            cnt[color[y]]++;
        }

        if (cnt[color[x]] > ans) {
            ans = cnt[color[x]];
            cl = color[x];
        }
        else if (cnt[color[x]] == ans)
            cl = min(color[x], cl);

        if (cnt[color[y]] > ans) {
            ans = cnt[color[y]];
            cl = color[y];
        }
        else if (cnt[color[y]] == ans)
            cl = min(color[y], cl);
    }

    cout << cl << endl;

    return 0;
}


// http://codeforces.com/problemset/problem/246/D

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

#define ll long long
#define eps 1e-10
#define fi first
#define se second
#define MOD1 1000000007LL
#define MOD 1000000009LL
#define p1 53
#define se second
#define inf 20000000000LL
#define pb push_back
#define mp make_pair
#define maxn 300005

ll gcd1(ll a, ll b) {
    if (a == 0)
        return b;
    return gcd1(b % a, a);
}

ll modx(ll base, ll ex)
{
    ll ans = 1LL, val = base;
    while(ex > 0LL)
    {
        if(ex&1LL)
            ans = (ans*val)%MOD;
        val = (val*val)%MOD;
        ex = ex>>1LL;
    }
    return ans;
}

int color[maxn];
set<int> cnt[maxn];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m;

    cin >> n >> m;

    int maxx = INT_MIN;
    for (int i = 1; i <= n; i++) {
        cin >> color[i];
        maxx = max(maxx, color[i]);
    }

    int x, y;
    for (int i = 1; i <= m; i++) {
        cin >> x >> y;
        if (color[x] != color[y]) {
            cnt[color[x]].insert(color[y]);
            cnt[color[y]].insert(color[x]);
        }
    }

    int ans = -1, cl;
    for (int i = 1; i <= maxx; i++) {
        int sz = cnt[i].size();
        if (sz > ans) {
            ans = sz;
            cl = i;
        }
    }

    cout << cl << endl;

    return 0;
}


// http://codeforces.com/problemset/problem/246/D

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

#define ll long long
#define eps 1e-10
#define fi first
#define se second
#define MOD1 1000000007LL
#define MOD 1000000009LL
#define p1 53
#define se second
#define inf 20000000000LL
#define pb push_back
#define mp make_pair
#define maxn 300005

ll gcd1(ll a, ll b) {
    if (a == 0)
        return b;
    return gcd1(b % a, a);
}

ll modx(ll base, ll ex)
{
    ll ans = 1LL, val = base;
    while(ex > 0LL)
    {
        if(ex&1LL)
            ans = (ans*val)%MOD;
        val = (val*val)%MOD;
        ex = ex>>1LL;
    }
    return ans;
}

int color[maxn];
set<int> cnt[maxn];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m;

    cin >> n >> m;

    int maxx = INT_MIN;
    for (int i = 1; i <= n; i++) {
        cin >> color[i];
        cnt[color[i]].insert(color[i]);
        maxx = max(maxx, color[i]);
    }

    int x, y;
    for (int i = 1; i <= m; i++) {
        cin >> x >> y;
        cnt[color[x]].insert(color[y]);
        cnt[color[y]].insert(color[x]);
    }

    int ans = -2, cl;
    for (int i = 1; i <= maxx; i++) {
        int sz = cnt[i].size();
        if (sz - 1 > ans) {
            ans = sz - 1;
            cl = i;
        }
    }

    cout << cl << endl;

    return 0;
}


// http://codeforces.com/problemset/problem/246/D

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

#define ll long long
#define eps 1e-10
#define fi first
#define se second
#define MOD1 1000000007LL
#define MOD 1000000009LL
#define p1 53
#define se second
#define inf 20000000000LL
#define pb push_back
#define mp make_pair
#define maxn 300005

ll gcd1(ll a, ll b) {
    if (a == 0)
        return b;
    return gcd1(b % a, a);
}

ll modx(ll base, ll ex)
{
    ll ans = 1LL, val = base;
    while(ex > 0LL)
    {
        if(ex&1LL)
            ans = (ans*val)%MOD;
        val = (val*val)%MOD;
        ex = ex>>1LL;
    }
    return ans;
}

int color[maxn];
set<int> cnt[maxn];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m;

    cin >> n >> m;

    int maxx = INT_MIN;
    for (int i = 1; i <= n; i++) {
        cin >> color[i];
        cnt[color[i]].insert(color[i]);
        maxx = max(maxx, color[i]);
    }

    int x, y;
    for (int i = 1; i <= m; i++) {
        cin >> x >> y;
        cnt[color[x]].insert(color[y]);
        cnt[color[y]].insert(color[x]);
    }

    int ans = -1, cl;
    for (int i = 1; i <= maxx; i++) {
        int sz = cnt[i].size();
        if (sz - 1 > ans) {
            ans = sz - 1;
            cl = i;
        }
    }

    cout << cl << endl;

    return 0;
}