#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define  print  cout<<
#define FRD freopen("in.txt", "r", stdin)
#define FWR freopen("out.txt", "w", stdout)
#define is_bit_on(n,k)((n>>k)&1)
#define setbit0(n,k)((n)&(!(1<<k)))
#define setbit1(n,k) ((n)|((1<<k)))
#define flipbit(n,k) ((n)^((1<<k)))
#define PI 3.14159265
const int OO = 0x3f3f3f3f ;
typedef long long ll ;
using namespace std  ;
void fastt() ;
int dx[] = { -1, 0, 1, 0 }          ;
int dy[] = { 0, 1, 0, -1}           ;
const int sizee = 1e5 + 5 ;
int N[sizee] ;
bool cmp( int a, int  b )
{
    return a > b ;
}
int meme[(int)1e5+5][4][2] ;
vector < int > ans ;
int n ;
bool term2 = 0 ;
int X = 0 ;
int solve( int ind, int sum, bool f )
{
    if( ind == n + 1  )
    {
        if( sum == 0 && f == 1 )
        {
            return 0 ;
        }
        else
        {
            return  -1 * 1e7 ;
        }
    }
    int &ret = meme[ind][sum][f]       ;
    if( ret != -1 )
    {
        return ret ;
    } //4 1 0
    ret = -2    ;
    if( solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) >= 0 )
    {
        ret  =   max( ret,  1 + solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) ) ;
        //print ret << endl ;
    }
    if( f == 0 )
    {
        if( solve( ind + 1, sum % 3, 0 ) >= 0 )
        {
            ret = max( ret, solve( ind + 1, sum, 0 ) ) ;
        }
    }
    else
    {
        if( solve( ind + 1, sum % 3, 1 ) >= 0 )
        {
            ret = max( ret, solve( ind + 1, sum % 3, 1 ) ) ;
        }
    }
    return ret ;
}
void print_path(int ind, int s, bool f )
{
    if(ind > n)
        return ;
    int Xx = solve(ind + 1, ( N[ind] + s ) % 3, 1 ) ;
    if( Xx >= 0 && meme[ind][s][f] == X )
    {
        if(N[ind] > 0 )
            term2 = 1 ;
        ans.push_back(N[ind]);
        --X ;
        print_path( ind + 1, ( N[ind] + s ) % 3, 1 )  ;
        return ;
    }
    else
    {
        if( !f )
        {
            print_path ( ind + 1, s, 0 ) ;
        }
        else
        {
            print_path(ind + 1, s, 1 ) ;
        }
    }
}
int main()
{
    fastt()  ;
    memset( meme, -1,  sizeof ( meme ) ) ;
    cin >> n      ;
    ll anss = 0   ;
    bool f =false ;
    bool term = 0 ;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> N[i] ;
        if(N[i] > 0 )
            term = 1 ;
        if(!N[i])
            f = true ;
        anss = (ll) anss + N[i]         ;
    }
    if(!term)
    {
        print 0 << endl ;
        return 0        ;
    }
    if(!f)
    {
        print -1  << '\n' ;
        return 0          ;
    }
    sort(N + 1, N + n + 1, cmp ) ;
    X =  solve ( 1, 0, 0 )       ;
    if(X != -1 )
    {
        for( int i = 1 ; i <= n ; i ++ )
        {
            if( meme[i][0][0] == X )
            {
                print_path( i, 0, 0 )    ;
                break ;
            }
        }
        if( !term2 )
        {
            print 0 << endl ;
            return 0 ;
        }
        sort(ans.begin(),ans.end());
        for(int i = ans.size() - 1  ; i >= 0  ; i -- )
        {
            print ans[i] ;
        }
    }
    else
    {
        print -1 << endl ;
    }
}
void fastt()
{
    ios::
    sync_with_stdio(false),
                    cin.tie(0),
                    cout.tie(0) ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define  print  cout<<
#define FRD freopen("in.txt", "r", stdin)
#define FWR freopen("out.txt", "w", stdout)
#define is_bit_on(n,k)((n>>k)&1)
#define setbit0(n,k)((n)&(!(1<<k)))
#define setbit1(n,k) ((n)|((1<<k)))
#define flipbit(n,k) ((n)^((1<<k)))
#define PI 3.14159265
const int OO = 0x3f3f3f3f ;
typedef long long ll ;
using namespace std  ;
void fastt() ;
int dx[] = { -1, 0, 1, 0 }          ;
int dy[] = { 0, 1, 0, -1}           ;
const int sizee = 1e5 + 5           ;
int N[sizee]                        ;
bool cmp( int a, int  b )
{
    return a > b ;
}
int meme[(int)1e5+5][4][2] ;
vector < int > ans ;
int n ;
bool term2 = 0 ;
int X = 0 ;
int solve( int ind, int sum, bool f )
{
    if( ind == n + 1  )
    {
        if( sum == 0 && f == 1 )
        {
            return 0 ;
        }
        else
        {
            return  -1 * 1e9 ;
        }
    }
    int &ret = meme[ind][sum][f]       ;
    if( ret != -1 )
    {
        return ret ;
    } //4 1 0
    ret = -2    ;
    if( solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) >= 0 )
    {
        ret  =   max( ret,  1 + solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) ) ;
        //print ret << endl ;
    }
    if( f == 0 )
    {
        ret = max( ret, solve( ind + 1, sum % 3, 0 ) ) ;
    }
    if( f == 1 )
    {
        ret = max( ret, solve( ind + 1, sum % 3, 1 ) ) ;
    }
    return ret ;
}
void print_path(int ind, int s, bool f )
{
    if(ind > n )
        return ;
    int Xx = solve(ind + 1, ( N[ind] + s ) % 3, 1 ) ;
    if( Xx >= 0 && meme[ind][s][f] == X )
    {
        if(N[ind] > 0 )
            term2 = 1 ;
        ans.push_back(N[ind]);
        --X                  ;
        print_path( ind + 1, ( N[ind] + s ) % 3, 1 )  ;
        return ;
    }
    else
    {
        if( !f )
        {
            print_path ( ind + 1, s % 3, 0 ) ;
        }
        else
        {
            print_path(ind + 1, s % 3, 1 ) ;
        }
    }
}
int main()
{
    fastt()  ;
    memset( meme, -1,  sizeof ( meme ) ) ;
    cin >> n      ;
    ll anss = 0   ;
    bool f =false ;
    bool term = 0 ;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> N[i] ;
        if(N[i] > 0 )
            term = 1 ;
        if(!N[i])
            f = true ;
        anss = (ll) anss + N[i]         ;
    }
    if(!term)
    {
        print 0 << endl ;
        return 0        ;
    }
    if(!f)
    {
        print -1  << '\n' ;
        return 0          ;
    }
    sort(N + 1, N + n + 1, cmp )  ;
    X =  solve ( 1, 0, 0 )        ;
    if(X != -2 )
    {
        for( int i = 1 ; i <= n ; i ++ )
        {
            if( meme[i][0][0] == X )
            {
                print_path( i, 0, 0 )    ;
                break ;
            }
        }
        if( !term2 )
        {
            print 0 << endl ;
            return 0 ;
        }
        sort( ans.begin(),ans.end() ) ;
        for(int i = ans.size() - 1  ; i >= 0  ; i -- )
        {
            print ans[i] ;
        }
    }
    else
    {
        print -1 << endl ;
    }
}
void fastt()
{
    ios::
    sync_with_stdio(false),
                    cin.tie(0),
                    cout.tie(0) ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define  print  cout<<
#define FRD freopen("in.txt", "r", stdin)
#define FWR freopen("out.txt", "w", stdout)
#define is_bit_on(n,k)((n>>k)&1)
#define setbit0(n,k)((n)&(!(1<<k)))
#define setbit1(n,k) ((n)|((1<<k)))
#define flipbit(n,k) ((n)^((1<<k)))
#define PI 3.14159265
const int OO = 0x3f3f3f3f ;
typedef long long ll ;
using namespace std  ;
void fastt() ;
int dx[] = { -1, 0, 1, 0 }          ;
int dy[] = { 0, 1, 0, -1}           ;
const int sizee = 1e5 + 5 ;
int N[sizee] ;
bool cmp( int a, int  b )
{
    return a > b ;
}
int meme[(int)1e5+5][3][2];
vector<int>ans;
int n ;
bool term2 = 0 ;
bool solve( int ind, int sum, bool f )
{
    if(ind > n )
        return sum == 0 && f ;
    int &ret = meme[ind][sum][f] ;
    if( ~ret )
        return ret ;
    ret = 0 ;
    ret = solve( ind + 1, ( N[ind] + sum ) % 3, 1 ) ;
    if(!f)
        ret |= solve( ind + 1, sum % 3, 0 ) ;
    else
        ret |= solve( ind + 1, sum % 3, 1 ) ;
    return ret ;
}
void print_path(int ind, int s, bool f )
{
    if(ind > n)
        return ;
    bool ff = false    ;
    ff = solve( ind + 1, ( N[ind] + s ) % 3, 1 ) ;
    if( ff )
    {
        if( N[ind] > 0 )
            term2 = 1 ;
        ans.push_back( N[ind] )    ;
        print_path( ind + 1, ( N[ind] + s ) % 3, 1 ) ;
        return       ;
    }
    if( !f )
        print_path( ind + 1, ( s ) % 3, 0 ) ;
    else
        print_path( ind + 1, ( s ) % 3, 1 )     ;
}
int main()
{
    fastt()  ;
    memset(meme,-1,sizeof(meme));
    cin >> n   ;
    ll anss = 0 ;
    bool f =false ;
    bool term = 0 ;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> N[i] ;
        if(N[i] > 0 )
            term = 1 ;
        if(!N[i])
            f = true ;
        anss = (ll) anss + N[i]         ;
    }
    sort( N + 1, N + n + 1, cmp )       ;
    if(!term)
    {
        print 0 << endl ;
        return 0        ;
    }
    if(!f)
    {
        print -1  << '\n' ;
        return 0          ;
    }
    if( anss % 3 == 0 && f )
    {
        for(int i = 1 ; i <= n ; i ++ )
            print N[i] ;
        return 0 ;
    }
    vector < int > temp    ;
    if( solve ( 1, 0, 0 ) )
    {
        for( int i = 1 ; i <= n ; i ++ )
        {
            if( meme[i][0][0] == 1 )
            {
                print_path( i, 0, 0 )    ;
                int X = temp.size() ;
                int Y = ans.size()  ;
                if(  X < Y )
                {
                    if(X) temp.clear() ;
                    for( int i = 0 ; i < ans.size() ; i ++ )
                    {
                        temp.push_back(ans[i]);
                    }
                }
                ans.clear() ;
            }
        }
        if( !term2 )
        {
            print 0 << endl ;
            return 0 ;
        }
        sort(temp.begin(),temp.end())   ;
        reverse(temp.begin(),temp.end())   ;
        for( int i = 0 ; i < temp.size() ; i ++ )
            print temp[i] ;
    }
    else
    {
        print -1 << endl ;
    }
}
void fastt()
{
    ios::
    sync_with_stdio(false),
                    cin.tie(0),
                    cout.tie(0) ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define  print  cout<<
#define FRD freopen("in.txt", "r", stdin)
#define FWR freopen("out.txt", "w", stdout)
#define is_bit_on(n,k)((n>>k)&1)
#define setbit0(n,k)((n)&(!(1<<k)))
#define setbit1(n,k) ((n)|((1<<k)))
#define flipbit(n,k) ((n)^((1<<k)))
#define PI 3.14159265
const int OO = 0x3f3f3f3f ;
typedef long long ll ;
using namespace std  ;
void fastt() ;
int dx[] = { -1, 0, 1, 0 }          ;
int dy[] = { 0, 1, 0, -1}           ;
const int sizee = 1e5 + 5 ;
int N[sizee] ;
bool cmp( int a, int  b )
{
    return a > b ;
}
int meme[(int)1e5+5][4][2] ;
vector < int > ans ;
int n ;
bool term2 = 0 ;
int X = 0 ;
int solve( int ind, int sum, bool f )
{
    if( ind == n + 1  )
    {
        if( sum == 0 && f == 1 )
        {
            return 0 ;
        }
        else
        {
            return  -1 * 1e7 ;
        }
    }
    int &ret = meme[ind][sum][f]       ;
    if( ret != -1 )
        return ret         ; //4 1 0
    ret = -1    ;
    if( solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) >= 0 )
    {
        ret  =   max( ret,  1 + solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) ) ;
        //print ret << endl ;
    }
    if( f == 0 )
    {
        if( solve( ind + 1, sum % 3, 0 ) >= 0 )
        {
            ret = max( ret, solve( ind + 1, sum, 0 ) ) ;
        }
    }
    else
    {
        if( solve( ind + 1, sum % 3, 1 ) >= 0 )
        {
            ret = max( ret, solve( ind + 1, sum % 3, 1 ) ) ;
        }
    }
    return ret ;
}
void print_path(int ind, int s, bool f )
{
    if(ind > n) return ;
    int Xx = solve(ind + 1, ( N[ind] + s ) % 3, 1 ) ;
    if( Xx >= 0 && meme[ind][s][f] == X )
    {
        if(N[ind] > 0 )
            term2 = 1 ;
        ans.push_back(N[ind]);
        --X ;
        print_path( ind + 1, ( N[ind] + s ) % 3, 1 )  ;
        return ;
    }
    else
    {
        if( !f )
        {
            print_path ( ind + 1, s, 0 ) ;
        }
        else
        {
            print_path(ind + 1, s, 1 ) ;
        }
    }
}
int main()
{
    fastt()  ;
    memset( meme, -1,  sizeof ( meme ) ) ;
    cin >> n      ;
    ll anss = 0   ;
    bool f =false ;
    bool term = 0 ;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> N[i] ;
        if(N[i] > 0 )
            term = 1 ;
        if(!N[i])
            f = true ;
        anss = (ll) anss + N[i]         ;
    }
    if(!term)
    {
        print 0 << endl ;
        return 0        ;
    }
    if(!f)
    {
        print -1  << '\n' ;
        return 0          ;
    }
    sort(N + 1, N + n + 1, cmp ) ;
    X =  solve ( 1, 0, 0 )       ;
    if(X != -1 )
    {
        for( int i = 1 ; i <= n ; i ++ )
        {
            if( meme[i][0][0] == X )
            {
                print_path( i, 0, 0 )    ;
                break ;
            }
        }
        if( !term2 )
        {
            print 0 << endl ;
            return 0 ;
        }
        sort(ans.begin(),ans.end());
        for(int i = ans.size() - 1  ; i >= 0  ; i -- )
        {
            print ans[i] ;
        }
    }
    else
    {
        print -1 << endl ;
    }
}
void fastt()
{
    ios::
    sync_with_stdio(false),
                    cin.tie(0),
                    cout.tie(0) ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define  print  cout<<
#define FRD freopen("in.txt", "r", stdin)
#define FWR freopen("out.txt", "w", stdout)
#define is_bit_on(n,k)((n>>k)&1)
#define setbit0(n,k)((n)&(!(1<<k)))
#define setbit1(n,k) ((n)|((1<<k)))
#define flipbit(n,k) ((n)^((1<<k)))
#define PI 3.14159265
const int OO = 0x3f3f3f3f ;
typedef long long ll ;
using namespace std  ;
void fastt() ;
int dx[] = { -1, 0, 1, 0 }          ;
int dy[] = { 0, 1, 0, -1}           ;
const int sizee = 1e5 + 5 ;
int N[sizee] ;
bool cmp( int a, int  b )
{
    return a > b ;
}
int meme[(int)1e5+5][3][2];
vector<int>ans;
int n ;
bool term2 = 0 ;
bool solve( int ind, int sum , bool f )
{
    if(ind > n ) return sum == 0 && f ;
    int &ret = meme[ind][sum][f] ;
    if( ~ret ) return ret ;
    ret = 0 ;
    ret = solve( ind + 1 , ( N[ind] + sum ) % 3 , 1 ) ;
    if(!f)
        ret |= solve( ind + 1 , sum % 3 , 0 ) ;
    else
        ret |= solve( ind + 1 , sum % 3 , 1 ) ;
    return ret ;
}
void print_path(int ind, int s, bool f )
{
    if(ind > n) return ;
    bool ff = false ;
    ff = solve(ind+1 , (N[ind]+s)%3 , 1 ) ;
    if(ff)
    {
        if(N[ind] > 0 )term2 = 1 ;
        ans.push_back(N[ind]);
        print_path(ind+1 , (N[ind]+s)%3 , 1 ) ;
        return       ;
    }
    if(!f)
    {
        print_path( ind + 1 , (s) % 3 , 0 ) ;
    }
    else
    {
        print_path( ind + 1 , (s) % 3 , 1 ) ;
    }
}
int main()
{
    fastt()  ;
    memset(meme,-1,sizeof(meme));
    cin >> n   ;
    ll anss = 0 ;
    bool f =false ;
    bool term = 0 ;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> N[i] ;
        if(N[i] > 0 ) term = 1 ;
        if(!N[i])
            f = true ;
        anss = (ll) anss + N[i]         ;
    }
    sort( N + 1, N + n + 1, cmp )  ;
    if(!term)
    {
        print 0 << endl ;
        return 0        ;
    }
    if(!f)
    {
        print -1  << '\n' ;
        return 0          ;
    }
    if( anss % 3 == 0 && f )
    {
        for(int i = 1 ; i <= n ; i ++ )
            print N[i] ;
        return 0 ;
    }
    if(solve(1,0,0))
    {
       print_path(1,0,0) ;
       if(!term2)
       {
           print 0 << endl ;
           return 0 ;
       }
       sort(ans.begin(),ans.end());
       reverse(ans.begin(),ans.end());
       for( int i = 0 ; i < ans.size() ; i ++ )
       {
           print ans[i];
       }
    }
    else
    {
        print -1 << endl ;
    }
}
void fastt()
{
    ios::
    sync_with_stdio(false),
                    cin.tie(0),
                    cout.tie(0) ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define  print  cout<<
#define FRD freopen("in.txt", "r", stdin)
#define FWR freopen("out.txt", "w", stdout)
#define is_bit_on(n,k)((n>>k)&1)
#define setbit0(n,k)((n)&(!(1<<k)))
#define setbit1(n,k) ((n)|((1<<k)))
#define flipbit(n,k) ((n)^((1<<k)))
#define PI 3.14159265
const int OO = 0x3f3f3f3f ;
typedef long long ll ;
using namespace std  ;
void fastt() ;
int dx[] = { -1, 0, 1, 0 }          ;
int dy[] = { 0, 1, 0, -1}           ;
const int sizee = 1e5 + 5           ;
int N[sizee]                        ;
bool cmp( int a, int  b )
{
    return a > b ;
}
int meme[(int)1e5+5][4][2] ;
vector < int > ans ;
int n ;
bool term2 = 0 ;
int X = 0 ;
int solve( int ind, int sum, bool f )
{
    if( ind == n + 1  )
    {
        if( sum == 0 && f == 1 )
        {
            return 0 ;
        }
        else
        {
            return  -1 * 1e9 ;
        }
    }
    int &ret = meme[ind][sum][f]       ;
    if( ret != -1 )
    {
        return ret ;
    } //4 1 0
    ret = -1e9    ;
    if( solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) >= 0 )
    {
        ret  =   max( ret,  1 + solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) ) ;
        //print ret << endl ;
    }
    if( f == 0 )
    {
        ret = max( ret, solve( ind + 1, sum % 3, 0 ) ) ;
    }
    if( f == 1 )
    {
        ret = max( ret, solve( ind + 1, sum % 3, 1 ) ) ;
    }
    return ret ;
}
void print_path( int ind, int s, bool f )
{
    if( ind == n + 1 )
        return ;
    int Xx = solve( ind + 1, (N[ind] + s) % 3, 1 )   ;
    if( Xx >= 0   )
    {
        ++Xx ;
        if(Xx == X )
        {

            if( N[ind] > 0 )
                term2 = 1 ;
            ans.push_back(N[ind]) ;
            --X                   ;
            print_path( ind + 1, ( N[ind] + s ) % 3, 1 )  ;
            return ;
        }
    }
    if( !f )
    {
        print_path ( ind + 1, s % 3, 0 ) ;
    }
    else
    {
        print_path(ind + 1, s % 3, 1 ) ;
    }
}
int main()
{
    fastt()  ;
    memset( meme, -1,  sizeof ( meme ) ) ;
    cin >> n      ;
    ll anss = 0   ;
    bool f =false ;
    bool term = 0 ;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> N[i] ;
        if(N[i] > 0 )
            term = 1 ;
        if(!N[i])
            f = true ;
        anss = (ll) anss + N[i]         ;
    }
    if(!term)
    {
        print 0 << endl ;
        return 0        ;
    }
    if(!f)
    {
        print -1  << '\n' ;
        return 0          ;
    }
    sort(N + 1, N + n + 1, cmp )  ;
    X =  solve ( 1, 0, 0 )        ;
    if(X != -1e9 )
    {
        for( int i = 1 ; i <= n ; i ++ )
        {
            if( meme[i][0][0] == X )
            {
                print_path( i, 0, 0 )    ;
                break ;
            }
        }
        if( !term2 )
        {
            print 0 << endl ;
            return 0 ;
        }
        sort( ans.begin(),ans.end() ) ;
        for(int i = ans.size() - 1  ; i >= 0  ; i -- )
        {
            print ans[i] ;
        }
    }
    else
    {
        print -1 << endl ;
    }
}
void fastt()
{
    ios::
    sync_with_stdio(false),
                    cin.tie(0),
                    cout.tie(0) ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define  print  cout<<
#define FRD freopen("in.txt", "r", stdin)
#define FWR freopen("out.txt", "w", stdout)
#define is_bit_on(n,k)((n>>k)&1)
#define setbit0(n,k)((n)&(!(1<<k)))
#define setbit1(n,k) ((n)|((1<<k)))
#define flipbit(n,k) ((n)^((1<<k)))
#define PI 3.14159265
const int OO = 0x3f3f3f3f ;
typedef long long ll ;
using namespace std  ;
void fastt() ;
int dx[] = { -1, 0, 1, 0 }          ;
int dy[] = { 0, 1, 0, -1}           ;
const int sizee = 1e5 + 5 ;
int N[sizee] ;
bool cmp( int a, int  b )
{
    return a > b ;
}
int meme[(int)1e5+5][4][2] ;
vector < int > ans ;
int n ;
bool term2 = 0 ;
int X = 0 ;
int solve( int ind, int sum, bool f )
{
    if( ind == n + 1  )
    {
        if( sum == 0 && f == 1 )
        {
            return 0 ;
        }
        else
        {
            return  -1 * 1e7 ;
        }
    }
    int &ret = meme[ind][sum][f]       ;
    if( ret != -1 )
        return ret         ; //4 1 0
    ret = -1    ;
    if( solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) >= 0 )
    {
        ret  =   max( ret,  1 + solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) ) ;
        //print ret << endl ;
    }
    if( f == 0 )
    {
        if( solve( ind + 1, sum % 3, 0 ) >= 0 )
        {
            ret = max( ret, solve( ind + 1, sum, 0 ) ) ;
        }
    }
    else
    {
        if( solve( ind + 1, sum % 3, 1 ) >= 0 )
        {
            ret = max( ret, solve( ind + 1, sum % 3, 1 ) ) ;
        }
    }
    return ret ;
}
void print_path(int ind, int s, bool f )
{
    if(ind > n)
        return ;
    int Xx = solve(ind + 1, ( N[ind] + s ) % 3, 1 ) ;
    if( Xx >= 0 && meme[ind][s][f] == X )
    {
        if(N[ind] > 0 )
            term2 = 1 ;
        ans.push_back(N[ind]);
        --X ;
        print_path( ind + 1, ( N[ind] + s ) % 3, 1 )  ;
        return ;
    }
    else
    {
        if( !f )
        {
            print_path ( ind + 1, s, 0 ) ;
        }
        else
        {
            print_path(ind + 1, s, 1 ) ;
        }
    }
}
int main()
{
    fastt()  ;
    memset( meme, -1,  sizeof ( meme ) ) ;
    cin >> n      ;
    ll anss = 0   ;
    bool f =false ;
    bool term = 0 ;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> N[i] ;
        if(N[i] > 0 )
            term = 1 ;
        if(!N[i])
            f = true ;
        anss = (ll) anss + N[i]         ;
    }
    if(!term)
    {
        print 0 << endl ;
        return 0        ;
    }
    if(!f)
    {
        print -1  << '\n' ;
        return 0          ;
    }
    X =  solve ( 1, 0, 0 ) ;
    if(X != -1 )
    {
        for( int i = 1 ; i <= n ; i ++ )
        {
            if( meme[i][0][0] == X )
            {
                print_path( i, 0, 0 )    ;
                break ;
            }
        }
        if(!term2)
        {
            print 0 << endl ;
            return 0 ;
        }
        sort(ans.begin(),ans.end());
        reverse(ans.begin(),ans.end());
        for(int i = 0 ; i < ans.size() ; i ++ )
        {
            print ans[i] ;
        }
    }
    else
    {
        print -1 << endl ;
    }
}
void fastt()
{
    ios::
    sync_with_stdio(false),
                    cin.tie(0),
                    cout.tie(0) ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define  print  cout<<
#define FRD freopen("in.txt", "r", stdin)
#define FWR freopen("out.txt", "w", stdout)
#define is_bit_on(n,k)((n>>k)&1)
#define setbit0(n,k)((n)&(!(1<<k)))
#define setbit1(n,k) ((n)|((1<<k)))
#define flipbit(n,k) ((n)^((1<<k)))
#define PI 3.14159265
const int OO = 0x3f3f3f3f ;
typedef long long ll ;
using namespace std  ;
void fastt() ;
int dx[] = { -1, 0, 1, 0 }          ;
int dy[] = { 0, 1, 0, -1}           ;
const int sizee = 1e5 + 5 ;
int N[sizee] ;
bool cmp( int a, int  b )
{
    return a > b ;
}
int meme[(int)1e5+5][4][2] ;
vector < int > ans ;
int n ;
bool term2 = 0 ;
int X = 0 ;
int solve( int ind, int sum, bool f )
{
    if( ind == n + 1  )
    {
        if( sum == 0 && f == 1 )
        {
            return 0 ;
        }
        else
        {
            return  -1 * 1e7 ;
        }
    }
    int &ret = meme[ind][sum][f]       ;
    if( ret != -1 )
        return ret         ; //4 1 0
    ret = -1    ;
    if( solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) >= 0 )
    {
        ret  =   max( ret,  1 + solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) ) ;
        //print ret << endl ;
    }
    if( f == 0 )
    {
        if( solve( ind + 1, sum % 3, 0 ) >= 0 )
        {
            ret = max( ret, solve( ind + 1, sum, 0 ) ) ;
        }
    }
    else
    {
        if( solve( ind + 1, sum % 3, 1 ) >= 0 )
        {
            ret = max( ret, solve( ind + 1, sum % 3, 1 ) ) ;
        }
    }
    return ret ;
}
void print_path(int ind, int s, bool f )
{
    if(ind > n)
        return ;
    int Xx = solve(ind + 1, ( N[ind] + s ) % 3, 1 ) ;
    if( Xx >= 0 && meme[ind][s][f] == X )
    {
        if(N[ind] > 0 ) term2 = 1 ;
        ans.push_back(N[ind]);
        --X ;
        print_path( ind + 1, ( N[ind] + s ) % 3, 1 )  ;
        return ;
    }
    else
    {
        if( !f )
        {
            print_path ( ind + 1, s, 0 ) ;
        }
        else
        {
            print_path(ind + 1, s, 1 ) ;
        }
    }
}
int main()
{
    fastt()  ;
    memset( meme, -1,  sizeof ( meme ) ) ;
    cin >> n      ;
    ll anss = 0   ;
    bool f =false ;
    bool term = 0 ;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> N[i] ;
        if(N[i] > 0 )
            term = 1 ;
        if(!N[i])
            f = true ;
        anss = (ll) anss + N[i]         ;
    }
    sort( N + 1, N + n + 1, cmp )       ;
    if(!term)
    {
        print 0 << endl ;
        return 0        ;
    }
    if(!f)
    {
        print -1  << '\n' ;
        return 0          ;
    }
    if( anss % 3 == 0 && f )
    {
        for(int i = 1 ; i <= n ; i ++ )
            print N[i] ;
        return 0 ;
    }
    X =  solve ( 1, 0, 0 ) ;
    if(X != -1 )
    {
        for( int i = 1 ; i <= n ; i ++ )
        {
            if( meme[i][0][0] == X )
            {
                print_path( i , 0, 0 )    ;
                break ;
            }
        }
        if(!term2)
        {
            print 0 << endl ;
            return 0 ;
        }
        sort(ans.begin(),ans.end());
        reverse(ans.begin(),ans.end());
        for(int i = 0 ; i < ans.size() ; i ++ )
        {
            print ans[i] ;
        }
    }
    else
    {
        print -1 << endl ;
    }
}
void fastt()
{
    ios::
    sync_with_stdio(false),
                    cin.tie(0),
                    cout.tie(0) ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define  print  cout<<
#define FRD freopen("in.txt", "r", stdin)
#define FWR freopen("out.txt", "w", stdout)
#define is_bit_on(n,k)((n>>k)&1)
#define setbit0(n,k)((n)&(!(1<<k)))
#define setbit1(n,k) ((n)|((1<<k)))
#define flipbit(n,k) ((n)^((1<<k)))
#define PI 3.14159265
const int OO = 0x3f3f3f3f ;
typedef long long ll ;
using namespace std  ;
void fastt() ;
int dx[] = { -1, 0, 1, 0 }          ;
int dy[] = { 0, 1, 0, -1}           ;
const int sizee = 1e5 + 5 ;
int N[sizee] ;
bool cmp( int a, int  b )
{
    return a > b ;
}
int meme[(int)1e5+5][4][2] ;
vector < int > ans ;
int n ;
bool term2 = 0 ;
int X = 0 ;
int solve( int ind, int sum, bool f )
{
    if( ind == n + 1  )
    {
        if( sum == 0 && f == 1 )
        {
            return 0 ;
        }
        else
        {
            return  -1 * 1e7 ;
        }
    }
    int &ret = meme[ind][sum][f]       ;
    if( ret != -1 )
    {
        return ret ;
    } //4 1 0
    ret = -2    ;
    if( solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) >= 0 )
    {
        ret  =   max( ret,  1 + solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) ) ;
        //print ret << endl ;
    }
    if( f == 0 )
    {
            ret = max( ret, solve( ind + 1 , sum , 0 ) ) ;
    }
    else
    {
            ret = max( ret, solve( ind + 1, sum % 3, 1 ) ) ;
    }
    return ret ;
}
void print_path(int ind, int s, bool f )
{
    if(ind > n)
        return ;
    int Xx = solve(ind + 1, ( N[ind] + s ) % 3, 1 ) ;
    if( Xx >= 0 && meme[ind][s][f] == X )
    {
        if(N[ind] > 0 )
            term2 = 1 ;
        ans.push_back(N[ind]);
        --X ;
        print_path( ind + 1, ( N[ind] + s ) % 3, 1 )  ;
        return ;
    }
    else
    {
        if( !f )
        {
            print_path ( ind + 1, s, 0 ) ;
        }
        else
        {
            print_path(ind + 1, s, 1 ) ;
        }
    }
}
int main()
{
    fastt()  ;
    memset( meme, -1,  sizeof ( meme ) ) ;
    cin >> n      ;
    ll anss = 0   ;
    bool f =false ;
    bool term = 0 ;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> N[i] ;
        if(N[i] > 0 )
            term = 1 ;
        if(!N[i])
            f = true ;
        anss = (ll) anss + N[i]         ;
    }
    if(!term)
    {
        print 0 << endl ;
        return 0        ;
    }
    if(!f)
    {
        print -1  << '\n' ;
        return 0          ;
    }
    sort(N + 1, N + n + 1, cmp ) ;
    X =  solve ( 1, 0, 0 )       ;
    if(X != -2 )
    {
        for( int i = 1 ; i <= n ; i ++ )
        {
            if( meme[i][0][0] == X )
            {
                print_path( i, 0, 0 )    ;
                break ;
            }
        }
        if( !term2 )
        {
            print 0 << endl ;
            return 0 ;
        }
        sort(ans.begin(),ans.end());
        for(int i = ans.size() - 1  ; i >= 0  ; i -- )
        {
            print ans[i] ;
        }
    }
    else
    {
        print -1 << endl ;
    }
}
void fastt()
{
    ios::
    sync_with_stdio(false),
                    cin.tie(0),
                    cout.tie(0) ;
}


