import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class BB {

    static StreamTokenizer st;
    public static void main(String[] args) throws IOException{
        st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int n = nextInt();
        int[]a = new int[n];
        int sum = 0;
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
            sum += a[i];
        }
        Arrays.sort(a);
        if (a[0] != 0) {
            System.out.println(-1);
            return;
        }
        int[]ans = new int[n+1];
        int t = 0;
        if (sum % 3==0) {
            for (int i = n-1; i >= 0; i--) {
                ans[t++] = a[i];
            }
        }
        else {
            boolean f = true;
            for (int i = 0; i < n; i++) {
                if ((sum-a[i]) % 3==0) {
                    f = false;
                    for (int j = n-1; j >= 0; j--) {
                        if (j != i) {
                            ans[t++] = a[j];
                        }
                    }
                    break;
                }
            }
            if (f) {
                for (int i = 0; i < n-1; i++) {
                    if ((sum-a[i]-a[i+1]) % 3==0) {
                        for (int j = n-1; j >= 0; j--) {
                            if (j != i && j != i+1) {
                                ans[t++] = a[j];
                            }
                        }
                        break;
                    }
                }
            }
        }
        if (t==0)
            pw.print(-1);
        else {
            if (ans[0]==0)
                pw.print(0);
            else {
                for (int i = 0; i < t; i++) {
                    pw.print(ans[i]);
                }
            }
        }
        pw.close();
    }

    private static int nextInt() throws IOException{
        st.nextToken();
        return (int) st.nval;
    }

}


import java.io.*;
import java.util.*;

public class Hometask {

    public static void main( String[] args ) throws IOException {

        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( System.out ) ) );

        int n = Integer.parseInt( in.readLine() );
        StringTokenizer tokens = new StringTokenizer( in.readLine() );

        int[] nums = new int[ n ];

        for( int i = 0; i < n; i++ ) {
            nums[ i ] = Integer.parseInt( tokens.nextToken() );
        }

        Arrays.sort( nums );

        if( nums[ 0 ] != 0 ) {
            out.println( "-1" );
            out.flush();
            out.close();
            System.exit( 0 );
        }

        int sum = 0;
        for( int i = 0; i < n; i++ ) {
            sum += nums[ i ];
        }
        //System.out.println( sum );
        if( sum % 3 == 0 ) {

            for( int i = n - 1; i >= 0; i-- ) {
                out.print( nums[ i ] );
            }
            out.println();
            out.flush();
            out.close();
            System.exit( 0 );
        }
        else if( sum % 3 == 1 ) {

            for( int i = 0; i < n; i++ ) {
                if( nums[ i ] % 3 == 1 ) {

                    for( int j = n - 1; j >= 0; j-- ) {
                        //System.out.println( i + " " + j + ( i != j ) );
                        if( i != j ) {
                            //System.out.println( "adf" );
                            out.print( nums[ j ] + "" );
                        }
                    }

                    out.println();
                    out.flush();
                    out.close();
                    System.exit( 0 );
                }
            }

            out.println( "-1" );
            out.flush();
            out.close();
            System.exit( 0 );
        }
        else if( sum % 3 == 2 ) {

            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 2 ) {

                    for( int j = n - 1; j >= 0; j-- ) {
                        if( i != j ) {
                            out.print( nums[ j ] );
                        }

                    }

                    out.println();
                    out.flush();
                    out.close();
                    System.exit( 0 );

                }
            }

            int counter = 0;
            int index = 0;
            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 1 ) {

                    counter++;
                    if( counter == 2 ) {

                        for( int j = n - 1; j >= 0; j-- ) {

                            if( i != j && index != j ) {
                                out.print( nums[ j ] );
                            }

                        }

                        out.println();
                        out.flush();
                        out.close();
                        System.exit( 0 );
                    }
                    else {
                        index = i;
                    }
                }
            }

            out.println( "-1" );
            out.flush();
            out.close();

        }
    }
}


import java.io.*;
import java.util.*;

public class Hometask {

    public static void main( String[] args ) throws IOException {

        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( System.out ) ) );

        int n = Integer.parseInt( in.readLine() );
        StringTokenizer tokens = new StringTokenizer( in.readLine() );

        int[] nums = new int[ n ];

        for( int i = 0; i < n; i++ ) {
            nums[ i ] = Integer.parseInt( tokens.nextToken() );
        }

        Arrays.sort( nums );

        if( nums[ 0 ] != 0 ) {
            out.println( "-1" );
            out.flush();
            out.close();
            System.exit( 0 );
        }

        int sum = 0;
        for( int i = 0; i < n; i++ ) {
            sum += nums[ i ];
        }
        //System.out.println( sum );
        if( sum % 3 == 0 ) {

            boolean leadingZeros = true;
            for( int i = n - 1; i >= 0; i-- ) {

                if( nums[ i ] != 0 ) {
                    out.print( nums[ i ] );
                    leadingZeros = false;
                }
                else if( nums[ i ] == 0 && leadingZeros && i > 0 ) {
                    continue;
                }
                else {
                    out.print( nums[ i ] );
                }
            }
            out.println();
            out.flush();
            out.close();
            System.exit( 0 );
        }
        else if( sum % 3 == 1 ) {

            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 1 ) {

                    boolean leadingZeros = true;
                    for( int j = n - 1; j >= 0; j-- ) {
                        //System.out.println( i + " " + j + ( i != j ) );
                        if( i != j ) {
                            //System.out.println( "adf" );
                            if( nums[ j ] != 0 ) {
                                out.print( nums[ j ] );
                                leadingZeros = false;
                            }
                            else if( nums[ j ] == 0 && leadingZeros && j > 0 ) {
                                continue;
                            }
                            else {
                                out.print( nums[ j ] );
                            }
                        }
                    }

                    out.println();
                    out.flush();
                    out.close();
                    System.exit( 0 );
                }
            }

            int counter = 0;
            int index = 0;

            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 2 ) {
                    counter++;

                    if( counter == 2 ) {

                        boolean leadingZeros = true;

                        for( int j = n - 1; j >= 0; j-- ) {
                            //System.out.println( i + " " + j + ( i != j )
                            if( i != j && index != j ) {

                                //System.out.println( "adf" );
                                if( nums[ j ] != 0 ) {
                                    out.print( nums[ j ] + "" );
                                    leadingZeros = false;
                                }
                                else if( nums[ j ] == 0 && leadingZeros && j > 0 ) {
                                    continue;
                                }
                                else {
                                    out.print( nums[ j ] );
                                }
                            }
                        }

                        out.println();
                        out.flush();
                        out.close();
                        System.exit( 0 );

                    }
                    else {
                        index = i;
                    }
                }
            }

            out.println( "-1" );
            out.flush();
            out.close();
            System.exit( 0 );
        }
        else if( sum % 3 == 2 ) {

            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 2 ) {

                    boolean leadingZeros = true;
                    for( int j = n - 1; j >= 0; j-- ) {
                        if( i != j ) {

                            if( nums[ j ] != 0 ) {
                                out.print( nums[ j ] );
                                leadingZeros = false;
                            }
                            else if( nums[ j ] == 0 && leadingZeros && j > 0 ) {
                                continue;
                            }
                            else {
                                out.print( nums[ j ] );
                            }
                        }

                    }

                    out.println();
                    out.flush();
                    out.close();
                    System.exit( 0 );

                }
            }

            int counter = 0;
            int index = 0;
            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 1 ) {

                    counter++;
                    if( counter == 2 ) {

                        boolean leadingZeros = true;
                        for( int j = n - 1; j >= 0; j-- ) {

                            if( i != j && index != j ) {

                                if( nums[ j ] != 0 ) {
                                    out.print( nums[ j ] );
                                    leadingZeros = false;
                                }
                                else if( nums[ j ] == 0 && leadingZeros && j > 0 ) {
                                    continue;
                                }
                                else {
                                    out.print( nums[ j ] );
                                }
                            }

                        }

                        out.println();
                        out.flush();
                        out.close();
                        System.exit( 0 );
                    }
                    else {
                        index = i;
                    }
                }
            }

            out.println( "-1" );
            out.flush();
            out.close();

        }
    }
}


import java.io.*;
import java.util.*;

public class Hometask {

    public static void main( String[] args ) throws IOException {

        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( System.out ) ) );

        int n = Integer.parseInt( in.readLine() );
        StringTokenizer tokens = new StringTokenizer( in.readLine() );

        int[] nums = new int[ n ];

        for( int i = 0; i < n; i++ ) {
            nums[ i ] = Integer.parseInt( tokens.nextToken() );
        }

        Arrays.sort( nums );

        if( nums[ 0 ] != 0 ) {
            out.println( "-1" );
            out.flush();
            out.close();
            System.exit( 0 );
        }

        int sum = 0;
        for( int i = 0; i < n; i++ ) {
            sum += nums[ i ];
        }
        //System.out.println( sum );
        if( sum % 3 == 0 ) {

            boolean leadingZeros = true;
            for( int i = n - 1; i >= 0; i-- ) {

                if( nums[ i ] != 0 ) {
                    out.print( nums[ i ] );
                    leadingZeros = true;
                }
                else if( nums[ i ] == 0 && leadingZeros && i > 0 ) {
                    continue;
                }
                else {
                    out.print( nums[ i ] );
                }
            }
            out.println();
            out.flush();
            out.close();
            System.exit( 0 );
        }
        else if( sum % 3 == 1 ) {

            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 1 ) {

                    boolean leadingZeros = true;
                    for( int j = n - 1; j >= 0; j-- ) {
                        //System.out.println( i + " " + j + ( i != j ) );
                        if( i != j ) {
                            //System.out.println( "adf" );
                            if( nums[ j ] != 0 ) {
                                out.print( nums[ j ] + "" );
                                leadingZeros = false;
                            }
                            else if( nums[ j ] == 0 && leadingZeros && j > 0 ) {
                                continue;
                            }
                            else {
                                out.print( nums[ j ] );
                            }
                        }
                    }

                    out.println();
                    out.flush();
                    out.close();
                    System.exit( 0 );
                }
            }

            int counter = 0;
            int index = 0;

            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 2 ) {
                    counter++;

                    if( counter == 2 ) {

                        boolean leadingZeros = true;

                        for( int j = n - 1; j >= 0; j-- ) {
                            //System.out.println( i + " " + j + ( i != j )
                            if( i != j && index != j ) {

                                //System.out.println( "adf" );
                                if( nums[ j ] != 0 ) {
                                    out.print( nums[ j ] + "" );
                                    leadingZeros = false;
                                }
                                else if( nums[ j ] == 0 && leadingZeros && j > 0 ) {
                                    continue;
                                }
                                else {
                                    out.print( nums[ j ] );
                                }
                            }
                        }

                        out.println();
                        out.flush();
                        out.close();
                        System.exit( 0 );

                    }
                    else {
                        index = i;
                    }
                }
            }

            out.println( "-1" );
            out.flush();
            out.close();
            System.exit( 0 );
        }
        else if( sum % 3 == 2 ) {

            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 2 ) {

                    boolean leadingZeros = true;
                    for( int j = n - 1; j >= 0; j-- ) {
                        if( i != j ) {

                            if( nums[ j ] != 0 ) {
                                out.print( nums[ j ] );
                                leadingZeros = false;
                            }
                            else if( nums[ j ] == 0 && leadingZeros && j > 0 ) {
                                continue;
                            }
                            else {
                                out.print( nums[ j ] );
                            }
                        }

                    }

                    out.println();
                    out.flush();
                    out.close();
                    System.exit( 0 );

                }
            }

            int counter = 0;
            int index = 0;
            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 1 ) {

                    counter++;
                    if( counter == 2 ) {

                        boolean leadingZeros = true;
                        for( int j = n - 1; j >= 0; j-- ) {

                            if( i != j && index != j ) {

                                if( nums[ j ] != 0 ) {
                                    out.print( nums[ j ] );
                                    leadingZeros = false;
                                }
                                else if( nums[ j ] == 0 && leadingZeros && j > 0 ) {
                                    continue;
                                }
                                else {
                                    out.print( nums[ j ] );
                                }
                            }

                        }

                        out.println();
                        out.flush();
                        out.close();
                        System.exit( 0 );
                    }
                    else {
                        index = i;
                    }
                }
            }

            out.println( "-1" );
            out.flush();
            out.close();

        }
    }
}


