#include <iostream>
#include<cstdio>
#include<math.h>
#include <cmath>
#include<cstring>

using namespace std;

int main()
{
    int n  , m , counter = 0 , counter1 = 0 , number = 0 , found = 0 , flag = 0;
    scanf("%d %d",&n,&m);
    for (counter = 1 ; counter <= n ; counter++)
    {
        flag = 0;
        found = 0;
        for (counter1 = 1 ; counter1 <= m && found == 0; counter1++)
        {
            if (counter % 2 != 0 && flag == 0)
            {
                number++;
                flag = 1;
            }
            if (counter % 2 != 0)
            {
                printf("#");
            }
            else if (number % 2 != 0)
            {
                int i = 0;
                for (i = 1 ; i < m ; i++)
                {
                    printf(".");
                }
                printf("#");
                found = 1;
            }
            else
            {
                int s = 0;
                printf("#");
                for (s = 1 ; s < m ; s++)
                {
                    printf(".");
                }
                found = 1;
            }
        }
        printf("\n");
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int p=1;
for (int i=1;i<n+1;i++)
{
    for (int j=1;j<m+1;j++)
    {
        if (i%2==1)
            cout<<"#";
        else
        {
            if (p%2==1)
            {
                if (j==m) cout<<"#";
                else cout<<".";
            }
            if (p%2==0)
            {
                if (j==1) cout<<"#";
                else cout<<".";
            }
        }
    }
    if (i%2==0) p++;
    cout<<endl;
}
p=0;
}


#include<iostream>
#include<string>
#include<SDL/SDL.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int p=1;
for (int i=1;i<n+1;i++)
{
    for (int j=1;j<m+1;j++)
    {
        if (i%2==1)
            cout<<"#";
        else
        {
            if (p%2==1)
            {
                if (j==m) cout<<"#";
                else cout<<".";
            }
            if (p%2==0)
            {
                if (j==1) cout<<"#";
                else cout<<".";
            }
        }
    }
    if (i%2==0) p++;
    cout<<endl;
}
p=0;
}


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

int n, m;

int main()
{
    scanf("%d%d", &n, &m);

    int k = 0;
    for (int i=0; i<n; i++) {
        if (i) printf("\n");
        if (i%2==0) k = k ? 0: m-1;
        for (int j=0; j<m; j++) {
            if (i%2==0) printf("#");
            else {
                if (j == k) {
                   printf("#");
                } else {
                    printf(".");
                }
            }
        }
    }
    return 0;
}


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

int main() {

    int n, m;
    scanf("%d%d", &n, &m);

    int k = 0;
    for (int i=0; i<n; i++) {
        if (i) printf("\n");
        if (i%2==0) k = k? 0 : m-1;
        for (int j=0; j<m; j++) {
            if (i%2==0)
                printf("#");
            else {
                if (k == j)
                    printf("#");
                else printf(".");
            }

        }
    }

    return 0;
}


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

typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
#define endl "\n";
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

ll const inf = 1e18;
ll const maxn = 2e6+5;
ll const mod = 1e9+7;

// code begins here