#include <iostream>
#include<bits/stdc++.h>
#define pb push_back
#define  print  cout<<
#define FRD freopen("in.txt", "r", stdin)
#define FWR freopen("out.txt", "w", stdout)
#define is_bit_on(n,k)((n>>k)&1)
#define setbit0(n,k)((n)&(!(1<<k)))
#define setbit1(n,k) ((n)|((1<<k)))
#define flipbit(n,k) ((n)^((1<<k)))
#define PI 3.14159265
const int OO = 0x3f3f3f3f ;
typedef long long ll ;
using namespace std  ;
void fastt() ;
int dx[] = { -1, 0, 1, 0 }          ;
int dy[] = { 0, 1, 0, -1}           ;
const int sizee = 1e5 + 5           ;
int N[sizee]                        ;
bool cmp( int a, int  b )
{
    return a > b ;
}
int meme[(int)1e5+5][4][2] ;
vector < int > ans ;
int n ;
bool term2 = 0 ;
int X = 0 ;
int solve( int ind, int sum, bool f )
{
    if( ind == n + 1  )
    {
        if( sum == 0 && f == 1 )
        {
            return 0 ;
        }
        else
        {
            return  -1 * 1e9 ;
        }
    }
    int &ret = meme[ind][sum][f]       ;
    if( ret != -1 )
    {
        return ret ;
    } //4 1 0
    ret = -2    ;
    if( solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) >= 0 )
    {
        ret  =   max( ret,  1 + solve( ind + 1, ( N[ind] % 3 + sum % 3 ) % 3, 1 ) ) ;
        //print ret << endl ;
    }
    if( f == 0 )
    {
        ret = max( ret, solve( ind + 1, sum % 3 , 0 ) ) ;
    }
    else
    {
        ret = max( ret, solve( ind + 1, sum % 3, 1 ) ) ;
    }
    return ret ;
}
void print_path(int ind, int s, bool f )
{
    if(ind > n )
        return ;
    int Xx = solve(ind + 1, ( N[ind] + s ) % 3, 1 ) ;
    if( Xx >= 0 && meme[ind][s][f] == X )
    {
        if(N[ind] > 0 )
            term2 = 1 ;
        ans.push_back(N[ind]);
        --X                  ;
        print_path( ind + 1, ( N[ind] + s ) % 3, 1 )  ;
        return ;
    }
    else
    {
        if( !f )
        {
            print_path ( ind + 1, s % 3, 0 ) ;
        }
        else
        {
            print_path(ind + 1, s % 3, 1 ) ;
        }
    }
}
int main()
{
    fastt()  ;
    memset( meme, -1,  sizeof ( meme ) ) ;
    cin >> n      ;
    ll anss = 0   ;
    bool f =false ;
    bool term = 0 ;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> N[i] ;
        if(N[i] > 0 )
            term = 1 ;
        if(!N[i])
            f = true ;
        anss = (ll) anss + N[i]         ;
    }
    if(!term)
    {
        print 0 << endl ;
        return 0        ;
    }
    if(!f)
    {
        print -1  << '\n' ;
        return 0          ;
    }
    sort(N + 1, N + n + 1, cmp )  ;
    X =  solve ( 1, 0, 0 )        ;
    if(X != -2 )
    {
        for( int i = 1 ; i <= n ; i ++ )
        {
            if( meme[i][0][0] == X )
            {
                print_path( i, 0, 0 )    ;
                break ;
            }
        }
        if( !term2 )
        {
            print 0 << endl ;
            return 0 ;
        }
        sort( ans.begin(),ans.end() ) ;
        for(int i = ans.size() - 1  ; i >= 0  ; i -- )
        {
            print ans[i] ;
        }
    }
    else
    {
        print -1 << endl ;
    }
}
void fastt()
{
    ios::
    sync_with_stdio(false),
                    cin.tie(0),
                    cout.tie(0) ;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define all(v)              ((v).begin()), ((v).end())
#define allr(v)             ((v).rbegin()), ((v).rend())
#define sz(v)               ((int)((v).size()))
#define clr(v, d)           memset(v, d, sizeof(v))
#define MP                  make_pair
#define lpv(i, v)           for(int i=0;i<sz(v);++i)
#define lpn(i, n)           for(int i=0;i<(int)(n);++i)
#define PI                  3.14159265359
#define pb                  push_back
#define ull                 unsigned long long
#define ll                  long long
#define LD                  long double
#define vi                  vector<int>
#define vl                  vector<ll>
#define vs                  vector<string>
#define vp                  vector<pair<int,int>>
using namespace std;
const ll MOD = 1e9 + 7;

int main()
{
	int n;
	ll sm = 0;
	scanf("%d", &n);
	vi v(n), vis(n, 1);
	for (int i = 0;i < n;i++)
		scanf("%d", &v[i]), sm += v[i];
	sort(allr(v));
	if (v[n - 1])
		printf("-1\n");
	else
	{
		if (sm % 3 == 0 && !v[n - 1])
			for (int i = 0;i < sz(v);i++)
				if (i + 1 < sz(v))
					printf("%d", v[i]);
				else
					printf("%d\n", v[i]);
		else if (v[0] == 0 && v[n - 1] == 0)
			printf("0\n");
		else
		{
			if ((sm % 3 == 1 || sm % 3 == 2) && !v[n - 1])
			{
				for (int i = n - 1;i >= 0;--i)
				{
					if (v[i] % 3 == 1 || v[i] % 3 == 2)
					{
						vis[i] = 0;
						sm -= v[i];
						if (sm % 3 == 0)
							break;
						vis[i] = 1;
						sm += v[i];
					}
				}
			}
			for (int i = 0;i < sz(v);i++)
			{
				if (vis[i])
					if (i + 1 < sz(v))
						printf("%d", v[i]);
					else
						printf("%d\n", v[i]);
			}
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define all(v)              ((v).begin()), ((v).end())
#define allr(v)             ((v).rbegin()), ((v).rend())
#define sz(v)               ((int)((v).size()))
#define clr(v, d)           memset(v, d, sizeof(v))
#define MP                  make_pair
#define lpv(i, v)           for(int i=0;i<sz(v);++i)
#define lpn(i, n)           for(int i=0;i<(int)(n);++i)
#define PI                  3.14159265359
#define pb                  push_back
#define ull                 unsigned long long
#define ll                  long long
#define LD                  long double
#define vi                  vector<int>
#define vl                  vector<ll>
#define vs                  vector<string>
#define vp                  vector<pair<int,int>>
using namespace std;
const ll MOD = 1e9 + 7;

int main()
{
	int n;
	ll sm = 0;
	scanf("%d", &n);
	vi v(n), vis(n, 1);
	for (int i = 0;i < n;i++)
		scanf("%d", &v[i]), sm += v[i];
	sort(allr(v));
	if (v[n - 1])
		printf("-1\n");
	else
	{
		if (sm % 3 == 0 && !v[n - 1])
			for (int i = 0;i < sz(v);i++)
				if (i + 1 < sz(v))
					printf("%d", v[i]);
				else
					printf("%d\n", v[i]);
		else if (v[0] == 0 && v[n - 1] == 0)
			printf("0\n");
		else
		{
			int c = 0;
			if ((sm % 3 == 1 || sm % 3 == 2) && !v[n - 1])
			{
				for (int i = n - 1;i >= 0;--i)
				{
					if (v[i] % 3 == 1)
					{
						vis[i] = 0, sm -= v[i];
						if (sm % 3 == 0)
							break;
					}
					if (v[i] % 3 == 1)
						continue;
					else if (v[i] % 3 == 2)
						v[i] = 0, sm -= v[i], c++;
					if (c == 2)
						break;
				}
			}
			for (int i = 0;i < sz(v);i++)
			{
				if (vis[i])
					if (i + 1 < sz(v))
						printf("%d", v[i]);
					else
						printf("%d\n", v[i]);
			}
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define all(v)              ((v).begin()), ((v).end())
#define allr(v)             ((v).rbegin()), ((v).rend())
#define sz(v)               ((int)((v).size()))
#define clr(v, d)           memset(v, d, sizeof(v))
#define MP                  make_pair
#define lpv(i, v)           for(int i=0;i<sz(v);++i)
#define lpn(i, n)           for(int i=0;i<(int)(n);++i)
#define PI                  3.14159265359
#define pb                  push_back
#define ull                 unsigned long long
#define ll                  long long
#define LD                  long double
#define vi                  vector<int>
#define vl                  vector<ll>
#define vs                  vector<string>
#define vp                  vector<pair<int,int>>
using namespace std;
const ll MOD = 1e9 + 7;

int main()
{
	int n;
	ll sm = 0;
	scanf("%d", &n);
	vi v(n), vis(n, 1);
	for (int i = 0;i < n;i++)
		scanf("%d", &v[i]), sm += v[i];
	sort(allr(v));
	if (v[n - 1])
		printf("-1\n");
	else
	{
		if (sm % 3 == 0 && !v[n - 1])
			for (int i = 0;i < sz(v);i++)
				if (i + 1 < sz(v))
					printf("%d", v[i]);
				else
					printf("%d\n", v[i]);
		else if (v[0] == 0 && v[n - 1] == 0)
			printf("0\n");
		else
		{
			if ((sm % 3 == 1 || sm % 3 == 2) && !v[n - 1])
			{
				for (int i = n - 1;i >= 0;--i)
				{
					if (v[i] % 3 == 1 || v[i] % 3 == 2)
					{
						vis[i] = 0;
						sm -= v[i];
						if (sm % 3 == 0)
							break;
					}
				}
			}
			for (int i = 0;i < sz(v);i++)
			{
				if (vis[i])
					if (i + 1 < sz(v))
						printf("%d", v[i]);
					else
						printf("%d\n", v[i]);
			}
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define all(v)              ((v).begin()), ((v).end())
#define allr(v)             ((v).rbegin()), ((v).rend())
#define sz(v)               ((int)((v).size()))
#define clr(v, d)           memset(v, d, sizeof(v))
#define MP                  make_pair
#define lpv(i, v)           for(int i=0;i<sz(v);++i)
#define lpn(i, n)           for(int i=0;i<(int)(n);++i)
#define PI                  3.14159265359
#define pb                  push_back
#define ull                 unsigned long long
#define ll                  long long
#define LD                  long double
#define vi                  vector<int>
#define vl                  vector<ll>
#define vs                  vector<string>
#define vp                  vector<pair<int,int>>
using namespace std;
const ll MOD = 1e9 + 7;

int main()
{
	int n;
	ll sm = 0;
	scanf("%d", &n);
	vi v(n);
	for (int i = 0;i < n;i++)
		scanf("%d", &v[i]), sm += v[i];
	sort(allr(v));
	if (v[n - 1])
		printf("-1\n");
	else
	{
		if (sm % 3 == 0 && !v[n - 1])
			for (int i = 0;i < sz(v);i++)
				if (i + 1 < sz(v))
					printf("%d", v[i]);
				else
					printf("%d\n", v[i]);
		else if (v[0] == 0 && v[n - 1] == 0)
			printf("0\n");
		else
		{
			if (sm % 3 == 1 && !v[n - 1])
			{
				for (int i = n - 1;i >= 0;--i)
					if (v[i] % 3 == 1)
					{
						v.erase(v.begin() + (n - i));
						break;
					}
			}
			else if (sm % 3 == 2 && !v[n - 1])
				for (int i = n - 1;i >= 0;--i)
					if (v[i] % 3 == 2)
					{
						v.erase(v.begin() + (n - i));
						break;
					}
			for (int i = 0;i < sz(v);i++)
				if (i + 1 < sz(v))
					printf("%d", v[i]);
				else
					printf("%d\n", v[i]);
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define all(v)              ((v).begin()), ((v).end())
#define allr(v)             ((v).rbegin()), ((v).rend())
#define sz(v)               ((int)((v).size()))
#define clr(v, d)           memset(v, d, sizeof(v))
#define MP                  make_pair
#define lpv(i, v)           for(int i=0;i<sz(v);++i)
#define lpn(i, n)           for(int i=0;i<(int)(n);++i)
#define PI                  3.14159265359
#define pb                  push_back
#define ull                 unsigned long long
#define ll                  long long
#define LD                  long double
#define vi                  vector<int>
#define vl                  vector<ll>
#define vs                  vector<string>
#define vp                  vector<pair<int,int>>
using namespace std;
const ll MOD = 1e9 + 7;

int main()
{
	int n;
	int sm = 0, e;
	scanf("%d", &n);
	vi v(10);
	for (int i = 0;i < n;i++)
		scanf("%d", &e), v[e]++, sm += e;
	if (!v[0])
		printf("-1\n");
	else
	{
		for (int i = 1;i <= 9 && sm % 3;i++)
			if (v[i] && (sm - i) % 3 == 0)
				--v[i], sm -= i;
		for (int i = 1;i <= 9 && sm % 3;i++)
			for (int j = 1;j <= 9;j++)
				if (i == j&&v[i] < 2)
					continue;
				else  if (v[i] && v[j] && (sm - i - j) % 3 == 0)
				{
					sm -= (i + j), --v[i], --v[j];
					break;
				}
		if (sm == 0)
			printf("0\n");
		else
		{
			int x = 9;
			do
			{
				while (v[x]--)
					printf("%d", x);
			} while (x--);
			printf("\n");
		}
		return 0;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define all(v)              ((v).begin()), ((v).end())
#define allr(v)             ((v).rbegin()), ((v).rend())
#define sz(v)               ((int)((v).size()))
#define clr(v, d)           memset(v, d, sizeof(v))
#define MP                  make_pair
#define lpv(i, v)           for(int i=0;i<sz(v);++i)
#define lpn(i, n)           for(int i=0;i<(int)(n);++i)
#define PI                  3.14159265359
#define pb                  push_back
#define ull                 unsigned long long
#define ll                  long long
#define LD                  long double
#define vi                  vector<int>
#define vl                  vector<ll>
#define vs                  vector<string>
#define vp                  vector<pair<int,int>>
using namespace std;
const ll MOD = 1e9 + 7;

int main()
{
	int n;
	ll sm = 0;
	scanf("%d", &n);
	vi v(n);
	for (int i = 0;i < n;i++)
		scanf("%d", &v[i]), sm += v[i];
	sort(allr(v));
	if (v[n - 1])
		printf("-1\n");
	else
	{
		if (sm % 3 == 0 && !v[n - 1])
			for (int i = 0;i < sz(v);i++)
				if (i + 1 < sz(v))
					printf("%d", v[i]);
				else
					printf("%d\n", v[i]);
		else if (v[0] == 0 && v[n - 1] == 0)
			printf("0\n");
		else
		{
			if (sm % 3 == 1 && !v[n - 1])
			{
				for (int i = n - 1;i >= 0;--i)
					if (v[i] % 3 == 1)
					{
						v.erase(v.begin() + i);
						break;
					}
			}
			else if (sm % 3 == 2 && !v[n - 1])
				for (int i = n - 1;i >= 0;--i)
					if (v[i] % 3 == 2)
					{
						v.erase(v.begin() + i);
						break;
					}
			for (int i = 0;i < sz(v);i++)
				if (i + 1 < sz(v))
					printf("%d", v[i]);
				else
					printf("%d\n", v[i]);
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define all(v)              ((v).begin()), ((v).end())
#define allr(v)             ((v).rbegin()), ((v).rend())
#define sz(v)               ((int)((v).size()))
#define clr(v, d)           memset(v, d, sizeof(v))
#define MP                  make_pair
#define lpv(i, v)           for(int i=0;i<sz(v);++i)
#define lpn(i, n)           for(int i=0;i<(int)(n);++i)
#define PI                  3.14159265359
#define pb                  push_back
#define ull                 unsigned long long
#define ll                  long long
#define LD                  long double
#define vi                  vector<int>
#define vl                  vector<ll>
#define vs                  vector<string>
#define vp                  vector<pair<int,int>>
using namespace std;
const ll MOD = 1e9 + 7;

int main()
{
	int n;
	int sm = 0, e;
	scanf("%d", &n);
	vi v(10);
	for (int i = 0;i < n;i++)
		scanf("%d", &e), v[e]++, sm += e;
	if (!v[0])
		printf("-1\n");
	else
	{
		for (int i = 0;i <= 9;i++)
			if (v[i] && (sm - i) % 3 == 0)
				--v[i], sm -= i;
		for (int i = 0;i <= 9 && sm % 3;i++)
			for (int j = 0;j <= 9;j++)
				if (i == j&&v[i] < 2)
					continue;
				else  if (v[i] && v[j] && (sm - i - j) % 3 == 0)
				{
					sm -= (i + j), --v[i], --v[j];
					break;
				}
		if (sm == 0)
			printf("0\n");
		else
		{
			int x = 9;
			do
			{
				while (v[x]--)
					printf("%d", x);
			} while (x--);
			printf("\n");
		}
		return 0;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define all(v)              ((v).begin()), ((v).end())
#define allr(v)             ((v).rbegin()), ((v).rend())
#define sz(v)               ((int)((v).size()))
#define clr(v, d)           memset(v, d, sizeof(v))
#define MP                  make_pair
#define lpv(i, v)           for(int i=0;i<sz(v);++i)
#define lpn(i, n)           for(int i=0;i<(int)(n);++i)
#define PI                  3.14159265359
#define pb                  push_back
#define ull                 unsigned long long
#define ll                  long long
#define LD                  long double
#define vi                  vector<int>
#define vl                  vector<ll>
#define vs                  vector<string>
#define vp                  vector<pair<int,int>>
using namespace std;
const ll MOD = 1e9 + 7;

int main()
{
	int n;
	ll sm = 0;
	scanf("%d", &n);
	vi v(n);
	for (int i = 0;i < n;i++)
		scanf("%d", &v[i]), sm += v[i];
	sort(allr(v));
	if (v[n - 1])
		printf("-1\n");
	else
	{
		if (sm % 3 == 0 && !v[n - 1])
			for (int i = 0;i < sz(v);i++)
				if (i + 1 < sz(v))
					printf("%d", v[i]);
				else
					printf("%d\n", v[i]);
		else if (v[0] == 0 && v[n - 1] == 0)
			printf("0\n");
		else
		{
			if ((sm % 3 == 1 || sm % 3 == 2) && !v[n - 1])
			{
				for (int i = n - 1;i >= 0;--i)
				{
					if (v[i] % 3 == 1 || v[i] % 3 == 2)
						sm -= v[i], v.erase(v.begin() + i);
					if (sm % 3 == 0)
						break;
				}
			}
			for (int i = 0;i < sz(v);i++)
				if (i + 1 < sz(v))
					printf("%d", v[i]);
				else
					printf("%d\n", v[i]);
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define all(v)              ((v).begin()), ((v).end())
#define allr(v)             ((v).rbegin()), ((v).rend())
#define sz(v)               ((int)((v).size()))
#define clr(v, d)           memset(v, d, sizeof(v))
#define MP                  make_pair
#define lpv(i, v)           for(int i=0;i<sz(v);++i)
#define lpn(i, n)           for(int i=0;i<(int)(n);++i)
#define PI                  3.14159265359
#define pb                  push_back
#define ull                 unsigned long long
#define ll                  long long
#define LD                  long double
#define vi                  vector<int>
#define vl                  vector<ll>
#define vs                  vector<string>
#define vp                  vector<pair<int,int>>
using namespace std;
const ll MOD = 1e9 + 7;

int main()
{
	int n;
	ll sm = 0;
	scanf("%d", &n);
	vi v(n), vis(n, 1);
	for (int i = 0;i < n;i++)
		scanf("%d", &v[i]), sm += v[i];
	sort(allr(v));
	if (v[n - 1])
		printf("-1\n");
	else
	{
		if (sm % 3 == 0 && !v[n - 1])
			for (int i = 0;i < sz(v);i++)
				if (i + 1 < sz(v))
					printf("%d", v[i]);
				else
					printf("%d\n", v[i]);
		else if (v[0] == 0 && v[n - 1] == 0)
			printf("0\n");
		else
		{
			int c = 0;
			if ((sm % 3 == 1 || sm % 3 == 2) && !v[n - 1])
			{
				for (int i = n - 1;i >= 0;--i)
				{
					if (v[i] % 3 == 1)
						vis[i] = 0, sm -= v[i];
					if (sm % 3 == 0)
						break;
					if (v[i] % 3 == 1)
						continue;
					else if (v[i] % 3 == 2)
						v[i] = 0, sm -= v[i], c++;
					if (c == 2)
						break;
				}
			}
			for (int i = 0;i < sz(v);i++)
			{
				if (vis[i])
					if (i + 1 < sz(v))
						printf("%d", v[i]);
					else
						printf("%d\n", v[i]);
			}
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define all(v)              ((v).begin()), ((v).end())
#define allr(v)             ((v).rbegin()), ((v).rend())
#define sz(v)               ((int)((v).size()))
#define clr(v, d)           memset(v, d, sizeof(v))
#define MP                  make_pair
#define lpv(i, v)           for(int i=0;i<sz(v);++i)
#define lpn(i, n)           for(int i=0;i<(int)(n);++i)
#define PI                  3.14159265359
#define pb                  push_back
#define ull                 unsigned long long
#define ll                  long long
#define LD                  long double
#define vi                  vector<int>
#define vl                  vector<ll>
#define vs                  vector<string>
#define vp                  vector<pair<int,int>>
using namespace std;
const ll MOD = 1e9 + 7;

int main()
{
	int n;
	int sm = 0, e;
	scanf("%d", &n);
	vi v(10);
	for (int i = 0;i < n;i++)
		scanf("%d", &e), v[e]++, sm += e;
	if (!v[0])
		printf("-1\n");
	else
	{
		for (int i = 0;i <= 9;i++)
			if (v[i] && (sm - i) % 3 == 0)
				--v[i], sm -= i;
		for (int i = 1;i <= 9 && sm % 3;i++)
			for (int j = 1;j <= 9;j++)
				if (i == j&&v[i] < 2)
					continue;
				else  if (v[i] && v[j] && (sm - i - j) % 3 == 0)
				{
					sm -= (i + j), --v[i], --v[j];
					break;
				}
		if (sm == 0)
			printf("0\n");
		else
		{
			int x = 9;
			do
			{
				while (v[x]--)
					printf("%d", x);
			} while (x--);
			printf("\n");
		}
		return 0;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
const int mxsz=1e5+5,three=3,look[6]={1,2,4,5,7,8},six=6;
int sz,arr[mxsz],freq[10],nums,mxsum;
vector<int>factors(six,0),output;
map<long long,vector<int>>mp;
long long solve(int idx,long long sum,int mod){
    if (idx==-1){
        if (mod) return 0;
        mp[sum]=factors;
        return sum;
    }
    long long ret=0;
    for (int i=0; i<=freq[look[idx]]%three; i++) {
        factors[idx]=(freq[look[idx]]%three)-i;
        if(i){
            sum+=look[idx];
            sum*=10;
        }
        ret=max(ret,solve(idx-1, sum,(mod+(i*look[idx]))%three));
    }
    return ret;
}
int main(){
    cin>>sz;
    for (int i=0; i<sz; i++) cin>>arr[i],freq[arr[i]]++;
    if (freq[0]==0) return puts("-1");
    mxsum=solve(5, 0ll,0);
    factors=mp[mxsum];
    reverse(factors.begin(), factors.end());
    for (int i=9; i>=0; i--) {
        int pos=(int)(find(look, look+six, i)-look);
        if (pos<6) freq[i]-=factors[pos];
        while (freq[i]>0) output.push_back(i),freq[i]--;
    }
    if (output.size()>2&&output[0]==0) return puts("-1");
    for (int i=0; i<output.size(); i++) cout<<output[i];
    return puts("");
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
const int mxsz=1e5+5,three=3,look[6]={1,2,4,5,7,8},six=6;
int sz,arr[mxsz],freq[10],nums,mxsum;
vector<int>factors(six,0),output;
map<int,vector<int>>mp;
int solve(int idx,int sum){
    if (idx==six){
        if (sum%3) return 0;
        mp[sum]=factors;
        return sum;
    }
    int ret=0;
    for (int i=0; i<=freq[look[idx]]%three; i++) {
        factors[idx]=(freq[look[idx]]%three)-i;
        ret=max(ret,solve(idx+1, sum+(i*freq[look[idx]])));
    }
    return ret;
}
int main(){
    cin>>sz;
    for (int i=0; i<sz; i++) cin>>arr[i],freq[arr[i]]++;
    if (freq[0]==0) return puts("-1");
    mxsum=solve(0, 0);
    factors=mp[mxsum];
    for (int i=9; i>=0; i--) {
        int pos=(int)(find(look, look+six, i)-look);
        if (pos<6) freq[look[pos]]-=factors[pos];
        while (freq[i]>0) output.push_back(i),freq[i]--;
    }
    if (output.size()>2&&output[0]==0) return puts("-1");
    for (int i=0; i<output.size(); i++) cout<<output[i];
    return puts("");
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
int sz;
multiset<int>mods[3],kbeer;
multiset<int>::iterator git;
vector<int>trying,output;
vector<int> get_value(){
    vector<int>ret;
    for (multiset<int>::iterator it=(--kbeer.end()); true; --it) {
        ret.push_back(*it);
        if (it==kbeer.begin()) break;
    }
    return ret;
}
void optimize(){
    if (kbeer.size()<output.size()) return;
    if (kbeer.size()>output.size()) {
        output=get_value();
        return;
    }
    vector<int>cmp=get_value();
    for (int i=0; i<cmp.size(); i++) {
        if (cmp[i]>output[i]) {
            output=cmp;
            return;
        }
    }
}
void span(int idx,int mod){
    if (idx==trying.size()){
        if (mod==0) {
            optimize();
        }
    }
    else{
        kbeer.insert(trying[idx]);
        span(idx+1,(mod+trying[idx])%3);
        kbeer.erase(git=kbeer.find(trying[idx]));
        span(idx+1,mod);
    }
}
int main(){
    cin>>sz;
    for (int i=0,t; i<sz; i++) cin>>t,mods[t%3].insert(t),kbeer.insert(t);
    while (mods[1].size()%3){
        trying.push_back(*mods[1].begin()),kbeer.erase(git=kbeer.find(*mods[1].begin())),mods[1].erase(mods[1].begin());
    }
    while (mods[2].size()%3){
        trying.push_back(*mods[2].begin()),kbeer.erase(git=kbeer.find(*mods[2].begin())),mods[2].erase(mods[2].begin());
    }
    span(0, 0);
    if (output[output.size()-1]) return puts("-1");
    else for(int i=0;i<output.size();i++) cout<<output[i];
    return puts("");
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
const int mxsz=1e5+5,three=3,look[6]={1,2,4,5,7,8},six=6;
int sz,arr[mxsz],freq[10],nums,mxsum;
vector<int>factors(six,0),output;
map<long long,vector<int>>mp;
long long solve(int idx,long long sum,int mod){
    if (idx==-1){
        if (mod) return 0;
        mp[sum]=factors;
        return sum;
    }
    long long ret=0;
    for (int i=0; i<=freq[look[idx]]%three; i++) {
        factors[idx]=(freq[look[idx]]%three)-i;
        if(i){
            sum+=look[idx];
            sum*=10;
        }
        ret=max(ret,solve(idx-1, sum,(mod+(i*look[idx]))%three));
    }
    return ret;
}
int main(){
    cin>>sz;
    for (int i=0; i<sz; i++) cin>>arr[i],freq[arr[i]]++;
    if (freq[0]==0) return puts("-1");
    mxsum=solve(5, 0ll,0);
    factors=mp[mxsum];
    for (int i=9; i>=0; i--) {
        int pos=(int)(find(look, look+six, i)-look);
        if (pos<6) freq[look[pos]]-=factors[pos];
        while (freq[i]>0) output.push_back(i),freq[i]--;
    }
    if (output.size()>2&&output[0]==0) return puts("-1");
    for (int i=0; i<output.size(); i++) cout<<output[i];
    return puts("");
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
const int mxsz=1e5+5,three=3,look[6]={1,2,4,5,7,8},six=6;
int sz,arr[mxsz],freq[10],nums,mxsum;
vector<int>factors(six,0),output;
map<long long,vector<int>>mp;
bool solve(int idx,int mod){
    if (idx==-1)return !mod;
    for (int i=freq[look[idx]]%three; i>=0; i--) {
        factors[idx]=(freq[look[idx]]%three)-i;
        if(solve(idx-1,(mod+(i*look[idx]))%three)) return true;
    }
    return false;
}
int main(){
    cin>>sz;
    for (int i=0; i<sz; i++) cin>>arr[i],freq[arr[i]]++;
    if (freq[0]==0) return puts("-1");
    solve(5,0);
    factors=mp[mxsum];
    for (int i=9; i>=0; i--) {
        int pos=(int)(find(look, look+six, i)-look);
        if (pos<6) freq[i]-=factors[pos];
        while (freq[i]>0) output.push_back(i),freq[i]--;
    }
    if (output.size()>2&&output[0]==0) return puts("-1");
    for (int i=0; i<output.size(); i++) cout<<output[i];
    return puts("");
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
int sz;
multiset<int>mods[3],kbeer;
multiset<int>::iterator git;
vector<int>trying,output;
vector<int> get_value(){
    vector<int>ret;
    for (multiset<int>::iterator it=(--kbeer.end()); true; --it) {
        ret.push_back(*it);
        if (it==kbeer.begin()) break;
    }
    return ret;
}
void optimize(){
    if (kbeer.size()<output.size()) return;
    if (kbeer.size()>output.size()) {
        output=get_value();
        return;
    }
    vector<int>cmp=get_value();
    for (int i=0; i<cmp.size(); i++) {
        if (cmp[i]>output[i]) {
            output=cmp;
            return;
        }
    }
}
void span(int idx,int mod){
    if (idx==trying.size()){
        if (mod==0) {
            optimize();
        }
    }
    else{
        kbeer.insert(trying[idx]);
        span(idx+1,(mod+trying[idx])%3);
        kbeer.erase(git=kbeer.find(trying[idx]));
        span(idx+1,mod);
    }
}
int main(){
    cin>>sz;
    for (int i=0,t; i<sz; i++) cin>>t,mods[t%3].insert(t),kbeer.insert(t);
    while (mods[1].size()%3){
        trying.push_back(*mods[1].begin()),kbeer.erase(git=kbeer.find(*mods[1].begin())),mods[1].erase(mods[1].begin());
    }
    while (mods[2].size()%3){
        trying.push_back(*mods[2].begin()),kbeer.erase(git=kbeer.find(*mods[2].begin())),mods[2].erase(mods[2].begin());
    }
    span(0, 0);
    if (output[output.size()-1]) return puts("-1");
    else for(int i=output[0]?0:output.size()-1;i<output.size();i++) cout<<output[i];
    return puts("");
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
int sz,sum,zero,freq[10];
vector<int>output;
void change_freq(bool two){
    int pos=-1;
    for (int i=two?8:7; i>=0; i-=3) pos=max(freq[i]?i:-1,pos);
    if (pos==-1) for (int i=two?1:2,t=2; i<10&&t; i+=3) while (t&&freq[i]) --t,freq[i]--;
    else freq[pos]--;
}
int main(){
    cin>>sz;
    for (int i=0,t; i<sz; i++) cin>>t,freq[t]++,sum+=t,sum%=3,zero+=t==0;
    if (zero==0) return puts("-1");
    if (sum==2) change_freq(1);
    if (sum) change_freq(0);
    for (int i=9; i>=0; i--) while(freq[i]--)output.push_back(i);
    for (int i=output[0]?0:output.size()-1; i<output.size(); i++) cout<<output[i];
    return puts("");
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
int sz,sum,zero,freq[10];
vector<int>output;
void change_freq(bool two){
    int pos=-1;
    for (int i=two?8:7; i>=0; i-=3) pos=freq[i]?i:max(pos,-1);
    if (pos==-1) for (int i=two?1:2,t=2; i<10&&t; i+=3) while (t&&freq[i]) --t,freq[i]--;
    else freq[pos]--;
}
int main(){
    cin>>sz;
    for (int i=0,t; i<sz; i++) cin>>t,freq[t]++,sum+=t,sum%=3,zero+=t==0;
    if (zero==0) return puts("-1");
    if (sum==2) change_freq(1);
    if (sum==1) change_freq(0);
    for (int i=9; i>=0; i--) while(freq[i]--)output.push_back(i);
    for (int i=output[0]?0:output.size()-1; i<output.size(); i++) cout<<output[i];
    return puts("");
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
const int mxsz=1e5+5,three=3,look[6]={1,2,4,5,7,8},six=6;
int sz,arr[mxsz],freq[10],nums,taken[10],factors[10];
vector<int>sol;
void comparing(vector<int> in){
    if (in.size()<sol.size()) return;
    if (in.size()>sol.size()){
        sol=in;
        return;
    }
    for (int i=0; i<in.size(); i++) {
        if (in[i]>sol[i]) {
            sol=in;
            return;
        }
    }
}
void counting(){
    vector<int>cur_sol;
    for (int i=9,t; i>=0; i--) {
        t=taken[i]+factors[i];
        while (t>0) {
            cur_sol.push_back(i);
            t--;
        }
    }
    comparing(cur_sol);
}
void track(int idx,int sum){
    if (idx==six) {
        if (sum%three==0) counting();
        return;
    }
    for (int i=0; i<=freq[look[idx]]; i++) static_cast<void>(factors[look[idx]]=i),track(idx+1, sum+i*look[idx]);
}
void output(){
    if ((sol.size()==0)||(sol.size()&&sol[sol.size()-1])) cout<<-1;
    else for (int i=0; i<sol.size(); i++) cout<<sol[i];
}
int main(){
    cin>>sz;
    for (int i=0; i<sz; i++) static_cast<void>(cin>>arr[i]),taken[arr[i]]++;
    for (int i=0; i<10; i++) if(i%three)static_cast<void>(freq[i]=taken[i]%three),taken[i]-=taken[i]%three;
    track(0, 0);
    output();
    return puts("");
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
int sz;
multiset<int>mods[3],kbeer;
multiset<int>::iterator git;
vector<int>trying,output;
vector<int> get_value(){
    vector<int>ret;
    for (multiset<int>::iterator it=(--kbeer.end()); true; --it) {
        ret.push_back(*it);
        if (it==kbeer.begin()) break;
    }
    return ret;
}
void optimize(){
    if (kbeer.size()<output.size()) return;
    if (kbeer.size()>output.size()) {
        output=get_value();
        return;
    }
    vector<int>cmp=get_value();
    for (int i=0; i<cmp.size(); i++) {
        if (cmp[i]>output[i]) {
            output=cmp;
            return;
        }
    }
}
void span(int idx,int mod){
    if (idx==trying.size()){
        if (mod==0) {
            optimize();
        }
    }
    else{
        kbeer.insert(trying[idx]);
        span(idx+1,(mod+trying[idx])%3);
        kbeer.erase(git=kbeer.find(trying[idx]));
        span(idx+1,mod);
    }
}
int main(){
    cin>>sz;
    for (int i=0,t; i<sz; i++) cin>>t,mods[t%3].insert(t),kbeer.insert(t);
    while (mods[1].size()%3){
        trying.push_back(*mods[1].begin()),kbeer.erase(git=kbeer.find(*mods[1].begin())),mods[1].erase(mods[1].begin());
    }
    while (mods[2].size()%3){
        trying.push_back(*mods[2].begin()),kbeer.erase(git=kbeer.find(*mods[1].begin())),mods[2].erase(mods[2].begin());
    }
    span(0, 0);
    if (output[output.size()-1]) return puts("-1");
    else for(int i=0;i<output.size();i++) cout<<output[i];
    return puts("");
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
int sz;
multiset<int>mods[3],kbeer;
vector<int>trying,output;
vector<int> get_value(){
    vector<int>ret;
    for (multiset<int>::iterator it=(--kbeer.end()); true; --it) {
        ret.push_back(*it);
        if (it==kbeer.begin()) break;
    }
    return ret;
}
void optimize(){
    if (kbeer.size()<output.size()) return;
    if (kbeer.size()>output.size()) {
        output=get_value();
        return;
    }
    vector<int>cmp=get_value();
    for (int i=0; i<cmp.size(); i++) {
        if (cmp[i]>output[i]) {
            output=cmp;
            return;
        }
    }
}
void span(int idx,int mod){
    if (idx==trying.size()){
        if (mod==0) optimize();
    }
    else{
        kbeer.insert(trying[idx]);
        span(idx+1,(mod+trying[idx])%3);
        kbeer.erase(trying[idx]);
        span(idx+1,mod);
    }
}
int main(){
    cin>>sz;
    for (int i=0,t; i<sz; i++) cin>>t,mods[t%3].insert(t),kbeer.insert(t);
    while (mods[1].size()%3) trying.push_back(*mods[1].begin()),mods[1].erase(mods[1].begin());
    while (mods[2].size()%3) trying.push_back(*mods[2].begin()),mods[2].erase(mods[2].begin());
    span(0, 0);
    if (output[output.size()-1]) return puts("-1");
    else for(int i=0;i<output.size();i++) cout<<output[i];
    return puts("");
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stack>
using namespace std;
const int mxsz=1e5+5,three=3,look[6]={1,2,4,5,7,8},six=6;
int sz,arr[mxsz],freq[10],nums;
vector<int>factors(six,0),output;
bool solve(int idx,int mod){
    if (idx==-1)return !mod;
    for (int i=freq[look[idx]]%three; i>=0; i--) {
        factors[idx]=(freq[look[idx]]%three)-i;
        if(solve(idx-1,(mod+(i*look[idx]))%three)) return true;
    }
    return false;
}
int main(){
    cin>>sz;
    for (int i=0; i<sz; i++) cin>>arr[i],freq[arr[i]]++;
    if (freq[0]==0) return puts("-1");
    solve(5,0);
    for (int i=9; i>=0; i--) {
        int pos=(int)(find(look, look+six, i)-look);
        if (pos<6) freq[i]-=factors[pos];
        while (freq[i]>0) output.push_back(i),freq[i]--;
    }
    if (output.size()>2&&output[0]==0) return puts("-1");
    for (int i=0; i<output.size(); i++) cout<<output[i];
    return puts("");
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sc(x)  scanf("%d",&x)
#define sc2(x,y)  scanf("%d%d",&x,&y)
#define scl(x) scanf("%lld",&x)
#define F first
#define S second
#define pb push_back
#define pi 3.1415926535897932384
int Dx[]= {1,-1,0,0};
int Dy[]= {0,0,1,-1};
void fast()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
}
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int c[10]= {0},sum;
int main()
{
    int n;
    sc(n);
    for (int i=0 ; i<n ; i++)
    {
        int x;
        sc(x);
        c[x]++;
        sum+=x;
    }
    if (!c[0]) return cout<<-1<<endl,0;
    bool b=(sum%3)==0;
    if (sum%3)
    {
        for (int i=1 ; i<10 ; i++)
            if (i%3==sum%3 && c[i] )
                c[i]--,sum-=i,i=10,b=1;
        if (!b)
        {
            for (int i=1 ; i<10 ; i++)
            {
                if (!(i%3)) continue;
                for (int j=1 ; j<10 ; j++)
                {
                    if (!(j%3)) continue;
                    if (sum%3==(i+j)%3 && c[i]&&c[j])
                    {
                        c[i]--;
                        c[j]--;
                        b=1;
                        sum-=i+j;
                        break;
                    }
                }
                if (b) break;
            }
        }
        if (!b)
        {
            for (int i=1 ; i<10 ; i++)
            {
                if (!(i%3)) continue;
                for (int j=1 ; j<10 ; j++)
                {
                    if (!(j%3)) continue;
                    for (int k=1 ; k<10 ; k++)
                    {
                        if (!(k%3)) continue;
                        if (sum%3==(i+j+k)%3 && c[i]&&c[j]&&c[k])
                        {
                            c[i]--;
                            c[j]--;
                            c[k]--;
                            b=1;
                            sum-=i+j+k;
                            break;
                        }
                    }
                    if (b) break;
                }
                if (b) break;
            }
        }
    }
    if (!b) cout<<-1<<endl;
    else
    {
        if (!sum) c[0]=1;
        for (int i=9 ; i>=0 ; i--)
            while (c[i]--) cout<<i;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sc(x)  scanf("%d",&x)
#define sc2(x,y)  scanf("%d%d",&x,&y)
#define scl(x) scanf("%lld",&x)
#define F first
#define S second
#define pb push_back
#define pi 3.1415926535897932384
int Dx[]= {1,-1,0,0};
int Dy[]= {0,0,1,-1};
void fast()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
}
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int c[10]= {0},sum;
int main()
{
    int n;
    sc(n);
    for (int i=0 ; i<n ; i++)
    {
        int x;
        sc(x);
        c[x]++;
        sum+=x;
    }
    if (!c[0]) return printf("-1\n"),0;
    bool b=(sum%3==0);
    if (sum%3)
    {
        for (int i=1 ; i<10 ; i++)
            if (i%3==sum%3 && c[i] )
                c[i]--,sum-=i,i=10,b=1;
        if (!b)
        {
            for (int i=1 ; i<10 ; i++)
            {
                if (!(i%3)) continue;
                for (int j=1 ; j<10 ; j++)
                {
                    if (!(j%3)) continue;
                    if (sum%3==(i+j)%3)
                    {
                        if ((i==j && c[i]>=2)||(i!=j&&c[i]&&c[j]))
                        {
                            c[i]--;
                            c[j]--;
                            b=1;
                            sum-=i+j;
                            break;
                        }
                    }
                }
                if (b) break;
            }
        }

        if (!b)
        {
            for (int i=1 ; i<10 ; i++)
            {
                if (!(i%3)) continue;
                for (int j=1 ; j<10 ; j++)
                {
                    if (!(j%3)) continue;
                    for (int k=1 ; k<10 ; k++)
                    {
                        if (!(k%3)) continue;
                        if (sum%3==(i+j+k)%3 && c[i]&&c[j]&&c[k])
                        {
                            c[i]--;
                            c[j]--;
                            c[k]--;
                            b=1;
                            sum-=i+j+k;
                            break;
                        }
                    }
                    if (b) break;
                }
                if (b) break;
            }
        }
    }
    if (!b) printf("-1\n");
    else
    {
        if (!sum) c[0]=1;
        for (int i=9 ; i>=0 ; i--)
            while (c[i]>0) printf("%d",i),c[i]--;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sc(x)  scanf("%d",&x)
#define sc2(x,y)  scanf("%d%d",&x,&y)
#define scl(x) scanf("%lld",&x)
#define F first
#define S second
#define pb push_back
#define pi 3.1415926535897932384
int Dx[]= {1,-1,0,0};
int Dy[]= {0,0,1,-1};
void fast()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
}
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int c[10]= {0},sum;
int main()
{
    int n;
    sc(n);
    for (int i=0 ; i<n ; i++)
    {
        int x;
        sc(x);
        c[x]++;
        sum+=x;
    }
    if (!c[0]) return printf("-1\n"),0;
    bool b=(sum%3)==0;
    if (sum%3)
    {
        for (int i=1 ; i<10 ; i++)
            if (i%3==sum%3 && c[i] )
                c[i]--,sum-=i,i=10,b=1;
        if (!b)
        {
            for (int i=1 ; i<10 ; i++)
            {
                if (!(i%3)) continue;
                for (int j=1 ; j<10 ; j++)
                {
                    if (!(j%3)) continue;
                    if (sum%3==(i+j)%3 && c[i]&&c[j])
                    {
                        c[i]--;
                        c[j]--;
                        b=1;
                        sum-=i+j;
                        break;
                    }
                }
                if (b) break;
            }
        }
        if (!b)
        {
            for (int i=1 ; i<10 ; i++)
            {
                if (!(i%3)) continue;
                for (int j=1 ; j<10 ; j++)
                {
                    if (!(j%3)) continue;
                    for (int k=1 ; k<10 ; k++)
                    {
                        if (!(k%3)) continue;
                        if (sum%3==(i+j+k)%3 && c[i]&&c[j]&&c[k])
                        {
                            c[i]--;
                            c[j]--;
                            c[k]--;
                            b=1;
                            sum-=i+j+k;
                            break;
                        }
                    }
                    if (b) break;
                }
                if (b) break;
            }
        }
    }
    if (!b) printf("-1\n");
    else
    {
        if (!sum) c[0]=1;
        for (int i=9 ; i>=0 ; i--)
            while (c[i]>0) printf("%d",i),c[i]--;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sc(x)  scanf("%d",&x)
#define sc2(x,y)  scanf("%d%d",&x,&y)
#define scl(x) scanf("%lld",&x)
#define F first
#define S second
#define pb push_back
#define pi 3.1415926535897932384
int Dx[]= {1,-1,0,0};
int Dy[]= {0,0,1,-1};
void fast()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
}
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int c[10]= {0},sum;
int main()
{
    int n;
    sc(n);
    for (int i=0 ; i<n ; i++)
    {
        int x;
        sc(x);
        c[x]++;
        sum+=x;
    }
    if (!c[0]) return printf("-1\n"),0;
    bool b=(sum%3)==0;
    if (sum%3)
    {
        for (int i=1 ; i<10 ; i++)
            if (i%3==sum%3 && c[i] )
                c[i]--,sum-=i,i=10,b=1;
        if (!b)
        {
            for (int i=1 ; i<10 ; i++)
            {
                if (!(i%3)) continue;
                for (int j=1 ; j<10 ; j++)
                {
                    if (!(j%3)) continue;
                    if (sum%3==(i+j)%3 && c[i]&&c[j])
                    {
                        c[i]--;
                        c[j]--;
                        b=1;
                        sum-=i+j;
                        break;
                    }
                }
                if (b) break;
            }
        }
        if (!b)
        {
            for (int i=1 ; i<10 ; i++)
            {
                if (!(i%3)) continue;
                for (int j=1 ; j<10 ; j++)
                {
                    if (!(j%3)) continue;
                    for (int k=1 ; k<10 ; k++)
                    {
                        if (!(k%3)) continue;
                        if (sum%3==(i+j+k)%3 && c[i]&&c[j]&&c[k])
                        {
                            c[i]--;
                            c[j]--;
                            c[k]--;
                            b=1;
                            sum-=i+j+k;
                            break;
                        }
                    }
                    if (b) break;
                }
                if (b) break;
            }
        }
    }
    if (!b) printf("-1\n");
    else
    {
        if (!sum) c[0]=1;
        for (int i=9 ; i>=0 ; i--)
            while (c[i]--) printf("%d",i);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sc(x)  scanf("%d",&x)
#define sc2(x,y)  scanf("%d%d",&x,&y)
#define scl(x) scanf("%lld",&x)
#define F first
#define S second
#define pb push_back
#define pi 3.1415926535897932384
int Dx[]= {1,-1,0,0};
int Dy[]= {0,0,1,-1};
void fast()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
}
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int c[10]= {0},sum;
int main()
{
    int n;
    sc(n);
    for (int i=0 ; i<n ; i++)
    {
        int x;
        sc(x);
        c[x]++;
        sum+=x;
    }
    if (!c[0]) return printf("-1\n"),0;
    bool b=(sum%3==0);
    if (sum%3)
    {
        for (int i=1 ; i<10 ; i++)
            if (i%3==sum%3 && c[i] )
                c[i]--,sum-=i,i=10,b=1;
        if (!b)
        {
            for (int i=1 ; i<10 ; i++)
            {
                if (!(i%3)) continue;
                for (int j=1 ; j<10 ; j++)
                {
                    if (!(j%3)) continue;
                    if (sum%3==(i+j)%3)
                    {
                        if ((i==j && c[i]>=2)||(i!=j&&c[i]&&c[j]))
                        {
                            c[i]--;
                            c[j]--;
                            b=1;
                            sum-=i+j;
                            break;
                        }
                    }
                }
                if (b) break;
            }
        }
    }
    if (!b) printf("-1\n");
    else
    {
        if (!sum) c[0]=1;
        for (int i=9 ; i>=0 ; i--)
            while (c[i]>0) printf("%d",i),c[i]--;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define SZ(X) (int)(X).size()
#define pb(X) push_back(X)
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
#define pi 3.1415926536
#define ll long long
const double EPS = 1e-9;
const int inf = 1 << 30;

ll pw(ll b, ll p)
{
    if (!p) return 1;
    ll sq = pw(b, p / 2);
    sq *= sq;
    if (p % 2) sq *= b;
    return sq;
}
ll gcd(ll a, ll b)
{
    return (b == 0 ? a : gcd(b, a % b));
}
ll sd(ll x)
{
    return x<10 ? x : x % 10 + sd(x / 10);
}
ll lcm(ll a, ll b)
{
    return ((a*b) / gcd(a, b));
}
int len_of_number(ll n)
{
    ll temp = n;
    int cnt = 0;
    while(temp)
    {
        cnt++;
        temp/=10;
    }
    return cnt;
}

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

    int n, sum = 0, num[10] = {0},v,flag = 0;;
    cin>>n;
    vector<int>g;
    for(int i = 0 ; i < n ; i++)
    {
        cin>>v;
        if(!num[v])
            g.push_back(v);
        num[v]++;
    }
    if(!num[0])
    {
        cout<<-1;
        return 0;
    }

    sort(ALL(g));
    int sz =SZ(g);
    for(int i = sz - 1  ; i >=0 ; i--)
    {
        if(g[i]%3 == 0)
        {
            for(int j = 0 ; j < num[g[i]] ; j++)
                cout<<g[i];
            flag++;
        }
        else
        {
            sz = num[g[i]];

            if(num[g[i]] % 3)
                sz -= (3+(sz%3))%3;

            for(int j = 0 ; j < sz ; j++)
                cout<<g[i];
            flag++;
        }
    }
    if(!flag)
        cout<<0;
    return 0;
}


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

using namespace std;

int main()
{
    int n, sum = 0, num[10] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;
    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;
    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        for(int i = 1 ; i <= 9 && sum%3 ;i++)
        {
            if(num[i] && i%3 == (i%3 && sum%3))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }

    }
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
            cout<<i;
    }
    return 0;
}


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

using namespace std;

int main()
{
    int n, sum = 0, num[11] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;

    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;

    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        for(int i = 1 ; i <= 9 && sum%3 ;i++)
        {
            if(num[i] && (i%3 == sum%3))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }
    }
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
            cout<<i;
    }
    return 0;
}


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

using namespace std;

int main()
{
    int n, sum = 0, num[10] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;
    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;
    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        for(int i = 1 ; i <= 10 &&sum%3 ;i++)
        {
            if(num[i] && (i%3 == sum%3))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }
    }
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
            cout<<i;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define SZ(X) (int)(X).size()
#define pb(X) push_back(X)
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
#define pi 3.1415926536
#define ll long long
const double EPS = 1e-9;
const int inf = 1 << 30;

ll pw(ll b, ll p)
{
    if (!p) return 1;
    ll sq = pw(b, p / 2);
    sq *= sq;
    if (p % 2) sq *= b;
    return sq;
}
ll gcd(ll a, ll b)
{
    return (b == 0 ? a : gcd(b, a % b));
}
ll sd(ll x)
{
    return x<10 ? x : x % 10 + sd(x / 10);
}
ll lcm(ll a, ll b)
{
    return ((a*b) / gcd(a, b));
}
int len_of_number(ll n)
{
    ll temp = n;
    int cnt = 0;
    while(temp)
    {
        cnt++;
        temp/=10;
    }
    return cnt;
}

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

    int n, sum = 0, num[12] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;

    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;
    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        int af,bf;
        af = (sum+(3-sum%3)%3) - sum; // First divisible number by 3 after sum
        bf = sum -(sum-(3+sum%3)%3); // First divisible number by 3 before sum
        for(int i = 1 ; i <= 9 && sum%3 ; i++)
        {
            if(num[i] && (i%3 == af ||  i%3 == bf))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }
        for(int i = 1 ; i <= 9 && sum%3 ; i++)
        {
            if(sum%3 && i%3 && num[i])
            {
                sum -= i;
                num[i]--;
            }
        }
    }
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
            cout<<i;

    }
    }


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define SZ(X) (int)(X).size()
#define pb(X) push_back(X)
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
#define pi 3.1415926536
#define ll long long
const double EPS = 1e-9;
const int inf = 1 << 30;

ll pw(ll b, ll p)
{
    if (!p) return 1;
    ll sq = pw(b, p / 2);
    sq *= sq;
    if (p % 2) sq *= b;
    return sq;
}
ll gcd(ll a, ll b)
{
    return (b == 0 ? a : gcd(b, a % b));
}
ll sd(ll x)
{
    return x<10 ? x : x % 10 + sd(x / 10);
}
ll lcm(ll a, ll b)
{
    return ((a*b) / gcd(a, b));
}
int len_of_number(ll n)
{
    ll temp = n;
    int cnt = 0;
    while(temp)
    {
        cnt++;
        temp/=10;
    }
    return cnt;
}

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

    int n, sum = 0, num[12] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;

    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;
    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        int af,bf;
        af = (sum+(3-sum%3)%3) - sum; // First divisible number by 3 after sum
        bf = sum -(sum-(3+sum%3)%3); // First divisible number by 3 before sum
        for(int i = 1 ; i <= 9 && sum%3 ; i++)
        {
            if(num[i] && (i%3 == sum%3))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }
        for(int k = 0 ; k < 2 ; k++)
        {
            for(int i = 1 ; i <= 9 && sum%3 ; i++)
            {
                if(sum%3 && i%3 && num[i])
                {
                    sum -= i;
                    num[i]--;
                }
            }
        }

    }
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
            cout<<i;

    }
    return 0;
}


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

using namespace std;

int main()
{
    int n, sum = 0, num[12] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;

    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;
    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        int af,bf;
        af = (sum+(3-sum%3)%3) - sum;
        bf = sum -(sum-(3+sum%3)%3);
        cout<<af<<' '<<bf<<endl;
        for(int i = 1 ; i <= 9 && sum%3 ;i++)
        {

            if(num[i] && (i%3 == af ||  i%3 == bf))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }
    }
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
            cout<<i;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define SZ(X) (int)(X).size()
#define pb(X) push_back(X)
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
#define pi 3.1415926536
#define ll long long
const double EPS = 1e-9;
const int inf = 1 << 30;

ll pw(ll b, ll p)
{
    if (!p) return 1;
    ll sq = pw(b, p / 2);
    sq *= sq;
    if (p % 2) sq *= b;
    return sq;
}
ll gcd(ll a, ll b)
{
    return (b == 0 ? a : gcd(b, a % b));
}
ll sd(ll x)
{
    return x<10 ? x : x % 10 + sd(x / 10);
}
ll lcm(ll a, ll b)
{
    return ((a*b) / gcd(a, b));
}
int len_of_number(ll n)
{
    ll temp = n;
    int cnt = 0;
    while(temp)
    {
        cnt++;
        temp/=10;
    }
    return cnt;
}

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

    int n, sum = 0, num[12] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;

    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;
    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        int af,bf;
        af = (sum+(3-sum%3)%3) - sum;
        bf = sum -(sum-(3+sum%3)%3);
        for(int i = 1 ; i <= 9 && sum%3 ; i++)
        {

            if(num[i] && (i%3 == af ||  i%3 == bf))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }
    }
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
            cout<<i;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define SZ(X) (int)(X).size()
#define pb(X) push_back(X)
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
#define pi 3.1415926536
#define ll long long
const double EPS = 1e-9;
const int inf = 1 << 30;

ll pw(ll b, ll p)
{
    if (!p) return 1;
    ll sq = pw(b, p / 2);
    sq *= sq;
    if (p % 2) sq *= b;
    return sq;
}
ll gcd(ll a, ll b)
{
    return (b == 0 ? a : gcd(b, a % b));
}
ll sd(ll x)
{
    return x<10 ? x : x % 10 + sd(x / 10);
}
ll lcm(ll a, ll b)
{
    return ((a*b) / gcd(a, b));
}
int len_of_number(ll n)
{
    ll temp = n;
    int cnt = 0;
    while(temp)
    {
        cnt++;
        temp/=10;
    }
    return cnt;
}

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

    int n, sum = 0, num[12] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;

    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;
    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        int af,bf;
        af = (sum+(3-sum%3)%3) - sum; // First divisible number by 3 after sum
        bf = sum -(sum-(3+sum%3)%3); // First divisible number by 3 before sum
        for(int i = 1 ; i <= 9 && sum%3 ; i++)
        {
            if(num[i] && (i%3 == af ||  i%3 == bf))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }
        for(int j=1;j<10&&sum%3;j++)
        {
            if(num[j]&&j%3)
            {
                num[j]--;
                sum-=j;
                break;
            }
        }
    }
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
            cout<<i;

    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define SZ(X) (int)(X).size()
#define pb(X) push_back(X)
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
#define pi 3.1415926536
#define ll long long
const double EPS = 1e-9;
const int inf = 1 << 30;

ll pw(ll b, ll p)
{
    if (!p) return 1;
    ll sq = pw(b, p / 2);
    sq *= sq;
    if (p % 2) sq *= b;
    return sq;
}
ll gcd(ll a, ll b)
{
    return (b == 0 ? a : gcd(b, a % b));
}
ll sd(ll x)
{
    return x<10 ? x : x % 10 + sd(x / 10);
}
ll lcm(ll a, ll b)
{
    return ((a*b) / gcd(a, b));
}
int len_of_number(ll n)
{
    ll temp = n;
    int cnt = 0;
    while(temp)
    {
        cnt++;
        temp/=10;
    }
    return cnt;
}

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

    int n, sum = 0, num[12] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;

    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;
    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        int af,bf;
        af = (sum+(3-sum%3)%3) - sum; // First divisible number by 3 after sum
        bf = sum -(sum-(3+sum%3)%3); // First divisible number by 3 before sum
        for(int i = 1 ; i <= 9 && sum%3 ; i++)
        {
            if(num[i] && (i%3 == sum%3))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }

        for(int i = 1 ; i <= 9 && sum%3 ; i++)
        {
            if(sum%3 && i%3 && num[i])
            {
                sum -= i;
                num[i]--;
            }
        }

    }
    int flag = 0;
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
        {
            if(i > 0)
                flag++;
            if(!flag && !i)
            {
                cout<<i;
                break;
            }
            cout<<i;

        }

    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define SZ(X) (int)(X).size()
#define pb(X) push_back(X)
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
#define pi 3.1415926536
#define ll long long
const double EPS = 1e-9;
const int inf = 1 << 30;

ll pw(ll b, ll p)
{
    if (!p) return 1;
    ll sq = pw(b, p / 2);
    sq *= sq;
    if (p % 2) sq *= b;
    return sq;
}
ll gcd(ll a, ll b)
{
    return (b == 0 ? a : gcd(b, a % b));
}
ll sd(ll x)
{
    return x<10 ? x : x % 10 + sd(x / 10);
}
ll lcm(ll a, ll b)
{
    return ((a*b) / gcd(a, b));
}
int len_of_number(ll n)
{
    ll temp = n;
    int cnt = 0;
    while(temp)
    {
        cnt++;
        temp/=10;
    }
    return cnt;
}

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

    int n, sum = 0, num[12] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;

    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;
    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        int af,bf;
        af = (sum+(3-sum%3)%3) - sum; // First divisible number by 3 after sum
        bf = sum -(sum-(3+sum%3)%3); // First divisible number by 3 before sum
        for(int i = 1 ; i <= 9 && sum%3 ; i++)
        {
            if(num[i] && (i%3 == af ||  i%3 == bf))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }
        for(int k = 0 ; k < 2 ; k++)
        {
            for(int i = 1 ; i <= 9 && sum%3 ; i++)
            {
                if(sum%3 && i%3 && num[i])
                {
                    sum -= i;
                    num[i]--;
                }
            }
        }

    }
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
            cout<<i;

    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define SZ(X) (int)(X).size()
#define pb(X) push_back(X)
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
#define pi 3.1415926536
#define ll long long
const double EPS = 1e-9;
const int inf = 1 << 30;

ll pw(ll b, ll p)
{
    if (!p) return 1;
    ll sq = pw(b, p / 2);
    sq *= sq;
    if (p % 2) sq *= b;
    return sq;
}
ll gcd(ll a, ll b)
{
    return (b == 0 ? a : gcd(b, a % b));
}
ll sd(ll x)
{
    return x<10 ? x : x % 10 + sd(x / 10);
}
ll lcm(ll a, ll b)
{
    return ((a*b) / gcd(a, b));
}
int len_of_number(ll n)
{
    ll temp = n;
    int cnt = 0;
    while(temp)
    {
        cnt++;
        temp/=10;
    }
    return cnt;
}

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

    int n, sum = 0, num[12] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;

    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;
    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        int af,bf;
        af = (sum+(3-sum%3)%3) - sum; // First divisible number by 3 after sum
        bf = sum -(sum-(3+sum%3)%3); // First divisible number by 3 before sum
        for(int i = 1 ; i <= 9 && sum%3 ; i++)
        {
            if(num[i] && (i%3 == sum%3))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }
        for(int k = 0 ; k < 2 ; k++)
        {
            for(int i = 1 ; i <= 9 && sum%3 ; i++)
            {
                if(sum%3 && i%3 && num[i])
                {
                    sum -= i;
                    num[i]--;
                }
            }
        }

    }
    int flag = 0;
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
        {
            if(i > 0)
                flag++;
            if(!flag && !i)
            {
               cout<<i;
               break;
            }
            cout<<i;

        }

    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define SZ(X) (int)(X).size()
#define pb(X) push_back(X)
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
#define pi 3.1415926536
#define ll long long
const double EPS = 1e-9;
const int inf = 1 << 30;

ll pw(ll b, ll p)
{
    if (!p) return 1;
    ll sq = pw(b, p / 2);
    sq *= sq;
    if (p % 2) sq *= b;
    return sq;
}
ll gcd(ll a, ll b)
{
    return (b == 0 ? a : gcd(b, a % b));
}
ll sd(ll x)
{
    return x<10 ? x : x % 10 + sd(x / 10);
}
ll lcm(ll a, ll b)
{
    return ((a*b) / gcd(a, b));
}
int len_of_number(ll n)
{
    ll temp = n;
    int cnt = 0;
    while(temp)
    {
        cnt++;
        temp/=10;
    }
    return cnt;
}

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

    int n, sum = 0, num[12] = {0},v;
    cin>>n;
    for(int i = 0 ; i < n ; i++)
        cin>>v,num[v]++;

    if(!num[0])
    {
        cout<<-1;
        return 0;
    }
    for(int i = 1 ; i <= 9 ; i++)
        sum += num[i]*i;
    while(sum%3)
    {
        if(sum < 3)
        {
            cout<<0;
            return 0;
        }
        int af,bf;
        af = (sum+(3-sum%3)%3) - sum; // First divisible number by 3 after sum
        bf = sum -(sum-(3+sum%3)%3); // First divisible number by 3 before sum
        for(int i = 1 ; i <= 9 && sum%3 ; i++)
        {
            if(num[i] && (i%3 == sum%3))
            {
                sum -= i;
                num[i]--;
                break;
            }
        }
        for(int k = 0 ; k < 2 ; k++)
        {
            for(int i = 1 ; i <= 9 && sum%3 ; i++)
            {
                if(sum%3 && i%3 && num[i])
                {
                    sum -= i;
                    num[i]--;
                }
            }
        }

    }
    for(int i = 9 ; i >=0 ; i--)
    {
        for(int j = 0; j <num[i] ; j++)
        {
            if(i == 0 && j == 0)
                cout<<i;
        }

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
    if (a == 0) return b;
    else return gcd(b % a, a);
}

int main(){
    ll n,fpb,sum=1, now; cin >> n;
    ll a[n];
    for (int i=0; i<n;i++){
    	cin >> a[i];
	}
	fpb=a[0];
	for (int i=0; i<n;i++){
		fpb=gcd(fpb,a[i]);
	}
	cerr << fpb << '\n';
	if (fpb>1) sum++;
	for (ll i=2; i*i<=fpb;i++){
		if (fpb%i==0){
			sum+=2;
			if (i*i==fpb){
				sum--;
			}
		}
	}
	cout << sum;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
    if (a == 0) return b;
    else return gcd(b % a, a);
}

int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n,fpb,sum=1, now; cin >> n;
    ll a[n];
    for (int i=0; i<n;i++){
    	cin >> a[i];
	}
	fpb=a[0];
	for (int i=0; i<n;i++){
		fpb=gcd(fpb,a[i]);
	}
	cerr << fpb << '\n';
	if (fpb>1) sum++;
	for (ll i=2; i*i<=fpb;i++){
		if (fpb%i==0){
			sum+=2;
			if (i*i==fpb){
				sum--;
			}
		}
	}
	cout << sum;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
    ll c;
	while(b){
    	c = a%b;	a = b;b = c;
	}
	return a;
}

int main(){
    ll a,n, now=0; cin >> n;
    for (ll i=0; i<n;i++){
    	cin >> a;
    	now=gcd(a,now);
	}
	ll d=2, ans=1;
	while(now != 1){
		int prime = 0;
		while(now%d == 0){
			now /= d; prime ++;
		}
		ans *= prime+1;
		d++;
	}
	cout << ans;
}