#include <iostream>
#include <stdio.h>
using namespace std;
int a[100010];
int c[10];
int n;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        int i,j;
        int flag1=1;
        int b;
        for(i=0;i<10;i++)
            c[i]=0;
        if(n==1)
        {
            scanf("%d",&b);
            if(b==0)
            {
                cout<<0<<endl;
                continue;
            }
            else
            {
                cout<<-1<<endl;
                continue;
            }
        }
        b=0;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]==0) flag1=0;
            b+=a[i];
            c[a[i]]++;
        }
        //cout<<b<<endl;
        if(flag1)
        {
            cout<<-1<<endl;
            continue;
        }
        if(b%3)
        {
            for(i=1;i<10;i++)
            {
                if((b-i)%3==0&&c[i])
                {
                    c[i]--;
                    break;
                }
            }
        }
        for(i=9;i>=0;i--)
        {
            if(c[i])
                for(j=0;j<c[i];j++)
                    printf("%d",i);
        }
        cout<<endl;
    }
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<map>
#include<numeric>
#include<sstream>
#include<cmath>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).end,(v).begin
#define pb push_back
#define f(i,x,y) for(int i=x;i<y;i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin();it!=A.end();it++)
#define sqr(x) (x)*(x)
#define mp make_pair
#define clr(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
int tengo[103];
int arr[100003];
void imp(){
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							cout<<i;
							tengo[i]--;
						}
					}
	cout<<endl;
}
int uno(int fa){
	f(i,1,10){
		if(i%3==fa && tengo[i]){
			tengo[i]--;
			return i;
		}
	}
	return -1;
}
void saca(int fa,int &a,int &b){
	//uno solo

	f(i,1,10){
		if(tengo[i]>=2 && (2*i)%3 == fa ){
			a=i;b=i;
		}
	}
	f(i,1,10)f(j,1,10){
		if(i!=j && tengo[i] && tengo[j] && (i+j)%3==fa){
			if(a==-1){
				a=i;b=j;
			}
			else{
				if(a>b)swap(a,b);
				int m=max(i,j);
				if(m<b){
					a=i;
					b=j;
				}
				else if(m==b){
					int mm=min(i,j);
					if(mm<a)a=mm;
				}
			}
		}
	}
	if(a!=-1){
		tengo[a]--;
		tengo[b]--;
	}
}
string gera(){
	string r="";
	char c;
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							c='0'+i;
							r+=c;
							tengo[i]--;
						}
					}
	return r;
}
bool com(string cad1,string cad2){
	if(cad1.size()!=cad2.size())return cad1.size()>cad2.size();
	f(i,0,cad1.size())if(cad1[i]>cad2[i])return true;else return false;

	return true;
}
int main(){
	int n;
	cin>>n;
	clr(tengo,0);
	int ceros=0;
	int s=0;
	f(i,0,n){
		cin>>arr[i];
		if(arr[i]==0)ceros++;
		s+=arr[i];
		tengo[arr[i]]++;
	}
	//cout<<s<<endl;
	if(ceros==0)cout<<-1<<endl;
	else{
		bool quite=false;
		int queda=s%3;
		s%=3;
		if(queda==0){
			//cout<<"ads"<<endl;
			imp();
			return 0;
		}
		else{
			if(uno(s)!=-1){
				imp();
				return 0;
			}
			else{
				vector <string> v;
				string cad;
				f(i,1,10)f(j,1,10){
					tengo[i]--;
					tengo[j]--;
					if(tengo[i]>0 && tengo[j]>0 && (i+j)%3==s){
						cad=gera();
					}
					tengo[i]++;
					tengo[j]++;
				}

				if(v.size()==0)cout<<-1<<endl;
				else{
				sort(all(v),com);
				cout<<v[0]<<endl;
				}
			}
		}

	}
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<map>
#include<numeric>
#include<sstream>
#include<cmath>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).end,(v).begin
#define pb push_back
#define f(i,x,y) for(int i=x;i<y;i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin();it!=A.end();it++)
#define sqr(x) (x)*(x)
#define mp make_pair
#define clr(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
int tengo[103];
int arr[100003];
void imp(){
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							cout<<i;
							tengo[i]--;
						}
					}
	cout<<endl;
}
int uno(int fa){
	f(i,1,10){
		if(i%3==fa && tengo[i]){
			tengo[i]--;
			return i;
		}
	}
	return -1;
}
void saca(int fa,int &a,int &b){
	//uno solo

	f(i,1,10){
		if(tengo[i]>=2 && (2*i)%3 == fa ){
			a=i;b=i;
		}
	}
	f(i,1,10)f(j,1,10){
		if(i!=j && tengo[i] && tengo[j] && (i+j)%3==fa){
			if(a==-1){
				a=i;b=j;
			}
			else{
				if(a>b)swap(a,b);
				int m=max(i,j);
				if(m<b){
					a=i;
					b=j;
				}
				else if(m==b){
					int mm=min(i,j);
					if(mm<a)a=mm;
				}
			}
		}
	}
	if(a!=-1){
		tengo[a]--;
		tengo[b]--;
	}
}
string gera(){
	string r="";
	char c;
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							c='0'+i;
							r+=c;
							tengo[i]--;
						}
					}
	return r;
}
bool com(string cad1,string cad2){
	if(cad1.size()!=cad2.size())return cad1.size()>cad2.size();
	f(i,0,cad1.size())if(cad1[i]>cad2[i])return true;else return false;

	return true;
}
int main(){
	int n;
	cin>>n;
	clr(tengo,0);
	int ceros=0;
	int s=0;
	f(i,0,n){
		cin>>arr[i];
		if(arr[i]==0)ceros++;
		s+=arr[i];
		tengo[arr[i]]++;
	}
	//cout<<s<<endl;
	if(ceros==0)cout<<-1<<endl;
	else{
		bool quite=false;
		int queda=s%3;
		s%=3;
		if(queda==0){
			cout<<"ads"<<endl;
			imp();
			return 0;
		}
		else{
			if(uno(s)!=-1){
				imp();
				return 0;
			}
			else{
				vector <string> v;
				string cad;
				f(i,1,10)f(j,1,10){
					tengo[i]--;
					tengo[j]--;
					if(tengo[i]>0 && tengo[j]>0 && (i+j)%3==s){
						cad=gera();
					}
					tengo[i]++;
					tengo[j]++;
				}

				if(v.size()==0)cout<<-1<<endl;
				else{
				sort(all(v),com);
				cout<<v[0]<<endl;
				}
			}
		}

	}
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<map>
#include<numeric>
#include<sstream>
#include<cmath>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).end,(v).begin
#define pb push_back
#define f(i,x,y) for(int i=x;i<y;i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin();it!=A.end();it++)
#define sqr(x) (x)*(x)
#define mp make_pair
#define clr(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
int tengo[103];
int arr[100003];
void imp(){
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							cout<<i;
							tengo[i]--;
						}
					}
	cout<<endl;
}
int uno(int fa){
	f(i,1,10){
		if(i%3==fa && tengo[i]){
			tengo[i]--;
			return i;
		}
	}
	return -1;
}
void saca(int fa,int &a,int &b){
	//uno solo

	f(i,1,10){
		if(tengo[i]>=2 && (2*i)%3 == fa ){
			a=i;b=i;
		}
	}
	f(i,1,10)f(j,1,10){
		if(i!=j && tengo[i] && tengo[j] && (i+j)%3==fa){
			if(a==-1){
				a=i;b=j;
			}
			else{
				if(a>b)swap(a,b);
				int m=max(i,j);
				if(m<b){
					a=i;
					b=j;
				}
				else if(m==b){
					int mm=min(i,j);
					if(mm<a)a=mm;
				}
			}
		}
	}
	if(a!=-1){
		tengo[a]--;
		tengo[b]--;
	}
}
string gera(){
	string r="";
	char c;
	int aa;
	for(int i=9;i>=0;i--){
		aa=tengo[i];
		while(aa){
			c='0'+i;
			r+=c;
			aa--;
			}
	}
	return r;
}
bool com(string cad1,string cad2){
	if(cad1.size()!=cad2.size())return cad1.size()>cad2.size();
	f(i,0,cad1.size())if(cad1[i]>cad2[i])return true;else return false;

	return true;
}
int main(){
	int n;
	cin>>n;
	clr(tengo,0);
	int ceros=0;
	int s=0;
	f(i,0,n){
		cin>>arr[i];
		if(arr[i]==0)ceros++;
		s+=arr[i];
		tengo[arr[i]]++;
	}
	//cout<<s<<endl;
	if(ceros==0)cout<<-1<<endl;
	else{
		bool quite=false;
		int queda=s%3;
		s%=3;
		if(queda==0){
			if(ceros==n)
			cout<<0<<endl;
			else
			imp();
			return 0;
		}
		else{
			if(uno(s)!=-1){
				imp();
				return 0;
			}
			else{
				vector <string> v;
				string cad;
				f(i,1,10)f(j,1,10){
					tengo[i]--;
					tengo[j]--;
					if(tengo[i]>0 && tengo[j]>0 && (i+j)%3==s){
						cad=gera();
					}
					tengo[i]++;
					tengo[j]++;
				}

				if(v.size()==0)cout<<-1<<endl;
				else{
				sort(all(v),com);
				cout<<v[0]<<endl;
				}
			}
		}

	}
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<map>
#include<numeric>
#include<sstream>
#include<cmath>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).end,(v).begin
#define pb push_back
#define f(i,x,y) for(int i=x;i<y;i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin();it!=A.end();it++)
#define sqr(x) (x)*(x)
#define mp make_pair
#define clr(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
int tengo[103];
int arr[100003];
void imp(){
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							cout<<i;
							tengo[i]--;
						}
					}
	cout<<endl;
}
void saca(int fa,int &a,int &b){
	//uno solo

	f(i,1,10){
		if(tengo[i]>=2 && (2*i)%3 == fa ){
			a=i;b=i;
		}
	}
	f(i,1,10)f(j,1,10){
		if(i!=j && tengo[i] && tengo[j] && (i+j)%3==fa){
			if(a==-1){
				a=i;b=j;
			}
			else{
				if(a>b)swap(a,b);
				int m=max(i,j);
				if(m<b){
					a=i;
					b=j;
				}
				else if(m==b){
					int mm=min(i,j);
					if(mm<a)a=mm;
				}
			}
		}
	}
}
int main(){
	int n;
	cin>>n;
	clr(tengo,0);
	int ceros=0;
	int s=0;
	f(i,0,n){
		cin>>arr[i];
		if(arr[i]==0)ceros++;
		s+=arr[i];
		tengo[arr[i]]++;
	}
	if(ceros==0)cout<<-1<<endl;
	else{
		bool quite=false;
		int queda=s%3;
		s%=3;
		if(queda==0){
			imp();
			return 0;
		}
		else{
			if(s==1){
				if(tengo[1]){
					tengo[1]--;
					imp();
					return 0;
				}
				else{
					int a=-1,b=-1;
					saca(s,a,b);
					if(a==-1)cout<<-1<<endl;
					else {tengo[a]--;tengo[b]--;imp();return 0;}
				}
			}
			else if(s==2){
				if(tengo[2]){
					tengo[2]--;
					imp();
				  	return 0;
				}
				else{
					int a=-1,b=-1;
					saca(s,a,b);
					if(a==-1)cout<<-1<<endl;
					else {tengo[a]--;tengo[b]--;imp();return 0;}
				}
			}
			if(!quite)cout<<-1<<endl;
		}

	}
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<map>
#include<numeric>
#include<sstream>
#include<cmath>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).end,(v).begin
#define pb push_back
#define f(i,x,y) for(int i=x;i<y;i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin();it!=A.end();it++)
#define sqr(x) (x)*(x)
#define mp make_pair
#define clr(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
int tengo[103];
int arr[100003];
void imp(){
	bool es=true;
	f(i,1,10)if(tengo[i]!=0)es=false;
	if(es)cout<<0<<endl;
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							cout<<i;
							tengo[i]--;
						}
					}
	cout<<endl;
}
int uno(int fa){
	f(i,1,10){
		if(i%3==fa && tengo[i]){
			tengo[i]--;
			return i;
		}
	}
	return -1;
}
string gera(){
	string r="";
	char c;
	int aa;
	for(int i=9;i>=0;i--){
		aa=tengo[i];
		while(aa){
			c='0'+i;
			r+=c;
			aa--;
			}
	}
	//while(r.size>0 && )
	return r;
}
bool com(string cad1,string cad2){
	if(cad1.size()!=cad2.size())return cad1.size()>cad2.size();
	f(i,0,cad1.size())if(cad1[i]>cad2[i])return true;else return false;

	return true;
}
int main(){
	int n;
	cin>>n;
	clr(tengo,0);
	int ceros=0;
	int s=0;
	f(i,0,n){
		cin>>arr[i];
		if(arr[i]==0)ceros++;
		s+=arr[i];
		tengo[arr[i]]++;
	}
	//cout<<s<<endl;
	if(ceros==0)cout<<-1<<endl;
	else{
		bool quite=false;
		int queda=s%3;
		if(queda==0){
			if(ceros==n)
			cout<<0<<endl;
			else
			imp();
			return 0;
		}
		else{
			if(uno(queda)!=-1){
				imp();
				return 0;
			}
			else{
				vector <string> v;
				string cad;
				f(i,1,10)f(j,1,10){
					tengo[i]--;
					tengo[j]--;
					if(tengo[i]>0 && tengo[j]>0 && (i+j)%3==queda && (s-i-j)>0){
						cad=gera();
					}
					tengo[i]++;
					tengo[j]++;
				}

				if(v.size()==0)cout<<-1<<endl;
				else{
				sort(all(v),com);
				cout<<v[0]<<endl;
				}
			}
		}

	}
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<map>
#include<numeric>
#include<sstream>
#include<cmath>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).end,(v).begin
#define pb push_back
#define f(i,x,y) for(int i=x;i<y;i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin();it!=A.end();it++)
#define sqr(x) (x)*(x)
#define mp make_pair
#define clr(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
int tengo[103];
int arr[100003];
void imp(){
	bool es=true;
	f(i,1,10)if(tengo[i]!=0)es=false;
	if(es){cout<<0<<endl;return ;}
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							cout<<i;
							tengo[i]--;
						}
					}
	cout<<endl;
}
int uno(int fa){
	f(i,1,10){
		if(i%3==fa && tengo[i]){
			tengo[i]--;
			return i;
		}
	}
	return -1;
}
string gera(){
	string r="";
	char c;
	int aa;
	for(int i=9;i>=0;i--){
		aa=tengo[i];
		while(aa){
			c='0'+i;
			r+=c;
			aa--;
			}
	}
	//while(r.size>0 && )
	return r;
}
bool com(string cad1,string cad2){
	if(cad1.size()!=cad2.size())return cad1.size()>cad2.size();
	f(i,0,cad1.size())if(cad1[i]>cad2[i])return true;else return false;

	return true;
}
int main(){
	int n;
	cin>>n;
	clr(tengo,0);
	int ceros=0;
	int s=0;
	f(i,0,n){
		cin>>arr[i];
		if(arr[i]==0)ceros++;
		s+=arr[i];
		tengo[arr[i]]++;
	}
//	cout<<s<<endl;
	if(ceros==0)cout<<-1<<endl;
	else{
		bool quite=false;
		int queda=s%3;
		if(queda==0){
			if(ceros==n)
			cout<<0<<endl;
			else
			imp();
			return 0;
		}
		else{
			if(uno(queda)!=-1){
				imp();
				return 0;
			}
			else{
				vector <string> v;
				string cad;
				//f(i,0,10)cout<<tengo[i]<<" ";
				//cout<<endl;
				//cout<<"ad"<<endl;
				f(i,1,10)f(j,1,10){
					tengo[i]--;
					tengo[j]--;
					if(tengo[i]>=0 && tengo[j]>=0 && (s-i-j)%3==0 && (s-i-j)>0){
					//	cout<<i<<" "<<j<<endl;
						cad=gera();
						v.pb(cad);
					}
					tengo[i]++;
					tengo[j]++;
				}

				if(v.size()==0)cout<<0<<endl;
				else{
				sort(all(v),com);
				cout<<v[0]<<endl;
				}
			}
		}

	}
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<map>
#include<numeric>
#include<sstream>
#include<cmath>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).end,(v).begin
#define pb push_back
#define f(i,x,y) for(int i=x;i<y;i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin();it!=A.end();it++)
#define sqr(x) (x)*(x)
#define mp make_pair
#define clr(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
int tengo[103];
int arr[100003];
void imp(){
	bool es=true;
	f(i,1,10)if(tengo[i]!=0)es=false;
	if(es){cout<<0<<endl;return ;}
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							cout<<i;
							tengo[i]--;
						}
					}
	cout<<endl;
}
int uno(int fa){
	f(i,1,10){
		if(i%3==fa && tengo[i]){
			tengo[i]--;
			return i;
		}
	}
	return -1;
}
string gera(){
	string r="";
	char c;
	int aa;
	for(int i=9;i>=0;i--){
		aa=tengo[i];
		while(aa){
			c='0'+i;
			r+=c;
			aa--;
			}
	}
	//while(r.size>0 && )
	return r;
}
bool com(string cad1,string cad2){
	if(cad1.size()!=cad2.size())return cad1.size()>cad2.size();
	f(i,0,cad1.size())if(cad1[i]>cad2[i])return true;else return false;

	return true;
}
int main(){
	int n;
	cin>>n;
	clr(tengo,0);
	int ceros=0;
	int s=0;
	f(i,0,n){
		cin>>arr[i];
		if(arr[i]==0)ceros++;
		s+=arr[i];
		tengo[arr[i]]++;
	}
//	cout<<s<<endl;
	if(ceros==0)cout<<-1<<endl;
	else{
		bool quite=false;
		int queda=s%3;
		if(queda==0){
			if(ceros==n)
			cout<<0<<endl;
			else
			imp();
			return 0;
		}
		else{
			if(uno(queda)!=-1){
				imp();
				return 0;
			}
			else{
				vector <string> v;
				string cad;
				//f(i,0,10)cout<<tengo[i]<<" ";
				//cout<<endl;
				//cout<<"ad"<<endl;
				f(i,1,10)f(j,1,10){
					tengo[i]--;
					tengo[j]--;
					if(tengo[i]>=0 && tengo[j]>=0 && (s-i-j)%3==0 && (s-i-j)>0){
					//	cout<<i<<" "<<j<<endl;
						cad=gera();
						v.pb(cad);
					}
					tengo[i]++;
					tengo[j]++;
				}

				if(v.size()==0)cout<<-1<<endl;
				else{
				sort(all(v),com);
				cout<<v[0]<<endl;
				}
			}
		}

	}
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<map>
#include<numeric>
#include<sstream>
#include<cmath>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).end,(v).begin
#define pb push_back
#define f(i,x,y) for(int i=x;i<y;i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin();it!=A.end();it++)
#define sqr(x) (x)*(x)
#define mp make_pair
#define clr(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
int tengo[103];
int arr[100003];
void imp(){
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							cout<<i;
							tengo[i]--;
						}
					}
	cout<<endl;
}
int uno(int fa){
	f(i,1,10){
		if(i%3==fa && tengo[i]){
			tengo[i]--;
			return i;
		}
	}
	return -1;
}
void saca(int fa,int &a,int &b){
	//uno solo

	f(i,1,10){
		if(tengo[i]>=2 && (2*i)%3 == fa ){
			a=i;b=i;
		}
	}
	f(i,1,10)f(j,1,10){
		if(i!=j && tengo[i] && tengo[j] && (i+j)%3==fa){
			if(a==-1){
				a=i;b=j;
			}
			else{
				if(a>b)swap(a,b);
				int m=max(i,j);
				if(m<b){
					a=i;
					b=j;
				}
				else if(m==b){
					int mm=min(i,j);
					if(mm<a)a=mm;
				}
			}
		}
	}
	if(a!=-1){
		tengo[a]--;
		tengo[b]--;
	}
}
string gera(){
	string r="";
	char c;
	int aa;
	for(int i=9;i>=0;i--){
						aa=tengo[i];
						while(aa){
							c='0'+i;
							r+=c;
							aa--;
						}
					}
	return r;
}
bool com(string cad1,string cad2){
	if(cad1.size()!=cad2.size())return cad1.size()>cad2.size();
	f(i,0,cad1.size())if(cad1[i]>cad2[i])return true;else return false;

	return true;
}
int main(){
	int n;
	cin>>n;
	clr(tengo,0);
	int ceros=0;
	int s=0;
	f(i,0,n){
		cin>>arr[i];
		if(arr[i]==0)ceros++;
		s+=arr[i];
		tengo[arr[i]]++;
	}
	//cout<<s<<endl;
	if(ceros==0)cout<<-1<<endl;
	else{
		bool quite=false;
		int queda=s%3;
		s%=3;
		if(queda==0){
			imp();
			return 0;
		}
		else{
			if(uno(s)!=-1){
				imp();
				return 0;
			}
			else{
				vector <string> v;
				string cad;
				f(i,1,10)f(j,1,10){
					tengo[i]--;
					tengo[j]--;
					if(tengo[i]>0 && tengo[j]>0 && (i+j)%3==s){
						cad=gera();
					}
					tengo[i]++;
					tengo[j]++;
				}

				if(v.size()==0)cout<<-1<<endl;
				else{
				sort(all(v),com);
				cout<<v[0]<<endl;
				}
			}
		}

	}
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<map>
#include<numeric>
#include<sstream>
#include<cmath>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).end,(v).begin
#define pb push_back
#define f(i,x,y) for(int i=x;i<y;i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin();it!=A.end();it++)
#define sqr(x) (x)*(x)
#define mp make_pair
#define clr(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
int tengo[103];
int arr[100003];
void imp(){
	bool es=true;
	f(i,1,10)if(tengo[i]!=0)es=false;
	if(es){cout<<0<<endl;return 0;}
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							cout<<i;
							tengo[i]--;
						}
					}
	cout<<endl;
}
int uno(int fa){
	f(i,1,10){
		if(i%3==fa && tengo[i]){
			tengo[i]--;
			return i;
		}
	}
	return -1;
}
string gera(){
	string r="";
	char c;
	int aa;
	for(int i=9;i>=0;i--){
		aa=tengo[i];
		while(aa){
			c='0'+i;
			r+=c;
			aa--;
			}
	}
	//while(r.size>0 && )
	return r;
}
bool com(string cad1,string cad2){
	if(cad1.size()!=cad2.size())return cad1.size()>cad2.size();
	f(i,0,cad1.size())if(cad1[i]>cad2[i])return true;else return false;

	return true;
}
int main(){
	int n;
	cin>>n;
	clr(tengo,0);
	int ceros=0;
	int s=0;
	f(i,0,n){
		cin>>arr[i];
		if(arr[i]==0)ceros++;
		s+=arr[i];
		tengo[arr[i]]++;
	}
//	cout<<s<<endl;
	if(ceros==0)cout<<-1<<endl;
	else{
		bool quite=false;
		int queda=s%3;
		if(queda==0){
			if(ceros==n)
			cout<<0<<endl;
			else
			imp();
			return 0;
		}
		else{
			if(uno(queda)!=-1){
				imp();
				return 0;
			}
			else{
				vector <string> v;
				string cad;
				//f(i,0,10)cout<<tengo[i]<<" ";
				//cout<<endl;
				//cout<<"ad"<<endl;
				f(i,1,10)f(j,1,10){
					tengo[i]--;
					tengo[j]--;
					if(tengo[i]>=0 && tengo[j]>=0 && (s-i-j)%3==0 && (s-i-j)>0){
					//	cout<<i<<" "<<j<<endl;
						cad=gera();
						v.pb(cad);
					}
					tengo[i]++;
					tengo[j]++;
				}

				if(v.size()==0)cout<<-1<<endl;
				else{
				sort(all(v),com);
				cout<<v[0]<<endl;
				}
			}
		}

	}
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<map>
#include<numeric>
#include<sstream>
#include<cmath>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).end,(v).begin
#define pb push_back
#define f(i,x,y) for(int i=x;i<y;i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin();it!=A.end();it++)
#define sqr(x) (x)*(x)
#define mp make_pair
#define clr(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
int tengo[103];
int arr[100003];
void imp(){
	bool es=true;
	f(i,1,10)if(tengo[i]!=0)es=false;
	if(es)cout<<0<<endl;
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							cout<<i;
							tengo[i]--;
						}
					}
	cout<<endl;
}
int uno(int fa){
	f(i,1,10){
		if(i%3==fa && tengo[i]){
			tengo[i]--;
			return i;
		}
	}
	return -1;
}
string gera(){
	string r="";
	char c;
	int aa;
	for(int i=9;i>=0;i--){
		aa=tengo[i];
		while(aa){
			c='0'+i;
			r+=c;
			aa--;
			}
	}
	//while(r.size>0 && )
	return r;
}
bool com(string cad1,string cad2){
	if(cad1.size()!=cad2.size())return cad1.size()>cad2.size();
	f(i,0,cad1.size())if(cad1[i]>cad2[i])return true;else return false;

	return true;
}
int main(){
	int n;
	cin>>n;
	clr(tengo,0);
	int ceros=0;
	int s=0;
	f(i,0,n){
		cin>>arr[i];
		if(arr[i]==0)ceros++;
		s+=arr[i];
		tengo[arr[i]]++;
	}
	cout<<s<<endl;
	if(ceros==0)cout<<-1<<endl;
	else{
		bool quite=false;
		int queda=s%3;
		if(queda==0){
			if(ceros==n)
			cout<<0<<endl;
			else
			imp();
			return 0;
		}
		else{
			if(uno(queda)!=-1){
				imp();
				return 0;
			}
			else{
				vector <string> v;
				string cad;
				//f(i,0,10)cout<<tengo[i]<<" ";
				//cout<<endl;
				//cout<<"ad"<<endl;
				f(i,1,10)f(j,1,10){
					tengo[i]--;
					tengo[j]--;
					if(tengo[i]>=0 && tengo[j]>=0 && (s-i-j)%3==0 && (s-i-j)>0){
					//	cout<<i<<" "<<j<<endl;
						cad=gera();
						v.pb(cad);
					}
					tengo[i]++;
					tengo[j]++;
				}

				if(v.size()==0)cout<<-1<<endl;
				else{
				sort(all(v),com);
				cout<<v[0]<<endl;
				}
			}
		}

	}
	return 0;
}