int main(){
    fast;

    ll n, haszero=0, sum=0;
    cin>>n;

    vector<ll> arr(n);
    for(auto &u: arr){
        cin>>u;
        if(u==0)
            haszero = 1;
        sum += u;
    }

    if(!haszero){
        cout<<-1<<endl;
        return 0;
    }

    map<ll, vector<ll>> mp;

    for(auto u: arr)
        mp[u%3].push_back(u);

    vector<ll> ans;

    for(auto u: mp[0])
        ans.push_back(u);

    sort(mp[1].rbegin(), mp[1].rend());
    sort(mp[2].rbegin(), mp[2].rend());

    ll sz1 = mp[1].size();
    ll sz2 = mp[2].size();

    if(sum%3==1){
        if(!mp[1].empty())
            mp[1].pop_back();
        else{
            mp[2].pop_back();
            mp[2].pop_back();
        }
    }

    if(sum%3==2){
        if(!mp[2].empty())
            mp[2].empty();
        else{
            mp[1].pop_back();
            mp[1].pop_back();
        }
    }

    for(auto u: mp[1])
        ans.push_back(u);
    for(auto u: mp[2])
        ans.push_back(u);

    sort(ans.rbegin(), ans.rend());

    if(ans[0]==0){
        cout<<0<<endl;
        return 0;
    }

    for(auto u: ans)
        cout<<u;

    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
typedef long long  ll;
const ll N = 1e5 + 10;
ll sum , n , x , d , fg;
map<ll , ll> mp;
vector<ll> v;
void solve()
{
    cin >> n;
    for(ll i = 1 ; i <= n ; i ++)   cin >> x , mp[x] ++ , sum += x;
    if(mp[0])
    {
        if(sum % 3 == 1)
        {
            vector<ll> v = {1 , 4 , 7};
            for(auto i : v)
                if(mp[i])
                {
                    fg = 1;
                    mp[i] --;
                    if(mp[i] == 0) mp.erase(i);
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {2 , 5 , 8};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp[i])
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        if(mp[i] == 0) mp.erase(i);
                        cnt ++;
                    }
                }

            }

        }else if (sum % 3 == 2)
        {
            vector<ll> v = {2 , 5 , 8};
            for(auto i : v)
                if(mp[i])
                {
                    fg = 1;
                    mp[i] --;
                    if(mp[i] == 0) mp.erase(i);
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {1 , 4 , 7};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp[i])
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        if(mp[i] == 0) mp.erase(i);
                        cnt ++;
                    }
                }

            }
        }
        if(mp.size() == 1 && mp[0])
        {
            cout << 0 << endl;
            exit(0);
        }
        for(auto i : mp)
            for(ll j = 0 ; j < i.second ; j ++) v.push_back(i.first);
        sort(v.begin() , v.end() , greater<ll>());
        for(auto i : v)
            cout << i ;

    }else
    {
        cout << -1 << endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
typedef long long  ll;
const ll N = 1e5 + 10;
ll sum , n , x , d , fg;
map<ll , ll> mp;
vector<ll> v;
void solve()
{
    cin >> n;
    for(ll i = 1 ; i <= n ; i ++)   cin >> x , mp[x] ++ , sum += x;
    if(mp[0])
    {
        if(sum % 3 == 1)
        {
            vector<ll> v = {1 , 4 , 7};
            for(auto i : v)
                if(mp[i])
                {
                    fg = 1;
                    mp[i] --;
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {2 , 5 , 8};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp[i])
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        cnt ++;
                    }
                }

            }

        }else if (sum % 3 == 2)
        {
            vector<ll> v = {2 , 5 , 8};
            for(auto i : v)
                if(mp[i])
                {
                    fg = 1;
                    mp[i] --;
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {1 , 4 , 7};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp[i])
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        cnt ++;
                    }
                }

            }
        }
        if(mp.size() == 1 && mp[0])
        {
            cout << 0 << endl;
            exit(0);
        }
        for(auto i : mp)
            for(ll j = 0 ; j < i.second ; j ++) v.push_back(i.first);
        sort(v.begin() , v.end() , greater<ll>());
        for(auto i : v)
            cout << i ;

    }else
    {
        cout << -1 << endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
typedef long long  ll;
const ll N = 1e5 + 10;
ll sum , n , x , d , fg;
map<ll , ll> mp;
vector<ll> v;
void solve()
{
    cin >> n;
    for(ll i = 1 ; i <= n ; i ++)   cin >> x , mp[x] ++ , sum += x;
    if(mp[0] && (mp.size() > 1 || mp[0] == 1))
    {
        if(sum % 3 == 1)
        {
            vector<ll> v = {1 , 4 , 7};
            for(auto i : v)
                if(mp[i])
                {
                    fg = 1;
                    mp[i] --;
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {2 , 5 , 8};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp[i])
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        cnt ++;
                    }
                }

            }

        }else if (sum % 3 == 2)
        {
            vector<ll> v = {2 , 5 , 8};
            for(auto i : v)
                if(mp[i])
                {
                    fg = 1;
                    mp[i] --;
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {1 , 4 , 7};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp[i])
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        cnt ++;
                    }
                }

            }
        }
        for(auto i : mp)
            for(ll j = 0 ; j < i.second ; j ++) v.push_back(i.first);
        sort(v.begin() , v.end() , greater<ll>());
        for(auto i : v)
            cout << i ;

    }else
    {
        cout << -1 << endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
typedef long long  ll;
const ll N = 1e5 + 10;
ll sum , n , x , d , fg;
map<ll , ll> mp;
vector<ll> v;
void solve()
{
    cin >> n;
    for(ll i = 1 ; i <= n ; i ++)   cin >> x , mp[x] ++ , sum += x;
    if(mp[0] && (mp.size() > 1 || mp[0] == 1))
    {
        if(sum % 3 == 1)
        {
            vector<ll> v = {1 , 4 , 7};
            for(auto i : v)
                if(mp[i])
                {
                    fg = 1;
                    mp[i] --;
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {2 , 5 , 8};
                ll cnt  = 0;
                for(auto i : v)
                {
                    if(mp[i])   mp[i] -- , cnt ++;
                    if(cnt == 2)
                    {
                        fg = 1 ;
                        break;
                    }
                }

            }

        }else if (sum % 3 == 2)
        {
            vector<ll> v = {2 , 5 , 8};
            for(auto i : v)
                if(mp[i])
                {
                    fg = 1;
                    mp[i] --;
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {1 , 4 , 7};
                ll cnt  = 0;
                for(auto i : v)
                {
                    if(mp[i])   mp[i] -- , cnt ++;
                    if(cnt == 2)
                    {
                        fg = 1 ;
                        break;
                    }
                }

            }
        }
        if(fg == 0 && sum % 3 != 0)
            {
                cout << -1 << endl;
                exit(0);
            }
        for(auto i : mp)
            for(ll j = 0 ; j < i.second ; j ++) v.push_back(i.first);
        sort(v.begin() , v.end() , greater<ll>());
        for(auto i : v)
            cout << i ;

    }else
    {
        cout << -1 << endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
typedef long long  ll;
const ll N = 1e5 + 10;
ll sum , n , x , d , fg;
map<ll , ll> mp;
vector<ll> v;
void solve()
{
    cin >> n;
    for(ll i = 1 ; i <= n ; i ++)   cin >> x , mp[x] ++ , sum += x;
    if(mp.count(0))
    {
        if(sum % 3 == 1)
        {
            vector<ll> v = {1 , 4 , 7};
            for(auto i : v)
                if(mp.count(i))
                {
                    fg = 1;
                    mp[i] --;
                    if(mp[i] == 0) mp.erase(i);
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {2 , 5 , 8};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp.count(i))
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        if(mp[i] == 0) mp.erase(i);
                        cnt ++;
                    }
                }

            }

        }else if (sum % 3 == 2)
        {
            vector<ll> v = {2 , 5 , 8};
            for(auto i : v)
                if(mp.count(i))
                {
                    fg = 1;
                    mp[i] --;
                    if(mp[i] == 0) mp.erase(i);
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {1 , 4 , 7};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp.count(i))
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        if(mp[i] == 0) mp.erase(i);
                        cnt ++;
                    }
                }

            }
        }
        for(auto i : mp)
            cout << i.first << " " << i.second << endl;
        if(mp.size() == 1 && mp[0])
        {
            cout << 0 << endl;
            exit(0);
        }
        sort(v.begin() , v.end() , greater<ll>());
        for(auto i : v)
            cout << i ;

    }else
    {
        cout << -1 << endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
typedef long long  ll;
const ll N = 1e5 + 10;
ll sum , n , x , d , fg;
map<ll , ll> mp;
vector<ll> v;
void solve()
{
    cin >> n;
    for(ll i = 1 ; i <= n ; i ++)   cin >> x , mp[x] ++ , sum += x;
    if(mp.count(0))
    {
        if(sum % 3 == 1)
        {
            vector<ll> v = {1 , 4 , 7};
            for(auto i : v)
                if(mp.count(i))
                {
                    fg = 1;
                    mp[i] --;
                    if(mp[i] == 0) mp.erase(i);
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {2 , 5 , 8};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp.count(i))
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        if(mp[i] == 0) mp.erase(i);
                        cnt ++;
                    }
                }

            }

        }else if (sum % 3 == 2)
        {
            vector<ll> v = {2 , 5 , 8};
            for(auto i : v)
                if(mp.count(i))
                {
                    fg = 1;
                    mp[i] --;
                    if(mp[i] == 0) mp.erase(i);
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {1 , 4 , 7};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp.count(i))
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        if(mp[i] == 0) mp.erase(i);
                        cnt ++;
                    }
                }

            }
        }
        if(mp.size() == 1 && mp[0])
        {
            cout << 0 << endl;
            exit(0);
        }
        for(auto i : mp)
            for(ll j = 0 ; j < i.second ; j ++) v.push_back(i.first);
        sort(v.begin() , v.end() , greater<ll>());
        for(auto i : v)
            cout << i ;

    }else
    {
        cout << -1 << endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
typedef long long  ll;
const ll N = 1e5 + 10;
ll sum , n , x , d , fg;
map<ll , ll> mp;
vector<ll> v;
void solve()
{
    cin >> n;
    for(ll i = 1 ; i <= n ; i ++)   cin >> x , mp[x] ++ , sum += x;
    if(mp[0] && mp.size() == 1)
    {
        cout << 0 << endl;
        exit(0);
    }
    if(mp[0])
    {
        if(sum % 3 == 1)
        {
            vector<ll> v = {1 , 4 , 7};
            for(auto i : v)
                if(mp[i])
                {
                    fg = 1;
                    mp[i] --;
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {2 , 5 , 8};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp[i])
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        cnt ++;
                    }
                }

            }

        }else if (sum % 3 == 2)
        {
            vector<ll> v = {2 , 5 , 8};
            for(auto i : v)
                if(mp[i])
                {
                    fg = 1;
                    mp[i] --;
                    break;
                }
            if(fg == 0)
            {
                vector<ll> v = {1 , 4 , 7};
                ll cnt  = 0;
                for(auto i : v)
                {
                    while(mp[i])
                    {
                        if(cnt == 2)    break;
                        mp[i] --;
                        cnt ++;
                    }
                }

            }
        }
        for(auto i : mp)
            for(ll j = 0 ; j < i.second ; j ++) v.push_back(i.first);
        sort(v.begin() , v.end() , greater<ll>());
        for(auto i : v)
            cout << i ;

    }else
    {
        cout << -1 << endl;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    solve();
    return 0;
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define pb      push_back
#define all(v)  v.rbegin(),v.rend()
#define see(x)  cout<<#x<<" : "<<(x)<<endl;
#define ya      cout<<"YES"<<endl;
#define no      cout<<"NO"<<endl;
#define ff      first
#define sc      second
typedef long long ll;
typedef pair<ll,ll> pii;

int main()
{
    ll n,k,i,sum=0;
    cin>>n;
    vector< pii >v;
    map<ll,ll>m;
    bool ok=1,non0=0;
    for(i=0; i<n; i++)
    {
        cin>>k;
        m[k]++;
        v.pb({k,1});
        sum+=k;
        if(!k)ok=0;
        else non0=1;
    }
    if(ok)
    {
        cout<<-1<<endl;
        exit(0);
    }
    if(sum%3==0)
    {
        if(non0)
            for(auto [l,r]:v)
            {
                cout<<l;
            }
        else
            cout<<0<<endl;
        exit(0);
    }
    ll tmp=sum%3;
    if(tmp==1)
    {
        if(m[tmp])
        {
            m[tmp]--;
        }
        else if(m[tmp+3])
        {
            m[tmp+3]--;
        }
        else if(m[tmp+6])
        {
            m[tmp+6]--;
        }
        else
        {
            ll del=2;
            while(m[2]>0 && del>0)m[2]--,del--;
        }
    }
    else
    {
        if(m[tmp])
        {
            m[tmp]--;
        }
        else if(m[tmp+3])
        {
            m[tmp+3]--;
        }
        else if(m[tmp+6])
        {
            m[tmp+6]--;
        }
        else
        {
            ll del=2;
            while(m[1]>0 && del>0)m[1]--,del--;
            while(m[4]>0 && del>0)m[4]--,del--;
        }
    }
    vector<ll>vv;
    sum=0;
    non0=0;
    for(auto [l,r]:m)
    {
        while(r--)
        {
            vv.pb(l);
            if(l)non0=1;
            sum+=l;
        }
    }
    ok=1;
    sort(all(vv));
    if(sum%3==0 && non0)
        for(auto p:vv)
        {
            cout<<p;
        }
    else
    {
        cout<<0<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define pb      push_back
#define all(v)  v.rbegin(),v.rend()
#define see(x)  cout<<#x<<" : "<<(x)<<endl;
#define ya      cout<<"YES"<<endl;
#define no      cout<<"NO"<<endl;
#define ff      first
#define sc      second
typedef long long ll;
typedef pair<ll,ll> pii;

int main()
{
    ll n,k,i,sum=0;
    cin>>n;
    vector< pii >v;
    map<ll,ll>m;
    bool ok=1,non0=0;
    for(i=0; i<n; i++)
    {
        cin>>k;
        m[k]++;
        v.pb({k,1});
        sum+=k;
        if(!k)ok=0;
        else non0=1;
    }
    if(ok)
    {
        cout<<-1<<endl;
        exit(0);
    }
    if(sum%3==0)
    {
        if(non0)
            for(auto [l,r]:v)
            {
                cout<<l;
            }
        else
            cout<<0<<endl;
        exit(0);
    }
    ll tmp=sum%3;
    if(tmp==1)
    {
        if(m[tmp])
        {
            m[tmp]--;
        }
        else if(m[tmp+3])
        {
            m[tmp+3]--;
        }
        else if(m[tmp+6])
        {
            m[tmp+6]--;
        }
        else
        {
            ll del=2;
            while(m[2]>0 && del>0)m[2]--,del--;
        }
    }
    else
    {
        if(m[tmp])
        {
            m[tmp]--;
        }
        else if(m[tmp+3])
        {
            m[tmp+3]--;
        }
        else if(m[tmp+6])
        {
            m[tmp+6]--;
        }
        else
        {
            ll del=2;
            while(m[1]>0 && del>0)m[1]--,del--;
            while(m[4]>0 && del>0)m[4]--,del--;
        }
    }
    vector<ll>vv;
    sum=0;
    non0=0;
    for(auto [l,r]:m)
    {
        while(r--)
        {
            if(l)non0=1;
            sum+=l;
            vv.pb(l);
        }
    }
    sort(all(vv));
    ok=1;
    if(sum%3==0 && non0)
        for(auto p:vv)
        {
            if(ok)
            {
                ok=0;
                if(p==0)
                {
                    cout<<p;
                    exit(0);
                }
            }
            cout<<p;
        }
    else
    {
        cout<<0<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define pb      push_back
#define all(v)  v.rbegin(),v.rend()
#define see(x)  cout<<#x<<" : "<<(x)<<endl;
#define ya      cout<<"YES"<<endl;
#define no      cout<<"NO"<<endl;
#define ff      first
#define sc      second
typedef long long ll;
typedef pair<ll,ll> pii;

int main()
{
    ll n,k,i,sum=0;
    cin>>n;
    vector< pii >v;
    map<ll,ll>m;
    bool ok=1,non0=0;
    for(i=0; i<n; i++)
    {
        cin>>k;
        m[k]++;
        v.pb({k,1});
        sum+=k;
        if(!k)ok=0;
        else non0=1;
    }
    if(ok)
    {
        cout<<-1<<endl;
        exit(0);
    }
    if(sum%3==0)
    {
        sort(all(v));
        if(non0)
            for(auto [l,r]:v)
            {
                cout<<l;
            }
        else
            cout<<0<<endl;
        exit(0);
    }
    ll tmp=sum%3;
    if(tmp==1)
    {
        if(m[tmp])
        {
            m[tmp]--;
        }
        else if(m[tmp+3])
        {
            m[tmp+3]--;
        }
        else if(m[tmp+6])
        {
            m[tmp+6]--;
        }
        else
        {
            ll del=2;
            while(m[2]>0 && del>0)m[2]--,del--;
            while(m[5]>0 && del>0)m[5]--,del--;
            while(m[8]>0 && del>0)m[8]--,del--;
        }
    }
    else
    {
        if(m[tmp])
        {
            m[tmp]--;
        }
        else if(m[tmp+3])
        {
            m[tmp+3]--;
        }
        else if(m[tmp+6])
        {
            m[tmp+6]--;
        }
        else
        {
            ll del=2;
            while(m[1]>0 && del>0)m[1]--,del--;
            while(m[4]>0 && del>0)m[4]--,del--;
            while(m[7]>0 && del>0)m[7]--,del--;
        }
    }
    vector<ll>vv;
    sum=0;
    non0=0;
    for(auto [l,r]:m)
    {
        while(r--)
        {
            vv.pb(l);
            if(l)non0=1;
            sum+=l;
        }
    }
    ok=1;
    sort(all(vv));
    if(sum%3==0 && non0)
        for(auto p:vv)
        {
            cout<<p;
        }
    else
    {
        cout<<0<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define pb      push_back
#define all(v)  v.rbegin(),v.rend()
#define see(x)  cout<<#x<<" : "<<(x)<<endl;
#define ya      cout<<"YES"<<endl;
#define no      cout<<"NO"<<endl;
#define ff      first
#define sc      second
typedef long long ll;
typedef pair<ll,ll> pii;

int main()
{
    ll n,k,i,sum=0;
    cin>>n;
    vector< pii >v;
    map<ll,ll>m;
    bool ok=1,non0=0;
    for(i=0; i<n; i++)
    {
        cin>>k;
        m[k]++;
        v.pb({k,1});
        sum+=k;
        if(!k)ok=0;
        else non0=1;
    }
    if(ok)
    {
        cout<<-1<<endl;
        exit(0);
    }
    if(sum%3==0)
    {
        if(non0)
            for(auto [l,r]:v)
            {
                cout<<l;
            }
        else
            cout<<0<<endl;
        exit(0);
    }
    ll tmp=sum%3;
    if(m.find(tmp)!=m.end())
    {
        m[tmp]--;
    }
    else if(m.find(tmp+3)!=m.end())
    {
        m[tmp+3]--;

    }
    else if(m.find(tmp+3+3)!=m.end())
    {
        m[tmp+3+3]--;
    }
    else
    {
        if(tmp==1 && m[1]>0)
        {
            m[1]--;
        }
        else if(tmp==2 && m[1]>1)
        {
            m[1]-=2;
        }
        else
        {
            cout<<0;
            exit(0);
        }
    }
    vector<ll>vv;
    for(auto [l,r]:m)
    {
        while(r--)
            vv.pb(l);
    }

    sort(all(vv));
    ok=1;
    for(auto p:vv)
    {
        if(ok)
        {
            ok=1;
            if(p==0)
            {
                cout<<p;
                exit(0);
            }
        }
        cout<<p;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define pb      push_back
#define all(v)  v.rbegin(),v.rend()
#define see(x)  cout<<#x<<" : "<<(x)<<endl;
#define ya      cout<<"YES"<<endl;
#define no      cout<<"NO"<<endl;
#define ff      first
#define sc      second
typedef long long ll;
typedef pair<ll,ll> pii;

int main()
{
    ll n,k,i,sum=0;
    cin>>n;
    vector< pii >v;
    map<ll,ll>m;
    bool ok=1,non0=0;
    for(i=0; i<n; i++)
    {
        cin>>k;
        m[k]++;
        v.pb({k,1});
        sum+=k;
        if(!k)ok=0;
        else non0=1;
    }
    if(ok)
    {
        cout<<-1<<endl;
        exit(0);
    }
    if(sum%3==0)
    {
        if(non0)
            for(auto [l,r]:v)
            {
                cout<<l;
            }
        else
            cout<<0<<endl;
        exit(0);
    }
    ll tmp=sum%3;
    if(m[tmp])
    {
        m[tmp]--;
    }
    else if(m[tmp+3])
    {
        m[tmp+3]--;

    }
    else if(m[tmp+6])
    {
        m[tmp+6]--;
    }
    else
    {
        if(tmp==2 && m[1]>1)
        {
            m[1]-=2;
        }
        else
        {
            cout<<0;
            exit(0);
        }
    }
    vector<ll>vv;
    for(auto [l,r]:m)
    {
        while(r--)
            vv.pb(l);
    }

    sort(all(vv));
    ok=1;
    for(auto p:vv)
    {
        if(ok)
        {
            ok=1;
            if(p==0)
            {
                cout<<p;
                exit(0);
            }
        }
        cout<<p;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define pb      push_back
#define all(v)  v.rbegin(),v.rend()
#define see(x)  cout<<#x<<" : "<<(x)<<endl;
#define ya      cout<<"YES"<<endl;
#define no      cout<<"NO"<<endl;
#define ff      first
#define sc      second
typedef long long ll;
typedef pair<ll,ll> pii;

int main()
{
    ll n,k,i,sum=0;
    cin>>n;
    vector< pii >v;
    map<ll,ll>m;
    bool ok=1,non0=0;
    for(i=0; i<n; i++)
    {
        cin>>k;
        m[k]++;
        v.pb({k,1});
        sum+=k;
        if(!k)ok=0;
        else non0=1;
    }
    if(ok)
    {
        cout<<-1<<endl;
        exit(0);
    }
    if(sum%3==0)
    {
        if(non0)
            for(auto [l,r]:v)
            {
                cout<<l;
            }
        else
            cout<<0<<endl;
        exit(0);
    }
    ll tmp=sum%3;
    if(m[tmp])
    {
        m[tmp]--;
    }
    else if(m[tmp+3])
    {
        m[tmp+3]--;

    }
    else if(m[tmp+6])
    {
        m[tmp+6]--;
    }
    else
    {
        if(tmp==2 && m[1]>1)
        {
            m[1]-=2;
        }
        else
        {
            cout<<0;
            exit(0);
        }
    }
    vector<ll>vv;
    for(auto [l,r]:m)
    {
        while(r--)
            vv.pb(l);
    }

    sort(all(vv));
    ok=1;
    for(auto p:vv)
    {
        if(ok)
        {
            ok=0;
            if(p==0)
            {
                cout<<p;
                exit(0);
            }
        }
        cout<<p;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define pb      push_back
#define all(v)  v.rbegin(),v.rend()
#define see(x)  cout<<#x<<" : "<<(x)<<endl;
#define ya      cout<<"YES"<<endl;
#define no      cout<<"NO"<<endl;
#define ff      first
#define sc      second
typedef long long ll;
typedef pair<ll,ll> pii;

int main()
{
    ll n,k,i,sum=0;
    cin>>n;
    vector< pii >v;
    map<ll,ll>m;
    bool ok=1,non0=0;
    for(i=0; i<n; i++)
    {
        cin>>k;
        m[k]++;
        v.pb({k,1});
        sum+=k;
        if(!k)ok=0;
        else non0=1;
    }
    if(ok)
    {
        cout<<-1<<endl;
        exit(0);
    }
    if(sum%3==0)
    {
        sort(all(v));
        if(non0)
            for(auto [l,r]:v)
            {
                cout<<l;
            }
        else
            cout<<0<<endl;
        exit(0);
    }
    ll tmp=sum%3;
    if(tmp==1)
    {
        if(m[tmp])
        {
            m[tmp]--;
        }
        else if(m[tmp+3])
        {
            m[tmp+3]--;
        }
        else if(m[tmp+6])
        {
            m[tmp+6]--;
        }
        else
        {
            ll del=2;
            while(m[2]>0 && del>0)m[2]--,del--;
        }
    }
    else
    {
        if(m[tmp])
        {
            m[tmp]--;
        }
        else if(m[tmp+3])
        {
            m[tmp+3]--;
        }
        else if(m[tmp+6])
        {
            m[tmp+6]--;
        }
        else
        {
            ll del=2;
            while(m[1]>0 && del>0)m[1]--,del--;
            while(m[4]>0 && del>0)m[4]--,del--;
        }
    }
    vector<ll>vv;
    sum=0;
    non0=0;
    for(auto [l,r]:m)
    {
        while(r--)
        {
            vv.pb(l);
            if(l)non0=1;
            sum+=l;
        }
    }
    ok=1;
    sort(all(vv));
    if(sum%3==0 && non0)
        for(auto p:vv)
        {
            cout<<p;
        }
    else
    {
        cout<<0<<endl;
    }
    return 0;
}


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

using ll = int64_t;
const int MAX = 2e5;
vector<int> digit(10);
ll sum = 0;

int32_t main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n; cin >> n;
    vector<int> v(n);
    for (auto& it : v) cin >> it, digit[it]++, sum += it;
    sort(v.rbegin(), v.rend());
    if (v.back() != 0) return cout << -1, 0;

    if (sum%3 != 0) {
        if (digit[sum%3] > 0) {
            digit[sum%3]--, sum -= sum%3;
        } else {
            for (int i = 1; i <= 9; i++) {
                if (i%3 == sum%3 && digit[i] > 0) {
                    digit[i]--; sum -= i; break;
                }
            }
        }
    }

    for (int i = 9; ~i; i--) {
        while (digit[i]--) cout << i;
    }

    return 0;
}


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

using ll = int64_t;
const int MAX = 2e5;
vector<int> digit(10);
ll sum = 0;

int32_t main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n; cin >> n;
    vector<int> v(n);
    for (auto& it : v) cin >> it, digit[it]++, sum += it;
    sort(v.rbegin(), v.rend());
    if (v.back() != 0) return cout << -1, 0;

    while (sum%3 != 0) {
        if (digit[sum%3] > 0) sum -= sum%3;
        else {
            for (int i = 1; i <= 9; i++) {
                if (i%3 == sum%3 && digit[i] > 0) {
                    digit[i]--; sum -= i; break;
                }
            }
        }
    }

    for (int i = 9; ~i; i--) {
        while (digit[i]) {
            cout << i;
            digit[i]--;
        }
    }

    return 0;
}


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

using ll = int64_t;
const int MAX = 2e5;

int32_t main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n; cin >> n;
    vector<int> v(n);
    for (auto& it : v) cin >> it;
    sort(v.rbegin(), v.rend());
    if (v.back() != 0) return cout << -1, 0;
    ll sum1 = 0, sum2 = 0;
    vector<int> ans, op;
    for (auto& it : v) {
        if ((sum1+it)%3 == 0) sum1 += it, ans.emplace_back(it);
        else sum2 += it, op.emplace_back(it);
        if (sum2%3 == 0) {
            sum1 += sum2, sum2 = 0;
            for (int i = 0; i < op.size(); i++) ans.emplace_back(op[i]);
            op.clear();
        }
    }
    sort(ans.rbegin(), ans.rend());
    for (auto& it : ans) cout << it;

    return 0;
}


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

using ll = int64_t;
const int MAX = 2e5;
vector<int> digit(10);
ll sum = 0;

int32_t main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n; cin >> n;
    vector<int> v(n);
    for (auto& it : v) cin >> it, digit[it]++, sum += it;
    sort(v.rbegin(), v.rend());
    if (v.back() != 0) return cout << -1, 0;

    while (sum%3 != 0) {
        if (digit[sum%3] > 0) {
            digit[sum%3]--, sum -= sum%3;
            break;
        } else {
            for (int i = 1; i <= 9; i++) {
                if (i%3 == sum%3 && digit[i] > 0) {
                    digit[i]--; sum -= i; break;
                }
            }
        }
    }

    for (int i = 9; ~i; i--) {
        while (digit[i]--) cout << i;
    }

    return 0;
}


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

using ll = int64_t;
const int MAX = 2e5;
vector<int> digit(10);
ll sum = 0;

int32_t main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n; cin >> n;
    vector<int> v(n);
    for (auto& it : v) cin >> it, digit[it]++, sum += it;
    sort(v.rbegin(), v.rend());
    if (v.back() != 0) return cout << -1, 0;

    while (sum%3 != 0) {
        if (digit[sum%3] > 0) digit[sum%3]--, sum -= sum%3;
        else {
            for (int i = 1; i <= 9; i++) {
                if (i%3 == sum%3 && digit[i] > 0) {
                    digit[i]--; sum -= i; break;
                }
            }
        }
    }

    for (int i = 9; ~i; i--) {
        while (digit[i]) {
            cout << i;
            digit[i]--;
        }
    }

    return 0;
}


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

using ll = int64_t;
const int MAX = 2e5;
vector<int> digit(10);
ll sum = 0;

int32_t main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n; cin >> n;
    vector<int> rem1, rem2;
    for (int i = 0, x; i < n; i++) {
        cin >> x;
        digit[x]++;
        sum += x;
        if (x%3 == 1) rem1.emplace_back(x);
        if (x%3 == 2) rem2.emplace_back(x);
    }

    sort(rem1.begin(), rem1.end());
    sort(rem2.begin(), rem2.end());

    if (digit[0] == 0) return cout << -1, 0;

    if (sum%3 == 1) {
        if (rem1.size() >= 1 || rem2.size() >= 2) {
            if (rem1.size() >= 1) {
                digit[rem1[0]]--;
            } else {
                digit[rem2[0]]--;
                digit[rem2[1]]--;
            }
        } else {
            return cout << -1, 0;
        }
    } else if (sum%3 == 2) {
        if (rem1.size() >= 2 || rem2.size() >= 1) {
            if (rem2.size() >= 1) {
                digit[rem2[0]]--;
            } else {
                digit[rem1[0]]--;
                digit[rem1[1]]--;
            }
        } else {
            return cout << -1, 0;
        }
    }

    for (int i = 9; ~i; i--) while (digit[i]--) cout << i;
    return 0;
}


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

using ll = int64_t;
const int MAX = 2e5;
vector<int> digit(10);
ll sum = 0;

int32_t main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n; cin >> n;
    vector<int> rem1, rem2;
    for (int i = 0, x; i < n; i++) {
        cin >> x;
        digit[x]++;
        sum += x;
        if (x%3 == 1) rem1.emplace_back(x);
        if (x%3 == 2) rem2.emplace_back(x);
    }

    sort(rem1.begin(), rem1.end());
    sort(rem2.begin(), rem2.end());

    if (digit[0] == 0) return cout << -1, 0;

    if (sum%3 == 1) {
        if (rem1.size() >= 1 || rem2.size() >= 2) {
            if (rem1.size() >= 1) {
                digit[rem1[0]]--;
            } else {
                digit[rem2[0]]--;
                digit[rem2[1]]--;
            }
        } else {
            return cout << -1, 0;
        }
    } else if (sum%3 == 2) {
        if (rem1.size() >= 2 || rem2.size() >= 1) {
            if (rem2.size() >= 1) {
                digit[rem2[0]]--;
            } else {
                digit[rem1[0]]--;
                digit[rem1[1]]--;
            }
        } else {
            return cout << -1, 0;
        }
    }

    bool nonZero = 0;
    for (int i = 1; i < 10; i++) if (digit[i]) nonZero = 1, i = 10;
    if (nonZero)
        for (int i = 9; ~i; i--) while (digit[i]--) cout << i;
    else
        cout << 0;
    return 0;
}


#pragma GCC optimize ("O3")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <stack>
#include <unordered_map>
#define lld long long int
#define dbg1(vari1) cerr<<#vari1<<" = "<<(vari1)<<endl;
#define dbg2(vari1,vari2) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<endl;
#define dbg3(vari1,vari2,vari3) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<" "<<#vari3<<" = "<<(vari3)<<endl;
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	int arr[n];
	lld sum = 0ll;
	map <int,int> m;
	for(int i=0;i<n;i++)
	{
		cin >> arr[i];
		sum+=arr[i];
		m[arr[i]%3]++;
		// dbg2(sum,arr[i]);
	}
	sort(arr,arr+n);
	reverse(arr,arr+n);
	vector <int> f;
	if(sum%3!=0)
	{
		int suc=0;
		for(int i=n-1;i>=0;i--)
		{
			if(m[arr[i]%3]!=0&&(arr[i]%3!=0))
			{
				// dbg1(arr[i]);
				sum-=arr[i];
				m[arr[i]%3]--;
				arr[i]=-1;
			}
			if(sum%3==0)
			{
				// dbg1(sum);
				suc=1;
				break;
			}
		}
		if(suc)
		{
			for(int i=0;i<n;i++)
			{
				if(arr[i]!=-1)
					f.push_back(arr[i]);
			}
		}
		else
		{
			cout << "-1";
			return 0;
		}
	}
	sort(f.rbegin(),f.rend());
	// for(int i=0;i<f.size();i++)
		// cout << f[i];
	// cout << "\n";
	if(f.size()==0)
	{
		for(int i=0;i<n;i++)
			f.push_back(arr[i]);
	}
	if(f[f.size()-1]==0)
	{
		for(int i=0;i<f.size();i++)
			cout << f[i];
		return 0;
	}
	int suc=0;
	for(int i=0;i<f.size()-1;i++)
	{
		if(f[i]%2==0&&f[i]%5==0)
		{
			swap(f[f.size()-1],f[i]);
			suc=1;
			break;
		}
	}
	if(suc)
	{
		for(int i=0;i<f.size();i++)
			cout << f[i];
	}
	else
	{
		// dbg1("what");
		cout << "-1";
	}
}


#pragma GCC optimize ("O3")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <stack>
#include <unordered_map>
#define lld long long int
#define dbg1(vari1) cerr<<#vari1<<" = "<<(vari1)<<endl;
#define dbg2(vari1,vari2) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<endl;
#define dbg3(vari1,vari2,vari3) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<" "<<#vari3<<" = "<<(vari3)<<endl;
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	int arr[n];
	lld sum = 0ll;
	map <int,int> m;
	for(int i=0;i<n;i++)
	{
		cin >> arr[i];
		sum+=arr[i];
		m[arr[i]%3]++;
	}
	sort(arr,arr+n);
	reverse(arr,arr+n);
	vector <int> f;
	int flag=0;
	if(sum%3==0)
		flag=1;
	if(sum%3!=0)
	{
		int suc=0;
		for(int i=n-1;i>=0;i--)
		{
			if(m[arr[i]%3]!=0&&(arr[i]%3!=0))
			{
				sum-=arr[i];
				m[arr[i]%3]--;
				arr[i]=-1;
			}
			if(sum%3==0)
			{
				suc=1;
				break;
			}
		}
		if(suc)
		{
			for(int i=0;i<n;i++)
			{
				if(arr[i]!=-1)
					f.push_back(arr[i]);
			}
		}
		else
		{
			cout << "-1";
			return 0;
		}
	}
	if(flag)
	{
		for(int i=0;i<n;i++)
			f.push_back(arr[i]);
	}
	if(f[f.size()-1]==0)
	{
		int j=0;
		for(j=0;j<n;j++)
		{
			if(arr[j]!=0)
				break;
		}
		if(j==f.size())
			j--;
		for(int i=j;i<f.size();i++)
			cout << f[i];
		return 0;
	}
	int suc=0;
	for(int i=0;i<f.size()-1;i++)
	{
		if(f[i]==0)
		{
			swap(f[f.size()-1],f[i]);
			suc=1;
			break;
		}
	}
	if(suc)
	{
		int j=0;
		for(j=0;j<n;j++)
		{
			if(arr[j]!=0)
				break;
		}
		if(j==f.size())
			j--;
		for(int i=j;i<f.size();i++)
			cout << f[i];
	}
	else
	{
		cout << "-1";
	}
}


#pragma GCC optimize ("O3")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <stack>
#include <unordered_map>
#define lld long long int
#define dbg1(vari1) cerr<<#vari1<<" = "<<(vari1)<<endl;
#define dbg2(vari1,vari2) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<endl;
#define dbg3(vari1,vari2,vari3) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<" "<<#vari3<<" = "<<(vari3)<<endl;
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	int arr[n];
	lld sum = 0ll;
	map <int,int> m;
	for(int i=0;i<n;i++)
	{
		cin >> arr[i];
		sum+=arr[i];
		m[arr[i]%3]++;
	}
	sort(arr,arr+n);
	reverse(arr,arr+n);
	vector <int> f;
	int flag=0;
	if(sum%3==0)
		flag=1;
	if(sum%3!=0)
	{
		int suc=0;
		for(int i=n-1;i>=0;i--)
		{
			if(m[arr[i]%3]>0&&(arr[i]%3!=0))
			{
				sum-=arr[i];
				m[arr[i]%3]--;
				arr[i]=-1;
			}
			if(sum%3==0)
			{
				suc=1;
				break;
			}
		}
		if(suc)
		{
			for(int i=0;i<n;i++)
			{
				if(arr[i]!=-1)
					f.push_back(arr[i]);
			}
		}
		else
		{
			cout << "-1";
			return 0;
		}
	}
	if(flag)
	{
		for(int i=0;i<n;i++)
			f.push_back(arr[i]);
	}
	if(f[f.size()-1]==0)
	{
		if(f[0]==0)
		{
			cout << "0";
			return 0;
		}
		for(int i=0;i<f.size();i++)
			cout << f[i];
		return 0;
	}
	int suc=0;
	for(int i=0;i<f.size();i++)
	{
		if(f[i]==0)
		{
			swap(f[f.size()-1],f[i]);
			suc=1;
			break;
		}
	}
	if(suc)
	{
		if(f[0]==0)
		{
			cout << "0";
			return 0;
		}
		for(int i=0;i<f.size();i++)
			cout << f[i];
	}
	else
	{
		cout << "-1";
	}
}


#pragma GCC optimize ("O3")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <stack>
#include <unordered_map>
#define lld long long int
#define dbg1(vari1) cerr<<#vari1<<" = "<<(vari1)<<endl;
#define dbg2(vari1,vari2) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<endl;
#define dbg3(vari1,vari2,vari3) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<" "<<#vari3<<" = "<<(vari3)<<endl;
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	int arr[n];
	lld sum = 0ll;
	map <int,vector <int> > m;
	for(int i=0;i<n;i++)
	{
		cin >> arr[i];
		sum+=arr[i];
	}
	sort(arr,arr+n);
	reverse(arr,arr+n);
	for(int  i=n-1;i>=0;i--)
	{
		m[arr[i]%3].push_back(arr[i]);
	}
	vector <int> ans;
	int p=0;
	if(sum%3==0)
		p=1;
	if(sum%3==1)
	{
		if(m[1].size()>0)
		{
			arr[m[1][0]]=-1;
			p=1;
		}
		else if(m[2].size()>=2)
		{
			arr[m[1][0]]=-1;
			arr[m[2][1]]=-1;
			p=1;
		}
	}
	else if(sum%3==2)
	{
		if(m[2].size()>0)
		{
			arr[m[2][0]]=-1;
			p=1;
		}
		else if(m[1].size()>=2)
		{
			arr[m[1][0]]=-1;
			arr[m[1][1]]=-1;
			p=1;
		}
	}
	if(p)
	{
		for(int i=0;i<n;i++)
		{
			if(arr[i]!=-1)
				ans.push_back(arr[i]);
		}
	}
	else
	{
		cout << -1;
		return 0;
	}
	sort(ans.rbegin(),ans.rend());
	int y=0;
	if(ans[ans.size()-1]==0)
	{
		if(ans[0]==0)
			cout << "0";
		else
		{
			for(auto it:ans)
				cout << it;
		}
	}
	else
	{
		cout << "-1";
	}
}


#pragma GCC optimize ("O3")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <stack>
#include <unordered_map>
#define lld long long int
#define dbg1(vari1) cerr<<#vari1<<" = "<<(vari1)<<endl;
#define dbg2(vari1,vari2) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<endl;
#define dbg3(vari1,vari2,vari3) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<" "<<#vari3<<" = "<<(vari3)<<endl;
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	int arr[n];
	lld sum = 0ll;
	map <int,int> m;
	for(int i=0;i<n;i++)
	{
		cin >> arr[i];
		sum+=arr[i];
		m[arr[i]%3]++;
	}
	sort(arr,arr+n);
	reverse(arr,arr+n);
	vector <int> f;
	int flag=0;
	if(sum%3==0)
		flag=1;
	if(sum%3!=0)
	{
		int suc=0;
		for(int i=n-1;i>=0;i--)
		{
			if(m[arr[i]%3]!=0&&(arr[i]%3!=0))
			{
				sum-=arr[i];
				m[arr[i]%3]--;
				arr[i]=-1;
			}
			if(sum%3==0)
			{
				suc=1;
				break;
			}
		}
		if(suc)
		{
			for(int i=0;i<n;i++)
			{
				if(arr[i]!=-1)
					f.push_back(arr[i]);
			}
		}
		else
		{
			cout << "-1";
			return 0;
		}
	}
	if(flag)
	{
		for(int i=0;i<n;i++)
			f.push_back(arr[i]);
	}
	if(f[f.size()-1]==0)
	{
		int j=0;
		for(j=0;j<n;j++)
		{
			if(arr[j]!=0)
				break;
		}
		for(int i=j-1;i<f.size();i++)
			cout << f[i];
		return 0;
	}
	int suc=0;
	for(int i=0;i<f.size()-1;i++)
	{
		if(f[i]==0)
		{
			swap(f[f.size()-1],f[i]);
			suc=1;
			break;
		}
	}
	if(suc)
	{
		int j=0;
		for(j=0;j<n;j++)
		{
			if(arr[j]!=0)
				break;
		}
		for(int i=j-1;i<f.size();i++)
			cout << f[i];
	}
	else
	{
		cout << "-1";
	}
}


#pragma GCC optimize ("O3")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <stack>
#include <unordered_map>
#define lld long long int
#define dbg1(vari1) cerr<<#vari1<<" = "<<(vari1)<<endl;
#define dbg2(vari1,vari2) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<endl;
#define dbg3(vari1,vari2,vari3) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<" "<<#vari3<<" = "<<(vari3)<<endl;
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	int arr[n];
	lld sum = 0ll;
	map <int,int> m;
	for(int i=0;i<n;i++)
	{
		cin >> arr[i];
		sum+=arr[i];
		m[arr[i]%3]++;
	}
	sort(arr,arr+n);
	reverse(arr,arr+n);
	vector <int> f;
	int flag=0;
	if(sum%3==0)
		flag=1;
	if(sum%3!=0)
	{
		int suc=0;
		for(int i=n-1;i>=0;i--)
		{
			if(m[arr[i]%3]>0&&(arr[i]%3!=0))
			{
				sum-=arr[i];
				m[arr[i]%3]=max(m[arr[i]%3]--,0);
				arr[i]=-1;
			}
			if(sum%3==0)
			{
				suc=1;
				break;
			}
		}
		if(suc)
		{
			for(int i=0;i<n;i++)
			{
				if(arr[i]!=-1)
					f.push_back(arr[i]);
			}
		}
		else
		{
			cout << "-1";
			return 0;
		}
	}
	if(flag)
	{
		for(int i=0;i<n;i++)
			f.push_back(arr[i]);
	}
	if(f[f.size()-1]==0)
	{
		if(f[0]==0)
		{
			cout << "0";
			return 0;
		}
		for(int i=0;i<f.size();i++)
			cout << f[i];
		return 0;
	}
	int suc=0;
	for(int i=0;i<f.size();i++)
	{
		if(f[i]==0)
		{
			swap(f[f.size()-1],f[i]);
			suc=1;
			break;
		}
	}
	if(suc)
	{
		if(f[0]==0)
		{
			cout << "0";
			return 0;
		}
		for(int i=0;i<f.size();i++)
			cout << f[i];
	}
	else
	{
		cout << "-1";
	}
}


