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;
        StringBuilder output = new StringBuilder();
        for(int i = 0; i < n; i ++){
            digit[i] = input.nextInt();
            sum += digit[i];
        }

        Arrays.sort(digit);

        if(digit[0] > 0){
            System.out.println(-1);
            return;
        }
        int res = sum%3;
        boolean remove = true;
        if(res == 0){
            for(int i = 0; i < n; i++){
                output.append(digit[i]);
            }
        }
        else{
            for(int i = 0; i < n; i++){
                if(digit[i]%3 == res && remove){
                    remove = false;
                    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;
        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;
        }

        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){
            for(int i = 0; i < n; i++){
                if(digit[i]%3 == 1 && remove){
                    remove = false;
                    continue;
                }
                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);
    }

}


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{
                System.out.println(0);
                return;
            }
        }
        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);
    }

}


import java.util.*;
public class Main {

    private void doit(){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int [] data = new int[n];
            int [] mode = new int[10];
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                data[i] = input;
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else{
                for(int i = 9; i >= 0; i--){
                    if(i % 3 == 0){
                        for(int times = 0; times < mode[i]; times++){
                            System.out.print(i);
                        }
                    }
                    else{
                        for(int times = 0; times < mode[i] / 3 * 3; times++){
                            System.out.print(i);
                        }
                    }

                }
                System.out.println();
            }

        }
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
import java.util.regex.Pattern;
public class Main {

