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


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


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

#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DECIMAL(n)  std::cout << std::fixed;std::cout << std::setprecision(n);
#define hell 1000000007
#define narak 998244353
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define maxc(v) max_element(all(v))
#define minc(v) min_element(all(v))
#define sqr(a) (a)*(a)
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define inputarr(a,n) for(int xxx=0;xxx<n;++xxx) cin>>a[xxx]
#define initarr(a,n,x) for (int xxx=0;xxx<n;++xxx) a[xxx]=x
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define invect(data,n,commands) for(int xxx = 0;xxx<n;xxx++){int tmp;cin>>tmp;data.pb(tmp);commands}
#define inset(data,n,commands) for(int xxx = 0;xxx<n;xxx++){int tmp;cin>>tmp;data.insert(tmp);commands}
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define debug cerr<<"bhau"<<endl

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());

#define ll long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi >
#define vii vector<pii >
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

//primes for hashing 937,991,1013,1409,1741

pii operator+(pii a, pii b)
{
	return {a.fi+b.fi,a.se+b.se};
}
pll operator+(pll a, pll b)
{
	return {a.fi+b.fi,a.se+b.se};
}
std::ostream& operator<<(std::ostream& out,pii a)
{
	out<<a.fi<<" "<<a.se<<endl;
	return out;
}
std::ostream& operator<<(std::ostream& out,pll a)
{
	out<<a.fi<<" "<<a.se<<endl;
	return out;
}
std::istream& operator>>(std::istream& in,pii &a)
{
    in>>a.fi>>a.se;
    return in;
}
std::istream& operator>>(std::istream& in,pll &a)
{
    in>>a.fi>>a.se;
    return in;
}

using namespace std;
using namespace __gnu_pbds;
void dog()
{
	int n;
	cin>>n;
	int s[n];
	inputarr(s,n);
	int sum=0;
	vi x[3];
	for (int i = 0; i < n; ++i)
	{
		sum+=s[i];
		x[s[i]%3].pb(s[i]);
	}
	SORT(x[0]);
	SORT(x[1]);
	SORT(x[2]);
	if(sum==0)
	{
		cout<<0<<endl;
		return ;
	}
	if(sum%3==1)
	{
		if(sz(x[1]))
			x[1].erase(x[1].begin());
		else{
			x[2].erase(x[2].begin());
			x[2].erase(x[2].begin());
		}
	}
	if(sum%3==2)
	{
		if(sz(x[2]))
			x[2].erase(x[2].begin());
		else{
			x[1].erase(x[1].begin());
			x[1].erase(x[1].begin());
		}
	}
	vi dong;
	for (int i = 0; i < 3; ++i)
	{
		trav(a,x[i])
			dong.pb(a);
	}
	SORT(dong);
	REVERSE(dong);
	if(dong[sz(dong)-1]!=0)
	{
		cout<<-1;
		return ;
	}
	for (int i = 0; i < sz(dong); ++i)
	{
		cout<<dong[i];
	}
}
int main()
{
    FAST;
    int testcases=1;
    //cin>>testcases;
    for(int i=0;i<testcases;++i)
    {
    	dog();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <iomanip>

#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define maxc(v) max_element(ALL(v))
#define minc(v) min_element(ALL(v))
#define GCD(m,n) __gcd(m,n)
#define debug cout<<"bhau"<<'\n';
#define DEBUG(x) cout<<x<<'\n';

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi >
#define vii vector<pii >

using namespace std;
void printff(int * dig)
{
	int flag=0;
	for(int i=9;i>=0;--i)
	{
		for(int j=0;j<dig[i];++j)
		{
			cout<<i;
			if(i!=0)
				flag=1;
			if((i==0)&&(flag==0))
				return ;
		}
	}
}
int main()
{
	int n;
	cin>>n;
	int a[n];
	int dig[10]={0};
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
		dig[a[i]]++;
	}
	if(dig[0]==0)
	{
		cout<<-1;
		return 0;
	}
	int digsum=dig[1]+2*dig[2]+3*dig[3]+4*dig[4]+5*dig[5]+6*dig[6]+7*dig[7]+8*dig[8]+9*dig[9];
	if(digsum%3==0)
	{
		printff(dig);
		return 0;
	}
	else if(digsum%3==1)
	{
		if(dig[1]>0)
		{
			dig[1]--;
		}
		else if(dig[4]>0)
		{
			dig[4]--;
		}
		else if(dig[7]>0)
		{
			dig[7]--;
		}
		else if(dig[2]+dig[5]+dig[8]>1)
		{
			if(dig[2]>0)dig[2]--;
			else if(dig[5]>0)dig[5]--;
			else dig[8]--;
			if(dig[2]>0)dig[2]--;
			else if(dig[5]>0)dig[5]--;
			else dig[8]--;
		}
		else {
			cout<<0;
			return 0;
		}
	}
	else if(digsum%3==2)
	{
		if(dig[2]>0)
		{
			dig[2]--;
		}
		else if(dig[5]>0)
		{
			dig[5]--;
		}
		else if(dig[8]>0)
		{
			dig[8]--;
		}
		else if(dig[1]+dig[4]+dig[7]>1)
		{
			if(dig[1]>0)dig[1]--;
			else if(dig[4]>0)dig[4]--;
			else dig[7]--;
			if(dig[1]>0)dig[1]--;
			else if(dig[4]>0)dig[4]--;
			else dig[7]--;
		}
		else {
			cout<<0;
			return 0;
		}
	}
	printff(dig);
		return 0;
}


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

#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DECIMAL(n)  std::cout << std::fixed;std::cout << std::setprecision(n);
#define hell 1000000007
#define narak 998244353
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define maxc(v) max_element(all(v))
#define minc(v) min_element(all(v))
#define sqr(a) (a)*(a)
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define inputarr(a,n) for(int xxx=0;xxx<n;++xxx) cin>>a[xxx]
#define initarr(a,n,x) for (int xxx=0;xxx<n;++xxx) a[xxx]=x
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define invect(data,n,commands) for(int xxx = 0;xxx<n;xxx++){int tmp;cin>>tmp;data.pb(tmp);commands}
#define inset(data,n,commands) for(int xxx = 0;xxx<n;xxx++){int tmp;cin>>tmp;data.insert(tmp);commands}
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define debug cerr<<"bhau"<<endl

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());

#define ll long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi >
#define vii vector<pii >
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

/*-----------------------Bitmask------------------*/
//int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
//bool check(int N,int pos){return (bool)(N & (1<<pos));}
/*------------------------------------------------*/

//primes for hashing 937,991,1013,1409,1741

pii operator+(pii a, pii b)
{
	return {a.fi+b.fi,a.se+b.se};
}
pll operator+(pll a, pll b)
{
	return {a.fi+b.fi,a.se+b.se};
}
std::ostream& operator<<(std::ostream& out,pii a)
{
	out<<a.fi<<" "<<a.se<<endl;
	return out;
}
std::ostream& operator<<(std::ostream& out,pll a)
{
	out<<a.fi<<" "<<a.se<<endl;
	return out;
}
std::istream& operator>>(std::istream& in,pii &a)
{
    in>>a.fi>>a.se;
    return in;
}
std::istream& operator>>(std::istream& in,pll &a)
{
    in>>a.fi>>a.se;
    return in;
}