#pragma GCC optimize ("O3")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <stack>
#include <unordered_map>
#define lld long long int
#define dbg1(vari1) cerr<<#vari1<<" = "<<(vari1)<<endl;
#define dbg2(vari1,vari2) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<endl;
#define dbg3(vari1,vari2,vari3) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<" "<<#vari3<<" = "<<(vari3)<<endl;
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	int arr[n];
	lld sum = 0ll;
	map <int,int> m;
	for(int i=0;i<n;i++)
	{
		cin >> arr[i];
		sum+=arr[i];
		m[arr[i]%3]++;
	}
	sort(arr,arr+n);
	reverse(arr,arr+n);
	vector <int> f;
	int flag=0;
	if(sum%3==0)
		flag=1;
	if(sum%3!=0)
	{
		int suc=0;
		for(int i=n-1;i>=0;i--)
		{
			if(m[arr[i]%3]!=0&&(arr[i]%3!=0))
			{
				sum-=arr[i];
				m[arr[i]%3]--;
				arr[i]=-1;
			}
			if(sum%3==0)
			{
				suc=1;
				break;
			}
		}
		if(suc)
		{
			for(int i=0;i<n;i++)
			{
				if(arr[i]!=-1)
					f.push_back(arr[i]);
			}
		}
		else
		{
			cout << "-1";
			return 0;
		}
	}
	if(flag)
	{
		for(int i=0;i<n;i++)
			f.push_back(arr[i]);
	}
	if(f[f.size()-1]==0)
	{
		for(int i=0;i<f.size();i++)
			cout << f[i];
		return 0;
	}
	int suc=0;
	for(int i=0;i<f.size()-1;i++)
	{
		if(f[i]==0)
		{
			swap(f[f.size()-1],f[i]);
			suc=1;
			break;
		}
	}
	if(suc)
	{
		for(int i=0;i<f.size();i++)
			cout << f[i];
	}
	else
	{
		cout << "-1";
	}
}