#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<map>
#include<numeric>
#include<sstream>
#include<cmath>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).end,(v).begin
#define pb push_back
#define f(i,x,y) for(int i=x;i<y;i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin();it!=A.end();it++)
#define sqr(x) (x)*(x)
#define mp make_pair
#define clr(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
int tengo[103];
int arr[100003];
void imp(){
	bool es=true;
	f(i,1,10)if(tengo[i]!=0)es=false;
	if(es)cout<<0<<endl;
	for(int i=9;i>=0;i--){
						while(tengo[i]){
							cout<<i;
							tengo[i]--;
						}
					}
	cout<<endl;
}
int uno(int fa){
	f(i,1,10){
		if(i%3==fa && tengo[i]){
			tengo[i]--;
			return i;
		}
	}
	return -1;
}
string gera(){
	string r="";
	char c;
	int aa;
	for(int i=9;i>=0;i--){
		aa=tengo[i];
		while(aa){
			c='0'+i;
			r+=c;
			aa--;
			}
	}
	//while(r.size>0 && )
	return r;
}
bool com(string cad1,string cad2){
	if(cad1.size()!=cad2.size())return cad1.size()>cad2.size();
	f(i,0,cad1.size())if(cad1[i]>cad2[i])return true;else return false;

	return true;
}
int main(){
	int n;
	cin>>n;
	clr(tengo,0);
	int ceros=0;
	int s=0;
	f(i,0,n){
		cin>>arr[i];
		if(arr[i]==0)ceros++;
		s+=arr[i];
		tengo[arr[i]]++;
	}
//	cout<<s<<endl;
	if(ceros==0)cout<<-1<<endl;
	else{
		bool quite=false;
		int queda=s%3;
		if(queda==0){
			if(ceros==n)
			cout<<0<<endl;
			else
			imp();
			return 0;
		}
		else{
			if(uno(queda)!=-1){
				imp();
				return 0;
			}
			else{
				vector <string> v;
				string cad;
				//f(i,0,10)cout<<tengo[i]<<" ";
				//cout<<endl;
				//cout<<"ad"<<endl;
				f(i,1,10)f(j,1,10){
					tengo[i]--;
					tengo[j]--;
					if(tengo[i]>=0 && tengo[j]>=0 && (s-i-j)%3==0 && (s-i-j)>0){
					//	cout<<i<<" "<<j<<endl;
						cad=gera();
						v.pb(cad);
					}
					tengo[i]++;
					tengo[j]++;
				}

				if(v.size()==0)cout<<-1<<endl;
				else{
				sort(all(v),com);
				cout<<v[0]<<endl;
				}
			}
		}

	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

vector <int> v;
bool p[100005];

int main()
{
    int n,x;
    cin >> n;
    bool flag = false;
    int sm = 0;
    while ( n-- ) {
          cin >> x;
          if ( x == 0 ) flag = true;
          v.push_back(x);
          sm = (sm + x)%3;
    }
    if ( !flag ) {
         cout << "-1" << endl;
         return 0;
    }
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());
    if ( v[0] == 0 ) {
       cout << "0" << endl;
       return 0;
    }
    else {
         n = (int)v.size();
         if ( sm == 1 ) {
            for ( int i = n-1; i >= 0; i-- ) {
                if ( v[i]%3 == 1 ) {
                     p[i] = true;
                     goto p1;
                }
            }
            int cnt = 0;
            for ( int i = n-1; i >= 0; i-- ) {
                if ( v[i]%3 == 2 ) {
                     p[i] = true;
                     cnt++;
                     if ( cnt == 2 ) goto p1;
                }
            }
         }
         else {
              for ( int i = n-1; i >= 0; i-- ) {
                  if ( v[i]%3 == 2 )  {
                       p[i] = true;
                       goto p1;
                  }
              }
              int cnt = 0;
              for ( int i = n-1; i >= 0; i-- ) {
                  if ( v[i]%3 == 1 ) {
                       p[i] = true;
                       cnt++;
                       if ( cnt == 2 ) goto p1;
                  }
              }
         }
    }
    p1: { }
    for ( int i = 0; i < n; i++ ) if ( !p[i] ) cout << v[i];
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

static bool b[100002];

int main()
{
    memset(b, false, sizeof(b));

    int n,x,i,c=0,sum=0,k;
    vector <int> v,v1;

    cin >> n;

    for ( i = 0; i < n; i++ ) {
        cin >> x;
        sum += x;
        if ( x == 0 ) c++;
        else v.push_back(x);
    }

    sort(v.begin(),v.end());
    if ( c == 0 ) {
       printf("-1\n");
       return 0;
    }

    else if ( sum%3 == 0 ) {
       for ( i = v.size()-1; i >= 0; i-- ) v1.push_back(v[i]);
    }
    else if ( sum%3 == 1 ) {
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 1 ) {
                b[i] = true;
                goto p;
             }
         }
         k = 0;
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 2 ) {
                k++;
                b[i] = true;
                if ( k == 2 ) goto p;
             }
         }
         printf("-1\n");
         return 0;
         p: { }
         for ( i = v.size()-1; i >= 0; i-- ) {
             if ( b[i] == false ) v1.push_back(v[i]);
         }
    }
    else {
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 2 ) {
                b[i] = true;
                goto q;
             }
         }
         k = 0;
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 1 ) {
                k++;
                b[i] = true;
                if ( k == 2 ) goto p;
             }
         }
         printf("-1\n");
         return 0;
         q: { }
         for ( i = v.size()-1; i >= 0; i-- ) {
             if ( b[i] == false ) v1.push_back(v[i]);
         }
    }

    if ( v1.size() == 0 ) {
       printf("0\n");
       return 0;
    }
    for ( i = 0; i < v1.size(); i++ ) cout << v1[i];
    while ( c-- ) cout << "0";
    cout << endl;

    return 0;

}