import java.io.*;
import java.util.*;

public class Hometask {

    public static void main( String[] args ) throws IOException {

        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( System.out ) ) );

        int n = Integer.parseInt( in.readLine() );
        StringTokenizer tokens = new StringTokenizer( in.readLine() );

        int[] nums = new int[ n ];

        for( int i = 0; i < n; i++ ) {
            nums[ i ] = Integer.parseInt( tokens.nextToken() );
        }

        Arrays.sort( nums );

        if( nums[ 0 ] != 0 ) {
            out.println( "-1" );
            out.flush();
            out.close();
            System.exit( 0 );
        }

        int sum = 0;
        for( int i = 0; i < n; i++ ) {
            sum += nums[ i ];
        }
        //System.out.println( sum );
        if( sum % 3 == 0 ) {

            boolean leadingZeros = true;
            for( int i = n - 1; i >= 0; i-- ) {

                if( nums[ i ] != 0 ) {
                    out.print( nums[ i ] );
                    leadingZeros = true;
                }
                else if( nums[ i ] == 0 && leadingZeros && i > 0 ) {
                    continue;
                }
                else {
                    out.print( nums[ i ] );
                }
            }
            out.println();
            out.flush();
            out.close();
            System.exit( 0 );
        }
        else if( sum % 3 == 1 ) {

            for( int i = 0; i < n; i++ ) {
                if( nums[ i ] % 3 == 1 ) {

                    boolean leadingZeros = true;
                    for( int j = n - 1; j >= 0; j-- ) {
                        //System.out.println( i + " " + j + ( i != j ) );
                        if( i != j ) {
                            //System.out.println( "adf" );
                            if( nums[ j ] != 0 ) {
                                out.print( nums[ j ] + "" );
                                leadingZeros = false;
                            }
                            else if( nums[ j ] == 0 && leadingZeros && j > 0 ) {
                                continue;
                            }
                            else {
                                out.print( nums[ j ] );
                            }
                        }
                    }

                    out.println();
                    out.flush();
                    out.close();
                    System.exit( 0 );
                }
            }

            out.println( "-1" );
            out.flush();
            out.close();
            System.exit( 0 );
        }
        else if( sum % 3 == 2 ) {

            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 2 ) {

                    boolean leadingZeros = true;
                    for( int j = n - 1; j >= 0; j-- ) {
                        if( i != j ) {

                            if( nums[ j ] != 0 ) {
                                out.print( nums[ j ] );
                                leadingZeros = false;
                            }
                            else if( nums[ j ] == 0 && leadingZeros && j > 0 ) {
                                continue;
                            }
                            else {
                                out.print( nums[ j ] );
                            }
                        }

                    }

                    out.println();
                    out.flush();
                    out.close();
                    System.exit( 0 );

                }
            }

            int counter = 0;
            int index = 0;
            for( int i = 0; i < n; i++ ) {

                if( nums[ i ] % 3 == 1 ) {

                    counter++;
                    if( counter == 2 ) {

                        boolean leadingZeros = true;
                        for( int j = n - 1; j >= 0; j-- ) {

                            if( i != j && index != j ) {

                                if( nums[ j ] != 0 ) {
                                    out.print( nums[ j ] );
                                    leadingZeros = false;
                                }
                                else if( nums[ j ] == 0 && leadingZeros && j > 0 ) {
                                    continue;
                                }
                                else {
                                    out.print( nums[ j ] );
                                }
                            }

                        }

                        out.println();
                        out.flush();
                        out.close();
                        System.exit( 0 );
                    }
                    else {
                        index = i;
                    }
                }
            }

            out.println( "-1" );
            out.flush();
            out.close();

        }
    }
}


import java.util.Arrays;
import java.util.Scanner;
public class affa {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner bahy = new Scanner(System.in);
        int n=bahy.nextInt();
        int[]array=new int[n];
        int total =0;
        boolean k=false;
        int counter=n;
        int data[]=new int[10];
        for(int h=0;h<10;h++)
        {
        	data[h]=0;
        }
        for(int i=0;i<n;i++)
        {
        	array[i]=bahy.nextInt();
        	total+=array[i];
        	if(array[i]==0)
        		k=true;
        	data[array[i]]++;
        }
        if(total==0)
        {
        	System.out.println(0);
        }
        else if(k==false)
        {
        	System.out.println(-1);
        }
        else
        {
        	boolean key=false;
        	int h=1;
        	while(total%3!=0)
        	{
        		if((total-(1*h))%3==0&&data[1]!=0)
        		{
        			while((total-(1*h))%3==0&&data[1]!=0)
        			{
        				 total-=1;
       		             data[1]--;
        			}
        		}
        		else if((total-(2*h))%3==0&&data[2]!=0)
        		{
        			while((total-(2*h))%3==0&&data[2]!=0)
        			{
        				 total-=2;
       		             data[2]--;
        			}
        		}

        		else if((total-(4*h))%3==0&&data[4]!=0)
        		{
        			while((total-(4*h))%3==0&&data[4]!=0)
        			{
        				 total-=4;
       		             data[4]--;
        			}
        		}
        		else if((total-(5*h))%3==0&&data[5]!=0)
        		{
        			while((total-(5*h))%3==0&&data[5]!=0)
        			{
        				 total-=5;
       		             data[5]--;
        			}
        		}
        		else if((total-(7*h))%3==0&&data[7]!=0)
        		{
        			while((total-(7*h))%3==0&&data[7]!=0)
        			{
        				 total-=7;
       		             data[7]--;
        			}
        		}
        		else if((total-(8*h))%3==0&&data[8]!=0)
        		{
        			while((total-(8*h))%3==0&&data[8]!=0)
        			{
        				 total-=8;
       		             data[8]--;
        			}
        		}

        		h++;
        	}
        	if(total==0)
        	{
        		System.out.println(0);
        	}
        	else
        	{
        		for(int hj=9;hj>=0;hj--)
			    {
					for(int f=0;f<data[hj];f++)
				    {
					System.out.print(hj);
				    }
			    }
        	}

        }
	}

}


import java.util.Arrays;
import java.util.Scanner;
public class gg {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner bahy = new Scanner(System.in);
        int n=bahy.nextInt();
        int[]array=new int[n];
        int total =0;
        boolean k=false;
        int counter=n;
        int data[]=new int[10];
        for(int h=0;h<10;h++)
        {
        	data[h]=0;
        }
        for(int i=0;i<n;i++)
        {
        	array[i]=bahy.nextInt();
        	total+=array[i];
        	if(array[i]==0)
        		k=true;
        	data[array[i]]++;
        }
        if(total==0)
        {
        	System.out.println(0);
        }
        else if(k==false)
        {
        	System.out.println(-1);
        }
        else
        {
        	boolean key=false;
        	int h=1;
        	while(total%3!=0)
        	{
        		if((total-(1*h))%3==0&&data[1]!=0)
        		{
        			while((total-(1*h))%3==0&&data[1]!=0)
        			{
        				 total-=1;
       		             data[1]--;
        			}
        		}
        		else if((total-(2*h))%3==0&&data[2]!=0)
        		{
        			while((total-(2*h))%3==0&&data[2]!=0)
        			{
        				 total-=2;
       		             data[2]--;
        			}
        		}

        		else if((total-(4*h))%3==0&&data[4]!=0)
        		{
        			while((total-(4*h))%3==0&&data[4]!=0)
        			{
        				 total-=4;
       		             data[4]--;
        			}
        		}
        		else if((total-(5*h))%3==0&&data[5]!=0)
        		{
        			while((total-(5*h))%3==0&&data[5]!=0)
        			{
        				 total-=5;
       		             data[5]--;
        			}
        		}
        		else if((total-(7*h))%3==0&&data[7]!=0)
        		{
        			while((total-(7*h))%3==0&&data[7]!=0)
        			{
        				 total-=7;
       		             data[7]--;
        			}
        		}
        		else if((total-(8*h))%3==0&&data[8]!=0)
        		{
        			while((total-(8*h))%3==0&&data[8]!=0)
        			{
        				 total-=8;
       		             data[8]--;
        			}
        		}

        		h++;
        	}
        	if(total==0)
        	{
        		System.out.println(0);
        	}
        	else
        	{
        		for(int hj=9;hj>=0;hj--)
			    {
					for(int f=0;f<data[hj];f++)
				    {
					System.out.print(hj);
				    }
			    }
        	}

        }
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class B implements Runnable {

	private void Solution() throws IOException {
		int n = nextInt();
		int[] mas = new int[10];
		int mod = 0;
		for (int i = 0; i < n; i++) {
			int a = nextInt();
			mas[a]++;
			mod = (mod + a) % 3;
		}
		if (mas[0] == 0) {
			print(-1);
			return;
		}
		if (mod == 1) {
			if (mas[1] > 0)
				mas[1]--;
			else if (mas[4] > 0)
				mas[4]--;
			else if (mas[7] > 0)
				mas[7]--;
			else if (mas[2] > 1)
				mas[2] -= 2;
			else if (mas[2] > 0 && mas[5] > 0) {
				mas[2]--;
				mas[5]--;
			} else if (mas[5] > 1)
				mas[5] -= 2;
			else if (mas[8] > 0 && mas[2] > 0) {
				mas[8]--;
				mas[2]--;
			} else if (mas[5] > 0 && mas[8] > 0) {
				mas[5]--;
				mas[8]--;
			} else if (mas[8] > 1)
				mas[8] -= 2;
			else {
				print(-1);
				return;
			}
		} else if (mod == 2) {
			if (mas[2] > 0)
				mas[2]--;
			else if (mas[5] > 0)
				mas[5]--;
			else if (mas[8] > 0)
				mas[8]--;
			else if (mas[1] > 1)
				mas[1] -= 2;
			else if (mas[1] > 0 && mas[4] > 0) {
				mas[1]--;
				mas[4]--;
			} else if (mas[4] > 1)
				mas[4] -= 2;
			else if (mas[1] > 0 && mas[7] > 0) {
				mas[1]--;
				mas[7]--;
			} else if (mas[4] > 0 && mas[7] > 0) {
				mas[4]--;
				mas[7]--;
			} else if (mas[7] > 1)
				mas[7] -= 2;
			else {
				print(-1);
				return;
			}
		}
		boolean ok = true;
		for (int i = 1; i < 10; i++)
			if (mas[i] != 0)
				ok = false;
		if (ok) {
			print(0);
			return;
		}
		for (int i = 9; i >= 0; i--)
			for (int j = 0; j < mas[i]; j++)
				print(i);
	}

	public static void main(String[] args) {
		new B().run();
	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer tokenizer;

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			Solution();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	void print(Object... o) {
		for (int i = 0; i < o.length; i++) {
			if (i != 0)
				out.print(" ");
			out.print(o[i]);
		}
	}

	void println(Object... o) {
		for (int i = 0; i < o.length; i++) {
			if (i != 0)
				print(" ");
			print(o[i]);
		}
		print("\n");
	}

	int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(next());
	}

	double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(next());
	}