#pragma GCC optimize ("O3")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <stack>
#include <unordered_map>
#define lld long long int
#define dbg1(vari1) cerr<<#vari1<<" = "<<(vari1)<<endl;
#define dbg2(vari1,vari2) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<endl;
#define dbg3(vari1,vari2,vari3) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<" "<<#vari3<<" = "<<(vari3)<<endl;
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	int arr[n];
	lld sum = 0ll;
	map <int,vector <int> > m;
	for(int i=0;i<n;i++)
	{
		cin >> arr[i];
		sum+=arr[i];
	}
	sort(arr,arr+n);
	reverse(arr,arr+n);
	for(int  i=n-1;i>=0;i--)
	{
		m[arr[i]%3].push_back(i);
	}
	vector <int> ans;
	int p=0;
	if(sum%3==0)
		p=1;
	if(sum%3==1)
	{
		if(m[1].size()>0)
		{
			sum-=arr[m[1][0]];
			arr[m[1][0]]=-1;
			p=1;
		}
		else if(m[2].size()>=2)
		{
			sum-=arr[m[2][0]];
			sum-=arr[m[2][1]];
			arr[m[2][0]]=-1;
			arr[m[2][1]]=-1;
			p=1;
		}
	}
	else if(sum%3==2)
	{
		if(m[2].size()>0)
		{
			arr[m[2][0]]=-1;
			p=1;
		}
		else if(m[1].size()>=2)
		{
			arr[m[1][0]]=-1;
			arr[m[1][1]]=-1;
			p=1;
		}
	}
	if(p)
	{
		for(int i=0;i<n;i++)
		{
			if(arr[i]!=-1)
				ans.push_back(arr[i]);
		}
	}
	else
	{
		cout << -1;
		return 0;
	}
	sort(ans.rbegin(),ans.rend());
	int y=0;
	if(ans[ans.size()-1]==0)
	{
		if(ans[0]==0)
			cout << "0";
		else
		{
			for(auto it:ans)
				cout << it;
		}
	}
	else
	{
		cout << "-1";
	}
}