using namespace std;
using namespace __gnu_pbds;
void dog()
{
	int n;
	cin>>n;
	int s[n];
	inputarr(s,n);
	int sum=0;
	vi x[3];
	for (int i = 0; i < n; ++i)
	{
		sum+=s[i];
		x[s[i]%3].pb(s[i]);
	}
	SORT(x[0]);
	SORT(x[1]);
	SORT(x[2]);
	if(sum==0)
	{
		cout<<0<<endl;
		return ;
	}
	if(sum%3==1)
	{
		if(sz(x[1]))
			x[1].erase(x[1].begin());
		else{
			x[2].erase(x[2].begin());
			x[2].erase(x[2].begin());
		}
	}
	if(sum%3==2)
	{
		if(sz(x[2]))
			x[2].erase(x[2].begin());
		else{
			x[1].erase(x[1].begin());
			x[1].erase(x[1].begin());
		}
	}
	vi dong;
	for (int i = 0; i < 3; ++i)
	{
		trav(a,x[i])
			dong.pb(a);
	}
	SORT(dong);
	REVERSE(dong);
	if(dong[0]==0)
	{
		cout<<0;
		return ;
	}
	if(dong[sz(dong)-1]!=0)
	{
		cout<<-1;
		return ;
	}
	for (int i = 0; i < sz(dong); ++i)
	{
		cout<<dong[i];
	}
}
int main()
{
    FAST;
    int testcases=1;
    //cin>>testcases;
    for(int i=0;i<testcases;++i)
    {
    	dog();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <iomanip>

#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define maxc(v) max_element(ALL(v))
#define minc(v) min_element(ALL(v))
#define GCD(m,n) __gcd(m,n)
#define debug cout<<"bhau"<<'\n';
#define DEBUG(x) cout<<x<<'\n';

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi >
#define vii vector<pii >

using namespace std;
void printff(int * dig)
{
	for(int i=9;i>=0;--i)
	{
		for(int j=0;j<dig[i];++j)
		{
			cout<<i;
		}
	}
}
int main()
{
	int n;
	cin>>n;
	int a[n];
	int dig[10]={0};
	int cntzero=0;
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
		dig[a[i]]++;
		if(a[i]==0)
			cntzero++;
	}
	if(cntzero==n)
	{
		cout<<0;
		return 0;
	}
	if(dig[0]==0)
	{
		cout<<-1;
		return 0;
	}
	int digsum=dig[1]+2*dig[2]+3*dig[3]+4*dig[4]+5*dig[5]+6*dig[6]+7*dig[7]+8*dig[8]+9*dig[9];
	if(digsum%3==0)
	{
		printff(dig);
		return 0;
	}
	else if(digsum%3==1)
	{
		if(dig[1]>0)
		{
			dig[1]--;
		}
		else if(dig[4]>0)
		{
			dig[4]--;
		}
		else if(dig[7]>0)
		{
			dig[7]--;
		}
		else if(dig[2]>1)
		{
			dig[2]-=2;
		}
		else if(dig[5]>1)
		{
			dig[5]-=2;
		}
		else if(dig[8]>1)
		{
			dig[8]-=2;
		}
		else {
			cout<<-1;
			return 0;
		}
	}
	else if(digsum%3==2)
	{
		if(dig[2]>0)
		{
			dig[2]--;
		}
		else if(dig[5]>0)
		{
			dig[5]--;
		}
		else if(dig[8]>0)
		{
			dig[8]--;
		}
		else if(dig[1]>1)
		{
			dig[1]-=2;
		}
		else if(dig[4]>1)
		{
			dig[4]-=2;
		}
		else if(dig[7]>1)
		{
			dig[7]-=2;
		}
		else {
			cout<<-1;
			return 0;
		}
	}
	printff(dig);
		return 0;
}


#include <bits/stdc++.h>
#include <iomanip>

#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define maxc(v) max_element(ALL(v))
#define minc(v) min_element(ALL(v))
#define GCD(m,n) __gcd(m,n)
#define debug cout<<"bhau"<<'\n';
#define DEBUG(x) cout<<x<<'\n';

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi >
#define vii vector<pii >

using namespace std;
void printff(int * dig)
{
	for(int i=9;i>=0;--i)
	{
		for(int j=0;j<dig[i];++j)
		{
			cout<<i;
		}
	}
}
int main()
{
	int n;
	cin>>n;
	int a[n];
	int dig[10]={0};
	int cntzero=0
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
		dig[a[i]]++;
		if(a[i]==0)
			cntzero++;
	}
	if(cntzero==n)
	{
		cout<<0;
		return 0;
	}
	if(dig[0]==0)
	{
		cout<<-1;
		return 0;
	}
	int digsum=dig[1]+2*dig[2]+3*dig[3]+4*dig[4]+5*dig[5]+6*dig[6]+7*dig[7]+8*dig[8]+9*dig[9];
	if(digsum%3==0)
	{
		printff(dig);
		return 0;
	}
	else if(digsum%3==1)
	{
		if(dig[1]>0)
		{
			dig[1]--;
		}
		else if(dig[4]>0)
		{
			dig[4]--;
		}
		else if(dig[7]>0)
		{
			dig[7]--;
		}
		else if(dig[2]>1)
		{
			dig[2]-=2;
		}
		else if(dig[5]>1)
		{
			dig[5]-=2;
		}
		else if(dig[8]>1)
		{
			dig[8]-=2;
		}
		else {
			cout<<-1;
			return 0;
		}
	}
	else if(digsum%3==2)
	{
		if(dig[2]>0)
		{
			dig[2]--;
		}
		else if(dig[5]>0)
		{
			dig[5]--;
		}
		else if(dig[8]>0)
		{
			dig[8]--;
		}
		else if(dig[1]>1)
		{
			dig[1]-=2;
		}
		else if(dig[4]>1)
		{
			dig[4]-=2;
		}
		else if(dig[7]>1)
		{
			dig[7]-=2;
		}
		else {
			cout<<-1;
			return 0;
		}
	}
	printff(dig);
		return 0;
}


#include <bits/stdc++.h>
#include <iomanip>

#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define maxc(v) max_element(ALL(v))
#define minc(v) min_element(ALL(v))
#define GCD(m,n) __gcd(m,n)
#define debug cout<<"bhau"<<'\n';
#define DEBUG(x) cout<<x<<'\n';

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi >
#define vii vector<pii >

using namespace std;
void printff(int * dig)
{
	for(int i=9;i>=0;--i)
	{
		for(int j=0;j<dig[i];++j)
		{
			cout<<i;
		}
	}
}
int main()
{
	int n;
	cin>>n;
	int a[n];
	int dig[10]={0};
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
		dig[a[i]]++;
	}
	if(dig[0]==0)
	{
		cout<<-1;
		return 0;
	}
	int digsum=dig[1]+2*dig[2]+3*dig[3]+4*dig[4]+5*dig[5]+6*dig[6]+7*dig[7]+8*dig[8]+9*dig[9];
	if(digsum%3==0)
	{
		printff(dig);
		return 0;
	}
	else if(digsum%3==1)
	{
		if(dig[1]>0)
		{
			dig[1]--;
		}
		else if(dig[4]>0)
		{
			dig[4]--;
		}
		else if(dig[7]>0)
		{
			dig[7]--;
		}
		else if(dig[2]>1)
		{
			dig[2]-=2;
		}
		else if(dig[5]>1)
		{
			dig[5]-=2;
		}
		else if(dig[8]>1)
		{
			dig[8]-=2;
		}
		else {
			cout<<-1;
			return 0;
		}
	}
	else if(digsum%3==2)
	{
		if(dig[2]>0)
		{
			dig[2]--;
		}
		else if(dig[5]>0)
		{
			dig[5]--;
		}
		else if(dig[8]>0)
		{
			dig[8]--;
		}
		else if(dig[1]>1)
		{
			dig[1]-=2;
		}
		else if(dig[4]>1)
		{
			dig[4]-=2;
		}
		else if(dig[7]>1)
		{
			dig[7]-=2;
		}
		else {
			cout<<-1;
			return 0;
		}
	}
	printff(dig);
		return 0;
}


#include <bits/stdc++.h>
#include <iomanip>

#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define maxc(v) max_element(ALL(v))
#define minc(v) min_element(ALL(v))
#define GCD(m,n) __gcd(m,n)
#define debug cout<<"bhau"<<'\n';
#define DEBUG(x) cout<<x<<'\n';

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi >
#define vii vector<pii >

using namespace std;
void printff(int * dig)
{
	int flag=0;
	for(int i=9;i>=0;--i)
	{
		for(int j=0;j<dig[i];++j)
		{
			cout<<i;
			if(i!=0)
				flag=1;
			if((i==0)&&(flag==0))
				return ;
		}
	}
}
int main()
{
	int n;
	cin>>n;
	int a[n];
	int dig[10]={0};
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
		dig[a[i]]++;
	}
	if(dig[0]==0)
	{
		cout<<-1;
		return 0;
	}
	int digsum=dig[1]+2*dig[2]+3*dig[3]+4*dig[4]+5*dig[5]+6*dig[6]+7*dig[7]+8*dig[8]+9*dig[9];
	if(digsum%3==0)
	{
		printff(dig);
		return 0;
	}
	else if(digsum%3==1)
	{
		if(dig[1]>0)
		{
			dig[1]--;
		}
		else if(dig[4]>0)
		{
			dig[4]--;
		}
		else if(dig[7]>0)
		{
			dig[7]--;
		}
		else if(dig[2]>1)
		{
			dig[2]-=2;
		}
		else if(dig[5]>1)
		{
			dig[5]-=2;
		}
		else if(dig[8]>1)
		{
			dig[8]-=2;
		}
		else {
			cout<<0;
			return 0;
		}
	}
	else if(digsum%3==2)
	{
		if(dig[2]>0)
		{
			dig[2]--;
		}
		else if(dig[5]>0)
		{
			dig[5]--;
		}
		else if(dig[8]>0)
		{
			dig[8]--;
		}
		else if(dig[1]>1)
		{
			dig[1]-=2;
		}
		else if(dig[4]>1)
		{
			dig[4]-=2;
		}
		else if(dig[7]>1)
		{
			dig[7]-=2;
		}
		else {
			cout<<0;
			return 0;
		}
	}
	printff(dig);
		return 0;
}


#include <bits/stdc++.h>
#include <iomanip>

#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define maxc(v) max_element(ALL(v))
#define minc(v) min_element(ALL(v))
#define GCD(m,n) __gcd(m,n)
#define debug cout<<"bhau"<<'\n';
#define DEBUG(x) cout<<x<<'\n';

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi >
#define vii vector<pii >