	String nextLine() throws IOException {
		return in.readLine();
	}

	String next() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(nextLine());
		return tokenizer.nextToken();
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class B implements Runnable {

	private void Solution() throws IOException {
		int n = nextInt();
		int[] mas = new int[10];
		int mod = 0;
		for (int i = 0; i < n; i++) {
			int a = nextInt();
			mas[a]++;
			mod = (mod + a) % 3;
		}
		if (mas[0] == 0) {
			print(-1);
			return;
		}
		if (mod == 1) {
			if (mas[1] > 0)
				mas[1]--;
			else if (mas[4] > 0)
				mas[4]--;
			else if (mas[7] > 0)
				mas[7]--;
			else if (mas[2] > 1)
				mas[2] -= 2;
			else if (mas[2] > 0 && mas[5] > 0) {
				mas[2]--;
				mas[5]--;
			} else if (mas[5] > 1)
				mas[5] -= 2;
			else if (mas[8] > 0 && mas[2] > 0) {
				mas[8]--;
				mas[2]--;
			} else if (mas[5] > 0 && mas[8] > 0) {
				mas[5]--;
				mas[8]--;
			} else if (mas[8] > 1)
				mas[8] -= 2;
			else {
				print(-1);
				return;
			}
		} else if (mod == 2) {
			if (mas[2] > 0)
				mas[2]--;
			else if (mas[5] > 0)
				mas[5]--;
			else if (mas[8] > 0)
				mas[8]--;
			else if (mas[1] > 1)
				mas[1] -= 2;
			else if (mas[1] > 0 && mas[4] > 0) {
				mas[1]--;
				mas[4]--;
			} else if (mas[4] > 1)
				mas[4] -= 2;
			else if (mas[1] > 0 && mas[7] > 0) {
				mas[1]--;
				mas[7]--;
			} else if (mas[4] > 0 && mas[7] > 0) {
				mas[4]--;
				mas[7]--;
			} else if (mas[7] > 1)
				mas[7] -= 2;
			else {
				print(-1);
				return;
			}
		}
		boolean ok = true;
		for (int i = 0; i < 10; i++)
			if (mas[i] != 0)
				ok = false;
		if (ok) {
			print(-1);
			return;
		}
		for (int i = 9; i >= 0; i--)
			for (int j = 0; j < mas[i]; j++)
				print(i);
	}

	public static void main(String[] args) {
		new B().run();
	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer tokenizer;

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			Solution();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	void print(Object... o) {
		for (int i = 0; i < o.length; i++) {
			if (i != 0)
				out.print(" ");
			out.print(o[i]);
		}
	}

	void println(Object... o) {
		for (int i = 0; i < o.length; i++) {
			if (i != 0)
				print(" ");
			print(o[i]);
		}
		print("\n");
	}

	int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(next());
	}

	double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(next());
	}

	String nextLine() throws IOException {
		return in.readLine();
	}

	String next() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(nextLine());
		return tokenizer.nextToken();
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class B implements Runnable {

	private void Solution() throws IOException {
		int n = nextInt();
		int[] mas = new int[10];
		int mod = 0;
		for (int i = 0; i < n; i++) {
			int a = nextInt();
			mas[a]++;
			mod = (mod + a) % 3;
		}
		if (mas[0] == 0) {
			print(-1);
			return;
		}
		if (mod == 1) {
			if (mas[1] > 0)
				mas[1]--;
			else if (mas[4] > 0)
				mas[4]--;
			else if (mas[7] > 0)
				mas[7]--;
			else if (mas[2] > 1)
				mas[2] -= 2;
			else if (mas[2] > 0 && mas[5] > 0) {
				mas[2]--;
				mas[5]--;
			} else if (mas[5] > 1)
				mas[5] -= 2;
			else if (mas[5] > 0 && mas[8] > 0) {
				mas[5]--;
				mas[8]--;
			} else if (mas[8] > 1)
				mas[8] -= 2;
			else if (mas[8] > 3 && mas[2] > 0) {
				mas[8] -= 4;
				mas[2]--;
			} else {
				print(-1);
				return;
			}
		} else if (mod == 2) {
			if (mas[2] > 0)
				mas[2]--;
			else if (mas[5] > 0)
				mas[5]--;
			else if (mas[8] > 0)
				mas[8]--;
			else if (mas[1] > 1)
				mas[1] -= 2;
			else if (mas[1] > 0 && mas[4] > 0) {
				mas[1]--;
				mas[4]--;
			} else if (mas[4] > 1)
				mas[4] -= 2;
			else if (mas[1] > 0 && mas[7] > 0) {
				mas[1]--;
				mas[7]--;
			} else if (mas[4] > 0 && mas[7] > 0) {
				mas[4]--;
				mas[7]--;
			} else if (mas[7] > 1)
				mas[7] -= 2;
			else {
				print(-1);
				return;
			}
		}
		boolean ok = true;
		for (int i = 0; i < 10; i++)
			if (mas[i] != 0)
				ok = false;
		if (ok) {
			print(-1);
			return;
		}
		for (int i = 9; i >= 0; i--)
			for (int j = 0; j < mas[i]; j++)
				print(i);
	}

	public static void main(String[] args) {
		new B().run();
	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer tokenizer;

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			Solution();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	void print(Object... o) {
		for (int i = 0; i < o.length; i++) {
			if (i != 0)
				out.print(" ");
			out.print(o[i]);
		}
	}

	void println(Object... o) {
		for (int i = 0; i < o.length; i++) {
			if (i != 0)
				print(" ");
			print(o[i]);
		}
		print("\n");
	}

	int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(next());
	}

	double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(next());
	}

	String nextLine() throws IOException {
		return in.readLine();
	}