#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

vector <int> v;
bool p[100005];

int main()
{
    int n,x;
    cin >> n;
    bool flag = false;
    int sm = 0;
    while ( n-- ) {
          cin >> x;
          if ( x == 0 ) flag = true;
          v.push_back(x);
          sm = (sm + x)%3;
    }
    if ( !flag ) {
         cout << "-1" << endl;
         return 0;
    }
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());
    if ( v[0] == 0 ) {
       cout << "0" << endl;
       return 0;
    }
    else {
         n = (int)v.size();
         if ( sm == 1 ) {
            for ( int i = n-1; i >= 0; i-- ) {
                if ( v[i]%3 == 1 ) {
                     p[i] = true;
                     goto p1;
                }
            }
            int cnt = 0;
            for ( int i = n-1; i >= 0; i-- ) {
                if ( v[i]%3 == 2 ) {
                     p[i] = true;
                     cnt++;
                     if ( cnt == 2 ) goto p1;
                }
            }
         }
         else if ( sm == 2 ) {
              for ( int i = n-1; i >= 0; i-- ) {
                  if ( v[i]%3 == 2 )  {
                       p[i] = true;
                       goto p1;
                  }
              }
              int cnt = 0;
              for ( int i = n-1; i >= 0; i-- ) {
                  if ( v[i]%3 == 1 ) {
                       p[i] = true;
                       cnt++;
                       if ( cnt == 2 ) goto p1;
                  }
              }
         }
    }
    p1: { }
    vector <int> ans;
    for ( int i = 0; i < n; i++ ) if ( !p[i] ) ans.push_back(v[i]);
    sort(ans.begin(),ans.end());
    for ( int i = n-1; i >= 0; i-- ) cout << v[i];
    return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