using namespace std;
void printff(int * dig)
{
	int flag=0;
	for(int i=9;i>=0;--i)
	{
		for(int j=0;j<dig[i];++j)
		{
			cout<<i;
			if(i!=0)
				flag=1;
			if((i==0)&&(flag==0))
				return ;
		}
	}
}
int main()
{
	int n;
	cin>>n;
	int a[n];
	int dig[10]={0};
	for (int i = 0; i < n; ++i)
	{
		cin>>a[i];
		dig[a[i]]++;
	}
	if(dig[0]==0)
	{
		cout<<-1;
		return 0;
	}
	int digsum=dig[1]+2*dig[2]+3*dig[3]+4*dig[4]+5*dig[5]+6*dig[6]+7*dig[7]+8*dig[8]+9*dig[9];
	if(digsum%3==0)
	{
		printff(dig);
		return 0;
	}
	else if(digsum%3==1)
	{
		if(dig[1]>0)
		{
			dig[1]--;
		}
		else if(dig[4]>0)
		{
			dig[4]--;
		}
		else if(dig[7]>0)
		{
			dig[7]--;
		}
		else if(dig[2]>1)
		{
			dig[2]-=2;
		}
		else if(dig[5]>1)
		{
			dig[5]-=2;
		}
		else if(dig[8]>1)
		{
			dig[8]-=2;
		}
		else {
			cout<<-1;
			return 0;
		}
	}
	else if(digsum%3==2)
	{
		if(dig[2]>0)
		{
			dig[2]--;
		}
		else if(dig[5]>0)
		{
			dig[5]--;
		}
		else if(dig[8]>0)
		{
			dig[8]--;
		}
		else if(dig[1]>1)
		{
			dig[1]-=2;
		}
		else if(dig[4]>1)
		{
			dig[4]-=2;
		}
		else if(dig[7]>1)
		{
			dig[7]-=2;
		}
		else {
			cout<<-1;
			return 0;
		}
	}
	printff(dig);
		return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	ll n, sum = 0;
	bool zero = 0;

	cin>>n;

	vector<ll> vec(n);

	for(ll i = 0; i < n; ++i){
		cin>>vec[i];
		sum += vec[i];

		if(vec[i] == 0){
			zero = 1;
		}
	}

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

	sort(vec.begin(), vec.end());

	while(sum%3 != 0){

		if(sum%3 == 1){

			vector<ll> :: iterator i = vec.begin();
			while(i != vec.end() && (*i)%3 != 1){
				i++;
			}

			if(i == vec.end()){
				i = vec.begin();
				while((*i)%3 != 2){
					i++;
				}

				sum -= (2 * (*i));
				vec.erase(i, i+2);
			} else {
				sum -= (*i);
				vec.erase(i);
			}

		} else {

			vector<ll> :: iterator i = vec.begin();
			while(i != vec.end() && (*i)%3 != 2 ){
				i++;
			}

			if(i == vec.end()){

				i = vec.begin();
				while((*i)%3 != 1){
					i++;
				}

				sum -= (2* (*i));
				vec.erase(i, i+2);
			} else {
				sum -= (*i);
				vec.erase(i);
			}

		}
	}

	vector<ll> :: reverse_iterator itr = vec.rbegin();

	if(*itr == 0){
		cout<<0;
		return 0;

	}
	for( ; itr != vec.rend(); ++itr){
		cout<<*itr;
	}

	return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	map<ll, ll> mp;

	ll n, tmp;
	bool zero = 0, isThree = 0;

	cin>>n;

	for(ll i = 0; i < n; ++i){
		cin>>tmp;
		mp[tmp]++;
		if(tmp == 0){
			zero = 1;
		}
	}

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

	for(auto itr : mp){
		if(itr.second >= 3){
			isThree = 1;
			break;
		}

		if(itr.first == 0 && itr.second > 0){
			isThree = 1;
			break;
		}
	}

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

	for(auto &itr : mp){
		if(itr.first != 0 && itr.first != 3) {
			itr.second -= ((itr.second)%3);

		}
	}

	map<ll, ll>::reverse_iterator it;
	string str;

	for(it = mp.rbegin(); it != mp.rend(); ++it){
		ll a = it -> second;
		while(a--){
			str.append(to_string(it -> first));
		}
	}

	if(str[0] == '0'){
		cout<<0;
	} else {
		cout<<str;
	}
	return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	ll n, sum = 0;
	bool zero = 0;

	cin>>n;

	vector<ll> vec(n);

	for(ll i = 0; i < n; ++i){
		cin>>vec[i];
		sum += vec[i];

		if(vec[i] == 0){
			zero = 1;
		}
	}

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

	sort(vec.begin(), vec.end());

	while(sum%3 != 0){

		if(sum%3 == 1){

			vector<ll> :: iterator i = vec.begin();
			while(i != vec.end() && (*i)%3 != 1){
				i++;
			}

			if(i == vec.end()){

				i = vec.begin();
				while((*i)%3 != 2){
					i++;
				}

				sum -= *i;
				vec.erase(i);

				while((*i)%3 != 2){
					i++;
				}

				sum -= *i;
				vec.erase(i);
			} else {

				sum -= (*i);
				vec.erase(i);
			}

		} else {

			vector<ll> :: iterator i = vec.begin();
			while(i != vec.end() && (*i)%3 != 2 ){
				i++;
			}

			if(i == vec.end()){

				i = vec.begin();
				while((*i)%3 != 1){
					i++;
				}

				sum -= *i;
				vec.erase(i);

				while((*i)%3 != 1){
					i++;
				}

				sum -= *i;
				vec.erase(i);
			} else {
				sum -= (*i);
				vec.erase(i);
			}

		}
	}

	vector<ll> :: reverse_iterator itr = vec.rbegin();

	if(*itr == 0){
		cout<<0;
		return 0;
	}
	for( ; itr != vec.rend(); ++itr){
		cout<<*itr;
	}

	return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	map<ll, ll> mp;

	ll n, tmp;
	bool zero = 0, isThree = 0;

	cin>>n;

	for(ll i = 0; i < n; ++i){
		cin>>tmp;
		mp[tmp]++;
		if(tmp == 0){
			zero = 1;
		}
	}

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

	for(auto itr : mp){
		if(itr.second >= 3){
			isThree = 1;
			break;
		}
	}

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

	for(auto &itr : mp){
		if(itr.first != 0){
			itr.second -= ((itr.second)%3);

		}
	}

	map<ll, ll>::reverse_iterator it;
	string str;

	for(it = mp.rbegin(); it != mp.rend(); ++it){
		ll a = it -> second;
		while(a--){
			str.append(to_string(it -> first));
		}
	}

	cout<<stoll(str);
	return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	ll n, sum = 0;
	bool zero = 0;

	cin>>n;

	vector<ll> vec(n);

	for(ll i = 0; i < n; ++i){
		cin>>vec[i];
		sum += vec[i];

		if(vec[i] == 0){
			zero = 1;
		}
	}

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

	sort(vec.begin(), vec.end());

	while(sum%3 != 0){
		if(sum%3 == 1){
			vector<ll> :: iterator i = vec.begin();
			while(i != vec.end() && (*i)%3 != 1){
				i++;
			}

			if(i == vec.end()){
				i = vec.begin();
				while((*i)%3 != 2){
					i++;
				}

				sum -= (2* (*i));
				vec.erase(i);
				vec.erase(i);
			}

			sum -= (*i);
			vec.erase(i);
		} else {
			vector<ll> :: iterator i = vec.begin();
			while(i != vec.end() && (*i)%3 != 2 ){
				i++;
			}

			if(i == vec.end()){
				i = vec.begin();
				while((*i)%3 != 1){
					i++;
				}

				sum -= (2* (*i));
				vec.erase(i);
				vec.erase(i);
			}

			sum -= (*i);
			vec.erase(i);
		}
	}

	vector<ll> :: reverse_iterator itr = vec.rbegin();

	for( ; itr != vec.rend(); ++itr){
		cout<<*itr;
	}

	return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	map<ll, ll> mp;

	ll n, tmp;
	bool zero = 0, isThree = 0;

	cin>>n;

	for(ll i = 0; i < n; ++i){
		cin>>tmp;
		mp[tmp]++;
		if(tmp == 0){
			zero = 1;
		}
	}

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

	for(auto itr : mp){
		if(itr.second >= 3){
			isThree = 1;
			break;
		}

		if(itr.first == 0 && itr.second > 0){
			isThree = 1;
			break;
		}
	}

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

	for(auto &itr : mp){
		if(itr.first != 0){
			itr.second -= ((itr.second)%3);

		}
	}

	map<ll, ll>::reverse_iterator it;
	string str;

	for(it = mp.rbegin(); it != mp.rend(); ++it){
		ll a = it -> second;
		while(a--){
			str.append(to_string(it -> first));
		}
	}

	cout<<stoll(str);
	return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	map<ll, ll> mp;

	ll n, tmp;
	bool zero = 0, isThree = 0;

	cin>>n;

	for(ll i = 0; i < n; ++i){
		cin>>tmp;
		mp[tmp]++;
		if(tmp == 0){
			zero = 1;
		}
	}

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

	for(auto itr : mp){
		if(itr.second >= 3){
			isThree = 1;
			break;
		}

		if(itr.first == 0 && itr.second > 0){
			isThree = 1;
			break;
		}
	}

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

	for(auto &itr : mp){
		if(itr.first != 0 && itr.first != 3) {
			itr.second -= ((itr.second)%3);

		}
	}

	map<ll, ll>::reverse_iterator it;
	string str;

	for(it = mp.rbegin(); it != mp.rend(); ++it){
		ll a = it -> second;
		while(a--){
			str.append(to_string(it -> first));
		}
	}

	cout<<stoll(str);
	return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	ll n, sum = 0;
	bool zero = 0;

	cin>>n;

	vector<ll> vec(n);

	for(ll i = 0; i < n; ++i){
		cin>>vec[i];
		sum += vec[i];

		if(vec[i] == 0){
			zero = 1;
		}
	}

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

	sort(vec.begin(), vec.end());

	while(sum%3 != 0){

		if(sum%3 == 1){

			vector<ll> :: iterator i = vec.begin();
			while(i != vec.end() && (*i)%3 != 1){
				i++;
			}

			if(i == vec.end()){

				i = vec.begin();
				while((*i)%3 != 2){
					i++;
				}

				//The two numbers that give rem = 2 does not need to be in consecutive positions.
				//E.g. :- 2 3 5, Here 2 and 5 gives rem 5 and they need to be deleted not 2 and 3.

				sum -= *i;
				vec.erase(i);

				while((*i)%3 != 2){
					i++;
				}

				sum -= *i;
				vec.erase(i);
			} else {

				sum -= (*i);
				vec.erase(i);
			}

		} else {

			vector<ll> :: iterator i = vec.begin();
			while(i != vec.end() && (*i)%3 != 2 ){
				i++;
			}

			if(i == vec.end()){

				i = vec.begin();
				while((*i)%3 != 1){
					i++;
				}

				//The two numbers that give rem = 1 does not need to be in consecutive positions.
				//E.g. :- 1 3 4, Here 1 and 4 gives rem 1 and they need to be deleted not 1 and 3.

				sum -= *i;
				vec.erase(i);

				while((*i)%3 != 1){
					i++;
				}

				sum -= *i;
				vec.erase(i);
			} else {
				sum -= (*i);
				vec.erase(i);
			}

		}
	}

	vector<ll> :: reverse_iterator itr = vec.rbegin();

	if(*itr == 0){
		cout<<0;
		return 0;
	}
	for( ; itr != vec.rend(); ++itr){
		cout<<*itr;
	}

	return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	ll n, sum = 0;
	bool zero = 0;

	cin>>n;

	vector<ll> vec(n);

	for(ll i = 0; i < n; ++i){
		cin>>vec[i];
		sum += vec[i];

		if(vec[i] == 0){
			zero = 1;
		}
	}

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

	sort(vec.begin(), vec.end());

	vector<ll>:: iterator i=vec.begin();

	for( ; i!=vec.end(); ++i){
		if(*i != 0 && *i != 3 && *i != 6 && *i != 9){
			break;
		}
	}
	while(sum%3 != 0){
		while(*i == 3 || *i == 0 || *i == 6 || *i == 9){
			i++;
		}
		sum -= *i;
		vec.erase(i);
	}

	vector<ll> :: reverse_iterator itr = vec.rbegin();

	for( ; itr != vec.rend(); ++itr){
		cout<<*itr;
	}

	return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	map<ll, ll> mp;

	ll n, tmp;
	bool zero = 0, isThree = 0;

	cin>>n;

	for(ll i = 0; i < n; ++i){
		cin>>tmp;
		mp[tmp]++;
		if(tmp == 0){
			zero = 1;
		}
	}

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

	for(auto itr : mp){
		if(itr.second >= 3){
			isThree = 1;
			break;
		}

		if(itr.first == 0 && itr.second > 0){
			isThree = 1;
			break;
		}
	}

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

	for(auto &itr : mp){
		if(itr.first != 0 && itr.first != 3 && itr.first != 6 && itr.first != 9) {
			itr.second -= ((itr.second)%3);

		}
	}

	map<ll, ll>::reverse_iterator it;
	string str;

	for(it = mp.rbegin(); it != mp.rend(); ++it){
		ll a = it -> second;
		while(a--){
			str.append(to_string(it -> first));
		}
	}

	if(str[0] == '0'){
		cout<<0;
	} else {
		cout<<str;
	}
	return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	ll n, sum = 0;
	bool zero = 0;

	cin>>n;

	vector<ll> vec(n);

	for(ll i = 0; i < n; ++i){
		cin>>vec[i];
		sum += vec[i];

		if(vec[i] == 0){
			zero = 1;
		}
	}

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

	sort(vec.begin(), vec.end());

	while(sum%3 != 0){

		if(sum%3 == 1){

			vector<ll> :: iterator i = vec.begin();
			while(i != vec.end() && (*i)%3 != 1){
				i++;
			}

			if(i == vec.end()){
				i = vec.begin();
				while((*i)%3 != 2){
					i++;
				}

				sum -= (2* (*i));
				vec.erase(i, i+2);
			} else {
				sum -= (*i);
				vec.erase(i);
			}

		} else {

			vector<ll> :: iterator i = vec.begin();
			while(i != vec.end() && (*i)%3 != 2 ){
				i++;
			}

			if(i == vec.end()){

				i = vec.begin();
				while((*i)%3 != 1){
					i++;
				}

				sum -= (2* (*i));
				vec.erase(i, i+2);
			} else {
				sum -= (*i);
				vec.erase(i);
			}

		}
	}

	vector<ll> :: reverse_iterator itr = vec.rbegin();

	for( ; itr != vec.rend(); ++itr){
		cout<<*itr;
	}

	return 0;
}


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