	String next() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(nextLine());
		return tokenizer.nextToken();
	}
}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        sc.nextLine();
        String in2 = sc.nextLine();
        sc.close();
        String[] in2array = in2.split(" ");

        int[] numbersCount = new int[10];
        int sum = 0;

        for (int i = 0; i < in2array.length; i++) {
            int number = Integer.parseInt(in2array[i]);
            numbersCount[number] += 1;
            sum += number;
        }

        if (numbersCount[0] > 0){
            if (sum % 3 == 0) {
                printNumber(numbersCount);
            } else {
                if (removeNumbers(numbersCount, sum)){
                    printNumber(numbersCount);
                } else {
                    System.out.println(-1);
                }
            }
        } else {
            System.out.println(-1);
        }

    }

    public static void printNumber(int[] arr){
        StringBuilder sb = new StringBuilder();
        boolean bool = false;

        for (int i = 9; i >= 0; --i) {
            for (int j = 0; j < arr[i]; j++) {
                if (i > 0)  bool = true;

                if (bool)
                    sb.append(i);
            }
        }

        if (sb.toString().isEmpty()) sb.append(0);

        System.out.println(sb.toString());
    }

    public static boolean removeNumbers(int[] arr, int sum){
        int rem = sum % 3;

        for (int i = rem; i < 10; i += 3) {
            if (arr[i] > 0){
                --arr[i];
                return true;
            }
        }

        if (rem == 1){
            for (int i = 4; i < 10; i += 3) {
                if (arr[i-2] > 0 && arr[2] > 0){
                    --arr[i-2];
                    --arr[2];
                    return true;
                }
            }
            if (arr[5] > 1){
                arr[5] -= 2;
                return true;
            }
            if (arr[8] > 1){
                arr[8] -= 2;
                return true;
            }
        } else {
            for (int i = 2; i < 10; i += 3) {
                if (arr[i-1] > 0 && arr[1] > 0){
                    --arr[i-1];
                    --arr[1];
                    return true;
                }
            }
            if (arr[4] > 1){
                arr[4] -= 2;
                return true;
            }
            if (arr[7] > 1){
                arr[7] -= 2;
                return true;
            }
        }

        return false;
    }

}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        int[] numbers = new int[in1];
        sc.nextLine();
        String in2 = sc.nextLine();
        String[] in2array = in2.split(" ");

        sc.close();

        for (int i = 0; i < in1; i++) {
            numbers[i] = Integer.parseInt(in2array[i]);
        }

        if (sum(numbers) % 3 == 0 || inArray(numbers, 0)) {

            sortArr(numbers);
            String str = "";
            StringBuilder sb = new StringBuilder(str);

            for (int i = 0; i < numbers.length; i++) {
                sb.append(numbers[i]);
            }

            System.out.println(sb);

        } else {
            System.out.println(-1);
        }

    }

    public static boolean inArray(int[] arr, int number) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == number)
                return true;
        }

        return false;
    }

    public static int sum(int[] arr) {
        int result = 0;

        for (int i = 0; i < arr.length; i++) {
            result += arr[i];
        }

        return result;
    }

    public static void sortArr(int[] arr) {
        int temp;

        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1; j++) {
                if (arr[j+1] >arr[j]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] =  temp;
                }
            }
        }
    }

}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        sc.nextLine();
        String in2 = sc.nextLine();
        sc.close();
        String[] in2array = in2.split(" ");

        int[] numbersCount = new int[10];
        int sum = 0;

        for (int i = 0; i < in2array.length; i++) {
            int number = Integer.parseInt(in2array[i]);
            numbersCount[number] += 1;
            sum += number;
        }

        if (numbersCount[0] > 0){
            if (sum % 3 == 0) {
                printNumber(numbersCount);
            } else {
                if (removeNumbers(numbersCount, sum)){
                    printNumber(numbersCount);
                } else {
                    System.out.println(-1);
                }
            }
        } else {
            System.out.println(-1);
        }

    }

    public static void printNumber(int[] arr){
        StringBuilder sb = new StringBuilder();

        for (int i = 9; i >= 0; --i) {
            for (int j = 0; j < arr[i]; j++) {
                sb.append(i);
            }
        }

        int strLen = sb.toString().length();

        while(sb.charAt(0) == '0' && strLen > 1)
        {
            sb.deleteCharAt(0);
            --strLen;
        }

        System.out.println(sb.toString());
    }

    public static boolean removeNumbers(int[] arr, int sum){
        int rem = sum % 3;

        for (int i = rem; i < 10; i += 3) {
            if (arr[i] > 0){
                --arr[i];
                return true;
            }
        }

        if (rem == 1){
            for (int i = 4; i < 10; i += 3) {
                if (arr[i-2] > 0 && arr[2] > 0){
                    --arr[i-2];
                    --arr[2];
                    return true;
                }
            }
            if (arr[5] > 1){
                arr[5] -= 2;
                return true;
            }
        } else {
            for (int i = 2; i < 10; i += 3) {
                if (arr[i-1] > 0 && arr[1] > 0){
                    --arr[i-1];
                    --arr[1];
                    return true;
                }
            }
            if (arr[4] > 1){
                arr[4] -= 2;
                return true;
            }
        }

        return false;
    }

}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        sc.nextLine();
        String in2 = sc.nextLine();
        sc.close();
        String[] in2array = in2.split(" ");

        int[] numbersCount = new int[10];
        int sum = 0;

        for (int i = 0; i < in2array.length; i++) {
            int number = Integer.parseInt(in2array[i]);
            numbersCount[number] += 1;
            sum += number;
        }

        if (numbersCount[0] > 0){
            if (sum % 3 == 0) {
                printNumber(numbersCount);
            } else {
                if (removeNumbers(numbersCount, sum)){
                    printNumber(numbersCount);
                } else {
                    System.out.println(-1);
                }
            }
        } else {
            System.out.println(-1);
        }

    }

    public static void printNumber(int[] arr){
        StringBuilder sb = new StringBuilder();

        for (int i = 9; i >= 0; --i) {
            for (int j = 0; j < arr[i]; j++) {
                sb.append(i);
            }
        }

        int strLen = sb.toString().length();

        while(sb.toString().startsWith("0") && strLen > 1)
        {
            sb.deleteCharAt(0);
            --strLen;
        }

        System.out.println(sb.toString());
    }

    public static boolean removeNumbers(int[] arr, int sum){
        int rem = sum % 3;

        for (int i = rem; i < 10; i += 3) {
            if (arr[i] > 0){
                --arr[i];
                return true;
            }
        }

        if (rem == 1){
            for (int i = 4; i < 10; i += 3) {
                if (arr[i-2] > 0 && arr[2] > 0){
                    --arr[i-2];
                    --arr[2];
                    return true;
                }
            }
            if (arr[5] > 1){
                arr[5] -= 2;
                return true;
            }
        } else {
            for (int i = 2; i < 10; i += 3) {
                if (arr[i-1] > 0 && arr[1] > 0){
                    --arr[i-1];
                    --arr[1];
                    return true;
                }
            }
            if (arr[4] > 1){
                arr[4] -= 2;
                return true;
            }
        }

        return false;
    }

}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        sc.nextLine();
        String in2 = sc.nextLine();
        sc.close();
        String[] in2array = in2.split(" ");

        int[] numbersCount = new int[10];
        int sum = 0;

        for (int i = 0; i < in2array.length; i++) {
            int number = Integer.parseInt(in2array[i]);
            numbersCount[number] += 1;
            sum += number;
        }

        if (numbersCount[0] > 0){
            if (sum % 3 == 0) {
                printNumber(numbersCount);
            } else {
                if (removeNumbers(numbersCount, sum)){
                    printNumber(numbersCount);
                } else {
                    System.out.println(-1);
                }
            }
        } else {
            System.out.println(-1);
        }

    }

    public static void printNumber(int[] arr){
        StringBuilder sb = new StringBuilder();

        for (int i = 9; i >= 0; --i) {
            for (int j = 0; j < arr[i]; j++) {
                sb.append(i);
            }
        }

        System.out.println(sb.toString());
    }

    public static boolean removeNumbers(int[] arr, int sum){
        int rem = sum % 3;

        for (int i = rem; i < 10; i += 3) {
            if (arr[i] > 0){
                --arr[i];
                return true;
            }
        }

        if (rem == 1){
            for (int i = 4; i < 10; i += 3) {
                if (arr[i-2] > 0 && arr[2] > 0){
                    --arr[i-2];
                    --arr[2];
                    return true;
                }
            }
            if (arr[5] > 1){
                arr[5] -= 2;
                return true;
            }
        } else {
            for (int i = 2; i < 10; i += 3) {
                if (arr[i-1] > 0 && arr[1] > 0){
                    --arr[i-1];
                    --arr[1];
                    return true;
                }
            }
            if (arr[4] > 1){
                arr[4] -= 2;
                return true;
            }
        }

        return false;
    }

}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        int[] numbers = new int[in1];
        sc.nextLine();
        String in2 = sc.nextLine();
        String[] in2array = in2.split(" ");
        boolean found = false;

        sc.close();

        for (int i = 0; i < in1; i++) {
            numbers[i] = Integer.parseInt(in2array[i]);
        }

        sortArr(numbers);

        if (sum(numbers) % 3 != 0) {
            int sum = sum(numbers);
            int number = sum % 3;

            do {
                if (numbersToDelete(numbers, number))
                {
                    found = true;
                    break;
                } else {
                    number += 3;
                }
            } while(number < sum);

        } else {found = true;}

        if (found && inArray(numbers, 0) >= 0) {

            String str = "";
            StringBuilder sb = new StringBuilder(str);

            for (int i = 0; i < numbers.length; i++) {
                if (numbers[i] >= 0)
                    sb.append(numbers[i]);
            }

            System.out.println(sb);

        } else {
            System.out.println(-1);
        }

    }

    public static int inArray(int[] arr, int number) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == number)
                return i;
        }

        return -1;
    }

    public static int sum(int[] arr) {
        int result = 0;

        for (int i = 0; i < arr.length; i++) {
            result += arr[i];
        }

        return result;
    }

    public static void sortArr(int[] arr) {
        int temp;

        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1; j++) {
                if (arr[j+1] >arr[j]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] =  temp;
                }
            }
        }
    }

    public static boolean numbersToDelete(int[] arr, int num) {
        int index = inArray(arr, num);

        if (index >= 0)
        {
            arr[index] = -1;
            return true;
        } else {
            int firstNull = inArray(arr, 0);

            if (firstNull >= 0) {
                for (int i = firstNull-1; i >= 0; i--) {
                    if (arr[i] < num)
                    {
                        index = inArray(arr, num-arr[i]);
                        if (index >= 0 && index != i)
                        {
                            arr[index] = -1;
                            arr[i] = -1;
                            return true;
                        }
                    } else {
                        return false;
                    }
                }
            }
        }

        return false;
    }
}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        int[] numbers = new int[in1];
        sc.nextLine();
        String in2 = sc.nextLine();
        String[] in2array = in2.split(" ");
        boolean found = false;

        sc.close();

        for (int i = 0; i < in1; ++i) {
            numbers[i] = Integer.parseInt(in2array[i]);
        }

        sortArr(numbers);

        if (sum(numbers) % 3 != 0) {
            int sum = sum(numbers);
            int number = sum % 3;

            do {
                if (numbersToDelete(numbers, number))
                {
                    found = true;
                    break;
                } else {
                    number += 3;
                }
            } while(number < sum);

        } else {found = true;}

        if (found && inArray(numbers, 0) >= 0) {

            String str = "";
            StringBuilder sb = new StringBuilder(str);

            for (int i = 0; i < numbers.length; i++) {
                if (numbers[i] >= 0)
                    sb.append(numbers[i]);
            }

            System.out.println(sb);

        } else {
            System.out.println(-1);
        }

    }

    public static int inArray(int[] arr, int number) {
        int arrLen = arr.length;
        for (int i = 0; i < arrLen; ++i) {
            if (arr[i] == number)
                return i;
        }

        return -1;
    }

    public static int sum(int[] arr) {
        int result = 0;
        int arrLen = arr.length;
        for (int i = 0; i < arrLen; ++i) {
            result += arr[i];
        }

        return result;
    }

    public static void sortArr(int[] arr) {
        int temp;
        int arrLen = arr.length-1;
        for (int i = 0; i < arrLen; ++i) {
            for (int j = 0; j < arrLen; ++j) {
                if (arr[j+1] >arr[j]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] =  temp;
                }
            }
        }
    }

    public static boolean numbersToDelete(int[] arr, int num) {
        int index = inArray(arr, num);

        if (index >= 0)
        {
            arr[index] = -1;
            return true;
        } else {
            int firstNull = inArray(arr, 0);

            if (firstNull >= 0) {
                for (int i = firstNull-1; i >= 0; --i) {
                    if (arr[i] < num)
                    {
                        index = inArray(arr, num-arr[i]);
                        if (index >= 0 && index != i)
                        {
                            arr[index] = -1;
                            arr[i] = -1;
                            return true;
                        }
                    } else {
                        return false;
                    }
                }
            }
        }

        return false;
    }
}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        sc.nextLine();
        String in2 = sc.nextLine();
        sc.close();
        String[] in2array = in2.split(" ");

        int[] numbersCount = new int[10];
        int sum = 0;

        for (int i = 0; i < in2array.length; i++) {
            int number = Integer.parseInt(in2array[i]);
            numbersCount[number] += 1;
            sum += number;
        }

        if (numbersCount[0] > 0){
            if (sum % 3 == 0) {
                printNumber(numbersCount);
            } else {
                if (removeNumbers(numbersCount, sum)){
                    printNumber(numbersCount);
                } else {
                    System.out.println(-1);
                }
            }
        } else {
            System.out.println(-1);
        }

    }

    public static void printNumber(int[] arr){
        StringBuilder sb = new StringBuilder();
        boolean bool = false;

        for (int i = 9; i >= 0; --i) {
            for (int j = 0; j < arr[i]; j++) {
                if (i > 0)  bool = true;

                if (bool)
                    sb.append(i);
            }
        }

        if (sb.toString().isEmpty()) sb.append(0);

        System.out.println(sb.toString());
    }

    public static boolean removeNumbers(int[] arr, int sum){
        int rem = sum % 3;

        for (int i = rem; i < 10; i += 3) {
            if (arr[i] > 0){
                --arr[i];
                return true;
            }
        }

        for (int i = 3 - rem; i <= 9 - rem; i+=3) {
            for (int j = 3 - rem; j <= i; j+=3) {
                if (i == j && arr[i] > 1) {
                    arr[i] -= 2;
                    return true;
                } else if(i != j && arr[i] > 0 && arr[j] > 0) {
                    --arr[i];
                    --arr[j];
                    return true;
                }
            }
        }

        return false;
    }

}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        int[] numbers = new int[in1];
        sc.nextLine();
        String in2 = sc.nextLine();
        String[] in2array = in2.split(" ");
        boolean found = false;

        sc.close();

        for (int i = 0; i < in1; i++) {
            numbers[i] = Integer.parseInt(in2array[i]);
        }

        if (sum(numbers) % 3 != 0) {
            int number = sum(numbers) % 3;

            for (int i = 0; i < 3; i++) {

                int index = inArray(numbers, number);

                if (index >= 0)
                {
                    numbers[index] = -1;
                    found = true;
                    break;
                } else {
                    number += 3;
                }
            }

        }

        if (found && inArray(numbers, 0) >= 0) {

            sortArr(numbers);
            String str = "";
            StringBuilder sb = new StringBuilder(str);

            for (int i = 0; i < numbers.length; i++) {
                if (numbers[i] >= 0)
                    sb.append(numbers[i]);
            }

            System.out.println(sb);

        } else {
            System.out.println(-1);
        }

    }

    public static int inArray(int[] arr, int number) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == number)
                return i;
        }

        return -1;
    }

    public static int sum(int[] arr) {
        int result = 0;

        for (int i = 0; i < arr.length; i++) {
            result += arr[i];
        }

        return result;
    }

    public static void sortArr(int[] arr) {
        int temp;

        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1; j++) {
                if (arr[j+1] >arr[j]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] =  temp;
                }
            }
        }
    }

}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        int[] numbers = new int[in1];
        sc.nextLine();
        String in2 = sc.nextLine();
        String[] in2array = in2.split(" ");
        boolean found = false;

        sc.close();

        for (int i = 0; i < in1; i++) {
            numbers[i] = Integer.parseInt(in2array[i]);
        }

        if (sum(numbers) % 3 != 0) {
            int number = sum(numbers) % 3;

            for (int i = 0; i < 3; i++) {

                int index = inArray(numbers, number);

                if (index >= 0)
                {
                    numbers[index] = -1;
                    found = true;
                    break;
                } else {
                    number += 3;
                }
            }

        } else {found = true;}

        if (found && inArray(numbers, 0) >= 0) {

            sortArr(numbers);
            String str = "";
            StringBuilder sb = new StringBuilder(str);

            for (int i = 0; i < numbers.length; i++) {
                if (numbers[i] >= 0)
                    sb.append(numbers[i]);
            }

            System.out.println(sb);

        } else {
            System.out.println(-1);
        }

    }

    public static int inArray(int[] arr, int number) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == number)
                return i;
        }

        return -1;
    }

    public static int sum(int[] arr) {
        int result = 0;

        for (int i = 0; i < arr.length; i++) {
            result += arr[i];
        }

        return result;
    }

    public static void sortArr(int[] arr) {
        int temp;

        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1; j++) {
                if (arr[j+1] >arr[j]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] =  temp;
                }
            }
        }
    }

}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        sc.nextLine();
        String in2 = sc.nextLine();
        sc.close();
        String[] in2array = in2.split(" ");

        int[] numbersCount = new int[10];
        int sum = 0;

        for (int i = 0; i < in2array.length; i++) {
            int number = Integer.parseInt(in2array[i]);
            numbersCount[number] += 1;
            sum += number;
        }

        if (numbersCount[0] > 0){
            if (sum % 3 == 0) {
                printNumber(numbersCount);
            } else {
                if (removeNumbers(numbersCount, sum)){
                    printNumber(numbersCount);
                } else {
                    System.out.println(-1);
                }
            }
        } else {
            System.out.println(-1);
        }

    }

    public static void printNumber(int[] arr){
        StringBuilder sb = new StringBuilder();
        boolean bool = false;

        for (int i = 9; i >= 0; --i) {
            for (int j = 0; j < arr[i]; j++) {
                if (i > 0)  bool = true;

                if (bool)
                    sb.append(i);
            }
        }

        if (sb.toString().isEmpty()) sb.append(0);

        System.out.println(sb.toString());
    }

    public static boolean removeNumbers(int[] arr, int sum){
        int rem = sum % 3;

        for (int i = rem; i < 10; i += 3) {
            if (arr[i] > 0){
                --arr[i];
                return true;
            }
        }

        if (rem == 1){
            for (int i = 4; i < 10; i += 3) {
                if (arr[i-2] > 0 && arr[2] > 0){
                    --arr[i-2];
                    --arr[2];
                    return true;
                }
            }
            if (arr[5] > 1){
                arr[5] -= 2;
                return true;
            }
            if (arr[8] > 1){
                arr[8] -= 2;
                return true;
            }
        } else {
            if (arr[1] > 1){
                arr[1] -= 2;
                return true;
            }
            for (int i = 5; i < 10; i += 3) {
                if (arr[i-1] > 0 && arr[1] > 0){
                    --arr[i-1];
                    --arr[1];
                    return true;
                }
            }
            if (arr[4] > 1){
                arr[4] -= 2;
                return true;
            }
            if (arr[7] > 1){
                arr[7] -= 2;
                return true;
            }
        }

        return false;
    }

}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        int[] numbers = new int[in1];
        sc.nextLine();
        String in2 = sc.nextLine();
        String[] in2array = in2.split(" ");
        boolean found = false;

        sc.close();

        for (int i = 0; i < in1; ++i) {
            numbers[i] = Integer.parseInt(in2array[i]);
        }

        sortArr(numbers);

        int sum = sum(numbers);

        if (sum % 3 != 0) {
            int number = sum % 3;

            do {
                if (numbersToDelete(numbers, number))
                {
                    found = true;
                    break;
                } else {
                    number += 3;
                }
            } while(number < sum);

        } else {found = true;}

        if (found && inArray(numbers, 0) >= 0) {

            String str = "";
            StringBuilder sb = new StringBuilder(str);

            for (int i = 0; i < numbers.length; i++) {
                if (numbers[i] >= 0)
                    sb.append(numbers[i]);
            }

            System.out.println(sb);

        } else {
            System.out.println(-1);
        }

    }

    public static int inArray(int[] arr, int number) {
        int arrLen = arr.length;
        for (int i = 0; i < arrLen; ++i) {
            if (arr[i] == number)
                return i;
        }

        return -1;
    }

    public static int sum(int[] arr) {
        int result = 0;
        int arrLen = arr.length;
        for (int i = 0; i < arrLen; ++i) {
            result += arr[i];
        }

        return result;
    }

    public static void sortArr(int[] arr) {
        int temp;
        int arrLen = arr.length-1;
        for (int i = 0; i < arrLen; ++i) {
            for (int j = 0; j < arrLen; ++j) {
                if (arr[j+1] >arr[j]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] =  temp;
                }
            }
        }
    }

    public static boolean numbersToDelete(int[] arr, int num) {
        int index = inArray(arr, num);

        if (index >= 0)
        {
            arr[index] = -1;
            return true;
        } else {
            int firstNull = inArray(arr, 0);

            if (firstNull >= 0) {
                for (int i = firstNull-1; i >= 0; --i) {
                    if (arr[i] < num)
                    {
                        index = inArray(arr, num-arr[i]);
                        if (index >= 0 && index != i)
                        {
                            arr[index] = -1;
                            arr[i] = -1;
                            return true;
                        }
                    } else {
                        return false;
                    }
                }
            }
        }

        return false;
    }
}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {
        Scanner sc1 = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
        int in1 = sc1.nextInt();
        int[] numbers = new int[in1];
        String in2 = sc2.nextLine();
        String[] in2array = in2.split(" ");

        sc1.close();
        sc2.close();

        for (int i = 0; i < in1; i++) {
            numbers[i] = Integer.parseInt(in2array[i]);
        }

        if (sum(numbers) % 3 == 0 || inArray(numbers, 0)) {

            sortArr(numbers);
            String str = "";
            StringBuilder sb = new StringBuilder(str);

            for (int i = 0; i < numbers.length; i++) {
                sb.append(numbers[i]);
            }

            System.out.println(sb);

        } else {
            System.out.println(-1);
        }

    }

    public static boolean inArray(int[] arr, int number) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == number)
                return true;
        }

        return false;
    }

    public static int sum(int[] arr) {
        int result = 0;

        for (int i = 0; i < arr.length; i++) {
            result += arr[i];
        }

        return result;
    }

    public static void sortArr(int[] arr) {
        int temp;

        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1; j++) {
                if (arr[j+1] >arr[j]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] =  temp;
                }
            }
        }
    }

}