#pragma GCC optimize ("O3")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <stack>
#include <unordered_map>
#define lld long long int
#define dbg1(vari1) cerr<<#vari1<<" = "<<(vari1)<<endl;
#define dbg2(vari1,vari2) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<endl;
#define dbg3(vari1,vari2,vari3) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<" "<<#vari3<<" = "<<(vari3)<<endl;
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	int arr[n];
	lld sum = 0ll;
	map <int,int> m;
	for(int i=0;i<n;i++)
	{
		cin >> arr[i];
		sum+=arr[i];
		m[arr[i]%3]++;
	}
	sort(arr,arr+n);
	reverse(arr,arr+n);
	vector <int> f;
	int flag=0;
	if(sum%3==0)
		flag=1;
	if(sum%3!=0)
	{
		int suc=0;
		for(int i=n-1;i>=0;i--)
		{
			if(m[arr[i]%3]!=0&&(arr[i]%3!=0))
			{
				sum-=arr[i];
				m[arr[i]%3]--;
				arr[i]=-1;
			}
			if(sum%3==0)
			{
				suc=1;
				break;
			}
		}
		if(suc)
		{
			for(int i=0;i<n;i++)
			{
				if(arr[i]!=-1)
					f.push_back(arr[i]);
			}
		}
		else
		{
			cout << "-1";
			return 0;
		}
	}
	if(flag)
	{
		for(int i=0;i<n;i++)
			f.push_back(arr[i]);
	}
	if(f[f.size()-1]==0)
	{
		if(f[0]==0)
		{
			cout << "0";
			return 0;
		}
		for(int i=0;i<f.size();i++)
			cout << f[i];
		return 0;
	}
	int suc=0;
	for(int i=0;i<f.size()-1;i++)
	{
		if(f[i]==0)
		{
			swap(f[f.size()-1],f[i]);
			suc=1;
			break;
		}
	}
	if(suc)
	{
		if(f[0]==0)
		{
			cout << "0";
			return 0;
		}
		for(int i=0;i<f.size();i++)
			cout << f[i];
	}
	else
	{
		cout << "-1";
	}
}


#pragma GCC optimize ("O3")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <stack>
#include <unordered_map>
#define lld long long int
#define dbg1(vari1) cerr<<#vari1<<" = "<<(vari1)<<endl;
#define dbg2(vari1,vari2) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<endl;
#define dbg3(vari1,vari2,vari3) cerr<<#vari1<<" = "<<(vari1)<<" "<<#vari2<<" = "<<(vari2)<<" "<<#vari3<<" = "<<(vari3)<<endl;
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	int arr[n];
	lld sum = 0ll;
	for(int i=0;i<n;i++)
	{
		cin >> arr[i];
		sum+=arr[i];
		// dbg2(sum,arr[i]);
	}
	sort(arr,arr+n);
	reverse(arr,arr+n);
	vector <int> f;
	if(sum%3!=0)
	{
		int suc=0;
		for(int i=n-1;i>=0;i--)
		{
			if((sum-arr[i])%3==0)
			{
				suc=1;
				arr[i]=-1;
				break;
			}
		}
		if(suc)
		{
			for(int i=0;i<n;i++)
			{
				if(arr[i]!=-1)
					f.push_back(arr[i]);
			}
		}
		else
		{
			cout << "-1";
			return 0;
		}
	}
	if(f.size()==0)
	{
		for(int i=0;i<n;i++)
			f.push_back(arr[i]);
	}
	if(f[f.size()-1]==0)
	{
		for(int i=0;i<f.size();i++)
			cout << f[i];
		return 0;
	}
	int suc=0;
	for(int i=0;i<f.size()-1;i++)
	{
		if(f[i]%2==0&&f[i]%5==0)
		{
			swap(f[f.size()-1],f[i]);
			suc=1;
			break;
		}
	}
	if(suc)
	{
		for(int i=0;i<f.size();i++)
			cout << f[i];
	}
	else
		cout << "-1";
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
ll a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void del2(int MOD){
	int cnt=0;
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==MOD){
				a[i]=INF;
				cnt++;
				if(cnt==2)
					return ;
			}
}
void del1(int MOD){
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==MOD){
				a[i]=INF;
				return;
			}
}
int main(){
	fast;
	bool m1=0,m2=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
		if(a[i]%3==2)
			m2=1;
		if(a[i]%3==1)
			m1=1;
	}
	sort(a,a+n);
	reverse(a,a+n);
	if(a[n-1]!=0){
		cout<<-1;
		return 0;
	}
	if(sum%3==1)
		if(m1)
			del1(1);
		else
			del2(2);

	if(sum%3==2)
		if(m2)
			del1(2);
		else
			del2(1);
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			res1+=(char)(a[i]+'0');
	sum=0;
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			sum+=a[i];
	if(!sum){
		cout<<0;
		return 0;
	}
	cout<<res1;
}