vector <int> v1,v2;

int main()
{
    string ans="";
    int n,x;
    cin >> n;
    while ( n-- ) {
          cin >> x;
          if ( x%3 == 0 ) ans += (char)(x+48);
          else {
               if ( x%3 == 1 ) v1.push_back(x);
               else v2.push_back(x);
          }
    }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    int idx1 = (int)v1.size();
    int idx2 = (int)v2.size();
    idx1--, idx2--;
    while ( idx1 >= 0 && idx2 >= 0 ) {
          ans.push_back((char)(v1[idx1]+48));
          ans.push_back((char)(v2[idx2]+48));
          idx1--;
          idx2--;
    }
    while ( idx1-2 >= 0 ) {
          for ( int j = idx1; j >= idx1-2; j-- ) ans.push_back((char)(v1[j]+48));
          idx1 -= 3;
    }
    while ( idx2-2 >= 0 ) {
          for ( int j = idx2; j >= idx2-2; j-- ) ans.push_back((char)(v2[j]+48));
          idx2 -= 3;
    }
    sort(ans.begin(),ans.end());
    reverse(ans.begin(),ans.end());
    if ( ans[0] == '0' ) ans = "0";
    int val = 0;
    for ( int i = 0; i < ans.size(); i++ ) val = (val*10 + ans[i]-48)%30;
    if ( val != 0 ) cout << "-1" << endl;
    else cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

vector <int> v;
bool p[100005];

int main()
{
    int n,x;
    cin >> n;
    bool flag = false;
    int sm = 0;
    while ( n-- ) {
          cin >> x;
          if ( x == 0 ) flag = true;
          v.push_back(x);
          sm = (sm + x)%3;
    }
    if ( !flag ) {
         cout << "-1" << endl;
         return 0;
    }
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());
    if ( v[0] == 0 ) {
       cout << "0" << endl;
       return 0;
    }
    else {
         n = (int)v.size();
         if ( sm == 1 ) {
            for ( int i = n-1; i >= 0; i-- ) {
                if ( v[i]%3 == 1 ) {
                     p[i] = true;
                     goto p1;
                }
            }
            int cnt = 0;
            for ( int i = n-1; i >= 0; i-- ) {
                if ( v[i]%3 == 2 ) {
                     p[i] = true;
                     cnt++;
                     if ( cnt == 2 ) goto p1;
                }
            }
         }
         else if ( sm == 2) {
              for ( int i = n-1; i >= 0; i-- ) {
                  if ( v[i]%3 == 2 )  {
                       p[i] = true;
                       goto p1;
                  }
              }
              int cnt = 0;
              for ( int i = n-1; i >= 0; i-- ) {
                  if ( v[i]%3 == 1 ) {
                       p[i] = true;
                       cnt++;
                       if ( cnt == 2 ) goto p1;
                  }
              }
         }
    }
    p1: { }
    for ( int i = 0; i < n; i++ ) if ( !p[i] ) cout << v[i];
    return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

vector <int> v;
bool p[100005];

int main()
{
    int n,x;
    cin >> n;
    bool flag = false;
    int sm = 0;
    while ( n-- ) {
          cin >> x;
          if ( x == 0 ) flag = true;
          v.push_back(x);
          sm = (sm + x)%3;
    }
    if ( !flag ) {
         cout << "-1" << endl;
         return 0;
    }
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());
    if ( v[0] == 0 ) {
       cout << "0" << endl;
       return 0;
    }
    else {
         n = (int)v.size();
         if ( sm == 1 ) {
            for ( int i = n-1; i >= 0; i-- ) {
                if ( v[i]%3 == 1 ) {
                     p[i] = true;
                     goto p1;
                }
            }
            int cnt = 0;
            for ( int i = n-1; i >= 0; i-- ) {
                if ( v[i]%3 == 2 ) {
                     p[i] = true;
                     cnt++;
                     if ( cnt == 2 ) goto p1;
                }
            }
         }
         else if ( sm == 2 ) {
              for ( int i = n-1; i >= 0; i-- ) {
                  if ( v[i]%3 == 2 )  {
                       p[i] = true;
                       goto p1;
                  }
              }
              int cnt = 0;
              for ( int i = n-1; i >= 0; i-- ) {
                  if ( v[i]%3 == 1 ) {
                       p[i] = true;
                       cnt++;
                       if ( cnt == 2 ) goto p1;
                  }
              }
         }
    }
    p1: { }
    vector <int> ans;
    for ( int i = 0; i < n; i++ ) if ( !p[i] ) ans.push_back(v[i]);
    sort(ans.begin(),ans.end());
    for ( int i = (int)(ans.size())-1; i >= 0; i-- ) cout << ans[i];
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

static bool b[100002];

int main()
{
    memset(b, false, sizeof(b));

    int n,x,i,c=0,sum=0,k,l;
    vector <int> v,v1;

    cin >> n;

    for ( i = 0; i < n; i++ ) {
        cin >> x;
        sum += x;
        if ( x == 0 ) c++;
        else v.push_back(x);
    }

    sort(v.begin(),v.end());
    if ( c == 0 ) {
       printf("-1\n");
       return 0;
    }

    else if ( sum%3 == 0 ) {
       for ( i = v.size()-1; i >= 0; i-- ) v1.push_back(v[i]);
    }
    else if ( sum%3 == 1 ) {
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 1 ) {
                b[i] = true;
                goto p;
             }
         }
         k = 0;
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 2 ) {
                k++;
                b[i] = true;
                if ( k == 2 ) goto p;
             }
         }
         printf("-1\n");
         return 0;
         p: { }
         for ( i = v.size()-1; i >= 0; i-- ) {
             if ( b[i] == false ) v1.push_back(v[i]);
         }
    }
    else {
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 2 ) {
                b[i] = true;
                goto q;
             }
         }
         k = 0;
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 1 ) {
                k++;
                b[i] = true;
                if ( k == 2 ) goto p;
             }
         }
         printf("-1\n");
         return 0;
         q: { }
         for ( i = v.size()-1; i >= 0; i-- ) {
             if ( b[i] == false ) v1.push_back(v[i]);
         }
    }

    if ( v1.size() == 0 ) {
       printf("0\n");
       return 0;
    }
    for ( i = 0; i < v1.size(); i++ ) cout << v1[i];
    while ( c-- ) cout << "0";
    cout << endl;

    return 0;

}


