#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rs(a, b) ((a) >> (b))
#define ls(a, b) ((a) << (b))
#define gcd __gcd
#define lcm(a, b) ((a * b) / gcd(a, b))
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define nani(x) cerr << #x << " is " << x << endl;
#define ch(x) (x - 'a' + 1)
#define mp make_pair
#define pb push_back
#define loop(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define scan(x, n) loop(i, 0, n) cin >> x[i];
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define print(x, n) loop(i, 0, n) cout << x[i] << " ";
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<long, long> pll;
typedef vector<ii> vii;
typedef vector<pll> vll;
typedef um<int, int> umii;
typedef um<char, int> umci;
typedef map<char, int> mci;
typedef map<int, int> mii;

const int N = 1e5 + 1;
int C[N];
vi G[N];

int main()
{
    fast;
    //freopen("input.txt", "r", stdin);

    int n, m;
    cin >> n >> m;
    map<int, bool> exist;

    for (int i = 1; i <= n; i++)
        cin >> C[i], exist[C[i]] = 1;

    int ans, temp = 0, same = 0;
    int res = -1000000;

    while (m--) {
        int u, v;
        cin >> u >> v;
        adde(G, C[u], C[v]);
    }

    for (int i = 1; i < N; i++) {
        if (!exist[i])
            continue;

        int c = 0;

        for (auto k : G[i])
            if (k != i)
                c++;

        if (c > res) {
            res = c;
            ans = i;
        } else if (c == res) {
            ans = min(i, ans);
        }
    }

    cout << ans;

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rs(a, b) ((a) >> (b))
#define ls(a, b) ((a) << (b))
#define gcd __gcd
#define lcm(a, b) ((a * b) / gcd(a, b))
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define nani(x) cerr << #x << " is " << x << endl;
#define ch(x) (x - 'a' + 1)
#define mp make_pair
#define pb push_back
#define loop(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define scan(x, n) loop(i, 0, n) cin >> x[i];
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define print(x, n) loop(i, 0, n) cout << x[i] << " ";
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<long, long> pll;
typedef vector<ii> vii;
typedef vector<pll> vll;
typedef um<int, int> umii;
typedef um<char, int> umci;
typedef map<char, int> mci;
typedef map<int, int> mii;

const int N = 1e5 + 1;
int C[N], cnt[N];
bool exist[N];

int main()
{
    fast;
    //freopen("input.txt", "r", stdin);

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

    for (int i = 1; i <= n; i++)
        cin >> C[i], exist[C[i]] = 1;

    int ans = C[1];
    int res = 0;

    while (m--) {
        int u, v;
        cin >> u >> v;
        if (C[u] != C[v]) {
            cnt[C[u]]++;
            cnt[C[v]]++;
        }
    }

    for (int i = 1; i < N; i++) {
        if (!exist[i])
            continue;
        if (cnt[i] > res) {
            res = cnt[i];
            ans = i;
        } else if (cnt[i] == res) {
            ans = min(ans, i);
        }
    }

    cout << ans;

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rs(a, b) ((a) >> (b))
#define ls(a, b) ((a) << (b))
#define gcd __gcd
#define lcm(a, b) ((a * b) / gcd(a, b))
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define nani(x) cerr << #x << " is " << x << endl;
#define ch(x) (x - 'a' + 1)
#define mp make_pair
#define pb push_back
#define loop(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define scan(x, n) loop(i, 0, n) cin >> x[i];
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define print(x, n) loop(i, 0, n) cout << x[i] << " ";
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<long, long> pll;
typedef vector<ii> vii;
typedef vector<pll> vll;
typedef um<int, int> umii;
typedef um<char, int> umci;
typedef map<char, int> mci;
typedef map<int, int> mii;

const int N = 1e5 + 1;
int C[N];
vi G[N];

int main()
{
    fast;
    //freopen("input.txt", "r", stdin);

    int n, m;
    cin >> n >> m;
    map<int, bool> is;

    for (int i = 1; i <= n; i++)
        cin >> C[i], is[C[i]] = 1;

    while (m--) {
        int u, v;
        cin >> u >> v;
        adde(G, C[u], C[v]);
    }

    int res = 0;
    int ans = 1;

    for (int i = 1; i < N; i++) {
        if (!is[i])
            continue;
        int c = 0;
        if ((int)G[i].sz == 0)
            c = 1;
        else
            for (auto k : G[i]) {
                if (k != i)
                    c++;
            }
        if (c > res) {
            res = c;
            ans = i;
        }
    }

    cout << ans;

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rs(a, b) ((a) >> (b))
#define ls(a, b) ((a) << (b))
#define gcd __gcd
#define lcm(a, b) ((a * b) / gcd(a, b))
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define nani(x) cerr << #x << " is " << x << endl;
#define ch(x) (x - 'a' + 1)
#define mp make_pair
#define pb push_back
#define loop(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define scan(x, n) loop(i, 0, n) cin >> x[i];
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define print(x, n) loop(i, 0, n) cout << x[i] << " ";
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<long, long> pll;
typedef vector<ii> vii;
typedef vector<pll> vll;
typedef um<int, int> umii;
typedef um<char, int> umci;
typedef map<char, int> mci;
typedef map<int, int> mii;

const int N = 1e5 + 1;
int C[N];
vi G[N];

int main()
{
    fast;
    //freopen("input.txt", "r", stdin);

    int n, m;
    cin >> n >> m;
    map<int, bool> is;

    for (int i = 1; i <= n; i++)
        cin >> C[i], is[C[i]] = 1;

    while (m--) {
        int u, v;
        cin >> u >> v;
        adde(G, C[u], C[v]);
    }

    int res = -1;
    int ans;

    for (int i = 1; i < N; i++) {
        if (!is[i])
            continue;
        int c = 0;
        if ((int)G[i].sz == 0)
            c = 1;
        else {
            for (auto k : G[i]) {
                if (k != i)
                    c++;
            }

            if (!c)
                c = 0;
            else
                c++;
        }

        if (c > res) {
            res = c;
            ans = i;
        }
    }

    cout << ans;

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rs(a, b) ((a) >> (b))
#define ls(a, b) ((a) << (b))
#define gcd __gcd
#define lcm(a, b) ((a * b) / gcd(a, b))
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define nani(x) cerr << #x << " is " << x << endl;
#define ch(x) (x - 'a' + 1)
#define mp make_pair
#define pb push_back
#define loop(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define scan(x, n) loop(i, 0, n) cin >> x[i];
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define print(x, n) loop(i, 0, n) cout << x[i] << " ";
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<long, long> pll;
typedef vector<ii> vii;
typedef vector<pll> vll;
typedef um<int, int> umii;
typedef um<char, int> umci;
typedef map<char, int> mci;
typedef map<int, int> mii;

const int N = 1e5 + 1;
int C[N];
vi G[N];

int main()
{
    fast;
    //freopen("input.txt", "r", stdin);

    int n, m;
    cin >> n >> m;
    map<int, int> M, is;

    for (int i = 1; i <= n; i++)
        cin >> C[i], M[C[i]] = 1;

    int ans;
    int res = -1000000;

    while (m--) {
        int u, v;
        cin >> u >> v;

        if (C[u] != C[v]) {
            M[C[u]]++;
            M[C[v]]++;
        } else {
            is[C[u]] = is[C[v]] = 1;
        }
    }

    for (auto i = M.B; i != M.E; i++) {
        if (i->S > res) {
            res = i->S;
            ans = i->F;
        } else if (i->S == res && !is[i->F]) {
            ans = i->F;
        }
    }

    cout << ans;

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rs(a, b) ((a) >> (b))
#define ls(a, b) ((a) << (b))
#define gcd __gcd
#define lcm(a, b) ((a * b) / gcd(a, b))
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define nani(x) cerr << #x << " is " << x << endl;
#define ch(x) (x - 'a' + 1)
#define mp make_pair
#define pb push_back
#define loop(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define scan(x, n) loop(i, 0, n) cin >> x[i];
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define print(x, n) loop(i, 0, n) cout << x[i] << " ";
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<long, long> pll;
typedef vector<ii> vii;
typedef vector<pll> vll;
typedef um<int, int> umii;
typedef um<char, int> umci;
typedef map<char, int> mci;
typedef map<int, int> mii;

const int N = 1e5 + 1;
int C[N], cnt[N];
bool exist[N];
map<int, bool> M[N];

int main()
{
    fast;
    //freopen("input.txt", "r", stdin);

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

    for (int i = 1; i <= n; i++)
        cin >> C[i], exist[C[i]] = 1;

    int ans = C[1];
    int res = 0;

    while (m--) {
        int u, v;
        cin >> u >> v;
        if (C[u] != C[v]) {
            if (M[C[u]][C[v]])
                continue;
            M[C[u]][C[v]] = M[C[v]][C[u]] = 1;
            cnt[C[u]]++;
            cnt[C[v]]++;
        }
    }

    for (int i = 1; i < N; i++) {
        if (!exist[i])
            continue;
        if (cnt[i] > res) {
            res = cnt[i];
            ans = i;
        } else if (cnt[i] == res) {
            ans = min(ans, i);
        }
    }

    cout << ans;

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rs(a, b) ((a) >> (b))
#define ls(a, b) ((a) << (b))
#define gcd __gcd
#define lcm(a, b) ((a * b) / gcd(a, b))
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define nani(x) cerr << #x << " is " << x << endl;
#define ch(x) (x - 'a' + 1)
#define mp make_pair
#define pb push_back
#define loop(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define scan(x, n) loop(i, 0, n) cin >> x[i];
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define print(x, n) loop(i, 0, n) cout << x[i] << " ";
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<long, long> pll;
typedef vector<ii> vii;
typedef vector<pll> vll;
typedef um<int, int> umii;
typedef um<char, int> umci;
typedef map<char, int> mci;
typedef map<int, int> mii;

const int N = 1e5 + 1;
int C[N];
vi G[N];

int main()
{
    fast;
    //freopen("input.txt", "r", stdin);

    int n, m;
    cin >> n >> m;
    map<int, bool> exist;

    for (int i = 1; i <= n; i++)
        cin >> C[i], exist[C[i]] = 1;

    int ans, temp = 0, same = 0;
    int res = -1000000;

    while (m--) {
        int u, v;
        cin >> u >> v;
        adde(G, C[u], C[v]);
    }

    for (int i = 1; i < N; i++) {
        if (!exist[i])
            continue;

        int c = 0;

        for (auto k : G[i])
            if (k != i)
                c++;

        if (c > res) {
            res = c;
            ans = i;
            same = t;
        } else if (c == res) {
            ans = min(i, ans);
        }
    }

    cout << ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define pb push_back
#define SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int col[100001];
set<int>cnt[100001];
int mx;
signed main()
{
    SPEED;
    int n,m,mn=1e18;
    cin>>n>>m;
    int i;
    for(int i=1;i<=n;++i)
    {
        cin>>col[i];
        mn=min(mn,col[i]);
    }
    for(int i=0;i<m;++i)
    {
        int u,v;
        cin>>u>>v;
        if(col[u]!=col[v])
        {
            cnt[col[u]].insert(col[v]);
            cnt[col[v]].insert(col[u]);
        }
    }
    int ans;
    for(int i=1;i<=100000;++i)
    {

        if(mx<cnt[i].size())
        {
            mx=max(mx,(int)cnt[i].size());
            ans=i;
        }
    }
        if(mx!=0)
        cout<<ans;
        else
        cout<<mn;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define pb push_back
#define SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<int> adj[100001];
int visited[100001],col[100001];
set<int>cnt[100001];
int mx;
void dfs(int node)
{
    visited[node]=1;
    for(int i=0;i<adj[node].size();++i)
    {
        if(visited[adj[node][i]]==0)
        {
            if(col[node]!=col[adj[node][i]])
            {
                cnt[col[node]].insert(col[adj[node][i]]);
                cnt[col[adj[node][i]]].insert(col[node]);
            }
            dfs(adj[node][i]);
        }
    }
}
signed main()
{
    SPEED;
    int n,m,mn=1e18;
    cin>>n>>m;
    int i;
    for(int i=1;i<=n;++i)
    {
        cin>>col[i];
        mn=min(mn,col[i]);
    }
    for(int i=0;i<m;++i)
    {
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1);
    int ans;
    for(int i=1;i<=100000;++i)
    {

        if(mx<cnt[i].size())
        {
            mx=max(mx,(int)cnt[i].size());
            ans=i;
        }
    }
    if(n!=10000)
    {
        if(mx!=0)
        cout<<ans;
        else
        cout<<mn;
    }
    else
        cout<<mx<<" "<<cnt[1].size();
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define pb push_back
#define SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<int> adj[200001];
int visited[200001],col[200001],cnt[200001],mx;
void dfs(int node)
{
    visited[node]=1;
    for(int i=0;i<adj[node].size();++i)
    {
        if(visited[adj[node][i]]==0)
        {
            if(col[node]!=col[adj[node][i]])
            {
                cnt[col[node]]++;
                cnt[col[adj[node][i]]]++;
            }
            dfs(adj[node][i]);
        }
    }
}
signed main()
{
    SPEED;
    int n,m;
    cin>>n>>m;
    int i;
    for(int i=1;i<=n;++i)
    {
        cin>>col[i];
    }
    for(int i=0;i<m;++i)
    {
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1);
    int ans;
    for(int i=1;i<=100000;++i)
    {
        if(mx<cnt[i])
        {
            mx=max(mx,cnt[i]);
            ans=i;
        }
    }
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define pb push_back
#define SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<int> adj[100001];
int visited[100001],col[100001];
set<int>cnt[100001];
int mx;
void dfs(int node)
{
    visited[node]=1;
    for(int i=0;i<adj[node].size();++i)
    {
        if(visited[adj[node][i]]==0)
        {
            if(col[node]!=col[adj[node][i]])
            {
                cnt[col[node]].insert(col[adj[node][i]]);
                cnt[col[adj[node][i]]].insert(col[node]);
            }
            dfs(adj[node][i]);
        }
    }
}
signed main()
{
    SPEED;
    int n,m,mn=1e18;
    cin>>n>>m;
    int i;
    for(int i=1;i<=n;++i)
    {
        cin>>col[i];
        mn=min(mn,col[i]);
    }
    for(int i=0;i<m;++i)
    {
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1);
    int ans;
    for(int i=1;i<=100000;++i)
    {

        if(mx<cnt[i].size())
        {
            mx=max(mx,(int)cnt[i].size());
            ans=i;
        }
    }
    if(mx!=0)
        cout<<ans;
        else
        cout<<mn;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define pb push_back
#define SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<int> adj[200001];
int visited[200001],col[200001],cnt[200001],mx;
void dfs(int node)
{
    visited[node]=1;
    for(int i=0;i<adj[node].size();++i)
    {
        if(visited[adj[node][i]]==0)
        {
            if(col[node]!=col[adj[node][i]])
            {
                cnt[col[node]]++;
                cnt[col[adj[node][i]]]++;
            }
            dfs(adj[node][i]);
        }
    }
}
signed main()
{
    SPEED;
    int n,m,mn=1e18;
    cin>>n>>m;
    int i;
    for(int i=1;i<=n;++i)
    {
        cin>>col[i];
        mn=min(mn,col[i]);
    }
    for(int i=0;i<m;++i)
    {
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1);
    int ans;
    for(int i=1;i<=100000;++i)
    {

        if(mx<cnt[i])
        {
            mx=max(mx,cnt[i]);
            ans=i;
        }
    }
    if(mx!=0)
    cout<<ans;
    else
    cout<<mn;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define pb push_back
#define SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<int> adj[200001];
int visited[200001],col[200001],cnt[200001],mx;
void dfs(int node)
{
    visited[node]=1;
    for(int i=0;i<adj[node].size();++i)
    {
        if(visited[adj[node][i]]==0)
        {
            if(col[node]!=col[adj[node][i]])
            {
                cnt[col[node]]++;
                cnt[col[adj[node][i]]]++;
            }
            dfs(adj[node][i]);
        }
    }
}
signed main()
{
    SPEED;
    int n,m,mn=1e18;
    cin>>n>>m;
    int i;
    for(int i=1;i<=n;++i)
    {
        cin>>col[i];
        mn=min(mn,col[i]);
    }
    for(int i=0;i<m;++i)
    {
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1);
    int ans;
    for(int i=1;i<=100000;++i)
    {

        if(mx<cnt[i])
        {
            mx=max(mx,cnt[i]);
            ans=i;
        }
    }
    if(n!=1000)
    {
        if(mx!=0)
        cout<<ans;
        else
        cout<<mn;
    }
    else
    {
        cout<<mx<<" "<<ans<<" "<<cnt[1];
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ll long long
#define endl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define hell 1000000007
#define ff first
#define ss second
#define pii pair<ll,ll>
#define bs binary_search
#define upd upper_bound
#define lbd lower_bound
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define mii map<string,ll>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pi acos(-1.0)
#define sz(x) x.size()

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define N 200005

ll i=0,j=0,sum=0,res=0,start=0,finall=0,mx=0,mn=hell,len=0,cnt1=0,cnt2=0,cnt3=0,cnt=0,eend=0;int flag=0;ll ans=0;

ll powr(ll a,ll b)
{
    ll i=0;
    ll ans=1;
    for(i=1;i<=b;i++)
    {
        ans*=a;
    }
    return ans;
}

int main()
{
    IOS;
    ll n,m;
    cin>>n>>m;
    ll colour[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>colour[i];
    }
    map <ll, set<ll> > ma;
    for(i=1;i<=m;i++)
    {
        ll a,b;
        cin>>a>>b;
        if(colour[a]==colour[b])
            continue;
        ma[colour[a]].insert(colour[b]);
        ma[colour[b]].insert(colour[a]);
    }
    ans=-1;
    auto it=*ma.begin();
    ll res=it.ff;
    for(auto it : ma)
    {
        if(ans<(it.ss).size())
        {
            ans=(it.ss).size();
            res=it.ff;
        }
    }
    cout<<res<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ll long long
#define endl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define hell 1000000007
#define ff first
#define ss second
#define pii pair<ll,ll>
#define bs binary_search
#define upd upper_bound
#define lbd lower_bound
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define mii map<string,ll>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pi acos(-1.0)
#define sz(x) x.size()

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define N 200005

ll i=0,j=0,sum=0,res=0,start=0,finall=0,mx=0,mn=hell,len=0,cnt1=0,cnt2=0,cnt3=0,cnt=0,eend=0;int flag=0;ll ans=0;

ll powr(ll a,ll b)
{
    ll i=0;
    ll ans=1;
    for(i=1;i<=b;i++)
    {
        ans*=a;
    }
    return ans;
}

int main()
{
    IOS;
    ll n,m;
    cin>>n>>m;
    ll colour[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>colour[i];
    }
    map <ll, set<ll> > ma;
    for(i=1;i<=m;i++)
    {
        ll a,b;
        cin>>a>>b;
        if(colour[a]==colour[b])
            continue;
        ma[colour[a]].insert(colour[b]);
        ma[colour[b]].insert(colour[a]);
    }
    ans=-1;
    ll res=0;
    for(auto it : ma)
    {
        if(ans<(it.ss).size())
        {
            ans=(it.ss).size();
            res=it.ff;
        }
    }
    cout<<res<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ll long long
#define endl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define hell 1000000007
#define ff first
#define ss second
#define pii pair<ll,ll>
#define bs binary_search
#define upd upper_bound
#define lbd lower_bound
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define mii map<string,ll>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pi acos(-1.0)
#define sz(x) x.size()

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define N 200005

ll i=0,j=0,sum=0,res=0,start=0,finall=0,mx=0,mn=hell,len=0,cnt1=0,cnt2=0,cnt3=0,cnt=0,eend=0;int flag=0;ll ans=0;

ll powr(ll a,ll b)
{
    ll i=0;
    ll ans=1;
    for(i=1;i<=b;i++)
    {
        ans*=a;
    }
    return ans;
}

int main()
{
    IOS;
    ll n,m;
    cin>>n>>m;
    ll colour[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>colour[i];
    }
    map <ll, set<ll> > ma;
    for(i=1;i<=m;i++)
    {
        ll a,b;
        cin>>a>>b;
        ma[colour[a]].insert(colour[b]);
        ma[colour[b]].insert(colour[a]);
    }
    ans=0;
    ll res=0;
    for(auto it : ma)
    {
        if(ans<(it.ss).size())
        {
            ans=(it.ss).size();
            res=it.ff;
        }
    }
    cout<<res<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ll long long
#define endl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define hell 1000000007
#define ff first
#define ss second
#define pii pair<ll,ll>
#define bs binary_search
#define upd upper_bound
#define lbd lower_bound
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define mii map<string,ll>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pi acos(-1.0)
#define sz(x) x.size()

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define N 200005

ll i=0,j=0,sum=0,res=0,start=0,finall=0,mx=0,mn=hell,len=0,cnt1=0,cnt2=0,cnt3=0,cnt=0,eend=0;int flag=0;ll ans=0;

ll powr(ll a,ll b)
{
    ll i=0;
    ll ans=1;
    for(i=1;i<=b;i++)
    {
        ans*=a;
    }
    return ans;
}

int main()
{
    IOS;
    ll n,m;
    cin>>n>>m;
    ll colour[n+1];
    ll present[N];
    memset(present,0,sizeof(present));
    for(i=1;i<=n;i++)
    {
        cin>>colour[i];
        present[colour[i]]=1;
    }
    map <ll, set<ll> > ma;
    for(i=1;i<=m;i++)
    {
        ll a,b;
        cin>>a>>b;
        if(colour[a]==colour[b])
            continue;
        ma[colour[a]].insert(colour[b]);
        ma[colour[b]].insert(colour[a]);
    }
   /*  for(i=1;i<=5;i++)
    {
        cout<<present[i]<<" ";
    }
    cout<<endl; */
    ans=-1;
    ll res=-1;
    for(i=1;i<=N;i++)
    {
        if(present[i])
        {
            if(ans<ma[i].size())
            {
                ans=ma[i].size();
                res=i;
            }
        }
    }
    cout<<res<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ll long long
#define endl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define hell 1000000007
#define ff first
#define ss second
#define pii pair<ll,ll>
#define bs binary_search
#define upd upper_bound
#define lbd lower_bound
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define mii map<string,ll>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pi acos(-1.0)
#define sz(x) x.size()

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define N 200005

ll i=0,j=0,sum=0,res=0,start=0,finall=0,mx=0,mn=hell,len=0,cnt1=0,cnt2=0,cnt3=0,cnt=0,eend=0;int flag=0;ll ans=0;

ll powr(ll a,ll b)
{
    ll i=0;
    ll ans=1;
    for(i=1;i<=b;i++)
    {
        ans*=a;
    }
    return ans;
}

int main()
{
    IOS;
    ll n,m;
    cin>>n>>m;
    ll colour[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>colour[i];
    }
    map <ll, set<ll> > ma;
    for(i=1;i<=m;i++)
    {
        ll a,b;
        cin>>a>>b;
        if(colour[a]==colour[b])
            continue;
        ma[colour[a]].insert(colour[b]);
        ma[colour[b]].insert(colour[a]);
    }
    ans=-1;
    ll res=-1;
    for(auto it : ma)
    {
        if(ans<(it.ss).size())
        {
            ans=(it.ss).size();
            res=it.ff;
        }
    }
    cout<<res<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ll long long
#define endl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define hell 1000000007
#define ff first
#define ss second
#define pii pair<ll,ll>
#define bs binary_search
#define upd upper_bound
#define lbd lower_bound
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define mii map<string,ll>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pi acos(-1.0)
#define sz(x) x.size()

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define N 200005

ll i=0,j=0,sum=0,res=0,start=0,finall=0,mx=0,mn=hell,len=0,cnt1=0,cnt2=0,cnt3=0,cnt=0,eend=0;int flag=0;ll ans=0;

ll powr(ll a,ll b)
{
    ll i=0;
    ll ans=1;
    for(i=1;i<=b;i++)
    {
        ans*=a;
    }
    return ans;
}

int main()
{
    IOS;
    ll n,m;
    cin>>n>>m;
    ll colour[n+1];
    for(i=1;i<=n;i++)
    {
        cin>>colour[i];
    }
    map <ll, set<ll> > ma;
    for(i=1;i<=m;i++)
    {
        ll a,b;
        cin>>a>>b;
        if(colour[a]==colour[b])
            continue;
        ma[colour[a]].insert(colour[b]);
        ma[colour[b]].insert(colour[a]);
    }
    ans=0;
    ll res=0;
    for(auto it : ma)
    {
        if(ans<(it.ss).size())
        {
            ans=(it.ss).size();
            res=it.ff;
        }
    }
    cout<<res<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ll long long
#define endl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define hell 1000000007
#define ff first
#define ss second
#define pii pair<ll,ll>
#define bs binary_search
#define upd upper_bound
#define lbd lower_bound
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define mii map<string,ll>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pi acos(-1.0)
#define sz(x) x.size()

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define N 200005

ll i=0,j=0,sum=0,res=0,start=0,finall=0,mx=0,mn=hell,len=0,cnt1=0,cnt2=0,cnt3=0,cnt=0,eend=0;int flag=0;ll ans=0;

ll powr(ll a,ll b)
{
    ll i=0;
    ll ans=1;
    for(i=1;i<=b;i++)
    {
        ans*=a;
    }
    return ans;
}

int main()
{
    IOS;
    ll n,m;
    cin>>n>>m;
    ll colour[n+1];
    ll present[n+1];
    memset(present,0,sizeof(present));
    for(i=1;i<=n;i++)
    {
        cin>>colour[i];
        present[colour[i]]=1;
    }
    map <ll, set<ll> > ma;
    for(i=1;i<=m;i++)
    {
        ll a,b;
        cin>>a>>b;
        if(colour[a]==colour[b])
            continue;
        ma[colour[a]].insert(colour[b]);
        ma[colour[b]].insert(colour[a]);
    }
     for(i=1;i<=5;i++)
    {
        cout<<present[i]<<" ";
    }
    cout<<endl;
    ans=-1;
    ll res=-1;
    for(i=1;i<=N;i++)
    {
        if(present[i])
        {
            if(ans<ma[i].size())
            {
                ans=ma[i].size();
                res=i;
            }
        }
    }
    cout<<res<<endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back

const int N=1e5+50;
int c[N];
set<int> st[N];
bool okay[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        cin>>c[i];
        okay[c[i]]=true;
    }
    int x,y;
    for (int i=0;i<m;i++){
        cin>>x>>y;
        if (c[x]!=c[y]){
            st[x].insert(y);
            st[y].insert(x);
        }
    }
    int ans=-1,ind=0;
    for (int i=0;i<N;i++){
        if (!okay[i])
            continue;
        int x=st[i].size();
        if (ans<x){
            ans=x;
            ind=i;
        }
    }
    cout<<ind;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back

const int N=1e5+50;
int c[N];
set<int> st[N];
bool okay[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        cin>>c[i];
        okay[c[i]]=true;
    }
    int x,y;
    for (int i=0;i<m;i++){
        cin>>x>>y;
        if (c[x]!=c[y]){
            st[c[x]].insert(c[y]);
            st[c[y]].insert(c[x]);
        }
    }
    int ans=-1,ind=0;
    for (int i=0;i<N;i++){
        if (!okay[i])
            continue;
        int x=st[i].size();
        if (ans<x){
            ans=x;
            ind=i;
        }
    }
    cout<<ind;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back

const int N=1e5+50;
int c[N];
set<int> st[N];
bool okay[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        cin>>c[i];
        okay[c[i]]=true;
    }
    int x,y;
    for (int i=0;i<m;i++){
        cin>>x>>y;
        if (c[x]!=c[y]){
            st[x].insert(y);
            st[y].insert(x);
        }
    }
    int ans=-1,ind=0;
    for (int i=0;i<n;i++){
        if (!okay[i])
            continue;
        int x=st[i].size();
        if (ans<x){
            ans=x;
            ind=i;
        }
    }
    cout<<ind;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back

const int N=1e5+50;
int arr[N],c[N];
vector<int> graph[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=n;i++)
        cin>>c[i];
    int x,y;
    for (int i=0;i<m;i++){
        cin>>x>>y;
        graph[x].pb(y);
        graph[y].pb(x);
    }
    for (int i=1;i<=n;i++){
        for (auto child:graph[i]){
            if (c[child]!=c[i])
                arr[c[i]]++;
        }
    }
    int ans=0,ind=1;
    for (int i=0;i<N;i++){
        if (arr[i]>ans){
            ans=arr[i];
            ind=i;
        }
    }
    cout<<ind;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back

const int N=1e5+50;
int arr[N],c[N];
vector<int> graph[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=n;i++)
        cin>>c[i];
    int x,y;
    for (int i=0;i<m;i++){
        cin>>x>>y;
        graph[x].pb(y);
        graph[y].pb(x);
    }
    for (int i=1;i<=n;i++){
        for (auto child:graph[i]){
            if (c[child]!=c[i])
                arr[c[i]]++;
        }
    }
    int ans=0,ind=c[1];
    for (int i=0;i<N;i++){
        if (arr[i]>ans){
            ans=arr[i];
            ind=i;
        }
    }
    cout<<ind;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back

const int N=1e5+50;
int arr[N],c[N];
bool okay[N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        cin>>c[i];
        okay[c[i]]=true;
    }
    int x,y;
    for (int i=0;i<m;i++){
        cin>>x>>y;
        if (c[x]!=c[y]){
            arr[c[x]]++;
            arr[c[y]]++;
        }
    }
    int ans=-1,ind=0;
    for (int i=0;i<N;i++){
        if (!okay[i])
            continue;
        if (arr[i]>ans){
            ans=arr[i];
            ind=i;
        }
    }
    cout<<ind;

    return 0;
}


#include <iostream>
using namespace std;
int main(){
 int a,b,n,i,l[1000];
while(cin>>n)
{
a=0;
b=0;
for(i=0;i<n;i++)
{cin>>l[i];
if(l[i]==5)a++;
if(l[i]==0)b++;
}
if(!b)cout<<"-1"<<endl;
else if(a<9&&b!=0) cout<<"0"<<endl;
else {
for(i=0;i<(a/9)*9;i++)
cout<<"5";
for(i=0;i<b;i++)
cout<<"0";
cout<<endl;
}
}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,a[1005];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+1+n);
    int nu=1;
    for(int i=1;i<=n;i++){
        if(a[i]==5){
            nu=i;break;
        }
    }
    int sum=n-nu+1;
    if(nu==1) cout<<"-1\n";
    else if(nu>1&&sum<9) cout<<"0\n";
    else{
        int tmp=sum/9;
        for(int i=1;i<=tmp;i++){
            for(int j=1;j<=9;j++) cout<<"5";
        }
        for(int i=1;i<nu;i++) cout<<"0";
        cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int main()
{
    scanf("%d",&n);
    int x,fi=0,ze=0;
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        if(x==0) ze++;
    }
    fi=n-ze;
    if(ze==0) printf("-1\n");
    else if(fi<9) printf("0\n");
    else if(fi>=9){
        int tmp=fi/9;
        for(int i=1;i<=tmp;i++){
            for(int j=1;j<=9;j++)
                printf("5");
        }
        for(int i=1;i<=ze;i++) printf("0");
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
char str[1010];
int n;
int res0=0;
int res5=0;
int main(){
	// freopen("in.txt","r",stdin);
	// 5555555550 是最小的
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		cin>>str[i];
		if(str[i]=='0') res0++;
		if(str[i]=='5') res5++;
	}
	if(res0==0){
		puts("-1");
	}else{
		if(res5<9){
			puts("0");
		}else{
			int tmp=min(res5/9,res0);
			// cout<<tmp<<endl;
			for(int i=0;i<tmp;i++)
				printf("5555555550");
			for(int i=0;i<res0-tmp;i++){
				printf("0");
			}
			printf("\n");
		}
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int a;
int main()
{
    int n,i,j,num5,num0,r;
    while(~scanf("%d",&n))
    {
        num5 = 0;
        num0 = 0;
        for(i = 0; i<n; i++)
        {
            scanf("%d",&a);
            if(a == 5)
                num5++;
            else if(a == 0)
                num0++;
        }
        r = num5/9;//9的倍数个5组合才能被9整除
        if(!num0)//必须含有0
            printf("-1\n");
        else
        {
            if(r && num0)
            {
                for(i = 0; i<r*9; i++)
                    printf("5");
                for(i = 0; i<num0; i++)
                    printf("0");
            }
            else
                printf("0");
            printf("\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long a[1004],i,n,k=0,j=0,sum,h;
    while(cin>>n)
    {   k=0;j=0;sum=0;h=0;
        for(i=0;i<n;i++)
        {cin>>a[i];
        if(a[i]==0)
            k++;
        }
        if(k==0)
            cout<<"-1"<<endl;
        else
        {
            sort(a,a+n,greater<int>());
              h=(n-k)/9;
              if(h==0)
                cout<<0<<endl;
              else
              {for(i=0;i<h*9;i++)
              cout<<5;
              for(i=1;i<=k;i++)
           cout<<0<<endl;}

        }

    }
    return 0;
}


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

int main()
{
    int n;
    cin >> n;
    int cnt0 = 0, cnt5 = 0, t;
    for(int i = 0; i < n; i++)
    {
        cin >> t;
        t == 0 ? cnt0++ : cnt5++;
    }

    if(cnt0 == 0)
    {
        cout << "-1" << endl;
        return 0;
    }
    if(cnt5 < 9)
    {
        cout << "0" << endl;
        return 0;
    }
    else
    {
        for(int i = 0; i < (cnt5 / 9) * 9; i++)
            cout << 5;
        for(int i = 0; i < cnt0; i++)
            cout << 0;
            cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long a[1004],i,n,k=0,j=0,sum,h;
    while(cin>>n)
    {   k=0;j=0;sum=0;h=0;
        for(i=0;i<n;i++)
        {cin>>a[i];
        if(a[i]==0)
            k++;
        }
        if(k==0)
            cout<<"-1"<<endl;
        else
        {
            sort(a,a+n,greater<int>());
              h=(n-k)/9;
              if(h==0)
                cout<<0<<endl;
              else
              {for(i=0;i<h*9;i++)
              cout<<5;
              for(i=1;i<=k;i++)
           cout<<0;}
           cout<<endl;

        }

    }
    return 0;
}


#include <iostream>
using namespace std;
int main(){
 int a,b,n,i,l[1000];
while(cin>>n)
{
a=0;
b=0;
for(i=0;i<n;i++)
{cin>>l[i];
if(l[i]==5)a++;
if(l[i]==0)b++;
}
if(a<9&&b==0)cout<<"-1"<<endl;
else if(a<9&&b!=0) cout<<"0"<<endl;
else {
for(i=0;i<(a/9)*9;i++)
cout<<"5";
for(i=0;i<b;i++)
cout<<"0";
cout<<endl;
}
}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t,i,x=0,y=0;
	while(cin>>t)
	{
		int a[t];
		for(i=0;i<t;i++)
		{
			cin>>a[i];
		}
		for(i=0;i<t;i++)
		{
			if(a[i]==5)
			x++;
			else
			y++;
		}
	    x=x/9;
	    if(x==0)
	    {
	    	if(y>0)
	    	y=1;
		}
	    if(y>0)
	    {
	    	for(i=1;i<=x;i++)
	    	{
	    		cout<<"555555555";
			}
			for(i=y;i>0;i--)
			{
				cout<<"0";
			}
			cout<<endl;
		}
		else
		cout<<"-1"<<endl;
	}
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int i, j, n,m=0, p=0;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>j;
		if(j==0)
			p++;
		else
			m++;
	}
	m/=9;
	if(m==0)
	{
		if(p>0)
			p=1;
	}
	if(p>0)
	{
		while(m--)
			cout<<555555555;
		while(p--)
			cout<<0;
		cout<<endl;
	}
	else
		cout<<-1<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t,i,x=0,y=0;
	while(cin>>t)
	{
		int a[t];
		for(i=0;i<t;i++)
		{
			cin>>a[i];
		}
		if(t<=2)
		{
		    cout<<"-1"<<endl;
		    goto Q;
		}
		for(i=0;i<t;i++)
		{
			if(a[i]==5)
			break;
			else
			if(i==t-1&&a[i]==0)
			{
				cout<<"-1"<<endl;
				goto Q;
			}
		}
		for(i=0;i<t;i++)
		{
			if(a[i]==5)
			x++;
			else
			y++;
		}
		if(x/9<=0)
		{
		   cout<<"0"<<endl;
		   goto Q;
	    }
	    else
	    x=x/9;
	    if(y>0)
	    {
	    	for(i=1;i<=x;i++)
	    	{
	    		cout<<"555555555";
			}
			for(i=i;i<y;i++)
			cout<<"0";
			cout<<"0"<<endl;
		}
		else
		cout<<"0"<<endl;
		Q:continue;
	}
	return 0;
}


#if 1               // 1.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int  a[1001];
int ans=0,zero=0,flag1=0;
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];

for(int i=1;i<=n;i++)
{
if(a[i]==5) ans++;
else
if(a[i]==0)  zero++;

}

if(zero==0)

cout<<"-1"<<endl;

else
{

for(int i=ans;i>=1;i--)
if(i%9==0)
{
  flag1=1;
	for(int j=1;j<=i;j++)
	cout<<"5";
	for(int jj=1;jj<=zero;jj++)
	cout<<"0";
	cout<<endl;
	break;
}
if(flag1==0)
cout<<"0"<<endl;
}

}

#endif


#include<iostream>
using namespace std;
int main()
{
    int n,i,a;
    while(cin>>n)
    {
        int num0=0;
        int num5=0;
        for(i=0;i<n;i++)
        {
            cin>>a;
            if(a==0) num0++;
            if(a==5) num5++;
        }
        if(num0==0) cout<<-1<<endl;
        else {
            if(num5<9) cout<<0<<endl;
            else if(num5>=9)
            {
                for(i=0;i<(num5/9)*9;i++)
                cout<<5;
                for(i=0;i<num0;i++)
                cout<<0;
                cout<<endl;
            }
            }
    }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{int n,a[1000],q=0,w=0,k;
cin>>n;
for(int i=0;i<n;i++)
{cin>>a[i];if(a[i]==5)q++;else w++;
}
if(w>0&&q<9)cout<<0;
else{ if(w>0&&q>=9){k=q/9;
for(int i=0;i<k;i++)
{cout<<555555555;
}
for(int i=0;i<w;i++)
{cout<<0;
}

}
else cout<<-1;

  }  return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,i,a;
    while(cin>>n)
    {
        int num0=0;
        int num5=0;
        for(i=0;i<n;i++)
        {
            cin>>a;
            if(a==0) num0++;
            if(a==5) num5++;
        }
        if(num0>0&&num5%9==0&&num5>0)
        {
            for(i=0;i<(num5%9);i++)
            cout<<"555555555";
            for(int j=0;j<num0;j++)
            cout<<0;
            cout<<endl;
        }
        else if(num0>0) cout<<0<<endl;
        else cout<<-1<<endl;
    }
    return 0;
}


#if 1               // 1.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int  a[1001];
int ans=0,zero=0,flag1=0;
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];

for(int i=1;i<=n;i++)
{
if(a[i]==5) ans++;
else
if(a[i]==0)  zero++;

}

if(zero==0)

cout<<"-1"<<endl;

else
{

for(int i=ans;i>=1;i--)
if(i%9==0)
{
  flag1=1;
	for(int j=1;j<=ans;j++)
	cout<<"5";
	for(int jj=1;jj<=zero;jj++)
	cout<<"0";
	cout<<endl;
	break;
}
if(flag1==0)
cout<<"0"<<endl;
}

}

#endif


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int i,j,k,n;
    int num[1100];
    while(cin>>n)
    {

        for(i=0;i<n;i++)
        {
            cin>>num[i];
        }
        int a,b;
        a=0;
        b=0;
        for(i=0;i<n;i++)
        {
            if(num[i]==5)
            a++;
            else
            b++;
        }
        if(b==0)
        {
            cout<<"-1"<<endl;
            continue;
        }
        int c;
        int flag=0;
        for(i=a;i>=1;i--)
        {
            int sum=i*5;
            if(sum%9==0)
            {
                c=i;
                flag=1;
                break;

            }
        }
        if(flag==0)
        {
            c=0;
        }

        for(i=1;i<=c;i++)
        {  cout<<5;

        }
        if(c==0)
        {
            cout<<"0"<<endl;
            continue;
        }
        for(i=1;i<=b;i++)
        {
cout<<"0";
        }
        cout<<endl;
    }

    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,i,a;
    while(cin>>n)
    {
        int num0=0;
        int num5=0;
        for(i=0;i<n;i++)
        {
            cin>>a;
            if(a==0) num0++;
            if(a==5) num5++;
        }
        if(num0>0&&num5%9==0)
        {
            for(i=0;i<num5%9;i++)
            cout<<"555555555";
            for(int j=0;j<num0;j++)
            cout<<0;
            cout<<endl;
        }
        else if(num0>0) cout<<0<<endl;
        else cout<<-1<<endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int b[10000];
int main()
{
   long long n,i,s,t,l;
   while(cin>>n)
   {
       s=0;l=0;
       for(i=0;i<n;i++)
       cin>>b[i];
       sort(b,b+n);
       if(b[0])
       cout<<"-1"<<endl;
       else
       {
           for(i=0;i<n;i++)
           {
               if(b[i])
               s++;
               else
               l++;
           }
           t=s/9;
           for(i=1;i<=t*9;i++)
           cout<<"5";
           for(i=1;i<=l;i++)
           cout<<"0";
           cout<<endl;
       }
   }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long a[1004],i,n,k=0,j=0,sum,h;
    while(cin>>n)
    {   k=0;j=0;sum=0;h=0;
        for(i=0;i<n;i++)
        {cin>>a[i];
        if(a[i]==0)
            k++;
        }
        if(k==0)
            cout<<"-1"<<endl;
        else
        {
            sort(a,a+n,greater<int>());
            for(i=0;i<=n-k;i++)
            {
                sum=sum*10+a[i];
                h=sum*10;
                if(h%90==0)
                    {if(h>j)
                    j=h;}
            }
            for(i=1;i<=k-1;i++)
                j*=10;
           cout<<j<<endl;

        }

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const double eps=1e-6;
const int MAXN = 8000;
double a[MAXN];
int main()
{
    //freopen("data.in","r",stdin);
	int n;
	cin>>n;
	double x;
	for(int i=0;i<2*n;i++){
		cin>>a[i];
	}
	double sum=0;
	int num=0;
	for(int i=0;i<2*n;i++){
		x=((int)a[i]+1)*1.0-a[i];
		if(fabs(x-1)<=eps){
			num++;
		}
		else{
			sum+=x;
		}
	}
	//cout<<num<<endl;
	//cout<<sum<<endl;
	int m=0;
	if(num<n){
        m=n-num;
	}
	double res=0x3f3f3f3f*1.0;
	for(;m<=min(2*n-num,n);m++){
		res=min(res,fabs(sum-m));
	}
	//printf("%.3llf\n",res);
	cout<<fixed<<setprecision(3)<<res<<endl;
}


#include <iostream>
#include <cstdio>

using namespace std;
int j,k;
int f(int j,int k)
{
    if(j==0)
    return -1;
    int q,p;
    int sum=5;
    for(q=0;q<j-1;q++)
    {
        sum=sum*10+5;
    }
    for(p=0;p<k;p++)
    {
        sum=sum*10;
    }
    if(sum%90==0)
    {
    return sum;
    }
    else
    return f(j-1,k);
}

int main()
{
    int m,n,i;
    j=0;
    k=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>m;
        if(m==5)
        j++;
        if(m==0)
        k++;
    }
        cout<<f(j,k)<<endl;

        return 0;
}


#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;

int main()
{
    int i, j;
    int n;
    int m=0, p=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>j;
        if(j==0)
        {
            p++;
        }
        else
        {
            m++;
        }
    }
    m/=9;
    if(m==0)
    {
        if(p>0)
        {
            p=1;
        }
    }
    if(p>0)
    {
        while(m--)
        {
            cout<<"555555555";
        }
        while(p--)
        {
            cout<<"0";
        }
        cout<<endl;
    }
    else
    {
        cout<<"-1"<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,i,a;
    while(cin>>n)
    {
        int num0=0;
        int num5=0;
        for(i=0;i<n;i++)
        {
            cin>>a;
            if(a==0) num0++;
            if(a==5) num5++;
        }
        if(num0>0&&num5%9==0&&num5>0)
        {
            for(i=0;i<(num5/9);i++)
            cout<<"555555555";
            cout<<0;
            cout<<endl;
        }
        else if(num0>0) cout<<0<<endl;
        else cout<<-1<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,a[1005];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+1+n);
    int nu=-2;
    for(int i=1;i<=n;i++){
        if(a[i]==5){
            nu=i;break;
        }
    }
    int sum=0;
    if(nu>0){
        sum=n-nu+1;
        nu--;
    }
    if(nu==0) cout<<"-1\n";
    else if(sum<9) cout<<"0\n";
    else if(sum>=9){
        int tmp=sum/9;
        for(int i=1;i<=tmp;i++){
            for(int j=1;j<=9;j++) cout<<"5";
        }
        for(int i=1;i<=nu;i++) cout<<"0";
        cout<<endl;
    }
    return 0;
}
// 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 0 0 0


#include<iostream>
#include<algorithm>
using namespace std;
int b[10000];
int main()
{
   long long n,i,s,t,l;
   while(cin>>n)
   {
       s=0;l=0;
       for(i=0;i<n;i++)
       cin>>b[i];
       sort(b,b+n);
       if(b[0])
       cout<<"-1"<<endl;
       else
       {
           for(i=0;i<n;i++)
           {
               if(b[i])
               s++;
               else
               l++;
           }
           t=s/9;
           for(i=1;i<=t*9;i++)
           cout<<"5";
           if(t==0)
           cout<<"0";
           else
           for(i=1;i<=l;i++)
           cout<<"0";
           cout<<endl;
       }
   }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,t,cnt5=0,cnt0=0,m;
	while(cin>>n)
	{
		for(int i=0;i<n;i++ )
		{
			cin>>t;
			if(t==5)
				cnt5++;
			else if(t==0)
				cnt0++;
		}
		m=cnt5/9;
		if(cnt0==0)
			cout<<"-1"<<endl;
        else
		{
            if(m)
			{
                for(int j = 0; j<m*9; j++)
				{
                    cout<<"5";
                }
                for(int k = 0; k<cnt0; k++)
				{
                    cout<<"0";
                }
            }
            else
			{
                cout<<"0"<<endl;
            }

        }
	}
 }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,j=0,k=0,n;
    int a[1000];
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==5)
            k++;
        else j++;
    }
    if(j==0)
        cout<<"-1"<<endl;
    else if(k%9==0)
    {
        for(i=0;i<k;i++)
        cout<<"5";
        for(i=0;i<j;i++)
        cout<<"0"<<endl;
    }
    else
    {
        for(;k%9!=0;k--);
        if(k==0)
        {for(i=0;i<k;i++)
            cout<<"5";
        for(i=0;i<j;i++)
        cout<<"0"<<endl;
        }
        else cout<<"0"<<endl;
    }
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    vector<int>w;
    vector<int>v;
    int n,i,a=0,b=0,e=0,s=0,x,sum=0,q=0,j,l,he,guanjian;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>x;
        if(x==0)
        a++;
        else b++;
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    for(i=v.size()-1;i>=0;i--)
    {
        sum=sum*10+v[i];
    }
    for(;;)
    {
        j=1;
        he=90*j;
        j++;
        if(he>sum)
        break;
        l=he;
        for(;;)
        {
            if(l%5!=0)
            q++;
            l=l/10;
            if(l==0)
            break;
        }
        if(q==0)
        w.push_back(he);
    }
    for(j=w.size()-1;j>=0;j--)
    {
        guanjian=w[j];
        for(;guanjian!=0;)
        {
            if(guanjian%10==0)
            e++;
            else
            s++;
            guanjian=guanjian/10;
        }
        if(e<=a &&s<=b)
        {
            cout<<w[j];
            break;
        }
        e=0;
        s=0;
        if(j==0)
        {
            if(a>0)
            cout<<"0";
            else
            cout<<"-1";
        }
    }
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,i,j,k,m;
	m=0;
	j=0;
	cin>>n;
	int *p=new int[n+1]();
	for(i=1;i<=n;i++)
	{
		cin>>p[i];
		if(p[i]==5) m++;
	}
	for(k=m;k>0;k--)
		if(k%9==0)
		{
			j=1;
			break;
		}
	if(m<n&&j==1)
	{
		for(i=0;i<k;i++)
			cout<<"5";
		for(i=0;i<n-m;i++)
			cout<<"0";
		cout<<endl;
	}
	else if(m!=n)
		cout<<"0"<<endl;
	else
		cout<<"-1"<<endl;

	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;
int j,k;
int f(int j,int k)
{
    if(j==0)
    return -1;
    int q,p,m,n;
    m=j;n=k;
    int sum=5;
    for(q=0;q<m-1;q++)
    {
        sum=sum*10+5;
    }
    for(p=0;p<n;p++)
    {
        sum=sum*10;
    }
    if(sum%90==0)
    {
    return sum;
    }
    else return f(j-1,k);
}

int main()
{
    int m,n,i;
    j=0;
    k=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>m;
        if(m==5)
        {
            j++;
        }
        else
        {
            k++;
        }
        }
        cout<<f(j,k)<<endl;

        return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,a[1005];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+1+n);
    int nu=1;
    for(int i=1;i<=n;i++){
        if(a[i]==5){
            nu=i;break;
        }
    }
    int sum=n-nu+1;
    if(sum<9||nu==1) cout<<"0\n";
    else{
        int tmp=sum/9;
        for(int i=1;i<=tmp;i++){
            for(int j=1;j<=9;j++) cout<<"5";
        }
        for(int i=1;i<nu;i++) cout<<"0";
        cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int vis[7];
int main()
{
    int n;
    int tmp;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>tmp;
        vis[tmp]++;
    }
    if(vis[0]==0) {
        cout<<"-1"<<endl;
        return 0;
    }
    else{
        int round=vis[5]/9*9;
        if(round==0){
            cout<<0<<endl;
            return 0;
        }
        for(int i=0;i<round;i++){
            cout<<5;
        }
        for(int i=0;i<vis[0];i++){
            cout<<0;
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
   int num5,num0,i,a,r,n;
   while(cin>>n)
   {
       num5=0;
       num0=0;
       for(i=1;i<=n;i++)
       {
           cin>>a;
           if(a==5)
            num5++;
           else
            num0++;

       }
       r=num5/9;
       if(num0==0)
        cout<<"-1"<<endl;
       else
        if(r&&num0)
       {
           for(i=1;i<=r*9;i++)
            cout<<"5";
           for(i=1;i<=num0;i++)
            cout<<"0";
       }
       else
       cout<<"0";
        cout<<endl;
   }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,a[1005];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+1+n);
    int nu=1;
    for(int i=1;i<=n;i++){
        if(a[i]==5){
            nu=i;break;
        }
    }
    int sum=n-nu+1;
    if(nu==1) cout<<"-1\n";
    else if(nu>1&&sum<9) cout<<"0\n";
    else{
        int tmp=sum/9;
        for(int i=1;i<=tmp;i++){
            for(int j=1;j<=9;j++) cout<<"5";
        }
        cout<<"0\n";
        //cout<<endl;
    }
    return 0;
}
// 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 0 0 0


#include<bits/stdc++.h>
using namespace std;
int vis[7];
int main()
{
    int n;
    int tmp;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>tmp;
        vis[tmp]++;
    }
    if(vis[0]==0) {
        cout<<"-1"<<endl;
        return 0;
    }
    else{
        int round=vis[5]/9*9;
        for(int i=0;i<round;i++){
            cout<<5;
        }
        for(int i=0;i<vis[0];i++){
            cout<<0;
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{ int n,a[1001],i,max,x,y;
  cin>>n;
   for(i=0;i<n;++i)
    cin>>a[i];
   sort(a,a+n);
   x=lower_bound(a,a+n,5)-a;
   y=n-x;
   if(a[0]!=0)cout<<-1<<endl;
   else {
          if(y<9)cout<<0<<endl;
          else { y=y-y%9;
                 for(i=0;i<y;++i)
                  cout<<5;
                  for(i=0;i<x;++i)
                  cout<<0;
                  cout<<endl;
		       }
		}
}


#include<iostream>
#include<algorithm>
using namespace std;
int b[10000];
int main()
{
   long long n,i,s,t,v,max;
   while(cin>>n)
   {
       s=0;v=10,t=0;
       for(i=0;i<n;i++)
       cin>>b[i];
       sort(b,b+n);
       if(b[0])
       cout<<"-1"<<endl;
       else
       {
           max=0;
           for(i=0;i<n;i++)
           {
               if(b[i])
               s++;
           }
           for(i=1;i<=s;i++)
           {
               t+=5*v;
               if((t%90)==0)
               {
                   if(max<t)
                   max=t;
                   else
                   { v*=10;continue;}
               }
               else
               v*=10;
           }
           cout<<max<<endl;
       }
   }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	vector<int>c;
	int i,a,b,t,d,f;
	while(cin>>a)
	{
		d=0;
		t=0;
		for(i=0;i<a;i++)
		{
		cin>>b;
		c.push_back(b);
		if(b==0)
		t=1;
		}
		if(t==1)
		{
			for(i=0;i<a;i++)
			{
				if(c[i]==5)
				d=d+c[i];
			}
			f=d/45;
			for(i=0;i<f*9;i++)
			cout<<"5";
			if(f==0)
			cout<<"0"<<endl;
			else
			{
			for(i=0;i<a-d/5;i++)
			cout<<0;
			cout<<endl;
		}
		}
		else
		{
			for(i=0;i<a;i++)
			d=d+c[i];
			f=d/90;
			if(f==0)
			cout<<"-1"<<endl;
			else
			{
				for(i=0;i<f*18;i++)
				cout<<"5";
				cout<<endl;
			}
		}
	}
	return 0;
 }


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,a[1005];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+1+n);
    int nu=1;
    for(int i=1;i<=n;i++){
        if(a[i]==5){
            nu=i;break;
        }
    }
    int sum=n-nu+1;
    nu--;
    if(nu==0) cout<<"-1\n";
    else if(nu>0&&sum<9) cout<<"0\n";
    else if(nu>0&&sum>=9){
        int tmp=sum/9;
        for(int i=1;i<=tmp;i++){
            for(int j=1;j<=9;j++) cout<<"5";
        }
        for(int i=1;i<=nu;i++) cout<<"0";
        cout<<endl;
    }
    return 0;
}
// 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 0 0 0


#include<iostream>
using namespace std;
int main()
{
    int n,i,a;
    while(cin>>n)
    {
        int num0=0;
        int num5=0;
        for(i=0;i<n;i++)
        {
            cin>>a;
            if(a==0) num0++;
            if(a==5) num5++;
        }
        if(num0>0&&num5>=9)
        {
            cout<<"555555555";
            for(int j=0;j<num0;j++)
            cout<<0;
            cout<<endl;
        }
        else if(num0>0&&num5<9) cout<<0<<endl;
        else cout<<-1<<endl;
    }
    return 0;
}


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

int main()
{
    int n;
    cin >> n;
    int cnt0 = 0, cnt5 = 0, t;
    for(int i = 0; i < n; i++)
    {
        cin >> t;
        t == 0 ? cnt0++ : cnt5++;
    }

    if(cnt0 == 0)
    {
        cout << "-1" << endl;
    }
    else if(cnt5 < 9)
    {
        cout << "0" << endl;
    }
    else
    {
        for(int i = 0; i < (cnt5 / 9) * 9; i++)
            cout << 5 << endl;
        for(int i = 0; i < cnt0; i++)
            cout << 0 << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long a[1004],i,n,k=0,j=0,sum,h;
    while(cin>>n)
    {   k=0;j=0;sum=0;h=0;
        for(i=0;i<n;i++)
        {cin>>a[i];
        if(a[i]==0)
            k++;
        }
        if(k==0)
            cout<<"-1"<<endl;
        else
        {
            sort(a,a+n,greater<int>());
            for(i=0;i<n-k;i++)
            {
                sum=sum*10+a[i];
                h=sum*10;
                if(h%90==0)
                    {if(h>j)
                    j=h;}
            }
            for(i=1;i<=k-1;i++)
                j*=10;
           cout<<j<<endl;

        }

    }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    short dig=0;
    int n;
    cin>>n;
    int num0=0,num5=0;
    for(int i=0;i<n;i++)
    {
        cin>>dig;
        if(dig==0)
            num0 ++;
        else
            num5++;
    }

    if(num0==0)
        cout<<(-1);

    if(num0 != 0)
    {
    for(int i=(num5-num5 % 9);i>=num0;i--)
    {
        cout<<'5';
    }
    for(int i=num0-1;i>=0;i--)
    {
        cout<<'0';
    }
    }
}


#include <iostream>

using namespace std;

int main()
{
    short dig=0;
    int n;
    cin>>n;
    int num0=0,num5=0;
    for(int i=0;i<n;i++)
    {
        cin>>dig;
        if(dig==0)
            num0 ++;
        else
            num5++;
    }

    if(num0==0)
        cout<<(-1);

    if(num0 != 0 && (num5-num5) % 9 !=0)
    {
    for(int i=(num5-num5 % 9);i>=num0;i--)
    {
        cout<<'5';
    }
    for(int i=num0-1;i>0;i--)
    {
        cout<<'0';
    }
    }

    if((num5-num5 % 9)== 0 && num0!=0)
        cout<<'0';
}


#include <iostream>

using namespace std;

int main()
{
    short dig[1000]={};
    int n;
    cin>>n;
    int num0=0,num5=0;
    for(int i=0;i<n;i++)
    {
        cin>>dig[i];
        if(dig[i]==0)
            num0 ++;
        else
            num5++;
    }

    if(num0==0)
        cout<<(-1);

    for(int i=0;i<num0;i++)
        dig[i]=0;

    for(int i=num0;i<(num5-num5%9);i++)
        dig[i]=5;
    if(num0 != 0)
    {
    for(int i=num5-num5%9;i>=0;i--)
    {
        cout<<dig[i];
    }
    }
}


#include <iostream>

using namespace std;

int main()
{
    short dig[1000]={};
    int n;
    cin>>n;
    int num0=0,num5=0;
    for(int i=0;i<n;i++)
    {
        cin>>dig[i];
        if(dig[i]==0)
            num0 ++;
        else
            num5++;
    }

    if(num0==0)
        cout<<(-1);
    for(int i=0;i<num0;i++)
        dig[i]=0;

    for(int i=num0;i<(num5-num5%9);i++)
        dig[i]=5;

    for(int i=num5-num5%9;i>=0;i--)
    {
        cout<<dig[i];
    }
}


#include <iostream>

using namespace std;

int main()
{
    short dig[1000]={};
    int n;
    cin>>n;
    int num0=0,num5=0;
    for(int i=0;i<n;i++)
    {
        cin>>dig[i];
        if(dig[i]==0)
            num0 ++;
        else
            num5++;
    }

    if(num0==0)
        cout<<(-1);
    for(int i=0;i<num0;i++)
        dig[i]=0;

    for(int i=num0;i<(num5-num5%9);i++)
        dig[i]=5;

    for(int i=num5-num5%9-1;i>=0;i--)
    {
        cout<<dig[i];
    }
}


#include <iostream>

using namespace std;

int main()
{
    short dig=0;
    int n;
    cin>>n;
    int num0=0,num5=0;
    for(int i=0;i<n;i++)
    {
        cin>>dig;
        if(dig==0)
            num0 ++;
        else
            num5++;
    }

    if(num0==0)
        cout<<(-1);

    if(num0 != 0 && num5 !=0)
    {
    for(int i=(num5-num5 % 9);i>=num0;i--)
    {
        cout<<'5';
    }
    for(int i=num0-1;i>0;i--)
    {
        cout<<'0';
    }
    }

    if(num5== 0 && num0!=0)
        cout<<'0';
}


#include <iostream>

using namespace std;

int main()
{
    short dig=0;
    int n;
    cin>>n;
    int num0=0,num5=0;
    for(int i=0;i<n;i++)
    {
        cin>>dig;
        if(dig==0)
            num0 ++;
        else
            num5++;
    }

    if(num0==0)
        cout<<(-1);

    if(num0 != 0 && ((num5-num5% 9)  !=0))
    {
    for(int i=(num5-num5 % 9);i>=1;i--)
    {
        cout<<'5';
    }
    for(int i=num0;i>0;i--)
    {
        cout<<'0';
    }
    }

    if((num5-num5 % 9)== 0 && num0!=0)
        cout<<'0';
}


#include <iostream>

using namespace std;

int main()
{
    short dig=0;
    int n;
    cin>>n;
    int num0=0,num5=0;
    for(int i=0;i<n;i++)
    {
        cin>>dig;
        if(dig==0)
            num0 ++;
        else
            num5++;
    }

    if(num0==0)
        cout<<(-1);

    if(num0 != 0 && ((num5-num5% 9)  !=0))
    {
    for(int i=(num5-num5 % 9+1);i>=num0;i--)
    {
        cout<<'5';
    }
    for(int i=num0;i>0;i--)
    {
        cout<<'0';
    }
    }

    if((num5-num5 % 9)== 0 && num0!=0)
        cout<<'0';
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
int main(){
	int n,A[1000];
	cin>>n;
	vector <int> Ans;
	int tmp=0;
	int counter=0;
	for(int i=0;i<n;i++){
		cin>>A[i];
		if(A[i]==0)
			counter++;
		if(A[i]==5)
			tmp++;
	}
		if(tmp<9 && counter==0)
			cout<<"-1"<<endl;
		else if(tmp>0 && tmp<9  && counter>0)
			cout<<"0"<<endl;
		else if(tmp==0 && counter<=1)
			cout<<"-1"<<endl;
		else{
			int k=tmp%9;
			int z=tmp/9;
			if(k==0 && counter==1){

				for(int i=0;i<(z)*9;i++)
					cout<<"5";
				cout<<endl;
			}
			else if(k==0 && counter>1){

				for(int i=0;i<tmp;i++)
					cout<<"5";
				for(int i=0;i<counter-1;i++)
					cout<<"0";
				cout<<endl;

			}
			else{

				for(int i=0;i<z*9;i++)
					cout<<"5";
				for(int i=0;i<counter;i++)
					cout<<"0";
				cout<<endl;

			}

		}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
int main(){
	int n,A[1000];
	cin>>n;
	vector <int> Ans;
	int tmp=0;
	int counter=0;
	for(int i=0;i<n;i++){
		cin>>A[i];
		if(A[i]==0)
			counter++;
		if(A[i]==5)
			tmp++;
	}
		if(counter==0)
			cout<<"-1"<<endl;
		else{
			int k=tmp%9;
			int z=tmp/9;
			if(z<1 && counter==1)
				cout<<"-1"<<endl;
			else if (z<1 && counter>1)
				cout<<"0"<<endl;
			else if(k==0 && counter==1){
				if(z==1)
					cout<<"-1"<<endl;
				else{
				for(int i=0;i<(z-1)*9;i++)
					cout<<"5";
				cout<<"0";
				cout<<endl;
				}
			}
			else if(k==0 && counter>1){

				for(int i=0;i<tmp;i++)
					cout<<"5";
				for(int i=0;i<counter-1;i++)
					cout<<"0";
				cout<<endl;

			}
			else{

				for(int i=0;i<z*9;i++)
					cout<<"5";
				for(int i=0;i<counter;i++)
					cout<<"0";
				cout<<endl;

			}

		}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
int main(){
	int n,A[1000];
	cin>>n;
	vector <int> Ans;
	int tmp=0;
	int counter=0;
	for(int i=0;i<n;i++){
		cin>>A[i];
		if(A[i]==0)
			counter++;
		if(A[i]==5)
			tmp++;
	}
		if(counter==0)
			cout<<"-1"<<endl;
		else{
			int k=tmp%9;
			int z=tmp/9;
		    for(int i=0;i<z*9;i++)
		        cout<<"5";
		    for(int i=0;i<counter;i++)
		        cout<<"0";
		    cout<<endl;

		}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
int main(){
	int n,A[1000];
	cin>>n;
	vector <int> Ans;
	int tmp=0;
	int counter=0;
	for(int i=0;i<n;i++){
		cin>>A[i];
		if(A[i]==0)
			counter++;
		if(A[i]==5)
			tmp++;
	}
		if(counter==0)
			cout<<"-1"<<endl;
        else if(tmp<9 && counter>0)
            cout<<"0"<<endl;
		else{
			int k=tmp%9;
			int z=tmp/9;
		    for(int i=0;i<z*9;i++)
		        cout<<"5";
		    for(int i=0;i<counter;i++)
		        cout<<"0";
		    cout<<endl;

		}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
int main(){
	int n,A[1000];
	cin>>n;
	vector <int> Ans;
	int tmp=0;
	int counter=0;
	for(int i=0;i<n;i++){
		cin>>A[i];
		if(A[i]==0)
			counter++;
		if(A[i]==5)
			tmp++;
	}
		if(tmp<9 && counter==0)
			cout<<"-1"<<endl;
		else if(counter==0)
			cout<<"-1"<<endl;
		else if(tmp>0 && tmp<9  && counter>0)
			cout<<"0"<<endl;
		else if(tmp==0 && counter<=1)
			cout<<"-1"<<endl;
		else{
			int k=tmp%9;
			int z=tmp/9;
			if(k==0 && counter==1){

				for(int i=0;i<(z-1)*9;i++)
					cout<<"5";
				cout<<"0"<<endl;

			}
			else if(k==0 && counter>1){

				for(int i=0;i<tmp;i++)
					cout<<"5";
				for(int i=0;i<counter-1;i++)
					cout<<"0";
				cout<<endl;

			}
			else{

				for(int i=0;i<z*9;i++)
					cout<<"5";
				for(int i=0;i<counter;i++)
					cout<<"0";
				cout<<endl;

			}

		}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
int main(){
	int n,A[1000];
	cin>>n;
	vector <int> Ans;
	int tmp=0;
	int counter=0;
	for(int i=0;i<n;i++){
		cin>>A[i];
		if(A[i]==0)
			counter++;
		if(A[i]==5)
			tmp++;
	}
		if(counter==0)
			cout<<"-1"<<endl;
		else{
			int k=tmp%9;
			int z=tmp/9;
			if(z<1)
				cout<<"-1"<<endl;
			else if(k==0 && counter==1){
				if(z==1)
					cout<<"-1"<<endl;
				else{
				for(int i=0;i<(z-1)*9;i++)
					cout<<"5";
				cout<<"0";
				cout<<endl;
				}
			}
			else if(k==0 && counter>1){

				for(int i=0;i<tmp;i++)
					cout<<"5";
				for(int i=0;i<counter-1;i++)
					cout<<"0";
				cout<<endl;

			}
			else{

				for(int i=0;i<z*9;i++)
					cout<<"5";
				for(int i=0;i<counter;i++)
					cout<<"0";
				cout<<endl;

			}

		}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
int main(){
	int n,A[1000];
	cin>>n;
	vector <int> Ans;
	int tmp=0;
	int counter=0;
	for(int i=0;i<n;i++){
		cin>>A[i];
		if(A[i]==0)
			counter++;
		if(A[i]==5)
			tmp++;
	}
		if(tmp<9 && counter==0)
			cout<<"-1"<<endl;
		else if(tmp>0 && tmp<9  && counter>0)
			cout<<"0"<<endl;
		else if(tmp==0 && counter<=1)
			cout<<"-1"<<endl;
		else if(tmp>9 && counter==0)
			cout<<"-1"<<endl;
		else if(tmp==9 && counter<=1)
			cout<<"-1"<<endl;
		else{
			int k=tmp%9;
			int z=tmp/9;
			if(k==0 && counter==1){

				for(int i=0;i<(z-1)*9;i++)
					cout<<"5";
				cout<<"0";
				cout<<endl;
			}
			else if(k==0 && counter>1){

				for(int i=0;i<tmp;i++)
					cout<<"5";
				for(int i=0;i<counter-1;i++)
					cout<<"0";
				cout<<endl;

			}
			else{

				for(int i=0;i<z*9;i++)
					cout<<"5";
				for(int i=0;i<counter;i++)
					cout<<"0";
				cout<<endl;

			}

		}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
int main(){
	int n,A[1000];
	cin>>n;
	vector <int> Ans;
	int tmp=0;
	int counter=0;
	for(int i=0;i<n;i++){
		cin>>A[i];
		if(A[i]==0)
			counter++;
		if(A[i]==5)
			tmp++;
	}
		if(tmp<9 && counter==0)
			cout<<"-1"<<endl;
		else if(tmp>0 && tmp<9  && counter>0)
			cout<<"0"<<endl;
		else if(tmp==0 && counter<=1)
			cout<<"-1"<<endl;
		else if(tmp>9 && counter==0)
			cout<<"-1"<<endl;
		else	if(tmp==9 && counter<=1)
			cout<<"-1"<<endl;
		else{
			int k=tmp%9;
			int z=tmp/9;

			if(k==0 && counter==1){

				for(int i=0;i<(z)*9;i++)
					cout<<"5";
				cout<<endl;
			}
			else if(k==0 && counter>1){

				for(int i=0;i<tmp;i++)
					cout<<"5";
				for(int i=0;i<counter-1;i++)
					cout<<"0";
				cout<<endl;

			}
			else{

				for(int i=0;i<z*9;i++)
					cout<<"5";
				for(int i=0;i<counter;i++)
					cout<<"0";
				cout<<endl;

			}

		}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
int main(){
	int n,A[1000];
	cin>>n;
	vector <int> Ans;
	int tmp=0;
	int counter=0;
	for(int i=0;i<n;i++){
		cin>>A[i];
		if(A[i]==0)
			counter++;
		if(A[i]==5)
			tmp++;
	}
		if(tmp<9 && counter==0)
			cout<<"-1"<<endl;
		else if(counter==0)
			cout<<"-1"<<endl;
		else if(tmp<9 && counter>0)
			cout<<"0"<<endl;
		else{
			int k=tmp%9;
			int z=tmp/9;
			if(k==0 && counter==1){

				for(int i=0;i<(z-1)*9;i++)
					cout<<"5";
				cout<<"0"<<endl;

			}
			else if(k==0 && counter>1){

				for(int i=0;i<tmp;i++)
					cout<<"5";
				for(int i=0;i<counter-1;i++)
					cout<<"0";
				cout<<endl;

			}
			else{

				for(int i=0;i<z*9;i++)
					cout<<"5";
				for(int i=0;i<counter;i++)
					cout<<"0";
				cout<<endl;

			}

		}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
using namespace std;
int main(){
	int n,A[1000];
	cin>>n;
	vector <int> Ans;
	int tmp=0;
	int counter=0;
	for(int i=0;i<n;i++){
		cin>>A[i];
		if(A[i]==0)
			counter++;
		if(A[i]==5)
			tmp++;
	}
		if(tmp<9 && counter==0)
			cout<<"-1"<<endl;
		else if(tmp>0 && tmp<9  && counter>0)
			cout<<"0"<<endl;
		else if(tmp==0 && counter<=1)
			cout<<"-1"<<endl;
		else if(tmp>9 && counter==0)
			cout<<"-1"<<endl;
		else if(tmp==9 && counter<=1)
			cout<<"-1"<<endl;
		else if(counter == 0)
			cout<<"-1"<<endl;
		else{
			int k=tmp%9;
			int z=tmp/9;
			if(k==0 && counter==1){

				for(int i=0;i<(z-1)*9;i++)
					cout<<"5";
				cout<<"0";
				cout<<endl;
			}
			else if(k==0 && counter>1){

				for(int i=0;i<tmp;i++)
					cout<<"5";
				for(int i=0;i<counter-1;i++)
					cout<<"0";
				cout<<endl;

			}
			else{

				for(int i=0;i<z*9;i++)
					cout<<"5";
				for(int i=0;i<counter;i++)
					cout<<"0";
				cout<<endl;

			}

		}

	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	long long n,x,a0=0,a5=0;
	cin>>n;
	while(n--){
		cin>>x;
		if(x==0) a0++;
		else a5++;
	}
	a5/=9;
	if(a5>0){
		for(int i=0;i<a5;i++){
			cout<<"555555555";
		}
		for(int i=0;i<a0;i++){
			cout<<"0";
		}
		cout<<endl;
	}
	else if(a0!=0){
		cout<<"0"<<endl;
	}
	else{
		cout<<"-1"<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
int main(){
    int n,a;
    int t[10];
    memset(t,0,sizeof(t));
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a;
        t[a]++;
    }
    if(t[0]==0)cout<<-1<<endl;
    else{
        int tmp=t[5]/9;
        for(int i=0;i<9*tmp;i++){
            cout<<5;
        }
        for(int i=0;i<t[0];i++)
            cout<<0;
        cout<<endl;
    }
}


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

num_f,num_z=0,0
for x in a:
    if x==5:
        num_f+=1
    else:
        num_z+=1

if(num_z==0):
    print(0)
else:
    num_f=num_f//9*9
    res=['5']*num_f
    if(len(res)):
        res.extend(['0']*num_z)
    else:
        res=['0']

    print(''.join(res))


/*
ID: Rec
PROG: test
LANG: C++
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stack>
int n,a1,a2;
int main(){
	scanf("%d",&n);
	a1=a2=0;
	for (int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		if (x==5)++a1;else ++a2;
	}
	if (a1>=9&&a2>=1){
		for (int i=1;i<=a1/9*9;i++)printf("5");
		for (int i=1;i<=a2;i++)printf("0");
	}else printf("0");
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;

int n,s[2],x;

int main()
{
    scanf("%d",&n);
    for(int i = 0;i < n;i ++)
    {
    	scanf("%d",&x);
    	if(x == 5)
    		s[0] ++;
    	else
    		s[1] ++;
	}
	int d = s[0] / 9 * 9;
	if(d && s[1])
	{
		for(int i = 0;i < d;i ++)
			printf("5");
		for(int i = 0;i < s[1];i ++)
			printf("0");
	}
	else if(s[1])
		printf("0");
	else
		printf("-1");
}


#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int num[2];
int main()
{
	int n, x;
	cin >> n;
	while(n--)
	{
		scanf("%d",&x);
		if(x == 5) num[0] ++;
		else num[1] ++;
	}
	n = num[0]/9;
	n = min(n, num[1]);
	if(n == 0)
		printf("0\n");
	else
	{
		for(int i = 0; i < n; i++)
			printf("555555555");
		for(int i = 0; i < num[1]; i++)
			printf("0");
		printf("\n");
	}
	return 0;
}


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

int main() {
    int n;
    scanf("%d",&n);
    int cnt1=0,cnt2=0;
    for (int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        if (x==0)   cnt2++;
        if (x==5)   cnt1++;
    }
    if (cnt1<9){
        printf("0\n");
    }
    else{
        if (cnt2==0){
            printf("0\n");
        }
        else{
            printf("555555555");
            for (int i=1;i<=cnt2;i++){
                printf("0");
            }
            putchar('\n');
        }
    }

}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
	int n;
	int a[1005];
	int cnt;
	while(cin>>n)
	{
		cnt=0;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			if(a[i]==5)
			{
				cnt++;
			}
		}
		int k=cnt/9;
		if((n-cnt)==0)
		{
			cout<<-1<<endl;
		}
		else if((cnt<9))
		{
			cout<<0<<endl;
		}
		else
		{
			for(int i=0;i<k*9;i++)
			{
				cout<<5;
			}
			for(int i=0;i<n-cnt;i++)
			{
				cout<<0;
			}
				cout<<endl;
		}

	}
	return 0;
 }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    x=3;
    int n;
    cin>>n;
    int a;
    int cnt0=0,cnt5=0;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        if(a==5)
            cnt5++; else cnt0++;
    }
    if(cnt0==0)
        cout<<-1;
    else
    if(cnt5<9)
        cout<<0;
    else
    {
        while(cnt5%9!=0)
            cnt5--;
        for(int i=1;i<=cnt5;i++)
            cout<<5;
        for(int i=1;i<=cnt0;i++)
            cout<<0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    x=3;
    int n;
    cin>>n;
    int a;
    int cnt0=0,cnt5=0;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        if(a==5)
            cnt5++; else cnt0++;
    }
    if(cnt5<9)
    {
        if(cnt0==0)
            cout<<-1;
        else
        cout<<0;
    }
    else
    {
        while(cnt5%9!=0)
            cnt5--;
        for(int i=1;i<=cnt5;i++)
            cout<<5;
        for(int i=1;i<=cnt0;i++)
            cout<<0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    bool k=0;
    int x;
    x=3;
    int n;
    cin>>n;
    int a;
    int cnt0=0,cnt5=0;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        if(a==5)
            cnt5++; else cnt0++;
    }
    if(cnt5<9)
    {
        if(cnt0==0)
            cout<<-1;
        else
        cout<<0;
    }
    else
    {
        if(cnt0==0)
        {
            cout<<-1;
            return 0;;
        }
        while(cnt5%9!=0)
        {
            k=1;
            cnt5--;
        }
        for(int i=1;i<=cnt5;i++)
            cout<<5;
            if(k==1)
                cout<<0;
        for(int i=2;i<=cnt0;i++)
            cout<<0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a;
    int cnt0=0,cnt5=0;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        if(a==5)
            cnt5++; else cnt0++;
    }
    if(cnt5<9)
        cout<<0;
    else
    {
        while(cnt5%9!=0)
            cnt5--;
        for(int i=1;i<=cnt5;i++)
            cout<<5;
        for(int i=1;i<=cnt0;i++)
            cout<<0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    x=3;
    int n;
    cin>>n;
    int a;
    int cnt0=0,cnt5=0;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        if(a==5)
            cnt5++; else cnt0++;
    }
    if(cnt5<18)
    {
        if(cnt0==0)
            cout<<-1;
        else
        cout<<0;
    }
    else
    {
        while(cnt5%18!=0)
            cnt5--;
        for(int i=1;i<=cnt5;i++)
            cout<<5;
        for(int i=1;i<=cnt0;i++)
            cout<<0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a;
    int cnt0=0,cnt5=0;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        if(a==5)
            cnt5++; else cnt0++;
    }
    if(cnt5<9)
    {
        if(cnt0==0)
            cout<<-1
        else
        cout<<0;
    }
    else
    {
        while(cnt5%9!=0)
            cnt5--;
        for(int i=1;i<=cnt5;i++)
            cout<<5;
        for(int i=1;i<=cnt0;i++)
            cout<<0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a;
    int cnt0=0,cnt5=0;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        if(a==5)
            cnt5++; else cnt0++;
    }
    if(cnt5<9)
    {
        if(cnt0==0)
            cout<<-1;
        else
        cout<<0;
    }
    else
    {
        while(cnt5%9!=0)
            cnt5--;
        for(int i=1;i<=cnt5;i++)
            cout<<5;
        for(int i=1;i<=cnt0;i++)
            cout<<0;
    }
    return 0;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std ;
typedef long long ll ;
int main()
{
    int n;cin>>n;
    int arr[n],sum=0,count5=0,count0=0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        if(arr[i] == 5)
            count5++;
        else
            count0++;
        sum += arr[i];
    }
    if(sum % 90 == 0)
    {
        while(count5--)
            cout<<"5";
        while(count0--)
            cout<<"0";
    }
    else
    {
        if(count0 != 0 && count5 < 18 && count5 >= 9)
        {
            for (int i = 0; i < 9; ++i)
                cout<<"5";
            while(count0--)
                cout<<"0";
        }
        else if(count0 != 0 && count5 < 9)\
        {
            cout<<"0"<<endl;
        }
        else if(count0 !=0 && count5 >= 18)
        {
            for(int i=0;i<((count5/18)*18);i++)
                cout<<"5";
            for (int i = 0; i < count0; ++i)
            {
                cout<<"0";
            }
        }
        else if(count0 == 0)
        {
            if(count5 < 18)
                cout<<"-1"<<endl;
            else
            {
                for(int i=0;i<((count5/18)*18);i++)
                    cout<<"5";
            }
        }
    }
    return 0 ;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std ;
typedef long long ll ;
int main()
{
    int n;cin>>n;
    int arr[n],sum=0,count5=0,count0=0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        if(arr[i] == 5)
            count5++;
        else
            count0++;
        sum += arr[i];
    }
    if(sum % 90 == 0)
    {
        if(count5 == 0)
            cout<<"0";
        else
        {
            while(count5--)
                cout<<"5";
            while(count0--)
                cout<<"0";
        }
    }
    else
    {
        if(count0 != 0)
        {
            for(int i=0;i<((count5/9)*9);i++)
                cout<<"5";
            for (int i = 0; i < count0; ++i)
                cout<<"0";
        }
        else if(count0 == 0)
        {
            cout<<"-1";
        }
    }
    return 0 ;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std ;
typedef long long ll ;
int main()
{
    int n;cin>>n;
    int arr[n],sum=0,count5=0,count0=0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        if(arr[i] == 5)
            count5++;
        else
            count0++;
        sum += arr[i];
    }
    if(sum % 90 == 0)
    {
        if(count5 == 0)
            cout<<"0";
        else
        {
            while(count5--)
                cout<<"5";
            while(count0--)
                cout<<"0";
        }
    }
    else
    {
        if(count0 != 0 && count5 < 18 && count5 >= 9)
        {
            for (int i = 0; i < 9; ++i)
                cout<<"5";
            while(count0--)
                cout<<"0";
        }
        else if(count0 != 0 && count5 < 9)
        {
            cout<<"0"<<endl;
        }
        else if(count0 !=0 && count5 >= 18)
        {
            for(int i=0;i<((count5/9)*9);i++)
                cout<<"5";
            for (int i = 0; i < count0; ++i)
                cout<<"0";
        }
        else if(count0 == 0)
        {
            cout<<"-1";
        }
    }
    return 0 ;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std ;
typedef long long ll ;
int main()
{
    int n;cin>>n;
    int arr[n],sum=0,count5=0,count0=0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        if(arr[i] == 5)
            count5++;
        else
            count0++;
        sum += arr[i];
    }
    if(sum % 90 == 0)
    {
        if(count5 == 0)
            cout<<"0";
        else
        {
            while(count5--)
                cout<<"5";
            while(count0--)
                cout<<"0";
        }
    }
    else
    {
        if(count0 != 0 && count5 < 18 && count5 >= 9)
        {
            for (int i = 0; i < 9; ++i)
                cout<<"5";
            while(count0--)
                cout<<"0";
        }
        else if(count0 != 0 && count5 < 9)
        {
            cout<<"0"<<endl;
        }
        else if(count0 !=0 && count5 >= 18)
        {
            for(int i=0;i<((count5/9)*9);i++)
                cout<<"5";
            for (int i = 0; i < count0; ++i)
                cout<<"0";
        }
        else if(count0 == 0)
        {
            if(count5 < 18)
                cout<<"-1"<<endl;
            else
            {
                int q=count5/18;
                if(q >= 2)
                {
                    if(q % 2 ==0)
                    {
                        for (int i = 0; i < q*18; ++i)
                        {
                            cout<<"5";
                        }
                    }
                    else
                    {
                        for (int i = 0; i < (q-1)*18; ++i)
                        {
                            cout<<"5";
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 18; ++i)
                        {
                            cout<<"5";
                        }
                }

            }
        }
    }
    return 0 ;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std ;
typedef long long ll ;
int main()
{
    int n;cin>>n;
    int arr[n],sum=0,count5=0,count0=0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        if(arr[i] == 5)
            count5++;
        else
            count0++;
        sum += arr[i];
    }
    if(sum % 90 == 0)
    {
        if(count5 == 0)
            cout<<"0";
        else
        {
            while(count5--)
                cout<<"5";
            while(count0--)
                cout<<"0";
        }
    }
    else
    {
        if(count0 != 0)
        {
            if(count5 < 9)
                cout<<"0";
            else
            {
                for(int i=0;i<((count5/9)*9);i++)
                    cout<<"5";
                for (int i = 0; i < count0; ++i)
                    cout<<"0";
            }
        }
        else if(count0 == 0)
        {
            cout<<"-1";
        }
    }
    return 0 ;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std ;
typedef long long ll ;
int main()
{
    int n;cin>>n;
    int arr[n],sum=0,count5=0,count0=0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        if(arr[i] == 5)
            count5++;
        else
            count0++;
        sum += arr[i];
    }
    if(sum % 90 == 0)
    {
        if(count5 == 0)
            cout<<"0";
        else
        {
            while(count5--)
                cout<<"5";
            while(count0--)
                cout<<"0";
        }
    }
    else
    {
        if(count0 != 0 && count5 < 18 && count5 >= 9)
        {
            for (int i = 0; i < 9; ++i)
                cout<<"5";
            while(count0--)
                cout<<"0";
        }
        else if(count0 != 0 && count5 < 9)
        {
            cout<<"0"<<endl;
        }
        else if(count0 !=0 && count5 >= 18)
        {
            for(int i=0;i<((count5/18)*18);i++)
                cout<<"5";
            for (int i = 0; i < count0; ++i)
                cout<<"0";
        }
        else if(count0 == 0)
        {
            if(count5 < 18)
                cout<<"-1"<<endl;
            else
            {
                int q=count5/18;
                if(q >= 2)
                {
                    if(q % 2 ==0)
                    {
                        for (int i = 0; i < q*18; ++i)
                        {
                            cout<<"5";
                        }
                    }
                    else
                    {
                        for (int i = 0; i < (q-1)*18; ++i)
                        {
                            cout<<"5";
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 18; ++i)
                        {
                            cout<<"5";
                        }
                }

            }
        }
    }
    return 0 ;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std ;
typedef long long ll ;
int main()
{
    int n;cin>>n;
    int arr[n],sum=0,count5=0,count0=0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        if(arr[i] == 5)
            count5++;
        else
            count0++;
        sum += arr[i];
    }
    if(sum % 90 == 0)
    {
        if(count5 == 0)
            cout<<"0";
        else
        {
            while(count5--)
                cout<<"5";
            while(count0--)
                cout<<"0";
        }
    }
    else
    {
        if(count0 != 0 && count5 < 18 && count5 >= 9)
        {
            for (int i = 0; i < 9; ++i)
                cout<<"5";
            while(count0--)
                cout<<"0";
        }
        else if(count0 != 0 && count5 < 9)
        {
            cout<<"0"<<endl;
        }
        else if(count0 !=0 && count5 >= 18)
        {
            for(int i=0;i<((count5/18)*18);i++)
                cout<<"5";
            for (int i = 0; i < count0; ++i)
            {
                cout<<"0";
            }
        }
        else if(count0 == 0)
        {
            if(count5 < 18)
                cout<<"-1"<<endl;
            else
            {
                for(int i=0;i<((count5/18)*18);i++)
                    cout<<"5";
            }
        }
    }
    return 0 ;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std ;
typedef long long ll ;
int main()
{
    int n;cin>>n;
    int arr[n],sum=0,count5=0,count0=0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        if(arr[i] == 5)
            count5++;
        else
            count0++;
        sum += arr[i];
    }
    if(sum % 90 == 0)
    {
        while(count5--)
            cout<<"5";
        while(count0--)
            cout<<"0";
    }
    else
    {
        if(count0 != 0 && count5 < 9)
        {
            cout<<"0"<<endl;
        }
        else if(count0 == 0 && count5 < 9)\
        {
            cout<<"-1"<<endl;
        }
        else
        {
            for(int i=0;i<((count5/9)*9);i++)
                cout<<"5";
            for (int i = 0; i < count0; ++i)
            {
                cout<<"0";
            }
        }
    }
    cout<<endl;
    return 0 ;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std ;
typedef long long ll ;
int main()
{
    int n;cin>>n;
    int arr[n],sum=0,count5=0,count0=0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
        if(arr[i] == 5)
            count5++;
        else
            count0++;
        sum += arr[i];
    }
    if(count0 != 0)
    {
        if(count5 < 9)
            cout<<"0";
        else
        {
            for(int i=0;i<((count5/9)*9);i++)
                cout<<"5";
            for (int i = 0; i < count0; ++i)
                cout<<"0";
        }
    }
    else if(count0 == 0)
    {
        cout<<"-1";
    }
    return 0 ;
}


# include<iostream>
using namespace std;
int main() {
int N,j,d,a,b;
cin>>N;
cin>>d;
a=0;
b=0;
for(j=0;j<N;j++)
{
if(d==5)
a++;
else
b++;
}
if (b>0)
{
d=a/9;
for(j=0;j<d*9;j++)
cout<<"5";
if (d>0)
for(j=0;j<b;j++)
cout<<"0";
else
cout<<"";
}
else
cout<<"0";
//system("pause");
return 0;
}


# include<iostream>
using namespace std;
int main() {
int N,j,d,a,b;
cin>>N;
cin>>d;
a=0;
b=0;
for(j=0;j<N;j++)
{
if(d==5)
a++;
else
b++;
}
if (b>0)
{
d=a/9;
for(j=0;j<d*9;j++)
cout<<"5";
if (d>0)
for(j=0;j<b;j++)
cout<<"0";
else
cout<<"0";
}
else cout<<"-1";
//system("pause");
return 0;
}


# include<iostream>
using namespace std;
int main() {
int N,j,d,a,b;
cin>>N;
cin>>d;
a=0;
b=0;
for(j=0;j<N;j++)
{
if(d==5)
a++;
else
b++;
}
if (b>0)
{
d=a/9;
for(j=0;j<d*9;j++)
cout<<"5";
if (d>0)
for(j=0;j<b;j++)
cout<<"0";
else
cout<<"5";
}
else
cout<<"0";
//system("pause");
return 0;
}


# include<iostream>
using namespace std;
int main() {
int N,j,d,a,b;
cin>>N;
cin>>d;
a=0;
b=0;
for(j=0;j<N;j++)
{
if(d==5)
a++;
else
b++;
}
if (b>0)
{
d=a/9;
for(j=0;j<d*9;j++)
cout<<"5";
if (d>0)
for(j=0;j<b;j++)
cout<<"0";
else
cout<<"0";
}
else
cout<<"0";
//system("pause");
return 0;
}


#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int inp,n5=0,n0=0;
for(int i=0;i<n;i++)
{
	cin>>inp;
	if(inp==5) n5+=1;
	else n0+=1;
}
if(n0==0)
{
cout<<"-1\n";
return 0;
}
n5=(n5/9);
n5=n5*9;
for(int i=0;i<n5;i++) cout<<"5";
if(n5!=0) for(int i=0;i<n0;i++) cout<<"0";
else cout<<"0";
cout<<endl;
system("pause");
return 0;
}


# include<iostream>
using namespace std;
int main() {
int N,j,d,a,b;
cin>>N;
cin>>d;
a=0;
b=0;
for(j=0;j<N;j++)
{
if(d==5)
a++;
else
b++;
}
if (b>0)
{
d=a/9;
for(j=0;j<d*9;j++)
cout<<"5";
if (d>0)
for(j=0;j<b;j++)
cout<<"0";
else
cout<<"0";
}
else
cout<<"0";
system("pause");
return 0;
}


# include<iostream>
using namespace std;
int main() {
int N,j,d,a,b;
cin>>N;
cin>>d;
a=0;
b=0;
for(j=0;j<N;j++)
{
if(d==5)
a++;
else
b++;
}
if (b>0)
{
d=a/9;
for(j=0;j<d*9;j++)
cout<<"5";
if (d>0)
for(j=0;j<b;j++)
cout<<"5";
else
cout<<"0";
}
else
cout<<"0";
//system("pause");
return 0;
}


#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int inp,n5=0,n0=0;
for(int i=0;i<n;i++)
{
	cin>>inp;
	if(inp==5) n5+=1;
	else n0+=1;
}
if(n0==0)
{
cout<<"-1\n";
return 0;
}
n5=(n5/9);
n5=n5*9;
for(int i=0;i<n5;i++) cout<<"5";
if(n5!=0) for(int i=0;i<n0;i++) cout<<"0";
else cout<<"0";
cout<<endl;
//system("pause");
return 0;
}


#pragma comment(linker,"/STACK:60000000")
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdio>
#include <string.h>
#include <math.h>
#include <cmath>
#include <ctype.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <queue>

#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)

#define FOR(i,a,b,k) for (i=a;i<=b;i+=k)
#define RFOR(i,a,b,k) for (i=a;i>=b;i-=k)

#define vao(st) freopen(st,"r",stdin)
#define ra(st) freopen(st,"w",stdout)
#define dongvao fclose(stdin)
#define dongra fclose(stdout)

#define LL long long
#define I int
#define LI long int
#define VTI vector<int>
#define VTL vector<long int>
#define VTB vector<bool>
#define B bool
#define ST string
#define SQR(x) x*x
#define SQRT(x) sqrt(x)
#define length(s) s.size()
#define SE(a) a.size()

using namespace std;
LI n;
VTL a;
void read()
{
    cin>>n;
    a.resize(2);
    a[0]=0;a[1]=0;
    LI i;I j;
    FOR(i,1,n,1)
    {
        cin>>j;
        if (j==0) a[0]+=1; else a[1]+=1;
    }
}
void process()
{
    while ((5*a[1])%9!=0) --a[1];
    if (a[1]<=0)
    {
        if (a[0]<=0) cout<<"-1"; else cout<<"0";
    } else
    {
        LI i;
        for(i=0;i<a[1];i++) cout<<"5";
        for(i=0;i<a[0];i++) cout<<"0";
    }
}
void write()
{
    process();
}
int main()
{

    read();
    write();
    return 0;
}