import java.util.Scanner;

public class Numbers {

    private static int firstNull;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        int[] numbers = new int[in1];
        sc.nextLine();
        String in2 = sc.nextLine();
        String[] in2array = in2.split(" ");
        boolean found = false;

        sc.close();

        for (int i = 0; i < in1; ++i) {
            numbers[i] = Integer.parseInt(in2array[i]);
        }

        sortArr(numbers);

        int sum = sum(numbers);
        firstNull = inArray(numbers, 0);

        if (sum % 3 != 0) {
            int number = sum % 3;

            do {
                if (numbersToDelete(numbers, number))
                {
                    found = true;
                    break;
                } else {
                    number += 3;
                }
            } while(number < sum);

        } else {found = true;}

        if (found && firstNull >= 0) {

            String str = "";
            StringBuilder sb = new StringBuilder(str);

            for (int i = 0; i < numbers.length; i++) {
                if (numbers[i] >= 0)
                    sb.append(numbers[i]);
            }

            System.out.println(sb);

        } else {
            System.out.println(-1);
        }

    }

    public static int inArray(int[] arr, int number) {
        int arrLen = arr.length;
        for (int i = 0; i < arrLen; ++i) {
            if (arr[i] == number)
                return i;
        }

        return -1;
    }

    public static int sum(int[] arr) {
        int result = 0;
        int arrLen = arr.length;
        for (int i = 0; i < arrLen; ++i) {
            result += arr[i];
        }

        return result;
    }

    public static void sortArr(int[] arr) {
        int temp;
        int arrLen = arr.length-1;
        for (int i = 0; i < arrLen; ++i) {
            for (int j = 0; j < arrLen; ++j) {
                if (arr[j+1] >arr[j]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] =  temp;
                }
            }
        }
    }

    public static boolean numbersToDelete(int[] arr, int num) {
        int index = inArray(arr, num);

        if (index >= 0)
        {
            arr[index] = -1;
            return true;
        } else {

            if (firstNull >= 0) {
                for (int i = firstNull-1; i >= 0; --i) {
                    if (arr[i] < num)
                    {
                        index = inArray(arr, num-arr[i]);
                        if (index >= 0 && index != i)
                        {
                            arr[index] = -1;
                            arr[i] = -1;
                            return true;
                        }
                    } else {
                        return false;
                    }
                }
            }
        }

        return false;
    }
}