#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

vector <int> v;
bool p[100005];

int main()
{
    int n,x;
    cin >> n;
    int sm = 0;
    bool flag = false;
    while ( n-- ) {
          cin >> x;
          if ( x == 0 ) flag = true;
          v.push_back(x);
          sm = (sm + x)%3;
    }
    if ( !flag ) {
         cout << "-1" << endl;
         return 0;
    }
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());
    n = (int)v.size();
    if ( sm == 1 ) {
    for ( int i = n-1; i >= 0; i-- ) {
        if ( v[i]%3 == 1 ) {
             p[i] = true;
             goto p1;
        }
    }
    int cnt = 0;
    for ( int i = n-1; i >= 0; i-- ) {
        if ( v[i]%3 == 2 ) {
             p[i] = true;
             cnt++;
             if ( cnt == 2 ) goto p1;
        }
    }
    }
    else if ( sm == 2 ) {
      for ( int i = n-1; i >= 0; i-- ) {
          if ( v[i]%3 == 2 )  {
               p[i] = true;
               goto p1;
          }
      }
      int cnt = 0;
      for ( int i = n-1; i >= 0; i-- ) {
          if ( v[i]%3 == 1 ) {
               p[i] = true;
               cnt++;
               if ( cnt == 2 ) goto p1;
          }
      }
    }
    p1: { }
    vector <int> ans;
    for ( int i = 0; i < n; i++ ) {
        if ( !p[i] ) ans.push_back(v[i]);
    }
    sort(ans.begin(),ans.end());
    int i = (int)(ans.size())-1;
    if ( ans[i] == 0 ) {
       cout << "0";
       while ( ans[i] == 0 && i >= 0 ) i--;
    }
    for ( int j = i; j >= 0; j-- ) cout << ans[j];
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

static bool b[100002];

int main()
{
    memset(b, false, sizeof(b));

    int n,x,i,c=0,sum=0,k;
    vector <int> v;

    cin >> n;

    for ( i = 0; i < n; i++ ) {
        cin >> x;
        sum += x;
        if ( x == 0 ) c++;
        else v.push_back(x);
    }

    sort(v.begin(),v.end());
    if ( c == 0 ) {
       printf("-1\n");
       return 0;
    }

    else if ( sum%3 == 0 ) {
       for ( i = v.size()-1; i >= 0; i-- ) cout << v[i];
    }
    else if ( sum%3 == 1 ) {
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 1 ) {
                b[i] = true;
                goto p;
             }
         }
         k = 0;
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 2 ) {
                k++;
                b[i] = true;
                if ( k == 2 ) goto p;
             }
         }
         printf("-1\n");
         return 0;
         p: { }
         for ( i = v.size()-1; i >= 0; i-- ) {
             if ( b[i] == false ) cout << v[i];
         }
    }
    else {
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 2 ) {
                b[i] = true;
                goto q;
             }
         }
         k = 0;
         for ( i = 0; i < v.size(); i++ ) {
             if ( v[i]%3 == 1 ) {
                k++;
                b[i] = true;
                if ( k == 2 ) goto p;
             }
         }
         printf("-1\n");
         return 0;
         q: { }
         for ( i = v.size()-1; i >= 0; i-- ) {
             if ( b[i] == false ) cout << v[i];
         }
    }

    while ( c-- ) cout << "0";
    cout << endl;

    return 0;

}


#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

vector <int> v1,v2;

int main()
{
    string ans="";
    int n,x;
    cin >> n;
    while ( n-- ) {
          cin >> x;
          if ( x%3 == 0 ) ans += (char)(x+48);
          else {
               if ( x%3 == 1 ) v1.push_back(x);
               else v2.push_back(x);
          }
    }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    int idx1 = (int)v1.size();
    int idx2 = (int)v2.size();
    idx1--, idx2--;
    while ( idx1 >= 0 && idx2 >= 0 ) {
          ans.push_back((char)(v1[idx1]+48));
          ans.push_back((char)(v2[idx2]+48));
          idx1--;
          idx2--;
    }
    while ( idx1-2 >= 0 ) {
          for ( int j = idx1; j >= idx1-2; j-- ) ans.push_back((char)(v1[idx1]+48));
          idx1 -= 3;
    }
    sort(ans.begin(),ans.end());
    reverse(ans.begin(),ans.end());
    if ( ans[0] == '0' ) ans = "0";
    int val = 0;
    for ( int i = 0; i < ans.size(); i++ ) val = (val*10 + ans[i]-48)%30;
    if ( val != 0 ) cout << "-1" << endl;
    else cout << ans << endl;
    return 0;
}


#include<cstdio>
#include<assert.h>
#include<vector>
#include<algorithm>

std::vector<int> one, two, ans;