#define ll long long
#define db double

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	map<ll, ll> mp;

	ll n, tmp;
	bool zero = 0;

	cin>>n;

	for(ll i = 0; i < n; ++i){
		cin>>tmp;
		mp[tmp]++;
		if(tmp == 0){
			zero = 1;
		}
	}

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

	for(auto &itr : mp){
		if(itr.first != 0 && itr.first != 3 && itr.first != 6 && itr.first != 9) {
			itr.second -= ((itr.second)%3);

		}
	}

	map<ll, ll>::reverse_iterator it;
	string str;

	for(it = mp.rbegin(); it != mp.rend(); ++it){
		ll a = it -> second;
		while(a--){
			str.append(to_string(it -> first));
		}
	}

	if(str[0] == '0'){
		cout<<0;
	} else {
		cout<<str;
	}

	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n],zero=0,sum=0,count1=0,count2=0,e=-1,f=-1,allzero=0;
	bool print=true,pre=true,prf=true;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
		if(a[i]!=0)
			allzero++;
		if(a[i]%3==1)
			count1++;
		else if(a[i]%3==2)
			count2++;

	}
	sort(a,a+n);
	if(a[0]==0)
	{
		if(allzero>0)
		{
			if(sum%3==0)
			{
				for(int i=n-1;i>=0;i--)
					cout<<a[i];
				cout<<endl;
			}
			else if(sum%3==1)
			{
				if(allzero<=1)
				{
					cout<<0<<endl;
				}
				else
				{
					if(count1>0)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==1)
							{
							e=a[i];
							break;
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((print==true) && (a[i]==e))
							{
								print=false;
							}
							else
								cout<<a[i];
						}
					}
					else if((allzero>2) && (count2>=2))
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==2)
							{
								if(e==-1)
									e=a[i];
								else
								{
									f=a[i];
									break;
								}
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((pre==true) && (a[i]==e))
							{
								pre=false;
							}
							else if((prf==true) && (a[i]==f))
							{
								prf=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else
						cout<<0<<endl;
				}
			}
			else
			{
				if(allzero<=1)
				{
					cout<<0<<endl;
				}
				else
				{
					if(count2>0)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==2)
							{
							e=a[i];
							break;
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((print==true) && (a[i]==e))
							{
								print=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else if((allzero>2) && (count1>=2))
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==1)
							{
								if(e==-1)
									e=a[i];
								else
								{
									f=a[i];
									break;
								}
							}
					  }
						for(int i=n-1;i>=0;i--)
						{
							if((pre==true) && (a[i]==e))
							{
								pre=false;
							}
							else if((prf==true) && (a[i]==f))
							{
								prf=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else
						cout<<-1<<endl;
				}
			}
		}
		else
			cout<<0<<endl;
	}
	else
		cout<<-1<<endl;
	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n],zero=0,sum=0,count1=0,count2=0,e=-1,f=-1,allzero=0;
	bool print=true,pre=true,prf=true;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
		if(a[i]!=0)
			allzero++;
		if(a[i]%3==1)
			count1++;
		else if(a[i]%3==2)
			count2++;

	}
	sort(a,a+n);
	if(a[0]==0)
	{
		if(allzero>0)
		{
			if(sum%3==0)
			{
				for(int i=n-1;i>=0;i--)
					cout<<a[i];
				cout<<endl;
			}
			else if(sum%3==1)
			{
				if(allzero<=1)
				{
					cout<<0<<endl;
				}
				else
				{
					if(count1>0)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==1)
							{
							e=a[i];
							break;
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((print==true) && (a[i]==e))
							{
								print=false;
							}
							else
								cout<<a[i];
						}
					}
					else if((allzero>=2) && (count2>=2))
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==2)
							{
								if(e==-1)
									e=a[i];
								else
								{
									f=a[i];
									break;
								}
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((pre==true) && (a[i]==e))
							{
								pre=false;
							}
							else if((prf==true) && (a[i]==f))
							{
								prf=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else
						cout<<0<<endl;
				}
			}
			else
			{
				if(allzero<=1)
				{
					cout<<0<<endl;
				}
				else
				{
					if(count2>0)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==2)
							{
							e=a[i];
							break;
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((print==true) && (a[i]==e))
							{
								print=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else if((allzero>=2) && (count1>=2))
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==1)
							{
								if(e==-1)
									e=a[i];
								else
								{
									f=a[i];
									break;
								}
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((pre==true) && (a[i]==e))
							{
								pre=false;
							}
							else if((prf==true) && (a[i]==f))
							{
								prf=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else
						cout<<-1<<endl;
				}
			}
		}
		else
			cout<<0<<endl;
	}
	else
		cout<<-1<<endl;
	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n],zero=0,sum=0,count1=0,count2=0,e=-1,f=-1,allzero=0;
	bool print=true,pre=true,prf=true;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
		if(a[i]!=0)
			allzero++;
		if(a[i]%3==1)
			count1++;
		else if(a[i]%3==2)
			count2++;

	}
	sort(a,a+n);
	if(a[0]==0)
	{
		if(allzero>0)
		{
			if(sum%3==0)
			{
				for(int i=n-1;i>=0;i--)
					cout<<a[i];
				cout<<endl;
			}
			else if(sum%3==1)
			{
				if(allzero<=1)
				{
					cout<<0<<endl;
				}
				else
				{
					if(count1>0)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==1)
							{
							e=a[i];
							break;
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((print==true) && (a[i]==e))
							{
								print=false;
							}
							else
								cout<<a[i];
						}
					}
					else if((allzero>2) && (count2>=2))
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==2)
							{
								if(e==-1)
									e=a[i];
								else
								{
									f=a[i];
									break;
								}
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((pre==true) && (a[i]==e))
							{
								pre=false;
							}
							else if((prf==true) && (a[i]==f))
							{
								prf=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else
						cout<<0<<endl;
				}
			}
			else
			{
				if(allzero<=1)
				{
					cout<<0<<endl;
				}
				else
				{
					if(count2>0)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==2)
							{
							e=a[i];
							break;
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((print==true) && (a[i]==e))
							{
								print=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else if((allzero>2) && (count1>=2))
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==1)
							{
								if(e==-1)
									e=a[i];
								else
								{
									f=a[i];
									break;
								}
							}
					  }
						for(int i=n-1;i>=0;i--)
						{
							if((pre==true) && (a[i]==e))
							{
								pre=false;
							}
							else if((prf==true) && (a[i]==f))
							{
								prf=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else
						cout<<0<<endl;
				}
			}
		}
		else
			cout<<0<<endl;
	}
	else
		cout<<-1<<endl;
	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n],zero=0,sum=0,count1=0,count2=0,e=-1,f=-1,allzero=0;
	bool print=true,pre=true,prf=true;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
		if(a[i]!=0)
			allzero=0;
		if(a[i]%3==1)
			count1++;
		else if(a[i]%3==2)
			count2++;

	}
	sort(a,a+n);
	if(a[0]==0)
	{
		if(allzero)
		{
			if(sum%3==0)
			{
				for(int i=0;i<n;i++)
					cout<<a[i];
				cout<<endl;
			}
			else if(sum%3==1)
			{
				if(count1>0)
				{
					for(int i=1;i<n;i++)
					{
						if(a[i]%3==1)
						{
						e=a[i];
						break;
						}
					}
					for(int i=n-1;i>=0;i--)
					{
						if((print==true) && (a[i]==e))
						{
							print=false;
						}
						else
							cout<<a[i];
					}
				}
				else if(count2>=2)
				{
					for(int i=1;i<n;i++)
					{
						if(a[i]%3==2)
						{
							if(e==-1)
								e=a[i];
							else
							{
								f=a[i];
								break;
							}
						}
					}
					for(int i=n-1;i>=0;i--)
					{
						if((pre==true) && (a[i]==e))
						{
							pre=false;
						}
						else if((prf==true) && (a[i]==f))
						{
							prf=false;
						}
						else
							cout<<a[i];
					}
					cout<<endl;
				}
				else
					cout<<-1<<endl;
			}
			else
			{
				if(count2>0)
				{
					for(int i=1;i<n;i++)
					{
						if(a[i]%3==2)
						{
						e=a[i];
						break;
						}
					}
					for(int i=n-1;i>=0;i--)
					{
						if((print==true) && (a[i]==e))
						{
							print=false;
						}
						else
							cout<<a[i];
					}
					cout<<endl;
				}
				else if(count1>=2)
				{
					for(int i=1;i<n;i++)
					{
						if(a[i]%3==1)
						{
							if(e==-1)
								e=a[i];
							else
							{
								f=a[i];
								break;
							}
						}
					}
					for(int i=n-1;i>=0;i--)
					{
						if((pre==true) && (a[i]==e))
						{
							pre=false;
						}
						else if((prf==true) && (a[i]==f))
						{
							prf=false;
						}
						else
							cout<<a[i];
					}
					cout<<endl;
				}
				else
					cout<<-1<<endl;
			}
		}
		else
			cout<<0<<endl;
	}
	else
		cout<<-1<<endl;
	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n],zero=0,sum=0,count1=0,count2=0,e=-1,f=-1,allzero=0;
	bool print=true,pre=true,prf=true;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
		if(a[i]!=0)
			allzero++;
		if(a[i]%3==1)
			count1++;
		else if(a[i]%3==2)
			count2++;

	}
	sort(a,a+n);
	if(a[0]==0)
	{
		if(allzero>0)
		{
			if(sum%3==0)
			{
				for(int i=0;i<n;i++)
					cout<<a[i];
				cout<<endl;
			}
			else if(sum%3==1)
			{
				if(allzero<=1)
				{
					cout<<0<<endl;
				}
				else
				{
					if(count1>0)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==1)
							{
							e=a[i];
							break;
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((print==true) && (a[i]==e))
							{
								print=false;
							}
							else
								cout<<a[i];
						}
					}
					else if((allzero>=2) && (count2>=2))
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==2)
							{
								if(e==-1)
									e=a[i];
								else
								{
									f=a[i];
									break;
								}
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((pre==true) && (a[i]==e))
							{
								pre=false;
							}
							else if((prf==true) && (a[i]==f))
							{
								prf=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else
						cout<<0<<endl;
				}
			}
			else
			{
				if(allzero<=1)
				{
					cout<<0<<endl;
				}
				else
				{
					if(count2>0)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==2)
							{
							e=a[i];
							break;
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((print==true) && (a[i]==e))
							{
								print=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else if((allzero>=2) && (count1>=2))
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==1)
							{
								if(e==-1)
									e=a[i];
								else
								{
									f=a[i];
									break;
								}
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((pre==true) && (a[i]==e))
							{
								pre=false;
							}
							else if((prf==true) && (a[i]==f))
							{
								prf=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else
						cout<<-1<<endl;
				}
			}
		}
		else
			cout<<0<<endl;
	}
	else
		cout<<-1<<endl;
	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n],zero=0,sum=0,count1=0,count2=0,e=-1,f=-1,allzero=0;
	bool print=true,pre=true,prf=true;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
		if(a[i]!=0)
			allzero=1;
		if(a[i]%3==1)
			count1++;
		else if(a[i]%3==2)
			count2++;

	}
	sort(a,a+n);
	if(a[0]==0)
	{
		if(allzero)
		{
			if(sum%3==0)
			{
				for(int i=0;i<n;i++)
					cout<<a[i];
				cout<<endl;
			}
			else if(sum%3==1)
			{
				if(count1>0)
				{
					for(int i=1;i<n;i++)
					{
						if(a[i]%3==1)
						{
						e=a[i];
						break;
						}
					}
					for(int i=n-1;i>=0;i--)
					{
						if((print==true) && (a[i]==e))
						{
							print=false;
						}
						else
							cout<<a[i];
					}
				}
				else if(count2>=2)
				{
					for(int i=1;i<n;i++)
					{
						if(a[i]%3==2)
						{
							if(e==-1)
								e=a[i];
							else
							{
								f=a[i];
								break;
							}
						}
					}
					for(int i=n-1;i>=0;i--)
					{
						if((pre==true) && (a[i]==e))
						{
							pre=false;
						}
						else if((prf==true) && (a[i]==f))
						{
							prf=false;
						}
						else
							cout<<a[i];
					}
					cout<<endl;
				}
				else
					cout<<-1<<endl;
			}
			else
			{
				if(count2>0)
				{
					for(int i=1;i<n;i++)
					{
						if(a[i]%3==2)
						{
						e=a[i];
						break;
						}
					}
					for(int i=n-1;i>=0;i--)
					{
						if((print==true) && (a[i]==e))
						{
							print=false;
						}
						else
							cout<<a[i];
					}
					cout<<endl;
				}
				else if(count1>=2)
				{
					for(int i=1;i<n;i++)
					{
						if(a[i]%3==1)
						{
							if(e==-1)
								e=a[i];
							else
							{
								f=a[i];
								break;
							}
						}
					}
					for(int i=n-1;i>=0;i--)
					{
						if((pre==true) && (a[i]==e))
						{
							pre=false;
						}
						else if((prf==true) && (a[i]==f))
						{
							prf=false;
						}
						else
							cout<<a[i];
					}
					cout<<endl;
				}
				else
					cout<<-1<<endl;
			}
		}
		else
			cout<<0<<endl;
	}
	else
		cout<<-1<<endl;
	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n],zero=0,sum=0,count1=0,count2=0,e=-1,f=-1,allzero=0;
	bool print=true,pre=true,prf=true;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
		if(a[i]!=0)
			allzero++;
		if(a[i]%3==1)
			count1++;
		else if(a[i]%3==2)
			count2++;

	}
	sort(a,a+n);
	if(a[0]==0)
	{
		if(allzero>0)
		{
			if(sum%3==0)
			{
				for(int i=0;i<n;i++)
					cout<<a[i];
				cout<<endl;
			}
			else if(sum%3==1)
			{
				if(allzero<=1)
				{
					cout<<0<<endl;
				}
				else
				{
					if(count1>0)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==1)
							{
							e=a[i];
							break;
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((print==true) && (a[i]==e))
							{
								print=false;
							}
							else
								cout<<a[i];
						}
					}
					else if(count2>=2)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==2)
							{
								if(e==-1)
									e=a[i];
								else
								{
									f=a[i];
									break;
								}
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((pre==true) && (a[i]==e))
							{
								pre=false;
							}
							else if((prf==true) && (a[i]==f))
							{
								prf=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else
						cout<<-1<<endl;
				}
			}
			else
			{
				if(allzero<=1)
				{
					cout<<0<<endl;
				}
				else
				{
					if(count2>0)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==2)
							{
							e=a[i];
							break;
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((print==true) && (a[i]==e))
							{
								print=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else if(count1>=2)
					{
						for(int i=1;i<n;i++)
						{
							if(a[i]%3==1)
							{
								if(e==-1)
									e=a[i];
								else
								{
									f=a[i];
									break;
								}
							}
						}
						for(int i=n-1;i>=0;i--)
						{
							if((pre==true) && (a[i]==e))
							{
								pre=false;
							}
							else if((prf==true) && (a[i]==f))
							{
								prf=false;
							}
							else
								cout<<a[i];
						}
						cout<<endl;
					}
					else
						cout<<-1<<endl;
				}
			}
			else
				cout<<0<<endl;
		}
		else
			cout<<0<<endl;
	}
	else
		cout<<-1<<endl;
	return 0;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n],zero=0,sum=0,count1=0,count2=0,e=-1,f=-1;
	bool print=true,pre=true,prf=true;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
		if(a[i]%3==1)
			count1++;
		else if(a[i]%3==2)
			count2++;
	}
	sort(a,a+n);
	if(a[0]==0)
	{
		if(sum%3==0)
		{
			for(int i=0;i<n;i++)
				cout<<a[i];
			cout<<endl;
		}
		else if(sum%3==1)
		{
			if(count1>0)
			{
				for(int i=1;i<n;i++)
				{
					if(a[i]%3==1)
					{
           e=a[i];
					 break;
					}
				}
				for(int i=n-1;i>=0;i--)
				{
					if((print==true) && (a[i]==e))
					{
						print=false;
					}
					else
						cout<<a[i];
				}
			}
			else if(count2>=2)
			{
				for(int i=1;i<n;i++)
				{
					if(a[i]%3==2)
					{
						if(e==-1)
							e=a[i];
						else
						{
							f=a[i];
							break;
						}
					}
				}
				for(int i=n-1;i>=0;i--)
				{
					if((pre==true) && (a[i]==e))
					{
						pre=false;
					}
					else if((prf==true) && (a[i]==f))
					{
						prf=false;
					}
					else
						cout<<a[i];
				}
				cout<<endl;
			}
			else
				cout<<-1<<endl;
		}
		else
		{
			if(count2>0)
			{
				for(int i=1;i<n;i++)
				{
					if(a[i]%3==2)
					{
           e=a[i];
					 break;
					}
				}
				for(int i=n-1;i>=0;i--)
				{
					if((print==true) && (a[i]==e))
					{
						print=false;
					}
					else
						cout<<a[i];
				}
				cout<<endl;
			}
			else if(count1>=2)
			{
				for(int i=1;i<n;i++)
				{
					if(a[i]%3==1)
					{
						if(e==-1)
							e=a[i];
						else
						{
							f=a[i];
							break;
						}
					}
				}
				for(int i=n-1;i>=0;i--)
				{
					if((pre==true) && (a[i]==e))
					{
						pre=false;
					}
					else if((prf==true) && (a[i]==f))
					{
						prf=false;
					}
					else
						cout<<a[i];
				}
				cout<<endl;
			}
			else
				cout<<-1<<endl;
		}
	}
	else
		cout<<-1<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin >> n;
    ll arr[n];
    ll check = 0;
    ll sum = 0;
    string ans;
    for (ll i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] == 0)
        {
            check++;
        }
        ans.push_back(arr[i] + '0');
        sum += arr[i];
    }
    if (!check)
    {
        cout << -1 << endl;
    }
    else
    {
        //ans.push_back('0');
        sort(ans.begin(), ans.end());
        //cout<<sum<<endl;
        ll a = sum % 3;
        if (a == 0)
        {
            reverse(ans.begin(), ans.end());
            if (ans.size() == check)
            {
                ans = "0";
            }
            cout << ans << endl;
        }
        else
        {
            for (int i = 1; i < n; i++)
            {
                if ((ans[i] - '0') % 3 == a)
                {
                    ans.erase(ans.begin() + i);
                    reverse(ans.begin(), ans.end());
                    if (ans.size() == check)
                    {
                        ans = "0";
                    }
                    cout << ans << endl;
                    return 0;
                }
            }
            int checks = 2;
            a = 3 - a;
            for (int i = 1; i < n; i++)
            {
                if ((ans[i] - '0') % 3 == a)
                {
                    ans.erase(ans.begin() + i);
                    checks--;
                    if (checks == 0)
                    {
                        reverse(ans.begin(), ans.end());
                        if (ans.size() == check)
                        {
                            ans = "0";
                        }
                        cout << ans << endl;
                        return 0;
                    }
                    i--;
                }
            }
            cout << -1;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin >> n;
    ll arr[n];
    int check = 0;
    ll sum = 0;
    string ans;
    for (ll i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] == 0)
        {
            check = 1;
        }
        ans.push_back(arr[i] + '0');
        sum += arr[i];
    }
    if (!check)
    {
        cout << -1 << endl;
    }
    else
    {
        sort(ans.begin(), ans.end());
        //cout<<sum<<endl;
        ll a = sum % 3;
        if (a == 0)
        {
            reverse(ans.begin(), ans.end());
            cout << ans << endl;
        }
        else
        {
            for (int i = 1; i < n; i++)
            {
                if ((ans[i] - '0') % 3 == a)
                {
                    ans.erase(ans.begin() + i);
                    reverse(ans.begin(), ans.end());
                    cout << ans << endl;
                    return 0;
                }
            }
            int check = 2;
            a = 3 - a;
            for (int i = 1; i < n; i++)
            {
                if ((ans[i] - '0') % 3 == a)
                {
                    ans.erase(ans.begin() + i);
                    check--;
                    if(check==0)
                    {
                        reverse(ans.begin(), ans.end());
                        cout << ans << endl;
                        return 0;
                    }
                    i--;
                }
            }
            cout << -1;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin >> n;
    ll arr[n];
    int check = 0;
    ll d3[3];
    vector<ll> d3v[3];
    vector<ll> temp;
    d3v[0] = temp;
    d3v[1] = temp;
    d3v[2] = temp;
    memset(d3, 0, sizeof(d3));
    for (ll i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] == 0)
        {
            check = 1;
        }
        else
        {
            d3[arr[i] % 3]++;
            d3v[arr[i] % 3].push_back(arr[i]);
        }
    }
    if (!check)
    {
        cout << -1 << endl;
    }
    else
    {
        string ans;
        ans.push_back('0');
        ll a = 0;
        ll b = 0;
        if (d3[1] >= d3[2])
        {
            a = d3[2];
            d3[1] -= d3[2];
            a += 3 * ((d3[1]) / 3);
            b = d3[2];
        }
        else
        {
            b = d3[1];
            d3[2] -= d3[1];
            b += 3 * ((d3[2]) / 3);
            b = d3[1];
        }
        for (auto i : d3v[0])
        {
            ans.push_back(i + '0');
        }
        sort(d3v[1].begin(), d3v[1].end(), greater<ll>());
        sort(d3v[2].begin(), d3v[2].end(), greater<ll>());
        //cout<<a;
        for (auto i : d3v[1])
        {
            ans.push_back(i + '0');
            a--;
            if (a == 0)
            {
                break;
            }
        }
        for (auto i : d3v[2])
        {
            ans.push_back(i + '0');
            b--;
            if (b == 0)
            {
                break;
            }
        }
        sort(ans.begin(), ans.end());
        reverse(ans.begin(), ans.end());
        cout << ans << endl;
        return 0;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin >> n;
    ll arr[n];
    int check = 0;
    ll d3[3];
    vector<ll> d3v[3];
    vector<ll> temp;
    d3v[0] = temp;
    d3v[1] = temp;
    d3v[2] = temp;
    memset(d3, 0, sizeof(d3));
    for (ll i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] == 0)
        {
            check = 1;
        }
        else
        {
            d3[arr[i] % 3]++;
            d3v[arr[i] % 3].push_back(arr[i]);
        }
    }
    if (!check)
    {
        cout << -1 << endl;
    }
    else
    {
        string ans;
        ans.push_back('0');
        ll a = 0;
        ll b = 0;
        if (d3[1] >= d3[2])
        {
            a = d3[2];
            d3[1] -= d3[2];
            a = 3 * ((d3[1]) / 3);
            b = d3[2];
        }
        else
        {
            b = d3[1];
            d3[2] -= d3[1];
            b = 3 * ((d3[2]) / 3);
            b = d3[1];
        }
        for (auto i : d3v[0])
        {
            ans.push_back(i + '0');
        }
        sort(d3v[1].begin(), d3v[1].end(), greater<ll>());
        sort(d3v[2].begin(), d3v[2].end(), greater<ll>());
        for (auto i : d3v[1])
        {
            ans.push_back(i + '0');
            a--;
            if (a == 0)
            {
                break;
            }
        }
        for (auto i : d3v[2])
        {
            ans.push_back(i + '0');
            b--;
            if (b == 0)
            {
                break;
            }
        }
        sort(ans.begin(), ans.end());
        reverse(ans.begin(), ans.end());
        cout << ans << endl;
        return 0;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin >> n;
    ll arr[n];
    int check = 0;
    ll sum = 0;
    string ans;
    for (ll i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] == 0)
        {
            check = 1;
        }
        else
        {
            ans.push_back(arr[i] + '0');
        }
        sum += arr[i];
    }
    if (!check)
    {
        cout << -1 << endl;
    }
    else
    {
        ans.push_back('0');
        sort(ans.begin(), ans.end());
        //cout<<sum<<endl;
        ll a = sum % 3;
        if (a == 0)
        {
            reverse(ans.begin(), ans.end());
            cout << ans << endl;
        }
        else
        {
            for (int i = 1; i < n; i++)
            {
                if ((ans[i] - '0') % 3 == a)
                {
                    ans.erase(ans.begin() + i);
                    reverse(ans.begin(), ans.end());
                    cout << ans << endl;
                    return 0;
                }
            }
            int check = 2;
            a = 3 - a;
            for (int i = 1; i < n; i++)
            {
                if ((ans[i] - '0') % 3 == a)
                {
                    ans.erase(ans.begin() + i);
                    check--;
                    if(check==0)
                    {
                        reverse(ans.begin(), ans.end());
                        cout << ans << endl;
                        return 0;
                    }
                    i--;
                }
            }
            cout << -1;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin >> n;
    ll arr[n];
    int check = 0;
    ll sum = 0;
    string ans;
    for (ll i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] == 0)
        {
            check = 1;
        }
        ans.push_back(arr[i] + '0');
        sum += arr[i];
    }
    if (!check)
    {
        cout << -1 << endl;
    }
    else
    {
        sort(ans.begin(), ans.end());
        ll a = sum % 3;
        if (a == 0)
        {
            reverse(ans.begin(), ans.end());
            cout << ans << endl;
        }
        else
        {
            for (int i = 1; i < n; i++)
            {
                if ((ans[i] - '0') % 3 == a)
                {
                    ans.erase(ans.begin() + i);
                    reverse(ans.begin(), ans.end());
                    cout << ans << endl;
                    return 0;
                }
            }
            int check = 2;
            a = 3 - a;
            for (int i = 1; i < n; i++)
            {
                if ((ans[i] - '0') % 3 == a)
                {
                    ans.erase(ans.begin() + i);
                    check--;
                    if(check==0)
                    {
                        reverse(ans.begin(), ans.end());
                        cout << ans << endl;
                        return 0;
                    }
                }
            }
            cout << -1;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin >> n;
    ll arr[n];
    ll check = 0;
    ll sum = 0;
    string ans;
    for (ll i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (arr[i] == 0)
        {
            check++;
        }
        ans.push_back(arr[i] + '0');
        sum += arr[i];
    }
    if (!check)
    {
        cout << -1 << endl;
    }
    else
    {
        //ans.push_back('0');
        sort(ans.begin(), ans.end());
        //cout<<sum<<endl;
        ll a = sum % 3;
        if (a == 0)
        {
            reverse(ans.begin(), ans.end());
            if (ans.size() == check)
            {
                ans = "0";
            }
            cout << ans << endl;
        }
        else
        {
            for (int i = 1; i < n; i++)
            {
                if ((ans[i] - '0') % 3 == a)
                {
                    ans.erase(ans.begin() + i);
                    reverse(ans.begin(), ans.end());
                    if (ans.size() == check)
                    {
                        ans = "0";
                    }
                    cout << ans << endl;
                    return 0;
                }
            }
            int check = 2;
            a = 3 - a;
            for (int i = 1; i < n; i++)
            {
                if ((ans[i] - '0') % 3 == a)
                {
                    ans.erase(ans.begin() + i);
                    check--;
                    if (check == 0)
                    {
                        reverse(ans.begin(), ans.end());
                        if (ans.size() == check)
                        {
                            ans = "0";
                        }
                        cout << ans << endl;
                        return 0;
                    }
                    i--;
                }
            }
            cout << -1;
        }
    }
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , a[N] , ko , k , l ;
vector <int> full , fix[3];
int main(){
fcin;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        if(a[i]==0) ko = 1;
        if(a[i]%3==0) {full.pb(a[i]); continue;}
        fix[a[i]%3].pb(a[i]);
    }
    if(!ko) {cout<<"-1\n"; return 0;}
    k = (max(fix[1].size(),fix[2].size()) - min(fix[1].size(),fix[2].size()))%3;
    for(int j = 1 ;j<=2 ; j++) sort(fix[j].begin(),fix[j].end());
    if(k==0){
        for(int j = 1 ; j<=2 ; j++){
            while(fix[j].size()>0){
                full.pb(fix[j].back());
                fix[j].pop_back();
            }
        }
    }
    else if(k>=(fix[1].size()%3 + fix[2].size()%3)){
    for(int j = 1 ;j<=2; j++){
      reverse(fix[j].begin(), fix[j].end());
        for(int i = 0 ; i < fix[j].size() ; i+=3){
            if(i+2>=fix[j].size())break;
            for(int k = i ; k<=i+2;k++)full.pb(fix[j][k]);
        }
    }
    }
    else{
        for(int j = 1 ;j<=2 ; j++){
            while(min(fix[1].size(),fix[2].size())>0){
                full.pb(fix[j].back());
                fix[j].pop_back();
            }
        }
        for(int j = 1 ;j<=2 ; j++){
        reverse(fix[j].begin(), fix[j].end());
        for(int i = 0 ; i < fix[j].size() ; i+=3){
            if(i+2>=fix[j].size())break;
            for(int k = i ; k<=i+2;k++)full.pb(fix[j][k]);
            }
         }
    }
    sort(full.begin(),full.end());reverse(full.begin(),full.end());
    if(full[0]==0){cout<<"0\n";return 0;}
    for(auto each:full)cout<<each;
    cout<<"\n";

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , a[N] , ko , k;
vector <int> full , fix[3];
int main(){
fcin;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        if(a[i]==0) ko = 1;
        if(a[i]%3==0) {full.pb(a[i]); continue;}
        fix[a[i]%3].pb(a[i]);
    }
    if(!ko) {cout<<"-1\n"; return 0;}
    for(int j = 1 ; j <= 2 ; j++){
        sort(fix[j].begin(), fix[j].end());
        reverse(fix[j].begin(), fix[j].end());
         for(int i = k ; i < fix[j].size() ; i+=3) {
            if(i+2>=fix[j].size()) break;
            full.pb(fix[j][i]);
            full.pb(fix[j][i+1]);
            full.pb(fix[j][i+2]);
        }
    }
    int f = min(fix[1].size()%3,fix[2].size()%3);
    for(int j = 1 ; j<= 2 ; j++){
        if(f==2) {
            full.pb(fix[j][fix[j].size()-1]);
            full.pb(fix[j][fix[j].size()-2]);
        }
        else if(f==1){
            full.pb(fix[j][fix[j].size()-2]);
        }
        else break;
    }
    sort(full.begin(),full.end());reverse(full.begin(),full.end());
    if(full[0]==0){cout<<"0\n";return 0;}
    for(auto each:full)cout<<each;

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , a[N] , ko , k , l ;
vector <int> full , fix[3];
int main(){
fcin;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        if(a[i]==0) ko = 1;
        if(a[i]%3==0) {full.pb(a[i]); continue;}
        fix[a[i]%3].pb(a[i]);
    }
    if(!ko) {cout<<"-1\n"; return 0;}
    for(int i = 1 ; i <= 2 ; i++){
        sort(fix[i].begin(),fix[i].end());
    }
    if(fix[1].size()%3!=fix[2].size()%3){
      while(min(fix[1].size(),fix[2].size())>0){
        for(int j = 1 ; j <= 2 ; j++){
            full.pb(fix[j].back());
            fix[j].pop_back();
        }
      }
      while(max(fix[1].size(),fix[2].size())>0){
        for(int j = 1 ;j<=2 ; j++){
            if(fix[j].empty()) continue;
            full.pb(fix[j].back());
            fix[j].pop_back();
        }
      }
    }
    else{
        for(int j = 1 ; j<= 2 ; j++){
            while(fix[j].size()>0){
                full.pb(fix[j].back());
                fix[j].pop_back();
            }
        }
    }
    sort(full.begin(),full.end());reverse(full.begin(),full.end());
    if(full[0]==0){cout<<"0\n";return 0;}
    for(auto each:full)cout<<each;

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , a[N] , ko , k , l , sum ;
vector <int> full;
int main(){
fcin;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        sum += a[i];
        if(a[i]==0) ko = 1;
        full.pb(a[i]);
    }
    if(!ko){cout<<"-1\n";return 0;}
    sort(full.begin(),full.end());
    if(sum%3==0){
        reverse(full.begin(),full.end());
        for(auto each:full)cout<<each;
            return 0;
    }
    else if(sum%3==1){
        int cmin = INF;
        for(int i = 0 ; i< n ; i++){
            if(a[i]%3==1) cmin = min(cmin,a[i]);
        }
        if(cmin==INF){
            int c = 0;
            for(int i = 0 ; i <full.size();i++){
                if(full[i]%3==2 && c<2) {
                    full[i]=-1;
                    c++;
                }
            }
        }
        else{
            for(int i = 0 ; i < n ; i++){
                if(full[i]==cmin) {full[i]=-1; break;}
            }
        }
    }
    else if(sum%3==2){
        int cmin = INF;
        for(int i = 0 ; i< n ; i++){
            if(a[i]%3==2) cmin = min(cmin,a[i]);
        }
        if(cmin==INF){
            int c = 0;
            for(int i = 0 ; i <full.size();i++){
                if(full[i]%3==1 && c<2) {
                    full[i]=-1;
                    c++;
                }
            }
        }
        else{
            for(int i = 0 ; i < n ; i++){
                if(full[i]==cmin) {full[i]=-1; break;}
            }
        }
    }
    reverse(full.begin(),full.end());
    bool ok = 0;
    for(auto each:full){
        if(each==-1) continue;
        if(each>0) {ok = 1;}
    }if(ok){for(auto each:full) if(each!=-1)cout<<each;}
    else cout<<"0\n";
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , a[N] , ko , k , l ;
vector <int> full , fix[3];
int main(){
fcin;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        if(a[i]==0) ko = 1;
        if(a[i]%3==0) {full.pb(a[i]); continue;}
        fix[a[i]%3].pb(a[i]);
    }
    if(!ko) {cout<<"-1\n"; return 0;}
    k = (max(fix[1].size(),fix[2].size()) - min(fix[1].size(),fix[2].size()))%3;
    for(int j = 1 ;j<=2 ; j++) sort(fix[j].begin(),fix[j].end()),reverse(fix[j].begin(), fix[j].end());
    if(fix[1].size() > fix[2].size()) l = 1;
    else l = 2;
    for(int i = 0 ; i < fix[l].size()-k ; i++){
        full.pb(fix[l][i]);
    }
    if(l==1) l =2;
    else l = 1;
    for(int i = 0 ; i < fix[l].size() ; i++){
        full.pb(fix[l][i]);
    }
    sort(full.begin(),full.end());reverse(full.begin(),full.end());
    if(full[0]==0){cout<<"0\n";return 0;}
    for(auto each:full)cout<<each;

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , a[N] , ko , k;
vector <int> full , fix[3];
int main(){
fcin;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        if(a[i]==0) ko = 1;
        if(a[i]%3==0) {full.pb(a[i]); continue;}
        fix[a[i]%3].pb(a[i]);
    }
    if(!ko) {cout<<"-1\n"; return 0;}
    int fr = fix[1].size(), sc = fix[2].size();
    k = min(fr,sc);
    for(int i = 1 ; i <= 2 ; i++){
        sort(fix[i].begin(),fix[i].end());
        reverse(fix[i].begin(),fix[i].end());
    }
    for(int i = 0 ; i < k ; i++) { full.pb(fix[1][i]); full.pb(fix[2][i]); }
    sort(full.begin(),full.end());reverse(full.begin(),full.end());
    for(auto each:full)cout<<each;

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , a[N] , ko , k , l ;
vector <int> full , fix[3];
int main(){
fcin;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        if(a[i]==0) ko = 1;
        if(a[i]%3==0) {full.pb(a[i]); continue;}
        fix[a[i]%3].pb(a[i]);
    }
    if(!ko) {cout<<"-1\n"; return 0;}
    k = (max(fix[1].size(),fix[2].size()) - min(fix[1].size(),fix[2].size()))%3;
    for(int j = 1 ;j<=2 ; j++) sort(fix[j].begin(),fix[j].end());
    if(k==0){
        for(int j = 1 ; j<=2 ; j++){
            while(fix[j].size()>0){
                full.pb(fix[j].back());
                fix[j].pop_back();
            }
        }
    }
    else if(k>=(max(fix[1].size(),fix[2].size())%3)){

    for(int j = 1 ;j<=2; j++){
      reverse(fix[j].begin(), fix[j].end());
        for(int i = 0 ; i < fix[j].size() ; i+=3){
            if(i+2>=fix[j].size())break;
            for(int k = i ; k<=i+2;k++)full.pb(fix[j][k]);
        }
    }
    }
    else{
        for(int j = 1 ;j<=2 ; j++){
            while(min(fix[1].size(),fix[2].size())>0){
                full.pb(fix[j].back());
                fix[j].pop_back();
            }
        }
        for(int j = 1 ;j<=2 ; j++){
        reverse(fix[j].begin(), fix[j].end());
        for(int i = 0 ; i < fix[j].size() ; i+=3){
            if(i+2>=fix[j].size())break;
            for(int k = i ; k<=i+2;k++)full.pb(fix[j][k]);
            }
         }
    }
    sort(full.begin(),full.end());reverse(full.begin(),full.end());
    if(full[0]==0){cout<<"0\n";return 0;}
    for(auto each:full)cout<<each;

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , a[N] , ko , k;
vector <int> full , fix[3];
int main(){
fcin;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        if(a[i]==0) ko = 1;
        if(a[i]%3==0) {full.pb(a[i]); continue;}
        fix[a[i]%3].pb(a[i]);
    }
    if(!ko) {cout<<"-1\n"; return 0;}
    int fr = fix[1].size(), sc = fix[2].size();
    k = min(fr,sc);
    for(int i = 1 ; i <= 2 ; i++){
        sort(fix[i].begin(),fix[i].end());
        reverse(fix[i].begin(),fix[i].end());
    }
    for(int i = 0 ; i < k ; i++) { full.pb(fix[1][i]); full.pb(fix[2][i]); }
    if(fr > sc || sc>fr){
        int f = max(fr,sc);
        for(int i = k ; i < fix[(f==fr ? 1 : 2)].size() ; i+=3) {
            if(i+2>=fix[(f==fr ? 1 : 2)].size()) break;
            full.pb(fix[(f==fr ? 1 : 2)][i]); full.pb(fix[(f==fr ? 1 : 2)][i+1]); full.pb(fix[(f==fr ? 1 : 2)][i+2]);
        }
    }
    sort(full.begin(),full.end());reverse(full.begin(),full.end());
    if(full[0]==0){cout<<"0\n";return 0;}
    for(auto each:full)cout<<each;

}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , a[N] , ko , k;
vector <int> full , fix[3];
int main(){
fcin;
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        if(a[i]==0) ko = 1;
        if(a[i]%3==0) {full.pb(a[i]); continue;}
        fix[a[i]%3].pb(a[i]);
    }
    if(!ko) {cout<<"-1\n"; return 0;}
    int fr = fix[1].size(), sc = fix[2].size();
    k = min(fr,sc);
    for(int i = 1 ; i <= 2 ; i++){
        sort(fix[i].begin(),fix[i].end());
        reverse(fix[i].begin(),fix[i].end());
    }
    for(int i = 0 ; i < k ; i++) { full.pb(fix[1][i]); full.pb(fix[2][i]); }
    sort(full.begin(),full.end());reverse(full.begin(),full.end());
    if(full[0]==0){cout<<"0\n";return 0;}
    for(auto each:full)cout<<each;

}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