import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int in1 = sc.nextInt();
        sc.nextLine();
        String in2 = sc.nextLine();
        sc.close();
        String[] in2array = in2.split(" ");

        int[] numbersCount = new int[10];
        int sum = 0;

        for (int i = 0; i < in2array.length; i++) {
            int number = Integer.parseInt(in2array[i]);
            numbersCount[number] += 1;
            sum += number;
        }

        if (numbersCount[0] > 0){
            if (sum % 3 == 0) {
                printNumber(numbersCount);
            } else {
                if (removeNumbers(numbersCount, sum)){
                    printNumber(numbersCount);
                } else {
                    System.out.println(-1);
                }
            }
        } else {
            System.out.println(-1);
        }

    }

    public static void printNumber(int[] arr){
        StringBuilder sb = new StringBuilder();
        boolean bool = false;

        for (int i = 9; i >= 0; --i) {
            for (int j = 0; j < arr[i]; j++) {
                if (i > 0)  bool = true;

                if (bool)
                    sb.append(i);
            }
        }

        if (sb.toString().isEmpty()) sb.append(0);

        System.out.println(sb.toString());
    }

    public static boolean removeNumbers(int[] arr, int sum){
        int rem = sum % 3;

        for (int i = rem; i < 10; i += 3) {
            if (arr[i] > 0){
                --arr[i];
                return true;
            }
        }

        if (rem == 1){
            for (int i = 4; i < 10; i += 3) {
                if (arr[i-2] > 0 && arr[2] > 0){
                    --arr[i-2];
                    --arr[2];
                    return true;
                }
            }
            if (arr[5] > 1){
                arr[5] -= 2;
                return true;
            }
        } else {
            for (int i = 2; i < 10; i += 3) {
                if (arr[i-1] > 0 && arr[1] > 0){
                    --arr[i-1];
                    --arr[1];
                    return true;
                }
            }
            if (arr[4] > 1){
                arr[4] -= 2;
                return true;
            }
        }

        return false;
    }

}


package R131;

import java.util.*;

public class Second {
  public static void main(String[] args) {
    Scanner sc =new Scanner("12\n5 3 3 3 2 5 5 1 2 1 4 1");
    sc.nextLine();
    String[] input = sc.nextLine().split(" ");
    int[] p = new int[input.length];
    for (int i=0;i<p.length;i++){
      p[i]=Integer.parseInt(input[i]);
    }
    Arrays.sort(p);
    if (Arrays.binarySearch(p,0)<0&&Arrays.binarySearch(p,5)<0){
      System.out.println(-1);
      return;
    }
    boolean e=false;
    for (int i=0;i<p.length;i++){
      if (p[i]%2==0) {
        e=true;
        break;
      }
    }
    if (!e){
      System.out.println(-1);
      return;
    }

    if (d3(p)){
      output(p);
      return;
    }
    if (r(p)){
      return;
    } else {
      narr=new ArrayList<int[]>(arr);
      while (narr.size()>0){
        for (int[] ar:narr) {
          if (r(ar)){
            return;
          }
        }
        narr=new ArrayList<int[]>(arr);
      }
      System.out.println(-1);
    }
  }
  static List<int[]> arr = new ArrayList<int[]>();
  static List<int[]> narr = new ArrayList<int[]>();
  static boolean r(int[] p){

    for(int i=0;i<p.length;i++){
      int[] pn = new int[p.length-1];
      int c=0;
      for(int j=0;j<p.length;j++){
        if(j!=i){
          pn[c]=p[j];
          c++;
        }
      }
      if (pn.length>0&&d3(pn)){
        for (int k=pn.length-1;k>-1;k--){
          System.out.print(pn[k]);
        }
        return true;
      }
      if (arr.size()>0&&pn.length<arr.get(0).length)
        arr.clear();
      if (pn.length>0&&(arr.size()==0||arr.get(0).length==pn.length)&&!isInList(arr,pn))
        arr.add(pn);
    }
    return false;
  }
  static boolean isInList(
            final List<int[]> list, final int[] candidate){

            for(final int[] item : list){
                if(Arrays.equals(item, candidate)){
                    return true;
                }
            }
            return false;
        }
  static void output(int[] p){
    for (int i=p.length-1;i>-1;i--){
        System.out.print(p[i]);
    }
  }
  static boolean d3(int[] p){
    long s = getNumber(p);
    return (s%3==0&&s%2==0&&s%5==0)?true:false;
  }
  static long getNumber(int[] p){
   StringBuilder n = new StringBuilder("");
   for (int i=p.length-1;i>-1;i--)
     n.append(p[i]);
   return Long.parseLong(n.toString());
  }
}


import java.util.*;

public class Second {
  public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    sc.nextLine();
    String[] input = sc.nextLine().split(" ");
    int[] p = new int[input.length];
    for (int i=0;i<p.length;i++){
      p[i]=Integer.parseInt(input[i]);
    }
    Arrays.sort(p);
    if (Arrays.binarySearch(p,0)<0){
      System.out.println(-1);
      return;
    }
    List<Integer> l = new ArrayList<Integer>();
    if (d3(p)==0){
      output(p,l);
      //return;
    } else if (d3(p)==1){
      int index = findRem1(p,1);
      if (index!=-1){
        for (int i=p.length-1;i>-1;i--){
          if (i!=index)
           l.add(p[i]);
        }
        //return;
      } else {
        int[] r = findRem2(p,2);
        for (int i=p.length-1;i>-1;i--){
          if (Arrays.binarySearch(r,i)<0)
            l.add(p[i]);
        }
        //return;
      }
    } else {
      int index = findRem1(p,2);
      if (index!=-1){
        for (int i=p.length-1;i>-1;i--){
          if (i!=index)
            l.add(p[i]);
        }
        //return;
      } else {
        int[] r = findRem2(p,1);
        for (int i=p.length-1;i>-1;i--){
          if (Arrays.binarySearch(r,i)<0)
            l.add(p[i]);
        }
        //return;
      }
    }
    if (nz(l)){
      for (Integer i : l){
        System.out.print(i);
      }
    } else {
      System.out.print(0);
    }
  }

  static boolean nz(List<Integer> l){
    for (Integer i : l){
      if (i!=0) return true;
    }
    return false;
  }

  static int findRem1(int[] p, int d){
   for(int i=0;i<p.length;i++){
     if (p[i]%3==d)
       return i;
   }
   return -1;
  }
  static int[] findRem2(int[] p, int d){
   int[] r = new int[2];
   int c=0;
   for(int i=0;i<p.length;i++){
     if (p[i]%3==d&&c<2)
       r[c]=p[i];
   }
   return r;
  }
  static void output(int[] p,List<Integer> l){
    for (int i=p.length-1;i>-1;i--){
        l.add(p[i]);
    }
  }
  static long d3(int[] p){
    long s = sum(p);
    return s%3;
  }
  static long sum(int[] p){
   long n = 0;
   for (int i=p.length-1;i>-1;i--)
     n+=p[i];
   return n;
  }
}


import java.util.*;

public class A {
  public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    sc.nextLine();
    String[] input = sc.nextLine().split(" ");
    int[] p = new int[input.length];
    for (int i=0;i<p.length;i++){
      p[i]=Integer.parseInt(input[i]);
    }
    Arrays.sort(p);
    if (Arrays.binarySearch(p,0)<0&&Arrays.binarySearch(p,5)<0){
      System.out.println(-1);
      return;
    }
    boolean e=false;
    for (int i=0;i<p.length;i++){
      if (p[i]%2==0) {
        e=true;
        break;
      }
    }
    if (!e){
      System.out.println(-1);
      return;
    }