int Max(int a, int b){return a<b ?a:b;}

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

    for (int i=0; i<n; i++){
        scanf("%d", &d);
        assert(d >= 0 && d <= 9);
        if (d % 3 == 0) ans.push_back(d);
        else if (d % 3 == 1)    one.push_back(d);
        else if (d % 3 == 2)    two.push_back(d);
    }

    std::sort(one.begin(), one.end());
    std::sort(two.begin(), two.end());

    std::vector<int>::reverse_iterator it = one.rbegin(), jt = two.rbegin();

    if ((one.size() % 3 == 0 && two.size() % 3 == 1) || (one.size() % 3 == 1 && two.size() % 3 == 0)){
        while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }
    else {
        while (it != one.rend() && jt != two.rend()){
            ans.push_back(*it); it++;
            ans.push_back(*jt); jt++;
        }

        while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }

    std::sort(ans.begin(), ans.end());

    if (ans[0] != 0)    puts("-1");
    else if(ans[ans.size() - 1] == 0)   puts("0");
    else {
        for (std::vector<int>::iterator kt=ans.end()-1; kt>=ans.begin(); kt--){
            printf("%d", *kt);
        }
        puts("");
    }
}


#include<cstdio>
#include<vector>
#include<algorithm>

std::vector<int> one, two, ans1, ans2, ans;

int Max(int a, int b){return a<b ?a:b;}

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

    for (int i=0; i<n; i++){
        scanf("%d", &d);
        if (d % 3 == 0){
            ans1.push_back(d);
            ans2.push_back(d);
        }
        else if (d % 3 == 1)    one.push_back(d);
        else if (d % 3 == 2)    two.push_back(d);
    }

    std::sort(one.begin(), one.end());
    std::sort(two.begin(), two.end());

    int my_cnt = Max(one.size() % 3, two.size() % 3);

    for (std::vector<int>::iterator it=one.begin() + one.size() % 3 - my_cnt; it!=one.end(); it++){
        ans1.push_back(*it);
    }
    for (std::vector<int>::iterator it=two.begin() + two.size() % 3 - my_cnt; it!=two.end(); it++){
        ans1.push_back(*it);
    }

    std::vector<int>::reverse_iterator it = one.rbegin(), jt = two.rbegin();

    while (it != one.rend() && jt != two.rend()){
        ans2.push_back(*it); it++;
        ans2.push_back(*jt); jt++;
    }

    while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend()){
        ans2.push_back(*it);
        ans2.push_back(*(it+1));
        ans2.push_back(*(it+2));
        it += 3;
    }

    while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend()){
        ans2.push_back(*jt);
        ans2.push_back(*(jt+1));
        ans2.push_back(*(jt+2));
        jt += 3;
    }

    if (ans1.size() < ans2.size())  ans = ans2;
    else    ans = ans1;
    std::sort(ans.begin(), ans.end());

    if (ans[0] != 0)    puts("-1");
    else if(ans[ans.size() - 1] == 0)   puts("0");
    else {
        for (std::vector<int>::iterator kt=ans.end()-1; kt>=ans.begin(); kt--){
            printf("%d", *kt);
        }
        puts("");
    }
}


#include<cstdio>
#include<vector>
#include<algorithm>

std::vector<int> one, two, ans;

int Max(int a, int b){return a<b ?a:b;}

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

    for (int i=0; i<n; i++){
        scanf("%d", &d);
        if (d % 3 == 0) ans.push_back(d);
        else if (d % 3 == 1)    one.push_back(d);
        else if (d % 3 == 2)    two.push_back(d);
    }

    std::sort(one.begin(), one.end());
    std::sort(two.begin(), two.end());

    std::vector<int>::iterator it = one.begin(), jt = two.begin();

    while (it != one.end() && jt != two.end()){
        ans.push_back(*it); it++;
        ans.push_back(*jt); jt++;
    }

    while (it != one.end() && it + 1 != one.end() && it + 2 != one.end()){
        ans.push_back(*it);
        ans.push_back(*(it+1));
        ans.push_back(*(it+2));
        it += 3;
    }

    while (jt != two.end() && jt + 1 != two.end() && jt + 2 != two.end()){
        ans.push_back(*jt);
        ans.push_back(*(jt+1));
        ans.push_back(*(jt+2));
        jt += 3;
    }

    std::sort(ans.begin(), ans.end());

    if (ans[0] != 0)    puts("-1");
    else if(ans[ans.size() - 1] == 0)   puts("0");
    else {
        for (std::vector<int>::iterator kt=ans.end()-1; kt>=ans.begin(); kt--){
            printf("%d", *kt);
        }
        puts("");
    }
}


#include<cstdio>
#include<vector>
#include<algorithm>

std::vector<int> one, two, ans;

int Max(int a, int b){return a<b ?a:b;}

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

    for (int i=0; i<n; i++){
        scanf("%d", &d);
        if (d % 3 == 0) ans.push_back(d);
        else if (d % 3 == 1)    one.push_back(d);
        else if (d % 3 == 2)    two.push_back(d);
    }

    std::sort(one.begin(), one.end());
    std::sort(two.begin(), two.end());

    std::vector<int>::iterator it = one.begin(), jt = two.begin();

    if ((one.size() % 3 == 0 && two.size() % 3 == 1) || (one.size() % 3 == 1 && two.size() % 3 == 0)){
        while (it != one.end() && it + 1 != one.end() && it + 2 != one.end()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.end() && jt + 1 != two.end() && jt + 2 != two.end()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }
    else {
        while (it != one.end() && jt != two.end()){
            ans.push_back(*it); it++;
            ans.push_back(*jt); jt++;
        }

        while (it != one.end() && it + 1 != one.end() && it + 2 != one.end()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.end() && jt + 1 != two.end() && jt + 2 != two.end()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }

    std::sort(ans.begin(), ans.end());

    if (ans[0] != 0)    puts("-1");
    else if(ans[ans.size() - 1] == 0)   puts("0");
    else {
        for (std::vector<int>::iterator kt=ans.end()-1; kt>=ans.begin(); kt--){
            printf("%d", *kt);
        }
        puts("");
    }
}


#include<cstdio>
#include<vector>
#include<algorithm>

std::vector<int> one, two, ans;

int Max(int a, int b){return a<b ?a:b;}

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

    for (int i=0; i<n; i++){
        scanf("%d", &d);
        if (d % 3 == 0) ans.push_back(d);
        else if (d % 3 == 1)    one.push_back(d);
        else if (d % 3 == 2)    two.push_back(d);
    }

    std::sort(one.begin(), one.end());
    std::sort(two.begin(), two.end());

    std::vector<int>::reverse_iterator it = one.rbegin(), jt = two.rbegin();

    if ((one.size() % 3 == 0 && two.size() % 3 == 1) || (one.size() % 3 == 1 && two.size() % 3 == 0)){
        while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }
    else {
        while (it != one.rend() && jt != two.rend()){
            ans.push_back(*it); it++;
            ans.push_back(*jt); jt++;
        }

        while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }

    std::sort(ans.begin(), ans.end());

    if (ans[0] != 0)    puts("-1");
    else if(ans[ans.size() - 1] == 0)   puts("0");
    else {
        for (std::vector<int>::iterator kt=ans.end()-1; kt>=ans.begin(); kt--){
            printf("%d", *kt);
        }
        puts("");
    }
}


#include<cstdio>
#include<vector>
#include<algorithm>

std::vector<int> one, two, ans;

int Max(int a, int b){return a<b ?a:b;}

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

    for (int i=0; i<n; i++){
        scanf("%d", &d);
        if (d % 3 == 0) ans.push_back(d);
        else if (d % 3 == 1)    one.push_back(d);
        else if (d % 3 == 2)    two.push_back(d);
    }

    std::sort(one.begin(), one.end());
    std::sort(two.begin(), two.end());

    int my_cnt = Max(one.size() % 3, two.size() % 3);

    for (std::vector<int>::iterator it=one.begin() + one.size() % 3 - my_cnt; it!=one.end(); it++){
        ans.push_back(*it);
    }
    for (std::vector<int>::iterator it=two.begin() + two.size() % 3 - my_cnt; it!=two.end(); it++){
        ans.push_back(*it);
    }
    std::sort(ans.begin(), ans.end());

    if (ans[0] != 0)    puts("-1");
    else {
        for (std::vector<int>::iterator it=ans.end()-1; it>=ans.begin(); it--){
            printf("%d", *it);
        }
        puts("");
    }
}


#include<cstdio>
#include<vector>
#include<algorithm>

std::vector<int> one, two, ans;

int Max(int a, int b){return a<b ?a:b;}

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

    for (int i=0; i<n; i++){
        scanf("%d", &d);
        if (d % 3 == 0) ans.push_back(d);
        else if (d % 3 == 1)    one.push_back(d);
        else if (d % 3 == 2)    two.push_back(d);
    }

    std::sort(one.begin(), one.end());
    std::sort(two.begin(), two.end());

    int my_cnt = Max(one.size() % 3, two.size() % 3);

    for (std::vector<int>::iterator it=one.begin() + one.size() % 3 - my_cnt; it!=one.end(); it++){
        ans.push_back(*it);
    }
    for (std::vector<int>::iterator it=two.begin() + two.size() % 3 - my_cnt; it!=two.end(); it++){
        ans.push_back(*it);
    }
    std::sort(ans.begin(), ans.end());

    if (ans[0] != 0)    puts("-1");
    else if(ans[ans.size() - 1] == 0)   puts("0");
    else {
        for (std::vector<int>::iterator it=ans.end()-1; it>=ans.begin(); it--){
            printf("%d", *it);
        }
        puts("");
    }
}