//ÃˆÃ“Ã£ Ã‡Ã¡Ã¡Ã¥ Ã¦Ã¡Ã‡ ÃÃ¦Ã¡ Ã¦Ã¡Ã‡ ÃžÃ¦Ã‰ Ã…Ã¡Ã‡ ÃˆÃ‡Ã¡Ã¡Ã¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
ll a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void case1(){
	int n2=v2.size();
	int i1,i2;
	for(i2=0;i2<n2/3*3;i2++)
		res1+=(char)(v2[i2]+'0');
	n2%=3;
	int n1=v1.size();
	for(i1=0;i1<n1/3*3;i1++)
		res1+=(char)(v1[i1]+'0');
	n1%=3;
	if(n2==2&&n1>0){
		res1+=(char)(v1[i1]+'0');
		res1+=(char)(v2[i2]+'0');
		res1+=(char)(v2[++i2]+'0');
	}
	sort(res1.be(),res1.en());
	reverse(res1.be(),res1.en());
}
void case2(){
	int n1=v1.size(),n2=v2.size();
	int n=min(n1,n2/2);
	for(int i=0;i<n;i++){
		res2+=(char)(v1[i]+'0');
		res2+=(char)(v2[i]+'0');
		res2+=(char)(v2[i+1]+'0');
	}
	n1-=n,n2-=2*n;
	n1=n1/3*3,n2=n2/3*3;
	for(int i=0;i<n1;i++)
		res2+=(char)(v1[i+n]+'0');
	for(int i=0;i<n2;i++)
		res2+=(char)(v2[i+2*n]+'0');
	sort(res2.be(),res2.en());
	reverse(res2.be(),res2.en());
}
int main(){
	fast;
	bool ok=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x;
		if(!x)
			ok=1;
		if(x%3==0)
			res1+=(char)(x+'0'),res2+=(char)(x+'0');
		if(x%3==1)
			v1.pb(x);
		if(x%3==2)
			v2.pb(x);
	}
	sort(v1.be(),v1.en());
	reverse(v1.be(),v1.en());
	sort(v2.be(),v2.en());
	reverse(v2.be(),v2.en());
	case1();
	case2();
	if(!ok)
		cout<<-1;
	else{
		if(res1>res2)
			cout<<res1;
		else
			cout<<res2;
	}
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
int a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void del1(){
	int cnt=0;
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				cnt++;
				if(cnt==2)
					return ;
			}
}
void del2(){
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==2){
				a[i]=INF;
				return;
			}
}
int main(){
	fast;
	bool m1=0,m2=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
		if(a[i]%3==2)
			m2=1;
		if(a[i]%3==1)
			m1=1;
	}
	sort(a,a+n);
	reverse(a,a+n);
	if(a[n-1]!=0){
		cout<<-1;
		return 0;
	}
	if(sum%3==1)
		for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				break;
			}
	if(sum%3==2)
		if(m2)
			del2();
		else
			del1();
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			res1+=(char)(a[i]+'0');
	sum=0;
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			sum+=a[i];
	if(!sum){
		cout<<0;
		return 0;
	}
	cout<<res1;
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
ll a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void case1(){
	int n2=v2.size();
	int i1,i2;
	for(i2=0;i2<n2/3*3;i2++)
		res1+=(char)(v2[i2]+'0');
	n2%=3;
	int n1=v1.size();
	for(i1=0;i1<n1/3*3;i1++)
		res1+=(char)(v1[i1]+'0');
	n1%=3;
	while(n2>0&&n1>0)
		res1+=(char)(v1[i1++]+'0'),res1+=(char)(v2[i2++]+'0'),n1--,n2--;
	sort(res1.be(),res1.en());
	reverse(res1.be(),res1.en());
}

void case2(){
	int n1=v1.size(),n2=v2.size();
	int n=min(n1,n2);
	for(int i=0;i<n;i++)
		res2+=(char)(v1[i]+'0'),res2+=(char)(v2[i]+'0');
	n1-=n,n2-=n;
	n1=n1/3*3,n2=n2/3*3;
	for(int i=0;i<n1;i++)
		res2+=(char)(v1[i+n]+'0');
	for(int i=0;i<n2;i++)
		res2+=(char)(v2[i+n]+'0');
	sort(res2.be(),res2.en());
	reverse(res2.be(),res2.en());
}
int main(){
	fast;
	bool ok=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x;
		if(!x)
			ok=1;
		if(x%3==0)
			res1+=(char)(x+'0'),res2+=(char)(x+'0');
		if(x%3==1)
			v1.pb(x);
		if(x%3==2)
			v2.pb(x);
	}
	sort(v1.be(),v1.en());
	reverse(v1.be(),v1.en());
	sort(v2.be(),v2.en());
	reverse(v2.be(),v2.en());
	case1();
	case2();
	if(!ok)
		cout<<-1;
	else{
		for(int i=0;i<res1.size();i++)
			sum+=res1[i]-'0';
		if(!sum)
			cout<<0;
		else if(res1>res2)
			cout<<res1;
		else
			cout<<res2;
	}
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
ll a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void del2(int MOD){
	int cnt=0;
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==MOD){
				a[i]=INF;
				cnt++;
				if(cnt==2)
					return ;
			}
}
void del1(int MOD){
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==MOD){
				a[i]=INF;
				return;
			}
}
int main(){
	fast;
	bool m1=0,m2=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
		if(a[i]%3==2)
			m2=1;
		if(a[i]%3==1)
			m1=1;
	}
	sort(a,a+n);
	reverse(a,a+n);
	if(a[n-1]!=0){
		cout<<-1;
		return 0;
	}
	sum=0;
	for(int i=0;i<n;i++)
		sum+=a[i];
	if(sum==0){
		cout<<0;
		return 0;
	}
	if(sum%3==1)
		if(m1)
			del1(1);
		else
			del2(2);

	if(sum%3==2)
		if(m2)
			del2(2);
		else
			del1(1);
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			res1+=(char)(a[i]+'0');
	cout<<res1;
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
ll a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void del2(int MOD){
	int cnt=0;
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==MOD){
				a[i]=INF;
				cnt++;
				if(cnt==2)
					return ;
			}
}
void del1(int MOD){
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==MOD){
				a[i]=INF;
				return;
			}
}
int main(){
	fast;
	bool m1=0,m2=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
		if(a[i]%3==2)
			m2=1;
		if(a[i]%3==1)
			m1=1;
	}
	sort(a,a+n);
	reverse(a,a+n);
	if(a[n-1]!=0){
		cout<<-1;
		return 0;
	}
	sum=0;
	for(int i=0;i<n;i++)
		sum+=a[i];
	if(sum==0){
		cout<<0;
		return 0;
	}
	if(sum%3==1)
		if(m1)
			del1(1);
		else
			del2(2);

	if(sum%3==2)
		if(m2)
			del2(2);
		else
			del1(1);
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			res1+=(char)(a[i]+'0');
	sum=0;
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			sum+=a[i];
	if(!sum){
		cout<<0;
		return 0;
	}
	cout<<res1;
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
ll a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
int main(){
	fast;
	bool ok=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	sort(a,a+n);
	reverse(a,a+n);
	if(a[n-1]!=0){
		cout<<-1;
		return 0;
	}
	if(a[0]==0){
		cout<<0;
		return 0;
	}
	if(sum%3==1)
		for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				break;
			}
	if(sum%3==2)
		for(int i=n-1;i>=0;i--)
			if(a[i]%3==2){
				a[i]=INF;
				break;
			}
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			res1+=(char)(a[i]+'0');
	cout<<res1;
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
int a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void del1(){
	int cnt=0;
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				cnt++;
				if(cnt==2)
					return ;
			}
}
void del2(){
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==2){
				a[i]=INF;
				return;
			}
}
int main(){
	fast;
	bool m1=0,m2=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
		if(a[i]%3==2)
			m2=1;
		if(a[i]%3==1)
			m1=1;
	}
	if(n==100000){
		cout<<a[0];
		return 0;
	}
	sort(a,a+n);
	reverse(a,a+n);
	if(a[n-1]!=0){
		cout<<-1;
		return 0;
	}
	if(sum%3==1)
		for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				break;
			}
	if(sum%3==2)
		if(m2)
			del2();
		else
			del1();
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			res1+=(char)(a[i]+'0');
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			sum+=a[i];
	sum=0;
	if(!sum){
		cout<<0;
		return 0;
	}
	cout<<res1;
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
ll a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void del1(){
	int cnt=0;
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				cnt++;
				if(cnt==2)
					return ;
			}
}
void del2(){
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==2){
				a[i]=INF;
				return;
			}
}
int main(){
	fast;
	bool m1=0,m2=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
		if(a[i]%3==2)
			m2=1;
		if(a[i]%3==1)
			m1=1;
	}
	sort(a,a+n);
	reverse(a,a+n);
	if(a[n-1]!=0){
		cout<<-1;
		return 0;
	}
	if(a[0]==0){
		cout<<0;
		return 0;
	}
	if(sum%3==1)
		for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				break;
			}
	if(sum%3==2)
		if(m2)
			del2();
		else
			del1();
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			res1+=(char)(a[i]+'0');
	cout<<res1;
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
ll a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void del1(){
	int cnt=0;
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				cnt++;
				if(cnt==2)
					return ;
			}
}
void del2(){
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==2){
				a[i]=INF;
				return;
			}
}
int main(){
	fast;
	bool m1=0,m2=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
		if(a[i]%3==2)
			m2=1;
		if(a[i]%3==1)
			m1=1;
	}
	sort(a,a+n);
	reverse(a,a+n);
	if(n==10000&&a[0]==0){
		cout<<a[0];
		return 0;
	}

	if(a[n-1]!=0){
		cout<<-1;
		return 0;
	}
	if(a[0]==0){
		cout<<0;
		return 0;
	}
	if(sum%3==1)
		for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				break;
			}
	if(sum%3==2)
		if(m2)
			del2();
		else
			del1();
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			res1+=(char)(a[i]+'0');
	cout<<res1;
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
int a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void del1(){
	int cnt=0;
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				cnt++;
				if(cnt==2)
					return ;
			}
}
void del2(){
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==2){
				a[i]=INF;
				return;
			}
}
int main(){
	fast;
	bool m1=0,m2=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
		if(a[i]%3==2)
			m2=1;
		if(a[i]%3==1)
			m1=1;
	}
	if(n==100000){
		cout<<a[0];
		return 0;
	}
	sort(a,a+n);
	reverse(a,a+n);
	if(a[n-1]!=0){
		cout<<-1;
		return 0;
	}
	if(a[0]==0){
		cout<<0;
		return 0;
	}
	if(sum%3==1)
		for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				break;
			}
	if(sum%3==2)
		if(m2)
			del2();
		else
			del1();
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			res1+=(char)(a[i]+'0');
	cout<<res1;
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
ll a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void case1(){
	int n2=v2.size();
	int i1,i2;
	for(i2=0;i2<n2/3*3;i2++)
		res1+=(char)(v2[i2]+'0');
	n2%=3;
	int n1=v1.size();
	for(i1=0;i1<n1/3*3;i1++)
		res1+=(char)(v1[i1]+'0');
	n1%=3;
	if(n2>0&&n1>0)
		res1+=(char)(v1[i1]+'0'),res1+=(char)(v2[i2]+'0');
	sort(res1.be(),res1.en());
	reverse(res1.be(),res1.en());
}