    if (d3(p)){
      output(p);
      return;
    }
    if (r(p)){
      return;
    } else {
      narr=new ArrayList<int[]>(arr);
      while (narr.size()>0){
        for (int[] ar:narr) {
          if (r(ar)){
            return;
          }
        }
        narr=new ArrayList<int[]>(arr);
      }

      System.out.println(-1);
    }
  }
  static List<int[]> arr = new ArrayList<int[]>();
  static List<int[]> narr = new ArrayList<int[]>();
  static boolean r(int[] p){
    int[] pn = new int[p.length-1];
    for(int i=0;i<p.length;i++){
      int c=0;
      for(int j=0;j<p.length;j++){
        if(j!=i){
          pn[c]=p[j];
          c++;
        }
      }
      if (pn.length>0&&d3(pn)){
        for (int k=pn.length-1;k>-1;k--){
          System.out.print(pn[k]);
        }
        return true;
      }
      if (arr.size()>0&&pn.length<arr.get(0).length)
        arr.clear();
      if (pn.length>0&&(arr.size()==0||arr.get(0).length==pn.length))
        arr.add(pn);
    }
    return false;
  }
  static void output(int[] p){
    for (int i=p.length-1;i>-1;i--){
        System.out.print(p[i]);
    }
  }
  static boolean d3(int[] p){
    long s = getNumber(p);
    return (s%3==0&&s%2==0&&s%5==0)?true:false;
  }
  static long getNumber(int[] p){
   int s=0;
   StringBuilder n = new StringBuilder("");
   for (int i=p.length-1;i>-1;i--)
     n.append(p[i]);
   return Long.parseLong(n.toString());
  }
}


import java.util.*;

public class Second {
  public static void main(String[] args) {
    Scanner sc =new Scanner("1\n0");
    sc.nextLine();
    String[] input = sc.nextLine().split(" ");
    int[] p = new int[input.length];
    for (int i=0;i<p.length;i++){
      p[i]=Integer.parseInt(input[i]);
    }
    Arrays.sort(p);
    if (Arrays.binarySearch(p,0)<0&&Arrays.binarySearch(p,5)<0){
      System.out.println(-1);
      return;
    }
    boolean e=false;
    for (int i=0;i<p.length;i++){
      if (p[i]%2==0) {
        e=true;
        break;
      }
    }
    if (!e){
      System.out.println(-1);
      return;
    }

    if (d3(p)){
      output(p);
      return;
    }
    if (r(p)){
      return;
    } else {
      narr=new ArrayList<int[]>(arr);
      while (narr.size()>0){
        for (int[] ar:narr) {
          if (r(ar)){
            return;
          }
        }
        narr=new ArrayList<int[]>(arr);
      }
      System.out.println(-1);
    }
  }
  static List<int[]> arr = new ArrayList<int[]>();
  static List<int[]> narr = new ArrayList<int[]>();
  static boolean r(int[] p){

    for(int i=0;i<p.length;i++){
      int[] pn = new int[p.length-1];
      int c=0;
      for(int j=0;j<p.length;j++){
        if(j!=i){
          pn[c]=p[j];
          c++;
        }
      }
      if (pn.length>0&&d3(pn)){
        for (int k=pn.length-1;k>-1;k--){
          System.out.print(pn[k]);
        }
        return true;
      }
      if (arr.size()>0&&pn.length<arr.get(0).length)
        arr.clear();
      if (pn.length>0&&(arr.size()==0||arr.get(0).length==pn.length)&&!isInList(arr,pn))
        arr.add(pn);
    }
    return false;
  }
  static boolean isInList(
            final List<int[]> list, final int[] candidate){

            for(final int[] item : list){
                if(Arrays.equals(item, candidate)){
                    return true;
                }
            }
            return false;
        }
  static void output(int[] p){
    for (int i=p.length-1;i>-1;i--){
        System.out.print(p[i]);
    }
  }
  static boolean d3(int[] p){
    long s = getNumber(p);
    return (s%3==0&&s%2==0&&s%5==0)?true:false;
  }
  static long getNumber(int[] p){
   StringBuilder n = new StringBuilder("");
   for (int i=p.length-1;i>-1;i--)
     n.append(p[i]);
   return Long.parseLong(n.toString());
  }
}


import java.util.*;

public class Second {
  public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    sc.nextLine();
    String[] input = sc.nextLine().split(" ");
    int[] p = new int[input.length];
    for (int i=0;i<p.length;i++){
      p[i]=Integer.parseInt(input[i]);
    }
    Arrays.sort(p);
    if (Arrays.binarySearch(p,0)<0&&Arrays.binarySearch(p,5)<0){
      System.out.println(-1);
      return;
    }
    boolean e=false;
    for (int i=0;i<p.length;i++){
      if (p[i]%2==0) {
        e=true;
        break;
      }
    }
    if (!e){
      System.out.println(-1);
      return;
    }

    if (d3(p)){
      output(p);
      return;
    }
    if (r(p)){
      return;
    } else {
      narr=new ArrayList<int[]>(arr);
      while (narr.size()>0){
        for (int[] ar:narr) {
          if (r(ar)){
            return;
          }
        }
        narr=new ArrayList<int[]>(arr);
      }
      System.out.println(-1);
    }
  }
  static List<int[]> arr = new ArrayList<int[]>();
  static List<int[]> narr = new ArrayList<int[]>();
  static boolean r(int[] p){

    for(int i=0;i<p.length;i++){
      int[] pn = new int[p.length-1];
      int c=0;
      for(int j=0;j<p.length;j++){
        if(j!=i){
          pn[c]=p[j];
          c++;
        }
      }
      if (pn.length>0&&d3(pn)){
        for (int k=pn.length-1;k>-1;k--){
          System.out.print(pn[k]);
        }
        return true;
      }
      if (arr.size()>0&&pn.length<arr.get(0).length)
        arr.clear();
      if (pn.length>0&&(arr.size()==0||arr.get(0).length==pn.length)&&!isInList(arr,pn))
        arr.add(pn);
    }
    return false;
  }
  static boolean isInList(
            final List<int[]> list, final int[] candidate){

            for(final int[] item : list){
                if(Arrays.equals(item, candidate)){
                    return true;
                }
            }
            return false;
        }
  static void output(int[] p){
    for (int i=p.length-1;i>-1;i--){
        System.out.print(p[i]);
    }
  }
  static boolean d3(int[] p){
    long s = getNumber(p);
    return (s%3==0&&s%2==0&&s%5==0)?true:false;
  }
  static long getNumber(int[] p){
   StringBuilder n = new StringBuilder("");
   for (int i=p.length-1;i>-1;i--)
     n.append(p[i]);
   return Long.parseLong(n.toString());
  }
}


import java.util.*;

public class Second {
  public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    sc.nextLine();
    String[] input = sc.nextLine().split(" ");
    int[] p = new int[input.length];
    for (int i=0;i<p.length;i++){
      p[i]=Integer.parseInt(input[i]);
    }
    Arrays.sort(p);
    if (Arrays.binarySearch(p,0)<0&&Arrays.binarySearch(p,5)<0){
      System.out.println(-1);
      return;
    }
    boolean e=false;
    for (int i=0;i<p.length;i++){
      if (p[i]%2==0) {
        e=true;
        break;
      }
    }
    if (!e){
      System.out.println(-1);
      return;
    }

    if (d3(p)){
      output(p);
      return;
    }
    if (r(p)){
      return;
    } else {
      narr=new ArrayList<int[]>(arr);
      while (narr.size()>0){
        for (int[] ar:narr) {
          if (r(ar)){
            return;
          }
        }
        narr=new ArrayList<int[]>(arr);
      }
      System.out.println(-1);
    }
  }
  static List<int[]> arr = new ArrayList<int[]>();
  static List<int[]> narr = new ArrayList<int[]>();
  static boolean r(int[] p){

    for(int i=0;i<p.length;i++){
      int[] pn = new int[p.length-1];
      int c=0;
      for(int j=0;j<p.length;j++){
        if(j!=i){
          pn[c]=p[j];
          c++;
        }
      }
      if (pn.length>0&&d3(pn)){
        for (int k=pn.length-1;k>-1;k--){
          System.out.print(pn[k]);
        }
        return true;
      }
      if (arr.size()>0&&pn.length<arr.get(0).length)
        arr.clear();
      if (pn.length>0&&(arr.size()==0||arr.get(0).length==pn.length)&&!isInList(arr,pn))
        arr.add(pn);
    }
    return false;
  }
  static boolean isInList(
            final List<int[]> list, final int[] candidate){

            for(final int[] item : list){
                if(Arrays.equals(item, candidate)){
                    return true;
                }
            }
            return false;
        }
  static void output(int[] p){
    for (int i=p.length-1;i>-1;i--){
        System.out.print(p[i]);
    }
  }
  static boolean d3(int[] p){
    long s = sum(p);
    return (s%3==0&&p[0]%2==0&&p[0]%5==0)?true:false;
  }
  static long sum(int[] p){
   long n = 0;
   for (int i=p.length-1;i>-1;i--)
     n+=p[i];
   return n;
  }
}


import java.util.*;

public class Second {
  public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    sc.nextLine();
    String[] input = sc.nextLine().split(" ");
    int[] p = new int[input.length];
    for (int i=0;i<p.length;i++){
      p[i]=Integer.parseInt(input[i]);
    }
    Arrays.sort(p);
    if (Arrays.binarySearch(p,0)<0){
      System.out.println(-1);
      return;
    }
    List<Integer> l = new ArrayList<Integer>();
    if (d3(p)==0){
      output(p,l);
    } else if (d3(p)==1){
      int index = findRem1(p,1);
      if (index!=-1){
        for (int i=p.length-1;i>-1;i--){
          if (i!=index)
           l.add(p[i]);
        }
      } else {
        int[] r = findRem2(p,2);
        for (int i=p.length-1;i>-1;i--){
          if (Arrays.binarySearch(r,i)<0)
            l.add(p[i]);
        }
      }
    } else {
      int index = findRem1(p,2);
      if (index!=-1){
        for (int i=p.length-1;i>-1;i--){
          if (i!=index)
            l.add(p[i]);
        }
      } else {
        int[] r = findRem2(p,1);
        for (int i=p.length-1;i>-1;i--){
          if (Arrays.binarySearch(r,i)<0)
            l.add(p[i]);
        }
      }
    }
    if (nz(l)){
      for (Integer i : l){
        System.out.print(i);
      }
    } else {
      System.out.print(0);
    }
  }

  static boolean nz(List<Integer> l){
    for (Integer i : l){
      if (i!=0) return true;
    }
    return false;
  }

  static int findRem1(int[] p, int d){
   for(int i=0;i<p.length;i++){
     if (p[i]%3==d)
       return i;
   }
   return -1;
  }
  static int[] findRem2(int[] p, int d){
   int[] r = new int[2];
   int c=0;
   for(int i=0;i<p.length;i++){
     if (p[i]%3==d&&c<2){
       r[c]=i;
       c++;
       if (c==2)
         return r;
     }
   }
   return r;
  }
  static void output(int[] p,List<Integer> l){
    for (int i=p.length-1;i>-1;i--){
        l.add(p[i]);
    }
  }
  static long d3(int[] p){
    long s = sum(p);
    return s%3;
  }
  static long sum(int[] p){
   long n = 0;
   for (int i=p.length-1;i>-1;i--)
     n+=p[i];
   return n;
  }
}