#include<cstdio>
#include<vector>
#include<algorithm>

std::vector<int> one, two, ans;

int Max(int a, int b){return a<b ?a:b;}

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

    for (int i=0; i<n; i++){
        scanf("%d", &d);
        if (d % 3 == 0) ans.push_back(d);
        else if (d % 3 == 1)    one.push_back(d);
        else if (d % 3 == 2)    two.push_back(d);
    }

    std::sort(one.begin(), one.end());
    std::sort(two.begin(), two.end());

    std::vector<int>::iterator it = one.rbegin(), jt = two.rbegin();

    if ((one.size() % 3 == 0 && two.size() % 3 == 1) || (one.size() % 3 == 1 && two.size() % 3 == 0)){
        while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }
    else {
        while (it != one.rend() && jt != two.rend()){
            ans.push_back(*it); it++;
            ans.push_back(*jt); jt++;
        }

        while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }

    std::sort(ans.begin(), ans.end());

    if (ans[0] != 0)    puts("-1");
    else if(ans[ans.size() - 1] == 0)   puts("0");
    else {
        for (std::vector<int>::iterator kt=ans.end()-1; kt>=ans.begin(); kt--){
            printf("%d", *kt);
        }
        puts("");
    }
}


#include<cstdio>
#include<vector>
#include<algorithm>

std::vector<int> one, two, ans;

int Max(int a, int b){return a<b ?a:b;}

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

    for (int i=0; i<n; i++){
        scanf("%d", &d);
        if (d % 3 == 0) ans.push_back(d);
        else if (d % 3 == 1)    one.push_back(d);
        else if (d % 3 == 2)    two.push_back(d);
    }

    std::sort(one.begin(), one.end());
    std::sort(two.begin(), two.end());

    std::vector<int>::reverse_iterator it = one.rbegin(), jt = two.rbegin();

    if ((one.size() % 3 == 0 && two.size() % 3 == 1) || (one.size() % 3 == 1 && two.size() % 3 == 0)){
        while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }
    else {
        while (it != one.rend() && jt != two.rend()){
            ans.push_back(*it); it++;
            ans.push_back(*jt); jt++;
        }

        while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }

    std::sort(ans.begin(), ans.end());

    if (ans.size() == 0 || ans[0] != 0)    puts("-1");
    else if(ans[ans.size() - 1] == 0)   puts("0");
    else {
        for (std::vector<int>::iterator kt=ans.end()-1; kt>=ans.begin(); kt--){
            printf("%d", *kt);
        }
        puts("");
    }
}


#include<cstdio>
#include<vector>
#include<algorithm>

std::vector<int> one, two, ans1, ans2, ans;

int Max(int a, int b){return a<b ?a:b;}

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

    for (int i=0; i<n; i++){
        scanf("%d", &d);
        if (d % 3 == 0){
            ans1.push_back(d);
            ans2.push_back(d);
        }
        else if (d % 3 == 1)    one.push_back(d);
        else if (d % 3 == 2)    two.push_back(d);
    }

    std::sort(one.begin(), one.end());
    std::sort(two.begin(), two.end());

    int my_cnt = Max(one.size() % 3, two.size() % 3);

    for (std::vector<int>::iterator it=one.begin() + one.size() % 3 - my_cnt; it!=one.end(); it++){
        ans1.push_back(*it);
    }
    for (std::vector<int>::iterator it=two.begin() + two.size() % 3 - my_cnt; it!=two.end(); it++){
        ans1.push_back(*it);
    }

    std::vector<int>::iterator it = one.begin(), jt = two.begin();

    while (it != one.end() && jt != two.end()){
        ans2.push_back(*it); it++;
        ans2.push_back(*jt); jt++;
    }

    while (it != one.end() && it + 1 != one.end() && it + 2 != one.end()){
        ans2.push_back(*it);
        ans2.push_back(*(it+1));
        ans2.push_back(*(it+2));
        it += 3;
    }

    while (jt != two.end() && jt + 1 != two.end() && jt + 2 != two.end()){
        ans2.push_back(*jt);
        ans2.push_back(*(jt+1));
        ans2.push_back(*(jt+2));
        jt += 3;
    }

    if (ans1.size() < ans2.size())  ans = ans2;
    else    ans = ans1;
    std::sort(ans.begin(), ans.end());

    if (ans[0] != 0)    puts("-1");
    else if(ans[ans.size() - 1] == 0)   puts("0");
    else {
        for (std::vector<int>::iterator kt=ans.end()-1; kt>=ans.begin(); kt--){
            printf("%d", *kt);
        }
        puts("");
    }
}


#include<cstdio>
#include<vector>
#include<algorithm>

std::vector<int> one, two, ans;