// (づ°ω°)づﾐ★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    long Gcd(long a, long b)
    {
        if (b == 0)
            return a;
        return Gcd(b, a % b);
    }

    public void Solve()
    {
        int n = ReadInt();
        var a = ReadLongArray();
        long ans = 1;
        var rnd = new Random(13);
        for (int iter = 0; iter < 10; iter++)
        {
            int p = rnd.Next(n);
            var dict = new SDictionary<long, int>();
            for (int i = 0; i < n; i++)
                dict[Gcd(a[i], a[p])]++;
            var list = dict.ToList();
            list.Sort((p1, p2) => p1.Key.CompareTo(p2.Key));
            for (int i = 0; i < list.Count; i++)
            {
                int c = list[i].Value;
                for (int j = i + 1; j < list.Count; j++)
                    if (list[j].Key % list[i].Key == 0)
                        c += list[j].Value;
                if (c >= (n + 1) / 2)
                    ans = Math.Max(ans, list[i].Key);
            }
        }

        Write(ans);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;

    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("subsequences.in");
        //writer = new StreamWriter("subsequences.out");
#endif
        try
        {
//            var thread = new Thread(new Solver().Solve, 1024 * 1024 * 256);
//            thread.Start();
//            thread.Join();
            new Solver().Solve();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            Console.WriteLine(ex);
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write

    private static Queue<string> currentLineTokens = new Queue<string>();

    private static string[] ReadAndSplitLine()
    {
        return reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
    }

    public static string ReadToken()
    {
        while (currentLineTokens.Count == 0)
            currentLineTokens = new Queue<string>(ReadAndSplitLine());
        return currentLineTokens.Dequeue();
    }

    public static int ReadInt()
    {
        return int.Parse(ReadToken());
    }

    public static long ReadLong()
    {
        return long.Parse(ReadToken());
    }

    public static double ReadDouble()
    {
        return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
    }

    public static int[] ReadIntArray()
    {
        return ReadAndSplitLine().Select(int.Parse).ToArray();
    }

    public static long[] ReadLongArray()
    {
        return ReadAndSplitLine().Select(long.Parse).ToArray();
    }

    public static double[] ReadDoubleArray()
    {
        return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
    }

    public static int[][] ReadIntMatrix(int numberOfRows)
    {
        int[][] matrix = new int[numberOfRows][];
        for (int i = 0; i < numberOfRows; i++)
            matrix[i] = ReadIntArray();
        return matrix;
    }

    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows);
        int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = new int[numberOfRows];
            for (int j = 0; j < numberOfRows; j++)
                ret[i][j] = matrix[j][i];
        }
        return ret;
    }

    public static string[] ReadLines(int quantity)
    {
        string[] lines = new string[quantity];
        for (int i = 0; i < quantity; i++)
            lines[i] = reader.ReadLine().Trim();
        return lines;
    }

    public static void WriteArray<T>(IEnumerable<T> array)
    {
        writer.WriteLine(string.Join(" ", array));
    }

    public static void Write(params object[] array)
    {
        WriteArray(array);
    }

    public static void WriteLines<T>(IEnumerable<T> array)
    {
        foreach (var a in array)
            writer.WriteLine(a);
    }

    class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }

    #endregion
}