import java.util.*;

public class Second {
  public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    sc.nextLine();
    String[] input = sc.nextLine().split(" ");
    int[] p = new int[input.length];
    for (int i=0;i<p.length;i++){
      p[i]=Integer.parseInt(input[i]);
    }
    Arrays.sort(p);
    if (Arrays.binarySearch(p,0)<0){
      System.out.println(-1);
      return;
    }
    List<Integer> l = new ArrayList<Integer>();
    if (d3(p)==0){
      output(p,l);
      //return;
    } else if (d3(p)==1){
      int index = findRem1(p,1);
      if (index!=-1){
        for (int i=p.length-1;i>-1;i--){
          if (i!=index)
           l.add(p[i]);
        }
        //return;
      } else {
        int[] r = findRem2(p,2);
        for (int i=p.length-1;i>-1;i--){
          if (Arrays.binarySearch(r,p[i])<0)
            l.add(p[i]);
        }
        //return;
      }
    } else {
      int index = findRem1(p,2);
      if (index!=-1){
        for (int i=p.length-1;i>-1;i--){
          if (i!=index)
            l.add(p[i]);
        }
        //return;
      } else {
        int[] r = findRem2(p,1);
        for (int i=p.length-1;i>-1;i--){
          if (Arrays.binarySearch(r,p[i])<0)
            l.add(p[i]);
        }
        //return;
      }
    }
    if (nz(l)){
      for (Integer i : l){
        System.out.print(i);
      }
    } else {
      System.out.print(0);
    }
  }

  static boolean nz(List<Integer> l){
    for (Integer i : l){
      if (i!=0) return true;
    }
    return false;
  }

  static int findRem1(int[] p, int d){
   for(int i=0;i<p.length;i++){
     if (p[i]%3==d)
       return i;
   }
   return -1;
  }
  static int[] findRem2(int[] p, int d){
   int[] r = new int[2];
   int c=0;
   for(int i=0;i<p.length;i++){
     if (p[i]%3==d&&c<2){
       r[c]=p[i];
       c++;
       if (c==2)
         return r;
     }
   }
   return r;
  }
  static void output(int[] p,List<Integer> l){
    for (int i=p.length-1;i>-1;i--){
        l.add(p[i]);
    }
  }
  static long d3(int[] p){
    long s = sum(p);
    return s%3;
  }
  static long sum(int[] p){
   long n = 0;
   for (int i=p.length-1;i>-1;i--)
     n+=p[i];
   return n;
  }
}


import java.util.*;

public class A {
  public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    sc.nextLine();
    String[] input = sc.nextLine().split(" ");
    int[] p = new int[input.length];
    for (int i=0;i<p.length;i++){
      p[i]=Integer.parseInt(input[i]);
    }
    Arrays.sort(p);
    if (Arrays.binarySearch(p,0)<0&&Arrays.binarySearch(p,5)<0){
      System.out.println(-1);
      return;
    }
    boolean e=false;
    for (int i=0;i<p.length;i++){
      if (p[i]%2==0) {
        e=true;
        break;
      }
    }
    if (!e){
      System.out.println(-1);
      return;
    }

    if (d3(p)){
      output(p);
      return;
    }
    if (r(p)){
      return;
    } else {
      narr=new ArrayList<int[]>(arr);
      while (narr.size()>0){
        for (int[] ar:narr) {
          if (r(ar)){
            return;
          }
        }
        narr=new ArrayList<int[]>(arr);
      }

      System.out.println("h");
    }
  }
  static List<int[]> arr = new ArrayList<int[]>();
  static List<int[]> narr = new ArrayList<int[]>();
  static boolean r(int[] p){
    int[] pn = new int[p.length-1];
    for(int i=0;i<p.length;i++){
      int c=0;
      for(int j=0;j<p.length;j++){
        if(j!=i){
          pn[c]=p[j];
          c++;
        }
      }
      if (pn.length>0&&d3(pn)){
        for (int k=pn.length-1;k>-1;k--){
          System.out.print(pn[k]);
        }
        return true;
      }
      if (arr.size()>0&&pn.length<arr.get(0).length)
        arr.clear();
      if (pn.length>0&&(arr.size()==0||arr.get(0).length==pn.length))
        arr.add(pn);
    }
    return false;
  }
  static void output(int[] p){
    for (int i=p.length-1;i>-1;i--){
        System.out.print(p[i]);
    }
  }
  static boolean d3(int[] p){
    long s = getNumber(p);
    return (s%3==0&&s%2==0&&s%5==0)?true:false;
  }
  static long getNumber(int[] p){
   int s=0;
   StringBuilder n = new StringBuilder("");
   for (int i=p.length-1;i>-1;i--)
     n.append(p[i]);
   return Long.parseLong(n.toString());
  }
}


import java.util.*;

public class Second {
  public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    sc.nextLine();
    String[] input = sc.nextLine().split(" ");
    int[] p = new int[input.length];
    for (int i=0;i<p.length;i++){
      p[i]=Integer.parseInt(input[i]);
    }
    Arrays.sort(p);
    if (Arrays.binarySearch(p,0)<0){
      System.out.println(-1);
      return;
    }
    if (d3(p)==0){
      output(p);
      return;
    } else if (d3(p)==1){
      int index = findRem1(p);
      if (index!=-1){
        for (int i=p.length-1;i>-1;i--){
          if (i!=index)
            System.out.print(p[i]);
        }
        return;
      } else {
        int[] r = findRem2(p);
        for (int i=p.length-1;i>-1;i--){
          if (Arrays.binarySearch(r,i)<0)
            System.out.print(p[i]);
        }
        return;
      }
    } else {

    }
    System.out.println(-1);
  }

  static int findRem1(int[] p){
   for(int i=0;i<p.length;i++){
     if (p[i]%3==1)
       return i;
   }
   return -1;
  }
  static int[] findRem2(int[] p){
   int[] r = new int[2];
   int c=0;
   for(int i=0;i<p.length;i++){
     if (p[i]%3==2&&c<2)
       r[c]=p[i];
   }
   return r;
  }
  static void output(int[] p){
    for (int i=p.length-1;i>-1;i--){
        System.out.print(p[i]);
    }
  }
  static long d3(int[] p){
    long s = sum(p);
    return s%3;
  }
  static long sum(int[] p){
   long n = 0;
   for (int i=p.length-1;i>-1;i--)
     n+=p[i];
   return n;
  }
}


import java.util.*;

public class dynasty {
    public static void main(String[] args) throws Exception{
        new dynasty().run(args);
    }

    private void run(String[] args) throws Exception{
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[] digit  = new int[n];
        int sum = 0;
        int res2count = 0;
        int res1count = 0;
        StringBuilder output = new StringBuilder();
        for(int i = 0; i < n; i ++){
            digit[i] = input.nextInt();
            sum += digit[i];
            if(digit[i]%3 == 2)
                res2count++;
            if(digit[i]%3 == 1)
                res1count++;
        }

        Arrays.sort(digit);

        if(digit[0] > 0){
            System.out.println(-1);
            return;
        }
        int res = sum%3;
        boolean remove = true;
        int res2 = 0;
        if(res == 0){
            for(int i = 0; i < n; i++){
                output.append(digit[i]);
            }
        }
        else if(res == 1){
            if(res1count > 0){
                for(int i = 0; i < n; i++){
                    if(digit[i]%3 == 1 && remove){
                        remove = false;
                        continue;
                    }
                    output.append(digit[i]);
                }
            }
            else{
                for(int i = 0; i < n; i++){
                    if(digit[i]%3 == 2 && res2count%3 != 0){
                        res2count--;
                        continue;
                    }
                    output.append(digit[i]);

                }
            }
        }
        else{
            if(res2count > 0){
                for(int i = 0; i < n; i++){
                    if(digit[i]%3 == 2 && remove){
                        remove = false;
                        continue;
                    }
                    output.append(digit[i]);
                }
            }
            else{
                for(int i = 0; i < n; i++){
                    if(digit[i]%3 == 1 && res2 < 2){
                        res2++;
                        continue;
                    }
                    output.append(digit[i]);
                }
            }
        }

        String s = output.reverse().toString();
        if(s.charAt(0) == '0')
            System.out.println(0);
        else
            System.out.println(s);
    }

}


import java.util.*;

public class dynasty {
    public static void main(String[] args) throws Exception{
        new dynasty().run(args);
    }

    private void run(String[] args) throws Exception{
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[] digit  = new int[n];
        int sum = 0;
        boolean res2count = false;
        boolean res1count = false;
        StringBuilder output = new StringBuilder();
        for(int i = 0; i < n; i ++){
            digit[i] = input.nextInt();
            sum += digit[i];
            if(digit[i]%3 == 2)
                res2count = true;
            if(digit[i]%3 == 1)
                res1count = true;
        }

        Arrays.sort(digit);

        if(digit[0] > 0){
            System.out.println(-1);
            return;
        }
        int res = sum%3;
        boolean remove = true;
        int res2 = 0;
        if(res == 0){
            for(int i = 0; i < n; i++){
                output.append(digit[i]);
            }
        }
        else if(res == 1){
            if(res1count){
                for(int i = 0; i < n; i++){
                    if(digit[i]%3 == 1 && remove){
                        remove = false;
                        continue;
                    }
                    output.append(digit[i]);
                }
            }
            else{
                for(int i = 0; i < n; i++){
                    if(digit[i]%3 == 0){
                        output.append(digit[i]);
                    }
                }
            }
        }
        else{
            if(res2count){
                for(int i = 0; i < n; i++){
                    if(digit[i]%3 == 2 && remove){
                        remove = false;
                        continue;
                    }
                    output.append(digit[i]);
                }
            }
            else{
                for(int i = 0; i < n; i++){
                    if(digit[i]%3 == 1 && res2 < 2){
                        res2++;
                        continue;
                    }
                    output.append(digit[i]);
                }
            }
        }

        String s = output.reverse().toString();
        if(s.charAt(0) == '0')
            System.out.println(0);
        else
            System.out.println(s);
    }

}