int Max(int a, int b){return a<b ?a:b;}

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

    for (int i=0; i<n; i++){
        scanf("%d", &d);
        if (d % 3 == 0) ans.push_back(d);
        else if (d % 3 == 1)    one.push_back(d);
        else if (d % 3 == 2)    two.push_back(d);
    }

    std::sort(one.begin(), one.end());
    std::sort(two.begin(), two.end());

    std::vector<int>::reverse_iterator it = one.rbegin(), jt = two.rbegin();

    while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend() && it + 3 != one.rend()){
        ans.push_back(*it);
        ans.push_back(*(it+1));
        ans.push_back(*(it+2));
        it += 3;
    }

    while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend() && jt + 3 != two.rend()){
        ans.push_back(*jt);
        ans.push_back(*(jt+1));
        ans.push_back(*(jt+2));
        jt += 3;
    }

    if ((one.size() % 3 == 0 && two.size() % 3 == 1) || (one.size() % 3 == 1 && two.size() % 3 == 0)){
        while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }
    else {
        while (it != one.rend() && jt != two.rend()){
            ans.push_back(*it); it++;
            ans.push_back(*jt); jt++;
        }

        while (it != one.rend() && it + 1 != one.rend() && it + 2 != one.rend()){
            ans.push_back(*it);
            ans.push_back(*(it+1));
            ans.push_back(*(it+2));
            it += 3;
        }

        while (jt != two.rend() && jt + 1 != two.rend() && jt + 2 != two.rend()){
            ans.push_back(*jt);
            ans.push_back(*(jt+1));
            ans.push_back(*(jt+2));
            jt += 3;
        }
    }

    std::sort(ans.begin(), ans.end());

    if (ans.size() == 0 || ans[0] != 0)    puts("-1");
    else if(ans[ans.size() - 1] == 0)   puts("0");
    else {
        for (std::vector<int>::iterator kt=ans.end()-1; kt>=ans.begin(); kt--){
            printf("%d", *kt);
        }
        puts("");
    }
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(s==0)
        {
            printf("0\n");
            continue;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        int t=0;
       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t) continue;
       for(i=1;i<9;i++)
       for(j=1;j<9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0)
           {
               mp[i]--;mp[j]--;
            for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
                   j=10;
               }
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1)
           {
               mp[i]=mp[i]-2;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
                   //return 0;
               }
               i=10;
                   break;
           }
       }
       printf("\n");

    }
    return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
	if(a%b==0)
		return b;
	else
		gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
	long n,i,k,s,x;
	while(scanf("%ld",&n)!=EOF)
	{
		s=0;x=0;
		int f=0;
		int a[100005]={0};
		map<int,int> mp;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			if(a[i]==0)
			f=1;
			  mp[a[i]]++;

		}
		k=0;
        k=MIN(mp[8],mp[7]);
		mp[8]-=k;
		mp[7]-=k;
	//	printf("%d\n",k);

		k=MIN(mp[7],mp[5]);
		mp[7]-=k;
		mp[5]-=k;
	//	printf("%d\n",k);

		k=MIN(mp[8],mp[3]);
		mp[8]-=k;
		mp[3]-=k;

	//	printf("%d\n",k);

		k=MIN(mp[7],mp[2]);
		mp[7]-=k;
		mp[2]-=k;
	//	printf("%d\n",k);

		k=MIN(mp[5],mp[4]);
		mp[5]-=k;
		mp[4]-=k;
		//printf("%d\n",k);
		k=MIN(mp[4],mp[2]);
		mp[4]-=k;
		mp[2]-=k;

		mp[2]%=3;
		mp[4]%=3;
		mp[5]%=3;
		mp[7]%=3;
		mp[8]%=3;

		k=0;
   sort(a,a+n);
   //cout<<mp[5]<<"jj";
   int x=0;
   if(f){
    for(i=n-1;i>=0;i--)
	{
	  if(a[i]%3!=0&&mp[a[i]]>0)
	   {

		  k=mp[a[i]];
		   mp[a[i]]=0;
		   i-=k-1;
	   }
	  else
	  {
	      f=0;
	      printf("%ld",a[i]);
	  }

	}
	if(f)
	printf("-1");
	printf("\n");
   }
   else printf("-1\n");
	}
	return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        if(s==0)
        {
            printf("0\n");
            continue;
        }
        if(s%3==0)
        {
             for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
            continue;
        }

        int t=0,g=0;

       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t)
       continue;
       for(i=1;i<=9;i++)
       for(j=1;j<=9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0)
           {
               t=1;
               mp[i]--;mp[j]--;
            for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   j=0;
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1&&n>3)
           {
               t=1;
               mp[i]=mp[i]-2;
               for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   //return 0;
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
               i=10;
                   break;
           }
       }

       if(t==0) printf("-1");
       printf("\n");

    }
    return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        if(s==0)
        {
            printf("0\n");
            continue;
        }
        if(s%3==0)
        {
             for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
            continue;
        }

        int t=0,g=0;

       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t) continue;

       for(i=1;i<9;i++)
       for(j=1;j<9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0&&n>2)
           {
               mp[i]--;mp[j]--;
            for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   j=10;
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1&&n>3)
           {
               mp[i]=mp[i]-2;
               for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   //return 0;
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
               i=10;
                   break;
           }
       }
      // if(g==0) printf("-1");
       printf("\n");

    }
    return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        if(s%3==0)
        {
             for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
            continue;
        }

        int t=0;
       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t) continue;
       for(i=1;i<9;i++)
       for(j=1;j<9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0&&n>2)
           {
               mp[i]--;mp[j]--;
            for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
                   j=10;
               }
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1&&n>3)
           {
               mp[i]=mp[i]-2;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
                   //return 0;
               }
               i=10;
                   break;
           }
       }
       printf("\n");

    }
    return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        if(s==0)
        {
            printf("0\n");
            continue;
        }
        if(s%3==0)
        {
             for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
            continue;
        }

        int t=0,g=0;

       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t)
       continue;
       for(i=1;i<=9;i++)
       for(j=1;j<=9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0)
           {
               t=1;
               mp[i]--;mp[j]--;
            for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1)
           {
               t=1;
               mp[i]=mp[i]-2;
               for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
               i=10;
                   break;
           }
       }

       if(t==0) printf("-1");
       printf("\n");

    }
    return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        if(s==0)
        {
            printf("0\n");
            continue;
        }
        if(s%3==0)
        {
             for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
            continue;
        }

        int t=0,g=0;

       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t) continue;

       for(i=1;i<9;i++)
       for(j=1;j<9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0&&n>2)
           {
               mp[i]--;mp[j]--;
            for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   j=10;
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1&&n>3)
           {
               mp[i]=mp[i]-2;
               for(j=9;j>=1;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   //return 0;
               }
               if(g==1)
               for(k=0;k<mp[0];k++)
               printf("0");
               else printf("0");
               i=10;
                   break;
           }
       }
       if(g==0) printf("-1");
       printf("\n");

    }
    return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
	if(a%b==0)
		return b;
	else
		gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
	long n,i,k,s,x;
	while(scanf("%ld",&n)!=EOF)
	{
		s=0;x=0;
		int f=0;
		int a[100005]={0};
		map<int,int> mp;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			if(a[i]==0)
			f=1;
			  mp[a[i]]++;

		}
		k=0;
        k=MIN(mp[8],mp[7]);
		mp[8]-=k;
		mp[7]-=k;
	//	printf("%d\n",k);

		k=MIN(mp[7],mp[5]);
		mp[7]-=k;
		mp[5]-=k;
	//	printf("%d\n",k);

		k=MIN(mp[8],mp[3]);
		mp[8]-=k;
		mp[3]-=k;

	//	printf("%d\n",k);

		k=MIN(mp[7],mp[2]);
		mp[7]-=k;
		mp[2]-=k;
	//	printf("%d\n",k);

		k=MIN(mp[5],mp[4]);
		mp[5]-=k;
		mp[4]-=k;
		//printf("%d\n",k);
		k=MIN(mp[4],mp[2]);
		mp[4]-=k;
		mp[2]-=k;
        k=MIN(MIN( MIN(mp[8],mp[7]),mp[5]),mp[4]);
        mp[8]-=k;
		mp[7]-=k;
		mp[5]-=k;
		mp[4]-=k;

         k=MIN(MIN( MIN(mp[8],mp[7]),mp[2]),mp[4]);
        mp[8]-=k;
		mp[7]-=k;
		mp[2]-=k;
		mp[4]-=k;

		mp[2]%=3;
		mp[4]%=3;
		mp[5]%=3;
		mp[7]%=3;
		mp[8]%=3;

		k=0;
   sort(a,a+n);
   //cout<<mp[5]<<"jj";
   int x=0;
   if(f){
    for(i=n-1;i>=0;i--)
	{
	  if(a[i]%3!=0&&mp[a[i]]>0)
	   {

		  k=mp[a[i]];
		   mp[a[i]]=0;
		   i-=k-1;
	   }
	  else
	  {
	      f=0;
	      printf("%ld",a[i]);
	  }

	}
	if(f)
	printf("-1");
	printf("\n");
   }
   else printf("-1\n");
	}
	return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        if(s==0)
        {
            printf("0\n");
            continue;
        }
        if(s%3==0)
        {
             for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
            continue;
        }

        int t=0,g=0;
       if(n>0)
       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
               }
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t) continue;

       for(i=1;i<9;i++)
       for(j=1;j<9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0&&n>2)
           {
               mp[i]--;mp[j]--;
            for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   j=10;
               }
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1&&n>3)
           {
               mp[i]=mp[i]-2;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   //return 0;
               }
               i=10;
                   break;
           }
       }
       if(g==0) printf("-1");
       printf("\n");

    }
    return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        if(s==0)
        {
            printf("0\n");
            continue;
        }
        if(s%3==0)
        {
             for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
            continue;
        }

        int t=0,g=0;
       if(n>2)
       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
               }
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t) continue;
       if(n>3)
       for(i=1;i<9;i++)
       for(j=1;j<9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0&&n>2)
           {
               mp[i]--;mp[j]--;
            for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   j=10;
               }
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1&&n>3)
           {
               mp[i]=mp[i]-2;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++){
                   printf("%ld",j);
                   g=1;
                   }
                   //return 0;
               }
               i=10;
                   break;
           }
       }
       if(g==0) printf("-1");
       printf("\n");

    }
    return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
    long n,i,k,s,x,j;
    while(scanf("%ld",&n)!=EOF)
    {
        s=0;x=0;
        long int f=0;
        long int a;
        map<long int,long int> mp;
        for(i=0;i<n;i++)
        {
            scanf("%ld",&a);
            s+=a;
            if(a==0)
            f=1;
              mp[a]++;
        }
        if(s==0)
        {
            printf("0\n");
            continue;
        }
        if(f==0)
        {
            printf("-1\n");
            continue;
        }
        int t=0;
       for(i=1;i<=9;i++)
       {
           if((s-i)%3==0&&mp[i]>0)
           {
               mp[i]--;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
               }
                 printf("\n");
                i=10;
                t=1;
                   break;
           }
       }
       if(t) continue;
       for(i=1;i<9;i++)
       for(j=1;j<9;j++)
       {
           if(i!=j&&(s-i-j)%3==0&&mp[i]>0&&mp[j]>0&&n>2)
           {
               mp[i]--;mp[j]--;
            for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
                   j=10;
               }
               i=10;
                   break;
           }
           else if(i==j&&(s-i-j)%3==0&&mp[i]>1&&n>3)
           {
               mp[i]=mp[i]-2;
               for(j=9;j>=0;j--)
               {
                   for(k=0;k<mp[j];k++)
                   printf("%ld",j);
                   //return 0;
               }
               i=10;
                   break;
           }
       }
       printf("\n");

    }
    return 0;
}


#include <algorithm>
#include<stdio.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-9
#define VI vector<long>
#define VS vector<string>
#define LL long long
#define MII map<long,long>
#define MSI map<string,long>
#define MIS map<long,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define pi 2*acos(0.0)
#define eps 1e-9

using namespace std;
/*long gcd(long a,long b)
{
	if(a%b==0)
		return b;
	else
		gcd(b%a,a);
}*/
/*double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;

}*/
int main()
{
	long n,i,k;
	while(scanf("%ld",&n)!=EOF)
	{
		int a[100005]={0},b[10]={0},x,r[100005]={0},t=0;
		long s=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&x);
			a[i]=x;
			if(x%3!=0)
				b[x]=b[x]+1;
			else
				r[t++]=x;
		}
		sort(a,a+n);
		if(a[0]>0)
		{
			printf("-1\n");
			continue;
		}
		for(i=1;i<9;i++)
		{
			x=0;
			x=b[i];
			x=x%3;
			x=b[i]-x;
			b[i]=b[i]%3;
			for(k=0;k<x;k++)
			{
				r[t++]=i;
			}
		}
		for(i=8;i>=1;i--)
		{
			for(k=i-1;k>=1;k--)
			{
				x=i+k;
				if(b[i]==2&&b[k]==2&&x%3==0)
				{
					r[t++]=i;
				    r[t++]=k;
					r[t++]=i;
				    r[t++]=k;
					b[i]=0;
					b[k]=0;
				}
				else if(b[i]>0&&b[k]>0&&x%3==0)
				{
					r[t++]=i;
				    r[t++]=k;
					b[i]=b[i]-1;
					b[k]=b[k]-1;
				}
			}
		}
		sort(r,r+t);
		for(i=t-1;i>=0;i--)
			printf("%d",r[i]);
		printf("\n");
	}
	return 0;
}