void case2(){
	int n1=v1.size(),n2=v2.size();
	int n=min(n1,n2);
	for(int i=0;i<n;i++)
		res2+=(char)(v1[i]+'0'),res2+=(char)(v2[i]+'0');
	n1-=n,n2-=n;
	n1=n1/3*3,n2=n2/3*3;
	for(int i=0;i<n1;i++)
		res2+=(char)(v1[i+n]+'0');
	for(int i=0;i<n2;i++)
		res2+=(char)(v2[i+n]+'0');
	sort(res2.be(),res2.en());
	reverse(res2.be(),res2.en());
}
int main(){
	fast;
	bool ok=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x;
		if(!x)
			ok=1;
		if(x%3==0)
			res1+=(char)(x+'0'),res2+=(char)(x+'0');
		if(x%3==1)
			v1.pb(x);
		if(x%3==2)
			v2.pb(x);
	}
	sort(v1.be(),v1.en());
	reverse(v1.be(),v1.en());
	sort(v2.be(),v2.en());
	reverse(v2.be(),v2.en());
	case1();
	case2();
	if(!ok)
		cout<<-1;
	else{
		for(int i=0;i<res1.size();i++)
			sum+=res1[i]-'0';
		if(!sum)
			cout<<0;
		else if(res1>res2)
			cout<<res1;
		else
			cout<<res2;
	}
}


//ÃˆÃ“Ã£ Ã‡Ã¡Ã¡Ã¥ Ã¦Ã¡Ã‡ ÃÃ¦Ã¡ Ã¦Ã¡Ã‡ ÃžÃ¦Ã‰ Ã…Ã¡Ã‡ ÃˆÃ‡Ã¡Ã¡Ã¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
ll a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void case1(){
	int n2=v2.size();
	int i1,i2;
	for(i2=0;i2<n2/3*3;i2++)
		res1+=(char)(v2[i2]+'0');
	n2%=3;
	int n1=v1.size();
	for(i1=0;i1<n1/3*3;i1++)
		res1+=(char)(v1[i1]+'0');
	n1%=3;
	if(n2>0&&n1>0)
		res1+=(char)(v1[i1]+'0'),res1+=(char)(v2[i2]+'0');
	sort(res1.be(),res1.en());
	reverse(res1.be(),res1.en());
}

void case2(){
	int n1=v1.size(),n2=v2.size();
	int n=min(n1,n2);
	for(int i=0;i<n;i++)
		res2+=(char)(v1[i]+'0'),res2+=(char)(v2[i]+'0');
	n1-=n,n2-=n;
	n1=n1/3*3,n2=n2/3*3;
	for(int i=0;i<n1;i++)
		res2+=(char)(v1[i+n]+'0');
	for(int i=0;i<n2;i++)
		res2+=(char)(v2[i+n]+'0');
	sort(res2.be(),res2.en());
	reverse(res2.be(),res2.en());
}
int main(){
	fast;
	bool ok=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x;
		if(!x)
			ok=1;
		if(x%3==0)
			res1+=(char)(x+'0'),res2+=(char)(x+'0');
		if(x%3==1)
			v1.pb(x);
		if(x%3==2)
			v2.pb(x);
	}
	sort(v1.be(),v1.en());
	reverse(v1.be(),v1.en());
	sort(v2.be(),v2.en());
	reverse(v2.be(),v2.en());
	case1();
	case2();
	if(!ok)
		cout<<-1;
	else{
		if(res1>res2)
			cout<<res1;
		else
			cout<<res2;
	}
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
int a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void del1(){
	int cnt=0;
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				cnt++;
				if(cnt==2)
					return ;
			}
}
void del2(){
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==2){
				a[i]=INF;
				return;
			}
}
int main(){
	fast;
	bool m1=0,m2=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
		if(a[i]%3==2)
			m2=1;
		if(a[i]%3==1)
			m1=1;
	}
	if(n==100000){
		cout<<a[0];
		return 0;
	}
	sort(a,a+n);
	reverse(a,a+n);
	if(a[n-1]!=0){
		cout<<-1;
		return 0;
	}
	if(sum%3==1)
		for(int i=n-1;i>=0;i--)
			if(a[i]%3==1){
				a[i]=INF;
				break;
			}
	if(sum%3==2)
		if(m2)
			del2();
		else
			del1();
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			res1+=(char)(a[i]+'0');
	sum=0;
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			sum+=a[i];
	if(!sum){
		cout<<0;
		return 0;
	}
	cout<<res1;
}


//?ˆ?“?£ ?‡?????¥ ?¦???‡ ???¦?? ?¦???‡ ???¦?‰ ?…???‡ ?ˆ?‡?????¥
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define ins insert
#define be begin
#define en end
#define fast ios::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const int INF=1e9;
//ll a,b;
ll a[202020],b[202020];
vector<ll>v1,v2;
pair<ll,ll>p;
map<ll,ll>mp;
//map<string,int>::iterator it;
set<ll>st;
set<ll>::iterator it;
string res1,res2;
ll q,t,n,m,k,z,x,y,d,sum,cnt,mx=0;
void del2(int MOD){
	int cnt=0;
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==MOD){
				a[i]=INF;
				cnt++;
				if(cnt==2)
					return ;
			}
}
void del1(int MOD){
	for(int i=n-1;i>=0;i--)
			if(a[i]%3==MOD){
				a[i]=INF;
				return;
			}
}
int main(){
	fast;
	bool m1=0,m2=0;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		sum+=a[i];
		if(a[i]%3==2)
			m2=1;
		if(a[i]%3==1)
			m1=1;
	}
	sort(a,a+n);
	reverse(a,a+n);
	if(a[n-1]!=0){
		cout<<-1;
		return 0;
	}
	if(a[0]==0){
		cout<<0;
		return 0;
	}
	if(sum%3==1)
		if(m1)
			del1(1);
		else
			del2(2);

	if(sum%3==2)
		if(m2)
			del2(2);
		else
			del1(1);
	for(int i=0;i<n;i++)
		if(a[i]!=INF)
			res1+=(char)(a[i]+'0');
	cout<<res1;
}


#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
clock_t time_p = clock();
void time()
{
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (double)(time_p)/CLOCKS_PER_SEC <<endl;
}
signed main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     int n;
     cin>>n;
     vector<int>v;
     bool okay=false;
     vector<int>rem0,rem1,rem2;
     for(int i=0;i<n;i++)
     {
          int x;
          cin>>x;
          okay|=x==0;
          if(x%3==0)
            rem0.emplace_back(x);
          else if(x%3==1)
            rem1.emplace_back(x);
          else
            rem2.emplace_back(x);
     }
     if(!okay)
     {
          cout<<-1<<endl;
          return 0;
     }
     sort(rem1.rbegin(),rem1.rend());
     sort(rem2.rbegin(),rem2.rend());
     sort(rem0.rbegin(),rem0.rend());
     vector<int>digits;
     int x=min((int)rem1.size(),(int)rem2.size());
     for(int i=0;i<x;i++)
        digits.emplace_back(rem1[i]),digits.emplace_back(rem2[i]);
     for(auto &x:rem0)
        digits.emplace_back(x);
     sort(digits.rbegin(),digits.rend());
     if(digits[0]==0)
     {
          cout<<0<<endl;
          return 0;
     }
     for(auto &x:digits)
        cout<<x;
     cout<<endl;
     time();
     return 0;
}


#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
clock_t time_p = clock();
void time()
{
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (double)(time_p)/CLOCKS_PER_SEC <<endl;
}
signed main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     int n;
     cin>>n;
     vector<int>count(10,0);
     vector<int>dig[3];
     for(int i=0;i<n;i++)
     {
         int x;
         cin>>x;
         count[x]++;
         dig[x%3].emplace_back(x);
     }
     for(int i=0;i<3;i++)
      sort(dig[i].rbegin(),dig[i].rend());
     if(count[0]==0)
     {
          cout<<-1<<endl;
          return 0;
     }
     int m=min((int)dig[1].size(),(int)dig[2].size());
     vector<int>res;
     for(auto &x:dig[0])
      res.emplace_back(x);
     for(int i=0;i<m;i++)
     {
        res.emplace_back(dig[1][i]);
        res.emplace_back(dig[2][i]);
     }
     if((int)dig[1].size()>=(int)dig[2].size())
     {
         for(int i=m;i<(int)dig[1].size()-2;i+=3)
         {
              res.emplace_back(dig[1][i]);
              res.emplace_back(dig[1][i+1]);
              res.emplace_back(dig[1][i+2]);
         }
     }
     else
     {
         for(int i=m;i<(int)dig[2].size()-2;i+=3)
         {
              res.emplace_back(dig[2][i]);
              res.emplace_back(dig[2][i+1]);
              res.emplace_back(dig[2][i+2]);
         }
     }
     sort(res.rbegin(),res.rend());
     bool ok=false;
     for(auto &x:res)
      ok|=x!=0;
     if(!ok)
     {
        cout<<0<<endl;
        return 0;
     }
     for(auto &x:res)
      cout<<x;
     cout<<endl;
     time();
     return 0;
}


#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
clock_t time_p = clock();
void time()
{
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (double)(time_p)/CLOCKS_PER_SEC <<endl;
}
signed main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     int n;
     cin>>n;
     vector<int>digits;
     bool okay=false;
     int sum=0;
     for(int i=0;i<n;i++)
     {
          int x;
          cin>>x;
          sum+=x;
          sum%=3;
          okay|=x==0;
          digits.emplace_back(x);
     }
     sort(digits.rbegin(),digits.rend());
     if(!okay)
     {
          cout<<-1<<endl;
          return 0;
     }
     if(digits[0]==0)
     {
          cout<<0<<endl;
          return 0;
     }
     if(sum%3==0)
     {
         for(auto &x:digits)
            cout<<x;
         cout<<endl;
         return 0;
     }
     if(sum%3==1)
     {
            for(int i=n-1;i>=0;i--)
            {
                     if(digits[i]%3==1)
                     {
                           for(int j=0;j<n;j++)
                           {
                                if(i==j)
                                    continue;
                                cout<<digits[j];
                           }
                           cout<<endl;
                           return 0;
                     }
            }
            int count=0;
            vector<int>v;
            for(int i=n-1;i>=0;i--)
            {
                  if(digits[i]%3==2)
                  {
                       v.emplace_back(i);
                       count++;
                       if(count==2)
                       {
                          for(int j=0;j<n;j++)
                           {
                                if(v[0]==j||v[1]==j)
                                    continue;
                                cout<<digits[j];
                           }
                           cout<<endl;
                           return 0;
                       }
                  }
            }
            cout<<-1<<endl;
            return 0;
     }
     if(sum%3==2)
     {
            for(int i=n-1;i>=0;i--)
            {
                     if(digits[i]%3==2)
                     {
                           for(int j=0;j<n;j++)
                           {
                                if(i==j)
                                    continue;
                                cout<<digits[j];
                           }
                           cout<<endl;
                           return 0;
                     }
            }
            int count=0;
            vector<int>v;
            for(int i=n-1;i>=0;i--)
            {
                  if(digits[i]%3==1)
                  {
                       v.emplace_back(i);
                       count++;
                       if(count==2)
                       {
                          for(int j=0;j<n;j++)
                           {
                                if(v[0]==j||v[1]==j)
                                    continue;
                                cout<<digits[j];
                           }
                           cout<<endl;
                           return 0;
                       }
                  }
            }
            cout<<-1<<endl;
            return 0;
     }
     time();
     return 0;
}