    private void doit(){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int [] mode = new int[10];
            boolean isread0 = true;
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                if(input != 0){
                    isread0 = false;
                }
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else if(isread0 && mode[0] > 1){
                System.out.println(0);
            }
            else{

                int div1sum = mode[1] + mode[4] + mode[7];
                int div2sum = (mode[2] + mode[5] + mode[8]) * 2;
                int sum = div1sum + div2sum;
                StringBuilder sb = new StringBuilder();
                for(int i = 9; i >= 0; i--){
                    if(i % 3 == 0 || sum % 3 == 0){
                        for(int times = 0; times < mode[i]; times++){
                            sb.append(i);
                        }
                    }
                    else if(i % 3 == 1){
                        if(div1sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }
                    else{
                        if(div2sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }

                }
                if(Pattern.matches("0+", sb.toString())){
                    System.out.println(0);
                }
                else{
                    System.out.println(sb.toString());
                }
            }

        }
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
import java.util.regex.Pattern;
public class Main {

    private void doit(){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int [] mode = new int[10];
            boolean isread0 = true;
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                if(input != 0){
                    isread0 = false;
                }
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else if(isread0 && mode[0] > 1){
                System.out.println(0);
            }
            else{

                int div1sum = mode[1] + mode[4] + mode[7];
                int div2sum = mode[2] + mode[5] + mode[8];
                StringBuilder sb = new StringBuilder();
                for(int i = 9; i >= 0; i--){
                    if(i % 3 == 0){
                        for(int times = 0; times < mode[i]; times++){
                            sb.append(i);
                        }
                    }
                    else if(i % 3 == 1){
                        if(div1sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }
                    else{
                        if(div2sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }

                }
                if(Pattern.matches("0+", sb.toString())){
                    System.out.println(0);
                }
                else{
                    System.out.println(sb.toString());
                }
            }

        }
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
import java.util.regex.Pattern;
public class Main {

    private void doit(){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int [] mode = new int[10];
            boolean isread0 = true;
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                if(input != 0){
                    isread0 = false;
                }
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else if(isread0 && mode[0] > 1){
                System.out.println(0);
            }
            else{
                long div1sum = mode[1] + mode[4] + mode[7];
                long div2sum = (mode[2] + mode[5] + mode[8]) * 2;
                long sum = div1sum + div2sum;
                StringBuilder sb = new StringBuilder();
                for(int i = 9; i >= 0; i--){
                    if(i % 3 == 0 || sum % 3 == 0){
                        for(int times = 0; times < mode[i]; times++){
                            sb.append(i);
                        }
                    }
                    else if(i % 3 == 1){
                        if(div1sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }
                    else{
                        if(div2sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }

                }
                if(Pattern.matches("0+", sb.toString())){
                    System.out.println(0);
                }
                else{
                    System.out.println(sb.toString());
                }
            }

        }
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
import java.util.regex.Pattern;
public class Main {

    private void doit(){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int [] mode = new int[10];
            boolean isread0 = true;
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                if(input != 0){
                    isread0 = false;
                }
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else if(isread0 && mode[0] > 1){
                System.out.println(0);
            }
            else{
                int div1sum = mode[1] + mode[4] + mode[7];
                int div2sum = (mode[2] + mode[5] + mode[8]) * 2;
                int sum = div1sum + div2sum;
                StringBuilder sb = new StringBuilder();
                for(int i = 9; i >= 0; i--){
                    if(i % 3 == 0 || sum % 3 == 0){
                        for(int times = 0; times < mode[i]; times++){
                            sb.append(i);
                        }
                    }
                    else if(i % 3 == 1){
                        if(div1sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }
                    else{
                        if(div2sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }

                }
                if(Pattern.matches("0+", sb.toString())){
                    System.out.println(-1);
                }
                else{
                    System.out.println(sb.toString());
                }
            }

        }
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
import java.util.regex.Pattern;
public class Main {

    private void doit(){
        Scanner sc = new Scanner(System.in);
        //while(sc.hasNext()){
            int n = sc.nextInt();
            int [] mode = new int[10];
            boolean isread0 = true;
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                if(input != 0){
                    isread0 = false;
                }
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else if(isread0 && mode[0] > 1){
                System.out.println(0);
            }
            else{
                long div1sum = mode[1] + mode[4] + mode[7];
                long div2sum = (mode[2] + mode[5] + mode[8]) * 2;
                long sum = div1sum + div2sum;
                StringBuilder sb = new StringBuilder();
                for(int i = 9; i >= 0; i--){
                    if(i % 3 == 0 || sum % 3 == 0){
                        for(int times = 0; times < mode[i]; times++){
                            sb.append(i);
                        }
                    }
                    else if(i % 3 == 1){
                        if(div1sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }
                    else{
                        if(div2sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }

                }
                if(Pattern.matches("0+", sb.toString())){
                    System.out.println(0);
                }
                else{
                    System.out.println(sb.toString());
                }
            }

        //}
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
import java.util.regex.Pattern;
public class Main {
    boolean isexistans;

    private void doit(){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            isexistans = true;
            int [] mode = new int[10];
            boolean isread0 = true;
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                if(input != 0){
                    isread0 = false;
                }
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else if(isread0 && mode[0] > 1){
                System.out.println(0);
            }
            else{
                long div1sum = mode[1] + mode[4] + mode[7];
                long div2sum = (mode[2] + mode[5] + mode[8]) * 2;
                long sum = div1sum + div2sum;
                int [] fixmode = reduce(mode, sum);
                StringBuilder sb = new StringBuilder();
                for(int i = 9; i >= 0; i--){
                    for(int times = 0; times < fixmode[i]; times++){
                        sb.append(i);
                    }
                }
                if(Pattern.matches("0+", sb.toString())){
                    System.out.println(0);
                }
                else if(isexistans){
                    System.out.println(sb.toString());
                }
                else{
                    System.out.println(-1);
                }
            }
        }
    }

    private int [] reduce(int [] mode, long sum){
        int [] res = new int[mode.length];
        for(int i = 0; i < mode.length; i++){
            res[i] = mode[i];
        }
        if(sum % 3 == 0) return res;
        else if(sum % 3 == 1){
            if(res[1] > 0) res[1]--;
            else if(res[4] > 0) res[4]--;
            else if(res[7] > 0) res[7]--;
            else if(res[2] > 0){
                res[2]--;
                if(res[2] > 0) res[2]--;
                else if(res[5] > 0) res[5]--;
                else if(res[8] > 0) res[8]--;
                else isexistans = false;
            }
            else if(res[5] > 0){
                res[5]--;
                if(res[5] > 0) res[5]--;
                else if(res[8] > 0) res[8]--;
                else isexistans = false;
            }
            else if(res[8] > 1) res[8] -= 2;
            else isexistans = false;
        }
        else{
            if(res[2] > 0) res[2]--;
            else if(res[5] > 0) res[5]--;
            else if(res[8] > 0) res[8]--;
            else if(res[1] > 0){
                res[1]--;
                if(res[1] > 0) res[1]--;
                else if(res[4] > 0) res[4]--;
                else if(res[7] > 0) res[7]--;
                else isexistans = false;
            }
            else if(res[4] > 0){
                res[4]--;
                if(res[4] > 0) res[4]--;
                else if(res[7] > 0) res[7]--;
                else isexistans = false;
            }
            else if(res[8] > 1) res[8] -= 2;
            else isexistans = false;
        }
        return res;
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
import java.util.regex.Pattern;
public class Main {

    private void doit(){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int [] mode = new int[10];
            boolean isread0 = true;
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                if(input != 0){
                    isread0 = false;
                }
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else if(isread0 && mode[0] > 1){
                System.out.println(0);
            }
            else{
                int div1sum = mode[1] + mode[4] + mode[7];
                int div2sum = (mode[2] + mode[5] + mode[8]) * 2;
                long sum = div1sum + div2sum;
                StringBuilder sb = new StringBuilder();
                for(int i = 9; i >= 0; i--){
                    if(i % 3 == 0 || sum % 3 == 0){
                        for(int times = 0; times < mode[i]; times++){
                            sb.append(i);
                        }
                    }
                    else if(i % 3 == 1){
                        if(div1sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }
                    else{
                        if(div2sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }

                }
                if(Pattern.matches("0+", sb.toString())){
                    System.out.println(0);
                }
                else{
                    System.out.println(sb.toString());
                }
            }

        }
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
public class Main {

    private void doit(){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int [] mode = new int[10];
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else{

                int div1sum = mode[1] + mode[4] + mode[7];
                int div2sum = mode[2] + mode[5] + mode[8];
                if(div1sum % 3 == 0 || div2sum % 3 == 0){

                }

                for(int i = 9; i >= 0; i--){
                    if(i % 3 == 0){
                        for(int times = 0; times < mode[i]; times++){
                            System.out.print(i);
                        }
                    }
                    else if(i % 3 == 1){
                        if(div1sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                System.out.print(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                System.out.print(i);
                            }
                        }
                    }
                    else{
                        if(div2sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                System.out.print(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                System.out.print(i);
                            }
                        }
                    }

                }
                System.out.println();
            }

        }
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
public class Main {

    private void doit(){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int [] mode = new int[10];
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else{

                int div1sum = mode[1] + mode[4] + mode[7];
                int div2sum = mode[2] + mode[5] + mode[8];
                if(div1sum % 3 == 0 || div2sum % 3 == 0){

                }

                for(int i = 9; i >= 0; i--){
                    if(i % 3 == 0){
                        for(int times = 0; times < mode[i]; times++){
                            System.out.print(i);
                        }
                    }
                    else{
                        if(div1sum % 3 == 0 || div2sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                System.out.print(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                System.out.print(i);
                            }
                        }
                    }

                }
                System.out.println();
            }

        }
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
import java.util.regex.Pattern;
public class Main {
    boolean isexistans;

    private void doit(){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            isexistans = true;
            int [] mode = new int[10];
            boolean isread0 = true;
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                if(input != 0){
                    isread0 = false;
                }
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else if(isread0 && mode[0] > 1){
                System.out.println(0);
            }
            else{
                long div1sum = mode[1] + mode[4] + mode[7];
                long div2sum = (mode[2] + mode[5] + mode[8]) * 2;
                long sum = div1sum + div2sum;
                int [] fixmode = reduce(mode, sum);
                StringBuilder sb = new StringBuilder();
                for(int i = 9; i >= 0; i--){
                    for(int times = 0; times < fixmode[i]; times++){
                        sb.append(i);
                    }
                }
                if(Pattern.matches("0+", sb.toString())){
                    System.out.println(0);
                }
                else if(isexistans){
                    System.out.println(sb.toString());
                }
                else{
                    System.out.println(-1);
                }
            }
        }
    }

    private int [] reduce(int [] mode, long sum){
        int [] res = new int[mode.length];
        for(int i = 0; i < mode.length; i++){
            res[i] = mode[i];
        }
        if(sum % 3 == 0) return res;
        else if(sum % 3 == 1){
            if(res[1] > 0) res[1]--;
            else if(res[4] > 0) res[4]--;
            else if(res[7] > 0) res[7]--;
            else if(res[2] > 0){
                res[2]--;
                if(res[2] > 0) res[2]--;
                else if(res[5] > 0) res[5]--;
                else if(res[8] > 0) res[8]--;
                else isexistans = false;
            }
            else if(res[5] > 0){
                res[5]--;
                if(res[5] > 0) res[5]--;
                else if(res[8] > 0) res[8]--;
                else isexistans = false;
            }
            else if(res[8] > 1) res[8] -= 2;
            else isexistans = false;
        }
        else{
            if(res[2] > 0) res[2]--;
            else if(res[5] > 0) res[5]--;
            else if(res[8] > 0) res[8]--;
            else if(res[1] > 0){
                res[1]--;
                if(res[1] > 0) res[1]--;
                else if(res[4] > 0) res[4]--;
                else if(res[7] > 0) res[7]--;
                else isexistans = false;
            }
            else if(res[4] > 0){
                res[4]--;
                if(res[4] > 0) res[4]--;
                else if(res[7] > 0) res[7]--;
                else isexistans = false;
            }
            else if(res[7] > 1) res[7] -= 2;
            else isexistans = false;
        }
        return res;
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
import java.util.regex.Pattern;
public class Main {

    private void doit(){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int [] mode = new int[10];
            boolean isread0 = true;
            for(int i = 0; i < n; i++){
                int input = sc.nextInt();
                if(input != 0){
                    isread0 = false;
                }
                mode[input]++;
            }
            if(mode[0] == 0){
                System.out.println(-1);
            }
            else if(isread0 && mode[0] > 1){
                System.out.println(0);
            }
            else{
                int div1sum = mode[1] + mode[4] + mode[7];
                int div2sum = (mode[2] + mode[5] + mode[8]) * 2;
                int sum = div1sum + div2sum;
                StringBuilder sb = new StringBuilder();
                for(int i = 9; i >= 0; i--){
                    if(i % 3 == 0 || sum % 3 == 0){
                        if(i == 0 && sb.length() == 0){
                            sb.append(0);
                            break;
                        }
                        for(int times = 0; times < mode[i]; times++){
                            sb.append(i);
                        }
                    }
                    else if(i % 3 == 1){
                        if(div1sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }
                    else{
                        if(div2sum % 3 == 0){
                            for(int times = 0; times < mode[i]; times++){
                                sb.append(i);
                            }
                        }
                        else{
                            for(int times = 0; times < mode[i] / 3 * 3; times++){
                                sb.append(i);
                            }
                        }
                    }

                }
                if(Pattern.matches("0+", sb.toString())){
                    System.out.println(0);
                }
                else{
                    System.out.println(sb.toString());
                }
            }

        }
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.doit();
    }
}


import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class B {
	Scanner sc = new Scanner(System.in);
	void doIt()
	{
		String line = sc.nextLine();
		int n = new Integer(line);
		int sum = 0;
		int [] cnt = new int[10];
		line = sc.nextLine();
		for(int i = 0; i < n; i++) {
			int v = line.charAt(i * 2) - '0';
			sum += v;
			cnt[v]++;
		}
		//System.out.println(sum);
		if(cnt[0] == 0) {
			System.out.println("-1");
			return;
		}
		int rem = sum % 3;
		switch (rem) {
		case 0: break;
		case 1:
			if(cnt[1] > 0) {cnt[1]--;}
			else if(cnt[4] > 0) {cnt[4]--; }
			else if(cnt[7] > 0) {cnt[7]--; }
			else if(cnt[2] >= 2) {cnt[2]-=2;}
			else if(cnt[2] > 0 && cnt[5] > 0) {cnt[2]--; cnt[5]--;}
			else if(cnt[5] >= 2) {cnt[5]-=2; }
			else if(cnt[2] > 0 && cnt[8] > 0) {cnt[2]--; cnt[8]--; }
			else if(cnt[5] > 0 && cnt[8] > 0) {cnt[5]--; cnt[8]--; }
			else if(cnt[8] >= 2) {cnt[8]-=2; }
			else { System.out.println("-1"); return;}
			break;
		case 2:
			if(cnt[2] > 0) {cnt[2]--; }
			else if(cnt[5] > 0) {cnt[5]--; }
			else if(cnt[8] > 0) {cnt[8]--; }
			else if(cnt[1] >= 2) {cnt[1]-=2; }
			else if(cnt[1] > 0 && cnt[4] > 0) {cnt[1]--; cnt[4]--; }
			else if(cnt[4] >= 2) {cnt[4]-=2; }
			else if(cnt[1] > 0 && cnt[7] > 0) {cnt[1]--; cnt[7]--; }
			else if(cnt[4] > 0 && cnt[7] > 0) {cnt[4]--; cnt[7]--; }
			else if(cnt[7] >= 2) {cnt[7]-=2; }
			else { System.out.println("-1"); return;}
			break;
		}
		for(int i = 9; i >= 0; i--) {
			char c = (char)(i + '0');
			for(int j = 0; j < cnt[i]; j++) {
				System.out.print(c);
			}
		}
		System.out.println();
	}
	public static void main(String[] args) {
		new B().doIt();
	}
	void debug(Object...os) {
		System.err.println(deepToString(os));
	}
}


import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class B {
	Scanner sc = new Scanner(System.in);
	void doIt()
	{
		String ans = solve();
		System.out.println(ans);
	}
	String solve()
	{
		int n = sc.nextInt();
		int sum = 0;
		int [] cnt = new int[10];
		for(int i = 0; i < n; i++) {
			int v = sc.nextInt();
			sum += v;
			cnt[v]++;
		}
		//System.out.println(sum);
		if(cnt[0] == 0) return "-1";
		int rem = sum % 3;
		switch (rem) {
		case 0: break;
		case 1:
			if(cnt[1] > 0) {cnt[1]--; break; }
			if(cnt[4] > 0) {cnt[4]--; break; }
			if(cnt[7] > 0) {cnt[7]--; break; }
			if(cnt[2] >= 2) {cnt[2]-=2; break; }
			if(cnt[2] > 0 && cnt[5] > 0) {cnt[2]--; cnt[5]--; break; }
			if(cnt[5] >= 2) {cnt[5]-=2; break; }
			if(cnt[2] > 0 && cnt[8] > 0) {cnt[2]--; cnt[8]--; break; }
			if(cnt[5] > 0 && cnt[8] > 0) {cnt[5]--; cnt[8]--; break; }
			if(cnt[8] >= 2) {cnt[8]-=2; break; }
			return "-1";
		case 2:
			if(cnt[2] > 0) {cnt[2]--; break; }
			if(cnt[5] > 0) {cnt[5]--; break; }
			if(cnt[8] > 0) {cnt[8]--; break; }
			if(cnt[1] >= 2) {cnt[1]-=2; break; }
			if(cnt[1] > 0 && cnt[4] > 0) {cnt[1]--; cnt[4]--; break; }
			if(cnt[4] >= 2) {cnt[4]-=2; break; }
			if(cnt[1] > 0 && cnt[7] > 0) {cnt[1]--; cnt[4]--; break; }
			if(cnt[4] > 0 && cnt[7] > 0) {cnt[4]--; cnt[7]--; break; }
			if(cnt[7] >= 2) {cnt[7]-=2; break; }
			return "-1";

		}
		String ans = "";
		for(int i = 9; i >= 0; i--) {
			char c = (char)(i + '0');
			for(int j = 0; j < cnt[i]; j++) {
				ans += c;
			}
		}
		return ans;
	}
	public static void main(String[] args) {
		new B().doIt();
	}
	void debug(Object...os) {
		System.err.println(deepToString(os));
	}
}


import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class B {
	Scanner sc = new Scanner(System.in);
	void doIt()
	{
		String line = sc.nextLine();
		int n = new Integer(line);
		int sum = 0;
		int [] cnt = new int[10];
		line = sc.nextLine();
		for(int i = 0; i < n; i++) {
			int v = line.charAt(i * 2) - '0';
			sum += v;
			cnt[v]++;
		}
		//System.out.println(sum);
		if(cnt[0] == 0) {
			System.out.println("-1");
			return;
		}
		int rem = sum % 3;
		switch (rem) {
		case 0: break;
		case 1:
			if(cnt[1] > 0) {cnt[1]--;}
			else if(cnt[4] > 0) {cnt[4]--; }
			else if(cnt[7] > 0) {cnt[7]--; }
			else if(cnt[2] >= 2) {cnt[2]-=2;}
			else if(cnt[2] > 0 && cnt[5] > 0) {cnt[2]--; cnt[5]--;}
			else if(cnt[5] >= 2) {cnt[5]-=2; }
			else if(cnt[2] > 0 && cnt[8] > 0) {cnt[2]--; cnt[8]--; }
			else if(cnt[5] > 0 && cnt[8] > 0) {cnt[5]--; cnt[8]--; }
			else if(cnt[8] >= 2) {cnt[8]-=2; }
			else { System.out.println("-1"); return;}
			break;
		case 2:
			if(cnt[2] > 0) {cnt[2]--; }
			else if(cnt[5] > 0) {cnt[5]--; }
			else if(cnt[8] > 0) {cnt[8]--; }
			else if(cnt[1] >= 2) {cnt[1]-=2; }
			else if(cnt[1] > 0 && cnt[4] > 0) {cnt[1]--; cnt[4]--; }
			else if(cnt[4] >= 2) {cnt[4]-=2; }
			else if(cnt[1] > 0 && cnt[7] > 0) {cnt[1]--; cnt[4]--; }
			else if(cnt[4] > 0 && cnt[7] > 0) {cnt[4]--; cnt[7]--; }
			else if(cnt[7] >= 2) {cnt[7]-=2; }
			else { System.out.println("-1"); return;}
			break;
		}
		for(int i = 9; i >= 0; i--) {
			char c = (char)(i + '0');
			for(int j = 0; j < cnt[i]; j++) {
				System.out.print(c);
			}
		}
		System.out.println();
	}
	public static void main(String[] args) {
		new B().doIt();
	}
	void debug(Object...os) {
		System.err.println(deepToString(os));
	}
}


import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class B {
	Scanner sc = new Scanner(System.in);
	void doIt()
	{
		String line = sc.nextLine();
		int n = new Integer(line);
		int sum = 0;
		int [] cnt = new int[10];
		line = sc.nextLine();
		for(int i = 0; i < n; i++) {
			int v = line.charAt(i * 2) - '0';
			sum += v;
			cnt[v]++;
		}
		//System.out.println(sum);
		if(cnt[0] == 0) {
			System.out.println("-1");
			return;
		}
		int rem = sum % 3;
		switch (rem) {
		case 0: break;
		case 1:
			if(cnt[1] > 0) {cnt[1]--;}
			else if(cnt[4] > 0) {cnt[4]--; }
			else if(cnt[7] > 0) {cnt[7]--; }
			else if(cnt[2] >= 2) {cnt[2]-=2;}
			else if(cnt[2] > 0 && cnt[5] > 0) {cnt[2]--; cnt[5]--;}
			else if(cnt[5] >= 2) {cnt[5]-=2; }
			else if(cnt[2] > 0 && cnt[8] > 0) {cnt[2]--; cnt[8]--; }
			else if(cnt[5] > 0 && cnt[8] > 0) {cnt[5]--; cnt[8]--; }
			else if(cnt[8] >= 2) {cnt[8]-=2; }
			else { System.out.println("-1"); return;}
			break;
		case 2:
			if(cnt[2] > 0) {cnt[2]--; }
			else if(cnt[5] > 0) {cnt[5]--; }
			else if(cnt[8] > 0) {cnt[8]--; }
			else if(cnt[1] >= 2) {cnt[1]-=2; }
			else if(cnt[1] > 0 && cnt[4] > 0) {cnt[1]--; cnt[4]--; }
			else if(cnt[4] >= 2) {cnt[4]-=2; }
			else if(cnt[1] > 0 && cnt[7] > 0) {cnt[1]--; cnt[7]--; }
			else if(cnt[4] > 0 && cnt[7] > 0) {cnt[4]--; cnt[7]--; }
			else if(cnt[7] >= 2) {cnt[7]-=2; }
			else { System.out.println("-1"); return;}
			break;
		}
		boolean zero = true;
		for(int i = 1; i <= 9; i++) {
			if(cnt[i] > 0) zero = false;
		}
		if(zero) {
			System.out.println("0");
			return;
		}
		for(int i = 9; i >= 0; i--) {
			char c = (char)(i + '0');
			for(int j = 0; j < cnt[i]; j++) {
				System.out.print(c);
			}
		}
		System.out.println();
	}
	public static void main(String[] args) {
		new B().doIt();
	}
	void debug(Object...os) {
		System.err.println(deepToString(os));
	}
}


import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class B {
	Scanner sc = new Scanner(System.in);
	void doIt()
	{
		String ans = solve();
		System.out.println(ans);
	}
	String solve()
	{
		String line = sc.nextLine();
		int n = new Integer(line);
		int sum = 0;
		int [] cnt = new int[10];
		line = sc.nextLine();
		for(int i = 0; i < n; i++) {
			int v = line.charAt(i * 2) - '0';
			sum += v;
			cnt[v]++;
		}
		//System.out.println(sum);
		if(cnt[0] == 0) return "-1";
		int rem = sum % 3;
		switch (rem) {
		case 0: break;
		case 1:
			if(cnt[1] > 0) {cnt[1]--; break; }
			if(cnt[4] > 0) {cnt[4]--; break; }
			if(cnt[7] > 0) {cnt[7]--; break; }
			if(cnt[2] >= 2) {cnt[2]-=2; break; }
			if(cnt[2] > 0 && cnt[5] > 0) {cnt[2]--; cnt[5]--; break; }
			if(cnt[5] >= 2) {cnt[5]-=2; break; }
			if(cnt[2] > 0 && cnt[8] > 0) {cnt[2]--; cnt[8]--; break; }
			if(cnt[5] > 0 && cnt[8] > 0) {cnt[5]--; cnt[8]--; break; }
			if(cnt[8] >= 2) {cnt[8]-=2; break; }
			return "-1";
		case 2:
			if(cnt[2] > 0) {cnt[2]--; break; }
			if(cnt[5] > 0) {cnt[5]--; break; }
			if(cnt[8] > 0) {cnt[8]--; break; }
			if(cnt[1] >= 2) {cnt[1]-=2; break; }
			if(cnt[1] > 0 && cnt[4] > 0) {cnt[1]--; cnt[4]--; break; }
			if(cnt[4] >= 2) {cnt[4]-=2; break; }
			if(cnt[1] > 0 && cnt[7] > 0) {cnt[1]--; cnt[4]--; break; }
			if(cnt[4] > 0 && cnt[7] > 0) {cnt[4]--; cnt[7]--; break; }
			if(cnt[7] >= 2) {cnt[7]-=2; break; }
			return "-1";

		}
		String ans = "";
		for(int i = 9; i >= 0; i--) {
			char c = (char)(i + '0');
			for(int j = 0; j < cnt[i]; j++) {
				ans += c;
			}
		}
		return ans;
	}
	public static void main(String[] args) {
		new B().doIt();
	}
	void debug(Object...os) {
		System.err.println(deepToString(os));
	}
}


import java.util.*;
import java.io.*;
public class cf {
public static void main(String[] args) throws Exception
{
    input.init(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int n = input.nextInt();
    int[] freq = new int[10];
    for(int i = 0; i<n; i++) freq[input.nextInt()-0]++;
    int mod3 = (freq[1]+freq[4]+freq[7] + 2*(freq[2]+freq[5]+freq[8]))%3;
    boolean good = freq[0] > 0;
    if(mod3 == 2)
    {
        if(freq[2]>0) freq[2]--;
        else if(freq[5] > 0) freq[5]--;
        else if(freq[8]>0) freq[8]--;
        else if(freq[1] > 1) freq[1] -= 2;
        else if(freq[4] > 1) freq[4] -=2;
        else if(freq[7]>1) freq[7]-=2;
        else good = false;
    }
    else if(mod3 == 1)
    {
        if(freq[1]>0) freq[1]--;
        else if(freq[4] > 0) freq[4]--;
        else if(freq[7]>0) freq[7]--;
        else if(freq[2] > 1) freq[2] -= 2;
        else if(freq[5] > 1) freq[5] -=2;
        else if(freq[8]>1) freq[8]-=2;
        else good = false;
    }
    boolean allz = true;
    for(int i = 1; i<9; i++) allz &= freq[i] ==0;
    if(allz) freq[0] = 1;
    if(good)
    {
        for(int i = 9; i>=0;i--)
            for(int j =0; j<freq[i]; j++)
                out.print(i);
    }
    else
        out.println(-1);
    out.close();
}
public static class input {
  static BufferedReader reader;
  static StringTokenizer tokenizer;

  /** call this method to initialize reader for InputStream */
  static void init(InputStream input) {
      reader = new BufferedReader(
                   new InputStreamReader(input) );
      tokenizer = new StringTokenizer("");
  }

  /** get next word */
  static String next() throws IOException {
      while ( ! tokenizer.hasMoreTokens() ) {
          //TODO add check for eof if necessary
          tokenizer = new StringTokenizer(
                 reader.readLine() );
      }
      return tokenizer.nextToken();
  }

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

  static double nextDouble() throws IOException {
      return Double.parseDouble( next() );
  }
  static long nextLong() throws IOException {
      return Long.parseLong( next() );
  }
}
}


import java.util.*;
import java.io.*;
public class cf {
public static void main(String[] args) throws Exception
{
    input.init(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int n = input.nextInt();
    int[] freq = new int[10];
    for(int i = 0; i<n; i++) freq[input.nextInt()-0]++;
    int mod3 = (freq[1]+freq[4]+freq[7] + 2*(freq[2]+freq[5]+freq[8]))%3;
    boolean good = freq[0] > 0;
    if(mod3 == 2)
    {
        if(freq[2]>0) freq[2]--;
        else if(freq[5] > 0) freq[5]--;
        else if(freq[8]>0) freq[8]--;
        else if(freq[1] > 1) freq[1] -= 2;
        else if(freq[4] > 1) freq[4] -=2;
        else if(freq[7]>1) freq[7]-=2;
        else good = false;
    }
    else if(mod3 == 1)
    {
        if(freq[1]>0) freq[1]--;
        else if(freq[4] > 0) freq[4]--;
        else if(freq[7]>0) freq[7]--;
        else if(freq[2] > 1) freq[2] -= 2;
        else if(freq[5] > 1) freq[5] -=2;
        else if(freq[8]>1) freq[8]-=2;
        else good = false;
    }
    else if(freq[0] == n) freq[0] = 1;
    if(good)
    {
        for(int i = 9; i>=0;i--)
            for(int j =0; j<freq[i]; j++)
                out.print(i);
    }
    else
        out.println(-1);
    out.close();
}
public static class input {
  static BufferedReader reader;
  static StringTokenizer tokenizer;

  /** call this method to initialize reader for InputStream */
  static void init(InputStream input) {
      reader = new BufferedReader(
                   new InputStreamReader(input) );
      tokenizer = new StringTokenizer("");
  }

  /** get next word */
  static String next() throws IOException {
      while ( ! tokenizer.hasMoreTokens() ) {
          //TODO add check for eof if necessary
          tokenizer = new StringTokenizer(
                 reader.readLine() );
      }
      return tokenizer.nextToken();
  }

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

  static double nextDouble() throws IOException {
      return Double.parseDouble( next() );
  }
  static long nextLong() throws IOException {
      return Long.parseLong( next() );
  }
}
}


import java.util.*;
import java.io.*;
public class cf {
public static void main(String[] args) throws Exception
{
    input.init(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int n = input.nextInt();
    int[] freq = new int[10];
    for(int i = 0; i<n; i++) freq[input.nextInt()-0]++;
    int mod3 = (freq[1]+freq[4]+freq[7] + 2*(freq[2]+freq[5]+freq[8]))%3;
    boolean good = freq[0] > 0;
    if(mod3 == 2)
    {
        if(freq[2]>0) freq[2]--;
        else if(freq[5] > 0) freq[5]--;
        else if(freq[8]>0) freq[8]--;
        else if(freq[1] > 1) freq[1] -= 2;
        else if(freq[4] > 1) freq[4] -=2;
        else if(freq[7]>1) freq[7]-=2;
        else if(freq[1] > 0 && freq[4]>0){freq[1]--; freq[4]--;}
        else if(freq[1] > 0 && freq[7]>0){freq[1]--; freq[7]--;}
        else if(freq[4] > 0 && freq[7]>0){freq[4]--; freq[7]--;}
        else good = false;
    }
    else if(mod3 == 1)
    {
        if(freq[1]>0) freq[1]--;
        else if(freq[4] > 0) freq[4]--;
        else if(freq[7]>0) freq[7]--;
        else if(freq[2] > 1) freq[2] -= 2;
        else if(freq[5] > 1) freq[5] -=2;
        else if(freq[8]>1) freq[8]-=2;
        else if(freq[2] > 0 && freq[5]>0){freq[2]--; freq[5]--;}
        else if(freq[2] > 0 && freq[8]>0){freq[2]--; freq[8]--;}
        else if(freq[5] > 0 && freq[8]>0){freq[5]--; freq[8]--;}
        else good = false;
    }
    boolean allz = true;
    for(int i = 1; i<9; i++) allz &= freq[i] ==0;
    if(allz) freq[0] = 1;
    if(good)
    {
        for(int i = 9; i>=0;i--)
            for(int j =0; j<freq[i]; j++)
                out.print(i);
    }
    else
        out.println(-1);
    out.close();
}
public static class input {
  static BufferedReader reader;
  static StringTokenizer tokenizer;

  /** call this method to initialize reader for InputStream */
  static void init(InputStream input) {
      reader = new BufferedReader(
                   new InputStreamReader(input) );
      tokenizer = new StringTokenizer("");
  }

  /** get next word */
  static String next() throws IOException {
      while ( ! tokenizer.hasMoreTokens() ) {
          //TODO add check for eof if necessary
          tokenizer = new StringTokenizer(
                 reader.readLine() );
      }
      return tokenizer.nextToken();
  }

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

  static double nextDouble() throws IOException {
      return Double.parseDouble( next() );
  }
  static long nextLong() throws IOException {
      return Long.parseLong( next() );
  }
}
}


import java.util.Arrays;
import java.util.Scanner;
public class HomeTask {
    public static int sum(int z[]){
        int sum = 0;
        for(int i=0;i<z.length;i++)
            sum+=z[i];
        return sum;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        int n = in.nextInt();
        int arr[] = new int [n];
        int all [] = new int [10];
        boolean check=true;
        for(int i=0;i<n;i++){
            arr[i]=in.nextInt();
            all[arr[i]]++;
            if(arr[i]!=0)
                check=false;
        }
        if(check){
            System.out.println(0);
            System.exit(0);
        }
        else if(all[0]==0){
            System.out.println(-1);
            System.exit(0);
        }
        else{
        int sum =sum(arr);
        int m=1;
        while(sum%3!=0){
            for(int i=0;i<10;i++){
                if((sum-m*i)%3==0&&all[i]!=0){
                    all[i]--;
                    sum-=i;
                    break;
                }
            }
            m++;
        }
        check=false;
        for(int i=9;i>=0;i--){
            while(all[i]!=0){
                if(i!=0)
                    check=true;
                if(check)
                    System.out.print(i);
                all[i]--;
            }
        }
        if(!check)
            System.out.println(0);
        else
            System.out.println();
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;
public class HomeTask {
    public static int sum(int z[]){
        int sum = 0;
        for(int i=0;i<z.length;i++)
            sum+=z[i];
        return sum;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        int n = in.nextInt();
        int arr[] = new int [n];
        int all [] = new int [10];
        for(int i=0;i<n;i++){
            arr[i]=in.nextInt();
            all[arr[i]]++;
        }
        if(all[0]==n){
            System.out.println(0);
            System.exit(0);
        }
        if(all[0]==0){
            System.out.println(-1);
            System.exit(0);
        }
        int sum =sum(arr);
        int m=1;
        while(sum%3!=0){
            for(int i=0;i<10;i++){
                if((sum-m*i)%3==0&&all[i]!=0){
                    all[i]--;
                    sum-=i;
                    break;
                }
            }
            m++;
        }
        for(int i=9;i>=0;i--){
            while(all[i]!=0){
                System.out.print(i);
                all[i]--;
            }
        }
        System.out.println();
    }
}


import java.util.Arrays;
import java.util.Scanner;
public class HomeTask {
    public static int sum(int z[]){
        int sum = 0;
        for(int i=0;i<z.length;i++)
            sum+=z[i];
        return sum;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        int n = in.nextInt();
        int arr[] = new int [n];
        int all [] = new int [10];
        for(int i=0;i<n;i++){
            arr[i]=in.nextInt();
            all[arr[i]]++;
        }
        if(all[0]==0){
            System.out.println(-1);
            System.exit(0);
        }
        int sum =sum(arr);
        int m=1;
        while(sum%3!=0){
            for(int i=0;i<10;i++){
                if((sum-m*i)%3==0&&all[i]!=0){
                    all[i]--;
                    sum-=i;
                    break;
                }
            }
            m++;
        }
        for(int i=9;i>=0;i--){
            while(all[i]!=0){
                System.out.print(i);
                all[i]--;
            }
        }
        System.out.println();
    }
}


import java.util.Arrays;
import java.util.Scanner;
public class HomeTask {
    public static int sum(int z[]){
        int sum = 0;
        for(int i=0;i<z.length;i++)
            sum+=z[i];
        return sum;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        int n = in.nextInt();
        int arr[] = new int [n];
        int all [] = new int [10];
        for(int i=0;i<n;i++){
            arr[i]=in.nextInt();
            all[arr[i]]++;
        }
        if(all[0]==0){
            System.out.println(-1);
            System.exit(0);
        }
        int sum =sum(arr);
        while(sum%3!=0){
            for(int i=0;i<10;i++){
                if((sum-i)%3==0&&all[i]!=0){
                    all[i]--;
                    sum-=i;
                    break;
                }
            }
        }
        for(int i=9;i>=0;i--){
            while(all[i]!=0){
                System.out.print(i);
                all[i]--;
            }
        }
        System.out.println();
    }
}


import java.util.Arrays;
import java.util.Scanner;
public class HomeTask {
    public static int sum(int z[]){
        int sum = 0;
        for(int i=0;i<z.length;i++)
            sum+=z[i];
        return sum;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        int n = in.nextInt();
        int arr[] = new int [n];
        int all [] = new int [10];
        boolean check=true;
        for(int i=0;i<n;i++){
            arr[i]=in.nextInt();
            all[arr[i]]++;
            if(arr[i]!=0)
                check=false;
        }
        if(check){
            System.out.println(0);
            System.exit(0);
        }
        else if(all[0]==0){
            System.out.println(-1);
            System.exit(0);
        }
        else{
        int sum =sum(arr);
        int m=1;
        while(sum%3!=0){
            for(int i=0;i<10;i++){
                if((sum-m*i)%3==0&&all[i]!=0){
                    all[i]--;
                    sum-=i;
                    break;
                }
            }
            m++;
        }
        for(int i=9;i>=0;i--){
            while(all[i]!=0){
                System.out.print(i);
                all[i]--;
            }
        }
        System.out.println();
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class B {
    public static void main(String[] args) throws IOException {
        BufferedReader buf = new BufferedReader(
                new InputStreamReader(System.in));
        int n = Integer.parseInt(buf.readLine());
        int[] a = new int[n];
        String[] line = buf.readLine().split(" ");
        int count = 0;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(line[i]);
            if (a[i] == 0)
                count++;
        }
        if (count == 0)
            System.out.println(-1);
        else {
            Arrays.sort(a);
            boolean[] v = new boolean[n];
            int[] mod = new int[3];

            for (int i = 0; i < n; i++) {
                if (a[i] % 3 == 0) {
                    v[i] = true;
                }
                mod[a[i] % 3]++;

            }
            int taken = Math.min(mod[1], mod[2]);
            mod[1] -= taken;
            mod[2] -= taken;
            int k = 0;
            int j = 0;
            for (int i = 0; i < n; i++) {
                if (a[i] % 3 == 1&&j<taken) {
                    v[i] = true;
                    j++;
                } else if (a[i] % 3 == 2&&k<taken) {
                    v[i] = true;
                    k++;
                }
            }
            int taken2 = (mod[1] / 3) * 3;
            j = 0;
            for (int i = 0; i < n && j < taken2; i++)
                if (!v[i]) {
                    if (a[i] % 3 == 1) {
                        v[i] = true;
                        j++;
                    }
                }
            int taken3 = (mod[2] / 3) * 3;
            j = 0;
            for (int i = 0; i < n && j < taken3; i++)
                if (!v[i]) {
                    if (a[i] % 3 == 2) {
                        v[i] = true;
                        j++;
                    }
                }

            StringBuilder s = new StringBuilder("");
            for (int i = a.length - 1; i >= 0; i--)
                if (v[i])
                    s.append(a[i]);
            System.out.println(s);
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class B131 {
	public static void main(String[] args) throws NumberFormatException,
			IOException {
		BufferedReader buf = new BufferedReader(
				new InputStreamReader(System.in));
		int n = Integer.parseInt(buf.readLine());
		int[] a = new int[n];
		String[] line = buf.readLine().split(" ");
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(line[i]);
		Arrays.sort(a);
		int[] b = new int[n];
		int mod = 0;
		boolean check = false;
		int[] count = new int[3];
		for (int i = 0; i < n; i++) {
			b[i] = a[i] % 3;
			mod += a[i];
			count[b[i]]++;
			if (a[i] == 0)
				check = true;
		}
		if (!check)
			System.out.println(-1);
		else {
			boolean[] v = new boolean[n];
			mod %= 3;
			if (mod == 1) {
				for (int i = 0; i < n; i++)
					if (b[i] == 1) {
						v[i] = true;
						break;
					}
			} else if (mod == 2) {
				if (count[2] >= 1)
					for (int i = 0; i < n; i++) {
						if (b[i] == 2) {
							v[i] = true;
							break;
						}
					}
				else {
					int count1 = 0;
					for (int i = 0; i < n && count1 < 2; i++)
						if (b[i] == 1) {
							count1++;
							v[i] = true;
						}
				}
			}
			StringBuilder ans = new StringBuilder("");
			for (int i = n - 1; i >= 0; i--)
				if (!v[i])
					ans.append(a[i]);
			int index = 0;
			while (index < ans.length() - 1 && ans.charAt(index) == '0')
				index++;
			System.out.println(ans.substring(index, ans.length()));
		}

	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class B {
    public static void main(String[] args) throws IOException {
        BufferedReader buf = new BufferedReader(
                new InputStreamReader(System.in));
        int n = Integer.parseInt(buf.readLine());
        int[] a = new int[n];
        String[] line = buf.readLine().split(" ");
        int count = 0;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(line[i]);
            if (a[i] == 0)
                count++;
        }
        if (count == 0)
            System.out.println(-1);
        else {
            Arrays.sort(a);
            boolean[] v = new boolean[n];
            int[] mod = new int[3];

            for (int i = 0; i < n; i++) {
                if (a[i] % 3 == 0) {
                    v[i] = true;
                }
                mod[a[i] % 3]++;

            }
            int taken = Math.min(mod[1], mod[2]);
            mod[1] -= taken;
            mod[2] -= taken;
            int k = 0;
            int j = 0;
            for (int i = n - 1; i >= 0; i--) {
                if (a[i] % 3 == 1 && j < taken) {
                    v[i] = true;
                    j++;
                } else if (a[i] % 3 == 2 && k < taken) {
                    v[i] = true;
                    k++;
                }
            }
            int taken2 = (mod[1] / 3) * 3;
            j = 0;
            for (int i = n - 1; i >= 0 && j < taken2; i--)
                if (!v[i]) {
                    if (a[i] % 3 == 1) {
                        v[i] = true;
                        j++;
                    }
                }
            int taken3 = (mod[2] / 3) * 3;
            j = 0;
            for (int i = n - 1; i >= 0 && j < taken3; i--)
                if (!v[i]) {
                    if (a[i] % 3 == 2) {
                        v[i] = true;
                        j++;
                    }
                }

            StringBuilder s = new StringBuilder("");
            for (int i = a.length - 1; i >= 0; i--)
                if (v[i])
                    s.append(a[i]);
            int index = 0;
            while (s.length() > 1 && s.charAt(0) == '0')
                s = s.deleteCharAt(0);
            System.out.println(s);
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class B {
    public static void main(String[] args) throws IOException {
        BufferedReader buf = new BufferedReader(
                new InputStreamReader(System.in));
        int n = Integer.parseInt(buf.readLine());
        int[] a = new int[n];
        String[] line = buf.readLine().split(" ");
        int count = 0;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(line[i]);
            if (a[i] == 0)
                count++;
        }
        if (count == 0)
            System.out.println(-1);
        else {
            Arrays.sort(a);
            boolean[] v = new boolean[n];
            int[] mod = new int[3];

            for (int i = 0; i < n; i++) {
                if (a[i] % 3 == 0) {
                    v[i] = true;
                }
                mod[a[i] % 3]++;

            }
            int taken = Math.min(mod[1], mod[2]);
            mod[1] -= taken;
            mod[2] -= taken;
            int k = 0;
            int j = 0;
            for (int i = n - 1; i >= 0; i--) {
                if (a[i] % 3 == 1 && j < taken) {
                    v[i] = true;
                    j++;
                } else if (a[i] % 3 == 2 && k < taken) {
                    v[i] = true;
                    k++;
                }
            }
            int taken2 = (mod[1] / 3) * 3;
            j = 0;
            for (int i = n - 1; i >= 0 && j < taken2; i--)
                if (!v[i]) {
                    if (a[i] % 3 == 1) {
                        v[i] = true;
                        j++;
                    }
                }
            int taken3 = (mod[2] / 3) * 3;
            j = 0;
            for (int i = n - 1; i >= 0 && j < taken3; i--)
                if (!v[i]) {
                    if (a[i] % 3 == 2) {
                        v[i] = true;
                        j++;
                    }
                }

            StringBuilder s = new StringBuilder("");
            for (int i = a.length - 1; i >= 0; i--)
                if (v[i])
                    s.append(a[i]);
            System.out.println(s);
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class B {
    public static void main(String[] args) throws IOException {
        BufferedReader buf = new BufferedReader(
                new InputStreamReader(System.in));
        int n = Integer.parseInt(buf.readLine());
        int[] a = new int[n];
        int[] b = new int[n];
        String[] line = buf.readLine().split(" ");
        int count = 0;

        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(line[i]);
            if (a[i] == 0)
                count++;
        }
        Arrays.sort(a);
        for (int i = 0; i < n; i++) {
            b[i] = a[i] % 3;
        }

        if (count == 0)
            System.out.println(-1);
        else {
            boolean[] v = new boolean[n];
            int[] mod = new int[3];

            for (int i = 0; i < n; i++) {
                if (b[i] == 0) {
                    v[i] = true;
                }
                mod[b[i]]++;

            }
            int taken = Math.min(mod[1], mod[2]);
            int taken2 = (mod[1] / 3) * 3;
            int taken3 = (mod[2] / 3) * 3;
            if (taken * 2 + 3 * ((mod[1] - taken) / 3) + 3
                    * ((mod[2] - taken) / 3) > taken2 + taken3 + 2
                    * (Math.min(mod[1] - taken2, mod[2] - taken3))) {
                mod[1] -= taken;
                mod[2] -= taken;
                int k = 0;
                int j = 0;
                for (int i = n - 1; i >= 0; i--) {
                    if (b[i] == 1 && j < taken) {
                        v[i] = true;
                        j++;
                    } else if (b[i] == 2 && k < taken) {
                        v[i] = true;
                        k++;
                    }
                }
                taken2 = (mod[1] / 3) * 3;
                taken3 = (mod[2] / 3) * 3;

                j = 0;
                for (int i = n - 1; i >= 0 && j < taken2; i--)
                    if (!v[i]) {
                        if (b[i] == 1) {
                            v[i] = true;
                            j++;
                        }
                    }
                j = 0;
                for (int i = n - 1; i >= 0 && j < taken3; i--)
                    if (!v[i]) {
                        if (b[i] == 2) {
                            v[i] = true;
                            j++;
                        }
                    }

            } else {
                int k = 0;
                int j = 0;
                j = 0;
                for (int i = n - 1; i >= 0 && j < taken2; i--)
                    if (!v[i]) {
                        if (b[i] == 1) {
                            v[i] = true;
                            j++;
                        }
                    }
                j = 0;
                for (int i = n - 1; i >= 0 && j < taken3; i--)
                    if (!v[i]) {
                        if (b[i] == 2) {
                            v[i] = true;
                            j++;
                        }
                    }
                mod[1] -= taken2;
                mod[2] -= taken3;
                taken = Math.min(mod[1], mod[2]);

                for (int i = n - 1; i >= 0; i--) {
                    if (b[i] == 1 && j < taken) {
                        v[i] = true;
                        j++;
                    } else if (b[i] == 2 && k < taken) {
                        v[i] = true;
                        k++;
                    }
                }

            }

            StringBuilder s = new StringBuilder("");
            for (int i = a.length - 1; i >= 0; i--)
                if (v[i])
                    s.append(a[i]);
            int index = 0;
            while (index < s.length() - 1 && s.charAt(index) == '0')
                index++;
            System.out.println(s.substring(index, s.length()));
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class B131 {
	public static void main(String[] args) throws NumberFormatException,
			IOException {
		BufferedReader buf = new BufferedReader(
				new InputStreamReader(System.in));
		int n = Integer.parseInt(buf.readLine());
		int[] a = new int[n];
		String[] line = buf.readLine().split(" ");
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(line[i]);
		Arrays.sort(a);
		int[] b = new int[n];
		int mod = 0;
		boolean check = false;
		int[] count = new int[3];
		for (int i = 0; i < n; i++) {
			b[i] = a[i] % 3;
			mod += a[i];
			count[b[i]]++;
			if (a[i] == 0)
				check = true;
		}
		if (!check)
			System.out.println(-1);
		else {
			boolean[] v = new boolean[n];
			mod %= 3;
			if (mod == 1) {
				if (count[1] != 0) {
					for (int i = 0; i < n; i++)
						if (b[i] == 1) {
							v[i] = true;
							break;
						}
				} else {
					int count1 = 0;
					for (int i = 0; i < n && count1 < 2; i++)
						if (b[i] == 2) {
							count1++;
							v[i] = true;
						}

				}
			} else if (mod == 2) {
				if (count[2] >= 1)
					for (int i = 0; i < n; i++) {
						if (b[i] == 2) {
							v[i] = true;
							break;
						}
					}
				else {
					int count1 = 0;
					for (int i = 0; i < n && count1 < 2; i++)
						if (b[i] == 1) {
							count1++;
							v[i] = true;
						}
				}
			}
			StringBuilder ans = new StringBuilder("");
			for (int i = n - 1; i >= 0; i--)
				if (!v[i])
					ans.append(a[i]);
			int index = 0;
			while (index < ans.length() - 1 && ans.charAt(index) == '0')
				index++;
			System.out.println(ans.substring(index, ans.length()));
		}

	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class B {
    public static void main(String[] args) throws IOException {
        BufferedReader buf = new BufferedReader(
                new InputStreamReader(System.in));
        int n = Integer.parseInt(buf.readLine());
        int[] a = new int[n];
        String[] line = buf.readLine().split(" ");
        int count = 0;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(line[i]);
            if (a[i] == 0)
                count++;
        }
        if (count == 0)
            System.out.println(-1);
        else {
            Arrays.sort(a);
            boolean[] v = new boolean[n];
            int[] mod = new int[3];

            for (int i = 0; i < n; i++) {
                if (a[i] % 3 == 0) {
                    v[i] = true;
                }
                mod[a[i] % 3]++;

            }
            int taken = Math.min(mod[1], mod[2]);
            int taken2 = (mod[1] / 3) * 3;
            int taken3 = (mod[2] / 3) * 3;
            if (taken * 2 + 3 * ((mod[1] - taken) / 3) + 3
                    * ((mod[2] - taken) / 3) > taken2 + taken3 + 2
                    * (Math.min(mod[1] - taken2, mod[2] - taken3))) {
                mod[1] -= taken;
                mod[2] -= taken;
                int k = 0;
                int j = 0;
                for (int i = n - 1; i >= 0; i--) {
                    if (a[i] % 3 == 1 && j < taken) {
                        v[i] = true;
                        j++;
                    } else if (a[i] % 3 == 2 && k < taken) {
                        v[i] = true;
                        k++;
                    }
                }
                taken2 = (mod[1] / 3) * 3;
                taken3 = (mod[2] / 3) * 3;

                j = 0;
                for (int i = n - 1; i >= 0 && j < taken2; i--)
                    if (!v[i]) {
                        if (a[i] % 3 == 1) {
                            v[i] = true;
                            j++;
                        }
                    }
                j = 0;
                for (int i = n - 1; i >= 0 && j < taken3; i--)
                    if (!v[i]) {
                        if (a[i] % 3 == 2) {
                            v[i] = true;
                            j++;
                        }
                    }

            } else {
                int k = 0;
                int j = 0;
                j = 0;
                for (int i = n - 1; i >= 0 && j < taken2; i--)
                    if (!v[i]) {
                        if (a[i] % 3 == 1) {
                            v[i] = true;
                            j++;
                        }
                    }
                j = 0;
                for (int i = n - 1; i >= 0 && j < taken3; i--)
                    if (!v[i]) {
                        if (a[i] % 3 == 2) {
                            v[i] = true;
                            j++;
                        }
                    }
                mod[1] -= taken2;
                mod[2] -= taken3;
                taken = Math.min(mod[1], mod[2]);

                for (int i = n - 1; i >= 0; i--) {
                    if (a[i] % 3 == 1 && j < taken) {
                        v[i] = true;
                        j++;
                    } else if (a[i] % 3 == 2 && k < taken) {
                        v[i] = true;
                        k++;
                    }
                }

            }

            StringBuilder s = new StringBuilder("");
            for (int i = a.length - 1; i >= 0; i--)
                if (v[i])
                    s.append(a[i]);
            while (s.length() > 1 && s.charAt(0) == '0')
                s = s.deleteCharAt(0);
            System.out.println(s);
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class B {
    public static void main(String[] args) throws IOException {
        BufferedReader buf = new BufferedReader(
                new InputStreamReader(System.in));
        int n = Integer.parseInt(buf.readLine());
        int[] a = new int[n];
        int[] b = new int[n];
        String[] line = buf.readLine().split(" ");
        int count = 0;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(line[i]);
            if (a[i] == 0)
                count++;
            b[i] = a[i] % 3;
        }
        if (count == 0)
            System.out.println(-1);
        else {
            Arrays.sort(a);
            boolean[] v = new boolean[n];
            int[] mod = new int[3];

            for (int i = 0; i < n; i++) {
                if (b[i] == 0) {
                    v[i] = true;
                }
                mod[b[i]]++;

            }
            int taken = Math.min(mod[1], mod[2]);
            int taken2 = (mod[1] / 3) * 3;
            int taken3 = (mod[2] / 3) * 3;
            if (taken * 2 + 3 * ((mod[1] - taken) / 3) + 3
                    * ((mod[2] - taken) / 3) > taken2 + taken3 + 2
                    * (Math.min(mod[1] - taken2, mod[2] - taken3))) {
                mod[1] -= taken;
                mod[2] -= taken;
                int k = 0;
                int j = 0;
                for (int i = n - 1; i >= 0; i--) {
                    if (b[i] == 1 && j < taken) {
                        v[i] = true;
                        j++;
                    } else if (b[i] == 2 && k < taken) {
                        v[i] = true;
                        k++;
                    }
                }
                taken2 = (mod[1] / 3) * 3;
                taken3 = (mod[2] / 3) * 3;

                j = 0;
                for (int i = n - 1; i >= 0 && j < taken2; i--)
                    if (!v[i]) {
                        if (b[i] == 1) {
                            v[i] = true;
                            j++;
                        }
                    }
                j = 0;
                for (int i = n - 1; i >= 0 && j < taken3; i--)
                    if (!v[i]) {
                        if (b[i] == 2) {
                            v[i] = true;
                            j++;
                        }
                    }

            } else {
                int k = 0;
                int j = 0;
                j = 0;
                for (int i = n - 1; i >= 0 && j < taken2; i--)
                    if (!v[i]) {
                        if (b[i] == 1) {
                            v[i] = true;
                            j++;
                        }
                    }
                j = 0;
                for (int i = n - 1; i >= 0 && j < taken3; i--)
                    if (!v[i]) {
                        if (b[i] == 2) {
                            v[i] = true;
                            j++;
                        }
                    }
                mod[1] -= taken2;
                mod[2] -= taken3;
                taken = Math.min(mod[1], mod[2]);

                for (int i = n - 1; i >= 0; i--) {
                    if (b[i] == 1 && j < taken) {
                        v[i] = true;
                        j++;
                    } else if (b[i] == 2 && k < taken) {
                        v[i] = true;
                        k++;
                    }
                }

            }

            StringBuilder s = new StringBuilder("");
            for (int i = a.length - 1; i >= 0; i--)
                if (v[i])
                    s.append(a[i]);
            int index = 0;
            while (index < s.length()-1 && s.charAt(index) == '0')
                index++;
            System.out.println(s.substring(index, s.length()));
        }
    }
}


import java.util.Scanner;

public class Prob214B {

	public static void main(String[] Args) {
		Scanner in = new Scanner(System.in);
		int digits = in.nextInt();

		int[] d = new int[10];
		for (int i = 0; i < digits; i++)
			d[in.nextInt()]++;

		if (d[0] == 0) {
			System.out.println(-1);
			System.exit(0);
		}

		int[] mod = new int[3];
		for (int i = 0; i < 10; i++)
			mod[i % 3] += d[i];

		int r = (2 * mod[2] + mod[1]) % 3;
		if (r == 2 || r == 1) {
			if (mod[r] > 0) {
				boolean done = false;
				int remove = 6 + r;
				while (!done) {
					if (d[remove] > 0) {
						d[remove]--;
						done = true;
					}
					remove -= 3;
				}
			} else {
				for (int i = 0; i < 2; i++) {
					boolean done = false;
					int remove = 9 - r;
					while (!done) {
						if (d[remove] > 0) {
							d[remove]--;
							done = true;
						}
						remove -= 3;
					}
				}
			}
		}

		StringBuffer sb = new StringBuffer();
		for (int i = 9; i >= 0; i--)
			for (int j = 0; j < d[i]; j++)
				sb = sb.append(i);

		if (sb.charAt(0) == '0')
			System.out.println(0);
		else
			System.out.println(sb);
	}
}


import java.util.Scanner;

public class Prob214B {

	public static void main(String[] Args) {
		Scanner in = new Scanner(System.in);
		int digits = in.nextInt();

		int[] d = new int[10];
		for (int i = 0; i < digits; i++)
			d[in.nextInt()]++;

		if (d[0] == 0) {
			System.out.println(-1);
			System.exit(0);
		}

		int[] mod = new int[3];
		for (int i = 0; i < 10; i++)
			mod[i % 3] += d[i];

		int r = (2 * mod[2] + 1 * mod[1]) % 3;
		if (r == 2 || r == 1) {
			if (mod[r] > 0) {
				boolean done = false;
				int start = 6 + r;
				while (!done) {
					if (d[start] > 0) {
						d[start]--;
						done = true;
					}
					start -= 3;
				}
			} else {
				for (int i = 0; i < 2; i++) {
					boolean done = false;
					int start = 9 - r;
					while (!done) {
						if (d[start] > 0) {
							d[start]--;
							done = true;
						}
						start -= 3;
					}
				}
			}
		}

		StringBuffer sb = new StringBuffer();
		for (int i = 9; i >= 0; i--)
			for (int j = 0; j < d[i]; j++)
				sb = sb.append(i);

		System.out.println(sb);
	}
}


import java.util.Scanner;

public class Prob214B {

	public static void main(String[] Args) {
		Scanner in = new Scanner(System.in);
		int digits = in.nextInt();

		int[] d = new int[10];
		for (int i = 0; i < digits; i++)
			d[in.nextInt()]++;

		if (d[0] == 0) {
			System.out.println(-1);
			System.exit(0);
		}

		int[] mod = new int[3];
		for (int i = 0; i < 10; i++)
			mod[i % 3] += d[i];

		int r = (2 * mod[2] + mod[1]) % 3;
		if (r == 2 || r == 1) {
			if (mod[r] > 0) {
				boolean done = false;
				int remove = r;
				while (!done) {
					if (d[remove] > 0) {
						d[remove]--;
						done = true;
					}
					remove += 3;
				}
			} else {
				for (int i = 0; i < 2; i++) {
					boolean done = false;
					int remove = 3 - r;
					while (!done) {
						if (d[remove] > 0) {
							d[remove]--;
							done = true;
						}
						remove += 3;
					}
				}
			}
		}

		StringBuffer sb = new StringBuffer();
		for (int i = 9; i >= 0; i--)
			for (int j = 0; j < d[i]; j++)
				sb = sb.append(i);

		if (sb.charAt(0) == '0')
			System.out.println(0);
		else
			System.out.println(sb);
	}
}


import java.util.Scanner;

public class Prob214B {

	public static void main(String[] Args) {
		Scanner in = new Scanner(System.in);
		int digits = in.nextInt();

		int[] d = new int[10];
		for (int i = 0; i < digits; i++)
			d[in.nextInt()]++;

		if (d[0] == 0) {
			System.out.println(-1);
			System.exit(0);
		}

		int[] mod = new int[3];
		for (int i = 0; i < 10; i++)
			mod[i % 3] += d[i];

		int r = 2 * mod[2] + 1 * mod[1];
		if (r == 2 || r == 1) {
			if (mod[r] > 0) {
				boolean done = false;
				int start = 6 + r;
				while (!done) {
					if (d[start] > 0) {
						d[start]--;
						done = true;
					}
					start -= 3;
				}
			} else {
				for (int i = 0; i < 2; i++) {
					boolean done = false;
					int start = 9 - r;
					while (!done) {
						if (d[start] > 0) {
							d[start]--;
							done = true;
						}
						start -= 3;
					}
				}
			}
		}

		StringBuffer sb = new StringBuffer();
		for (int i = 9; i >= 0; i--)
			for (int j = 0; j < d[i]; j++)
				sb = sb.append(i);

		System.out.println(sb);
	}
}


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

public class Main {

   // static class Scanner {
   // StreamTokenizer in;
   // boolean forceMode = false;
   //
   // Scanner(InputStream is, String codePage, boolean forceMode) {
   // in = new StreamTokenizer(new BufferedReader(new InputStreamReader(is)));
   // if (!forceMode) {
   // in.resetSyntax();
   // in.wordChars(33, 255);
   // in.whitespaceChars(0, 32);
   // }
   // }
   //
   // Scanner(InputStream is, String codePage) {
   // in = new StreamTokenizer(new BufferedReader(new InputStreamReader(is)));
   // if (!forceMode) {
   // in.resetSyntax();
   // in.wordChars(33, 255);
   // in.whitespaceChars(0, 32);
   // }
   // }
   //
   // String next() {
   // try {
   // in.nextToken();
   // return in.sval;
   // } catch (Exception e) {
   // throw new Error();
   // }
   // }
   //
   // int nextInt() {
   // if (forceMode) {
   // try {
   // in.nextToken();
   // return (int) in.nval;
   // } catch (Exception e) {
   // throw new Error();
   // }
   // } else {
   // return Integer.parseInt(next());
   // }
   // }
   //
   // long nextLong() {
   // if (forceMode) {
   // throw new Error("No long at force mod!");
   // } else {
   // return Long.parseLong(next());
   // }
   //
   // }
   //
   // double nextDouble() {
   // if (forceMode) {
   // try {
   // in.nextToken();
   // return in.nval;
   // } catch (Exception e) {
   // throw new Error();
   // }
   // } else {
   // return Double.parseDouble(next());
   // }
   // }
   // }

   static class Assertion {
      static void checkRE(boolean e) {
         if (!e) {
            throw new Error();
         }
      }

      static void checkTL(boolean e) {
         if (!e) {
            int idx = 1;
            while (idx > 0) {
               idx++;
            }
         }
      }
   }

   Scanner in;
   PrintWriter out;

   void solve() {
      int n = in.nextInt();
      int ar[] = new int[n];
      int nZeros = 0;
      int sum = 0;
      for (int i = 0; i < ar.length; i++) {
         ar[i] = in.nextInt();
         if (ar[i] == 0) {
            nZeros++;
         }
         sum += ar[i];
      }

      Arrays.sort(ar);
      if (sum % 3 == 0) {
         print(ar);
      } else if (sum % 3 == 1) {
         boolean removed = false;
         for (int i = 0; i < ar.length; i++) {
            if (ar[i] % 3 == 1) {
               removed = true;
               ar[i] = -1;
               break;
            }
         }
         if (!removed) {
            int cnt = 0;
            for (int i = 0; i < ar.length; i++) {
               if (ar[i] % 3 == 2) {
                  cnt++;
                  ar[i] = -1;
               }
               if (cnt == 2) {
                  break;
               }
            }
            print(ar);
         } else {
            print(ar);
         }
      } else if (sum % 3 == 2) {
         boolean removed = false;
         for (int i = 0; i < ar.length; i++) {
            if (ar[i] % 3 == 2) {
               removed = true;
               ar[i] = -1;
               break;
            }
         }
         if (removed) {
            print(ar);
         } else {
            int cnt = 0;
            for (int i = 0; i < ar.length; i++) {
               if (ar[i] % 3 == 1) {
                  cnt++;
                  ar[i] = -1;
               }
               if (cnt == 2) {
                  break;
               }
            }
            print(ar);
         }
      }

   }

   void print(int ar[]) {
      ArrayList<Integer> output = new ArrayList<Integer>();
      int nZeros = 0;
      for (int i = ar.length - 1; i >= 0; i--) {
         if (ar[i] >= 0) {
            output.add(ar[i]);
         }
         if (ar[i] == 0) {
            nZeros++;
         }
      }
      if (nZeros==0){
         out.print(-1);
      }
      if (output.size() == nZeros) {
         out.print(0);
      } else {
         for (int num : output) {
            out.print(num);
         }
      }
   }

   final static String fileName = "";

   void run() {
      // try {
      // in = new Scanner(new FileInputStream(fileName + ".in"), "ISO-8859-1");
      // out = new PrintWriter(fileName + ".out", "ISO-8859-1");
      // } catch (FileNotFoundException e) {
      // throw new Error(e);
      //
      // } catch (UnsupportedEncodingException e) {
      // throw new Error(e);
      // }
      in = new Scanner(System.in);
      out = new PrintWriter(System.out, true);
      try {
         solve();
      } catch (Exception e) {
         throw new Error(e);
      } finally {
         out.close();
      }
   }

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


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

public class Main {

   // static class Scanner {
   // StreamTokenizer in;
   // boolean forceMode = false;
   //
   // Scanner(InputStream is, String codePage, boolean forceMode) {
   // in = new StreamTokenizer(new BufferedReader(new InputStreamReader(is)));
   // if (!forceMode) {
   // in.resetSyntax();
   // in.wordChars(33, 255);
   // in.whitespaceChars(0, 32);
   // }
   // }
   //
   // Scanner(InputStream is, String codePage) {
   // in = new StreamTokenizer(new BufferedReader(new InputStreamReader(is)));
   // if (!forceMode) {
   // in.resetSyntax();
   // in.wordChars(33, 255);
   // in.whitespaceChars(0, 32);
   // }
   // }
   //
   // String next() {
   // try {
   // in.nextToken();
   // return in.sval;
   // } catch (Exception e) {
   // throw new Error();
   // }
   // }
   //
   // int nextInt() {
   // if (forceMode) {
   // try {
   // in.nextToken();
   // return (int) in.nval;
   // } catch (Exception e) {
   // throw new Error();
   // }
   // } else {
   // return Integer.parseInt(next());
   // }
   // }
   //
   // long nextLong() {
   // if (forceMode) {
   // throw new Error("No long at force mod!");
   // } else {
   // return Long.parseLong(next());
   // }
   //
   // }
   //
   // double nextDouble() {
   // if (forceMode) {
   // try {
   // in.nextToken();
   // return in.nval;
   // } catch (Exception e) {
   // throw new Error();
   // }
   // } else {
   // return Double.parseDouble(next());
   // }
   // }
   // }

   static class Assertion {
      static void checkRE(boolean e) {
         if (!e) {
            throw new Error();
         }
      }

      static void checkTL(boolean e) {
         if (!e) {
            int idx = 1;
            while (idx > 0) {
               idx++;
            }
         }
      }
   }

   Scanner in;
   PrintWriter out;

   void solve() {
      int n = in.nextInt();
      int ar[] = new int[n];
      int nZeros = 0;
      int sum = 0;
      for (int i = 0; i < ar.length; i++) {
         ar[i] = in.nextInt();
         if (ar[i] == 0) {
            nZeros++;
         }
         sum += ar[i];
      }
      if (nZeros == 0) {
         out.println(-1);
      } else if (nZeros == n) {
         out.println(0);
      } else {
         Arrays.sort(ar);
         if (sum % 3 == 0) {
            print(ar);
         } else if (sum % 3 == 1) {
            boolean removed = false;
            for (int i = 0; i < ar.length; i++) {
               if (ar[i] % 3 == 1) {
                  removed = true;
                  ar[i] = -1;
                  break;
               }
            }
            if (!removed) {
               int cnt = 0;
               for (int i = 0; i < ar.length; i++) {
                  if (ar[i] % 3 == 2) {
                     cnt++;
                     ar[i] = -1;
                  }
                  if (cnt == 2) {
                     break;
                  }
               }
               print(ar);
            } else {
               print(ar);
            }
         } else if (sum % 3 == 2) {
            boolean removed = false;
            for (int i = 0; i < ar.length; i++) {
               if (ar[i] % 3 == 2) {
                  removed = true;
                  ar[i] = -1;
                  break;
               }
            }
            if (removed) {
               print(ar);
            } else {
               int cnt = 0;
               for (int i = 0; i < ar.length; i++) {
                  if (ar[i] % 3 == 1) {
                     cnt++;
                     ar[i] = -1;
                  }
                  if (cnt == 2) {
                     break;
                  }
               }
               print(ar);
            }
         }
      }
   }

   void print(int ar[]) {
      for (int i = ar.length - 1; i >= 0; i--) {
         if (ar[i] >= 0) {
            out.print(ar[i]);
         }
      }
   }

   final static String fileName = "";

   void run() {
      // try {
      // in = new Scanner(new FileInputStream(fileName + ".in"), "ISO-8859-1");
      // out = new PrintWriter(fileName + ".out", "ISO-8859-1");
      // } catch (FileNotFoundException e) {
      // throw new Error(e);
      //
      // } catch (UnsupportedEncodingException e) {
      // throw new Error(e);
      // }
      in = new Scanner(System.in);
      out = new PrintWriter(System.out, true);
      try {
         solve();
      } catch (Exception e) {
         throw new Error(e);
      } finally {
         out.close();
      }
   }

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


//package solution;

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

public final class Solution {
    BufferedReader br;
    StringTokenizer stk;

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

    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    long mod = 1000_000_000 + 7;
    StringBuilder res = new StringBuilder(1000005);

    void run() throws Exception {
        char[] c = nt().toCharArray();
        int[] a = new int[c.length];
        for(int i = 0; i < c.length; i++) {
            a[i] = (c[i] - '0') % 3;
        }

        boolean[] used = new boolean[c.length];
        int count = 0;
        for(int i = 0; i < c.length; i++) {
            if(a[i] == 0) {
                used[i] = true;
                count++;
            } else {
                try {
                    if(!used[i - 1]) {
                        String s = "" + a[i - 1] + a[i];
                        if(s.equals("12") || s.equals("21")) {
                            count++;
                            used[i] = used[i - 1] = true;
                        }
                    }
                    if(!used[i - 1] && !used[i - 2]) {
                        String s = "" + a[i - 2] + a[i - 1] + a[i];
                        if(s.equals("111")) {
                            count++;
                            used[i] = used[i - 1] = used[i - 2] = true;
                        }
                    }
                } catch(Exception ex) {}
            }
        }

        System.out.println(count);
    }

    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        }
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }
}


//package solution;

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

public final class Solution {
    BufferedReader br;
    StringTokenizer stk;

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

    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    long mod = 1000_000_000 + 7;
    StringBuilder res = new StringBuilder(1000005);

    void run() throws Exception {
        char[] c = nt().toCharArray();
        int[] a = new int[c.length];
        for(int i = 0; i < c.length; i++) {
            a[i] = (c[i] - '0') % 3;
        }

        boolean[] used = new boolean[c.length];
        int count = 0;
        for(int i = 0; i < c.length; i++) {
            if(a[i] == 0) {
                used[i] = true;
                count++;
            } else {
                try {
                    if(!used[i - 1]) {
                        String s = "" + a[i - 1] + a[i];
                        if(s.equals("12") || s.equals("21")) {
                            count++;
                            used[i] = used[i - 1] = true;
                        }
                    }
                    if(!used[i - 1] && !used[i - 2]) {
                        String s = "" + a[i - 2] + a[i - 1] + a[i];
                        if(s.equals("111")) {
                            count++;
                            used[i] = used[i - 1] = used[i - 2] = true;
                        }
                    }
                } catch(Exception ex) {}
            }
        }
        int count2 = 0;
        used = new boolean[c.length];
        for(int i = c.length - 1; i >= 0; i--) {
            if(a[i] == 0) {
                used[i] = true;
                count2++;
            } else {
                try {
                    if(!used[i + 1]) {
                        String s = "" + a[i + 1];
                        if(s.equals("12") || s.equals("21")) {
                            count2++;
                            used[i] = used[i + 1] = true;
                        }
                    }
                    if(!used[i + 1] && !used[i + 2]) {
                        String s = "" + a[i] + a[i + 1] + a[i + 2];
                        if(s.equals("111")) {
                            count2++;
                            used[i] = used[i + 1] = used[i + 2] = true;
                        }
                    }
                } catch(Exception ex) {}
            }
        }

        System.out.println(Math.max(count, count2));
    }

    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        }
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }
}


import java.util.*;
import java.io.*;
/**
 *
 * @author User
 */
public class JavaApplication37 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        String s=in.next();
        int a[]=new int[3];
        int c=0; int b;
        for (int i = 0; i < s.length(); i++) {
            b=s.charAt(i)-'0';
            if(b%3==0){
                c++;
            }
            else{
                if(i+1<s.length()){
                if((s.charAt(i+1)-'0')%3==0){
                    c++; i++; continue;
                }
                else{
                    b=s.charAt(i)+s.charAt(i+1)-'0';
                    if(b%3==0){
                        c++;
                        i++; continue;
                    }
                    else{
                        if(i+2<s.length()){
                        if((s.charAt(i+2)-'0')%3==0){
                            c++; i+=2; continue;
                        }
                        else {
                                c++;
                                i+=2; continue;
                            }
                        }
                    }
                    }
                }
            }
            }
        out.print(c);
        out.flush();
        }

}


import java.util.*;
import java.io.*;
/**
 *
 * @author User
 */
public class JavaApplication37 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        String s=in.next();
        int a[]=new int[3];
        int c=0; int b;
        for (int i = 0; i < s.length(); i++) {
            b=s.charAt(i)-'0';
            if(b%3==0){
                c++;
            }
            else{
                if(i+1<s.length()){
                if((s.charAt(i+1)-'0')%3==0){
                    c++; i++; continue;
                }
                else{
                    b=s.charAt(i)+s.charAt(i+1)-'0';
                    if(b%3==0){
                        c++;
                        i++; continue;
                    }
                    else{
                        if(i+2<s.length()){
                            c++; i+=2; continue;
                        }

                    }
                    }
                }
            }
            }
        out.print(c);
        out.flush();
        }

}