#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
clock_t time_p = clock();
void time()
{
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (double)(time_p)/CLOCKS_PER_SEC <<endl;
}
signed main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     int n;
     cin>>n;
     vector<int>digits;
     bool okay=false;
     int sum=0;
     for(int i=0;i<n;i++)
     {
          int x;
          cin>>x;
          sum+=x;
          sum%=3;
          okay|=x==0;
          digits.emplace_back(x);
     }
     sort(digits.rbegin(),digits.rend());
     if(!okay)
     {
          cout<<-1<<endl;
          return 0;
     }
     if(digits[0]==0)
     {
          cout<<0<<endl;
          return 0;
     }
     if(sum%3==0)
     {
         for(auto &x:digits)
            cout<<x;
         cout<<endl;
         return 0;
     }
     if(sum%3==1)
     {
            vector<int>res;
            bool okay=false;
            for(int i=n-1;i>=0;i--)
            {
                     if(digits[i]%3==1)
                     {
                           for(int j=0;j<n;j++)
                           {
                                if(i==j)
                                    continue;
                                res.emplace_back(digits[j]);
                                okay|=digits[j]!=0;
                           }
                           break;
                     }
            }
            if(okay)
            {
                for(auto &x:res)
                    cout<<x;
                cout<<endl;
                return 0;
            }
            else if(!res.empty())
            {
                  cout<<0<<endl;
                  return 0;
            }
            int count=0;
            vector<int>v;
            for(int i=n-1;i>=0;i--)
            {
                  if(digits[i]%3==2)
                  {
                       v.emplace_back(i);
                       count++;
                       if(count==2)
                       {
                          for(int j=0;j<n;j++)
                           {
                                if(v[0]==j||v[1]==j)
                                    continue;
                                res.emplace_back(digits[j]);
                                okay|=digits[j]!=0;
                           }
                           cout<<endl;
                           break;
                       }
                  }
            }
            if(okay)
            {
                for(auto &x:res)
                    cout<<x;
                cout<<endl;
                return 0;
            }
            else if(!res.empty())
            {
                  cout<<0<<endl;
                  return 0;
            }
            cout<<-1<<endl;
            return 0;
     }
     if(sum%3==2)
     {
            vector<int>res;
            bool okay=false;
            for(int i=n-1;i>=0;i--)
            {
                     if(digits[i]%3==2)
                     {
                           for(int j=0;j<n;j++)
                           {
                                if(i==j)
                                    continue;
                                res.emplace_back(digits[j]);
                                okay|=digits[j]!=0;
                           }
                           break;
                     }
            }
            if(okay)
            {
                for(auto &x:res)
                    cout<<x;
                cout<<endl;
                return 0;
            }
            else if(!res.empty())
            {
                  cout<<0<<endl;
                  return 0;
            }
            int count=0;
            vector<int>v;
            for(int i=n-1;i>=0;i--)
            {
                  if(digits[i]%3==1)
                  {
                       v.emplace_back(i);
                       count++;
                       if(count==2)
                       {
                          for(int j=0;j<n;j++)
                           {
                                if(v[0]==j||v[1]==j)
                                    continue;
                                res.emplace_back(digits[j]);
                                okay|=digits[j]!=0;
                           }
                           cout<<endl;
                           return 0;
                       }
                  }
            }
            if(okay)
            {
                for(auto &x:res)
                    cout<<x;
                cout<<endl;
                return 0;
            }
            else if(!res.empty())
            {
                  cout<<0<<endl;
                  return 0;
            }
            cout<<-1<<endl;
            return 0;
     }
     time();
     return 0;
}


#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
clock_t time_p = clock();
void time()
{
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (double)(time_p)/CLOCKS_PER_SEC <<endl;
}
signed main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     int n;
     cin>>n;
     vector<int>digits;
     bool okay=false;
     int sum=0;
     for(int i=0;i<n;i++)
     {
          int x;
          cin>>x;
          sum+=x;
          sum%=3;
          okay|=x==0;
          digits.emplace_back(x);
     }
     sort(digits.rbegin(),digits.rend());
     if(!okay)
     {
          cout<<-1<<endl;
          return 0;
     }
     if(digits[0]==0)
     {
          cout<<0<<endl;
          return 0;
     }
     if(sum%3==0)
     {
         for(auto &x:digits)
            cout<<x;
         cout<<endl;
         return 0;
     }
     if(sum%3==1)
     {
            for(int i=n-1;i>=0;i--)
            {
                     if(digits[i]%3==1)
                     {
                           for(int j=0;j<n;j++)
                           {
                                if(i==j)
                                    continue;
                                cout<<digits[j];
                           }
                           cout<<endl;
                           return 0;
                     }
            }
            int count=0;
            vector<int>v;
            for(int i=n-1;i>=0;i--)
            {
                  if(digits[i]%3==2)
                  {
                       v.emplace_back(i);
                       count++;
                       if(count==2)
                       {
                          for(int j=0;j<n;j++)
                           {
                                if(v[0]==j||v[1]==j)
                                    continue;
                                cout<<digits[j];
                           }
                           cout<<endl;
                           return 0;
                       }
                  }
            }
            cout<<-1<<endl;
            return 0;
     }
     if(sum%3==2)
     {
            for(int i=n-1;i>=0;i--)
            {
                     if(digits[i]%3==2)
                     {
                           for(int j=0;j<n;j++)
                           {
                                if(i==j)
                                    continue;
                                cout<<digits[j];
                           }
                           cout<<endl;
                           return 0;
                     }
            }
            int count=0;
            vector<int>v;
            for(int i=n-1;i>=0;i--)
            {
                  if(digits[i]%3==1)
                  {
                       v.emplace_back(i);
                       count++;
                       if(count==2)
                       {
                          for(int j=0;j<n;j++)
                           {
                                if(v[0]==j||v[1]==j)
                                    continue;
                                cout<<digits[j];
                           }
                           cout<<endl;
                           return 0;
                       }
                  }
            }
            cout<<-1<<endl;
            cout<<-1<<endl;
            return 0;
     }
     time();
     return 0;
}


#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
clock_t time_p = clock();
void time()
{
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (double)(time_p)/CLOCKS_PER_SEC <<endl;
}
signed main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     int n;
     cin>>n;
     vector<int>v;
     bool okay=false;
     vector<int>rem0,rem1,rem2;
     for(int i=0;i<n;i++)
     {
          int x;
          cin>>x;
          okay|=x==0;
          if(x%3==0)
            rem0.emplace_back(x);
          else if(x%3==1)
            rem1.emplace_back(x);
          else
            rem2.emplace_back(x);
     }
     if(!okay)
     {
          cout<<-1<<endl;
          return 0;
     }
     sort(rem1.begin(),rem1.end());
     sort(rem2.begin(),rem2.end());
     sort(rem0.begin(),rem0.end());
     vector<int>digits;
     if(!rem1.empty())
     {
          while((int)rem1.size()>2)
          {
                digits.emplace_back(rem1.back());
                rem1.pop_back();
                digits.emplace_back(rem1.back());
                rem1.pop_back();
                digits.emplace_back(rem1.back());
                rem1.pop_back();
          }
     }
     if(!rem2.empty())
     {
         while((int)rem2.size()>2)
          {
                digits.emplace_back(rem2.back());
                rem2.pop_back();
                digits.emplace_back(rem2.back());
                rem2.pop_back();
                digits.emplace_back(rem2.back());
                rem2.pop_back();
          }
     }
     int x=min((int)rem1.size(),(int)rem2.size());
     for(int i=0;i<x;i++)
     {
           digits.emplace_back(rem1.back());
           rem1.pop_back();
           digits.emplace_back(rem2.back());
           rem2.pop_back();
     }
     for(auto &x:rem0)
        digits.emplace_back(x);
     sort(digits.rbegin(),digits.rend());
     if(digits[0]==0)
     {
          cout<<0<<endl;
          return 0;
     }
     for(auto &x:digits)
        cout<<x;
     cout<<endl;
     time();
     return 0;
}


#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
clock_t time_p = clock();
void time()
{
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (double)(time_p)/CLOCKS_PER_SEC <<endl;
}
signed main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     int n;
     cin>>n;
     vector<int>dig[3];
     int sum=0;
     bool ok=false;
     bool go=false;
     vector<int>digits;
     for(int i=0;i<n;i++)
     {
         int x;
         cin>>x;
         sum+=x;
         digits.emplace_back(x);
         sum%=3;
         ok|=x!=0;
         go|=x==0;
         dig[x%3].emplace_back(x);
     }
     sort(digits.rbegin(),digits.rend());
     for(int i=0;i<3;i++)
      sort(dig[i].rbegin(),dig[i].rend());
     if(!go)
     {
          cout<<-1<<endl;
          return 0;
     }
     if(!ok)
     {
         cout<<0<<endl;
         return 0;
     }
     if(sum==0)
     {
         for(auto &x:digits)
            cout<<x;
         cout<<endl;
         return 0;
     }
     vector<int>res;
     if(sum==1)
     {
           if(!dig[1].empty())
           {
               int least=dig[1].back();
               bool ok=false;
               for(auto &x:digits)
               {
                     if(x==least&&!ok)
                     {
                           ok=true;
                           continue;
                     }
                     res.emplace_back(x);
               }

           }
           else
           {
                int least1=dig[2].back();
                dig[2].pop_back();
                int least2=dig[2].back();
                bool ok1=false;
                bool ok2=false;
                for(auto &x:digits)
                {
                      if(x==least1&&!ok1)
                      {
                           ok1=true;
                           continue;
                      }
                      if(x==least2&&!ok2)
                      {
                           ok2=true;
                           continue;
                      }
                      res.emplace_back(x);
                }

           }
     }
     else
     {

           if(!dig[2].empty())
           {
               int least=dig[2].back();
               bool ok=false;
               for(auto &x:digits)
               {
                     if(x==least&&!ok)
                     {
                           ok=true;
                           continue;
                     }
                     res.emplace_back(x);
               }

           }
           else
           {
                int least1=dig[1].back();
                dig[1].pop_back();
                int least2=dig[1].back();
                bool ok1=false;
                bool ok2=false;
                for(auto &x:digits)
                {
                      if(x==least1&&!ok1)
                      {
                           ok1=true;
                           continue;
                      }
                      if(x==least2&&!ok2)
                      {
                           ok2=true;
                           continue;
                      }
                      res.emplace_back(x);
                }

           }
     }
     ok=false;
     for(auto &x:res)
       ok|=x!=0;
     if(!ok)
     {
         cout<<0<<endl;
         return 0;
     }
     for(auto &x:res)
      cout<<x;
     cout<<endl;
     time();
     return 0;
}