//#include<iostream>

int main(){

	int n,m;
	scanf("%d %d",&n,&m);

	int mat[n][m];
	int b[n][m];
	int count = 0;
	int sum = 0;
	for(int i = 0;i < n;i++){
		for(int j = 0;j < m;j++){
			scanf("%d",&mat[i][j]);
			count += mat[i][j];
			b[i][j] = 0;
		}
	}

	int resu[3000][2] ;

	int k = 0;
	for(int i = 0;i < n-1;i++){
		for(int j = 0;j < m-1;j++){
			if(mat[i][j] == 1 && mat[i+1][j] == 1 && mat[i][j+1] == 1 && mat[i+1][j+1] == 1){
				resu[k][0] = i;
				resu[k][1] = j;
				k++;
				b[i][j]++;
				b[i+1][j]++;
				b[i][j+1]++;
				b[i+1][j+1]++;

				//printf("%d %d %d %d\n",b[i][j],b[i][j+1],b[i+1][j],b[i+1][j+1]);
			}
		}
	}
	sum += k*4;
	int over = 0;
	for(int i = 0;i < n;i++){
		for(int j = 0;j < m;j++){
		//	printf("%d ",b[i][j]);
			if(b[i][j] > 1){
				over += b[i][j] - 1;
			}
		}printf("\n");
	}

	//printf("%d %d %d ",count ,over ,sum);
	if(count == 0){
		printf("0\n");
		return 0;
	}
	if(k*4 - over != count){
		printf("-1\n");
		return 0;
	}

	printf("%d\n",k);
	for(int i = 0;i < k;i++){
		printf("%d %d\n",resu[i][0]+1,resu[i][1]+1);
	}

}
//��5�δ���ԭ�����������С����
//��9�δ���ԭ���ж��߼����󣬵��³��ִ���K��= 0 ���� �����޷�ת����B�����
//���󣺸�ֵʹ�� b[n][m] = {0} ����  Ӧ��ѭ����ֵ


#include<stdio.h>
#include<stdlib.h>

int main(){
	int n, m;
	int pos[5000];
	int k = 0;
	int *matrix1, *matrix2;
	int not_equal = 0;

	scanf("%d %d", &n, &m);
	matrix1 = malloc(sizeof(int) * n * m);
	matrix2 = calloc(m * n, sizeof(int));

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			scanf("%d", matrix1 + (i * m) + j);
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m - 1; j++){
			if(*(matrix1 + (i * m) + j) &&
				*(matrix1 + (i * m) + j + 1) &&
				*(matrix1 + ((i + 1) * m) + j) &&
				*(matrix1 + ((i + 1) * m) + j + 1)){

				*(matrix2 + (i * m) + j) = 1;
				*(matrix2 + (i * m) + j + 1) = 1;
				*(matrix2 + ((i + 1) * m) + j) = 1;
				*(matrix2 + ((i + 1) * m) + j + 1) = 1;

				pos[2 * k] = i + 1;
				pos[2 * k + 1] = j + 1;
				//j += 1;
				k += 1;
				printf("colored%d\n", k);
			}
		}
	}

	for(int i = 0; i < n; i += 2){
		for(int j = 0; j < m - 1; j++){
			not_equal += (*(matrix1 + (i * m) + j) != *(matrix2 + (i * m) + j));
		}
	}

	if(not_equal) printf("-1");
	else{
		printf("%d\n", k);
		for(int i = 0; i < k; i ++){
			printf("%d %d\n", pos[2 * i], pos[2 * i + 1]);
		}
	}

	free(matrix1);
	free(matrix2);

	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<stdlib.h>

int main(){
	int n, m;
	int pos[5000];
	int k = 0;
	int *matrix1, *matrix2;
	int not_equal = 0;

	scanf("%d %d", &n, &m);
	matrix1 = malloc(sizeof(int) * n * m);
	matrix2 = calloc(m * n, sizeof(int));

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			scanf("%d", matrix1 + (i * m) + j);
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(*(matrix1 + (i * m) + j) &&
				*(matrix1 + (i * m) + j + 1) &&
				*(matrix1 + ((i + 1) * m) + j) &&
				*(matrix1 + ((i + 1) * m) + j + 1)){

				*(matrix2 + (i * m) + j) = 1;
				*(matrix2 + (i * m) + j + 1) = 1;
				*(matrix2 + ((i + 1) * m) + j) = 1;
				*(matrix2 + ((i + 1) * m) + j + 1) = 1;

				pos[k] = i + 1;
				pos[k + 1] = j + 1;

				//j += 1;

				k += 2;
			}
		}
	}

	for(int i = 0; i < n; i += 2){
		for(int j = 0; j < m - 1; j++){
			not_equal += (*(matrix1 + (i * m) + j) != *(matrix2 + (i * m) + j));
		}
	}

	if(not_equal) printf("-1");
	else{
		printf("%d\n", k / 2);
		for(int i = 0; i < k; i += 2){
			printf("%d %d\n", pos[i], pos[i + 1]);
		}
	}

	free(matrix1);
	free(matrix2);

	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<stdlib.h>

int main(){
	int n, m;
	int pos[5000];
	int k = 0;
	int *matrix1, *matrix2;
	int not_equal = 0;

	scanf("%d %d", &n, &m);
	matrix1 = malloc(sizeof(int) * n * m);
	matrix2 = calloc(m * n, sizeof(int));

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			scanf("%d", matrix1 + (i * m) + j);
		}
	}

	for(int i = 0; i < n - 1; i++){
		for(int j = 0; j < m - 1; j++){
			if(*(matrix1 + (i * m) + j) &&
				*(matrix1 + (i * m) + j + 1) &&
				*(matrix1 + ((i + 1) * m) + j) &&
				*(matrix1 + ((i + 1) * m) + j + 1)){

				*(matrix2 + (i * m) + j) = 1;
				*(matrix2 + (i * m) + j + 1) = 1;
				*(matrix2 + ((i + 1) * m) + j) = 1;
				*(matrix2 + ((i + 1) * m) + j + 1) = 1;

				pos[2 * k] = i + 1;
				pos[2 * k + 1] = j + 1;
				//j += 1;
				k += 1;
			}
		}
	}

	for(int i = 0; i < n; i += 2){
		for(int j = 0; j < m - 1; j++){
			not_equal += (*(matrix1 + (i * m) + j) != *(matrix2 + (i * m) + j));
		}
	}

	if(not_equal) printf("-1");
	else{
		printf("%d\n", k);
		for(int i = 0; i < k; i ++){
			printf("%d %d\n", pos[2 * i], pos[2 * i + 1]);
		}
	}

	free(matrix1);
	free(matrix2);

	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<stdlib.h>

int main(){
	int n, m;
	int pos[5000];
	int k = 0;
	int *matrix1, *matrix2;
	int not_equal = 0;

	scanf("%d %d", &n, &m);
	matrix1 = malloc(sizeof(int) * n * m);
	matrix2 = calloc(m * n, sizeof(int));

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			scanf("%d", matrix1 + (i * m) + j);
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(*(matrix1 + (i * m) + j) &&
				*(matrix1 + (i * m) + j + 1) &&
				*(matrix1 + ((i + 1) * m) + j) &&
				*(matrix1 + ((i + 1) * m) + j + 1)){

				*(matrix2 + (i * m) + j) = 1;
				*(matrix2 + (i * m) + j + 1) = 1;
				*(matrix2 + ((i + 1) * m) + j) = 1;
				*(matrix2 + ((i + 1) * m) + j + 1) = 1;

				pos[2 * k] = i + 1;
				pos[2 * k + 1] = j + 1;
				//j += 1;
				k += 1;
			}
		}
	}

	for(int i = 0; i < n; i += 2){
		for(int j = 0; j < m - 1; j++){
			not_equal += (*(matrix1 + (i * m) + j) != *(matrix2 + (i * m) + j));
		}
	}

	if(not_equal) printf("-1");
	else{
		printf("%d\n", k);
		for(int i = 0; i < k; i ++){
			printf("%d %d\n", pos[2 * i], pos[2 * i + 1]);
		}
	}

	free(matrix1);
	free(matrix2);

	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<stdlib.h>

int main(){
	int n, m;
	int pos[5000];
	int k = 0;
	int *matrix1, *matrix2;
	int not_equal = 0;

	scanf("%d %d", &n, &m);
	matrix1 = malloc(sizeof(int) * n * m);
	matrix2 = calloc(m * n, sizeof(int));

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			scanf("%d", matrix1 + (i * m) + j);
		}
	}

	for(int i = 0; i < n - 1; i++){
		for(int j = 0; j < m - 1; j++){
			if(*(matrix1 + (i * m) + j) &&
				*(matrix1 + (i * m) + j + 1) &&
				*(matrix1 + ((i + 1) * m) + j) &&
				*(matrix1 + ((i + 1) * m) + j + 1)){

				*(matrix2 + (i * m) + j) = 1;
				*(matrix2 + (i * m) + j + 1) = 1;
				*(matrix2 + ((i + 1) * m) + j) = 1;
				*(matrix2 + ((i + 1) * m) + j + 1) = 1;

				pos[2 * k] = i + 1;
				pos[2 * k + 1] = j + 1;
				//j += 1;
				k += 1;
			}
		}
	}

	for(int i = 0; i < n; i += 1){
		for(int j = 0; j < m; j++){
			not_equal += (*(matrix1 + (i * m) + j) != *(matrix2 + (i * m) + j));
		}
	}

	if(not_equal) printf("-1");
	else{
		printf("%d\n", k);
		for(int i = 0; i < k; i ++){
			printf("%d %d\n", pos[2 * i], pos[2 * i + 1]);
		}
	}

	free(matrix1);
	free(matrix2);

	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<stdlib.h>

int main(){
	int n, m;
	int pos[5000];
	int k = 0;
	int *matrix1, *matrix2;
	int not_equal = 0;

	scanf("%d %d", &n, &m);
	matrix1 = malloc(sizeof(int) * n * m);
	matrix2 = calloc(m * n, sizeof(int));

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			scanf("%d", matrix1 + (i * m) + j);
		}
	}

	for(int i = 0; i < n - 1; i++){
		for(int j = 0; j < m - 1; j++){
			if(*(matrix1 + (i * m) + j) &&
				*(matrix1 + (i * m) + j + 1) &&
				*(matrix1 + ((i + 1) * m) + j) &&
				*(matrix1 + ((i + 1) * m) + j + 1)){

				*(matrix2 + (i * m) + j) = 1;
				*(matrix2 + (i * m) + j + 1) = 1;
				*(matrix2 + ((i + 1) * m) + j) = 1;
				*(matrix2 + ((i + 1) * m) + j + 1) = 1;

				pos[2 * k] = i + 1;
				pos[2 * k + 1] = j + 1;
				//j += 1;
				k += 1;
			}
		}
	}

	for(int i = 0; i < n; i += 2){
		for(int j = 0; j < m; j++){
			not_equal += (*(matrix1 + (i * m) + j) != *(matrix2 + (i * m) + j));
		}
	}

	if(not_equal) printf("-1");
	else{
		printf("%d\n", k);
		for(int i = 0; i < k; i ++){
			printf("%d %d\n", pos[2 * i], pos[2 * i + 1]);
		}
	}

	free(matrix1);
	free(matrix2);

	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<stdlib.h>

int main(){
	int n, m;
	int pos[5000];
	int k = 0;
	int *matrix1, *matrix2;
	int not_equal = 0;

	scanf("%d %d", &n, &m);
	matrix1 = malloc(sizeof(int) * n * m);
	matrix2 = calloc(m * n, sizeof(int));

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			scanf("%d", matrix1 + (i * m) + j);
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m - 1; j++){
			if(*(matrix1 + (i * m) + j) &&
				*(matrix1 + (i * m) + j + 1) &&
				*(matrix1 + ((i + 1) * m) + j) &&
				*(matrix1 + ((i + 1) * m) + j + 1)){

				*(matrix2 + (i * m) + j) = 1;
				*(matrix2 + (i * m) + j + 1) = 1;
				*(matrix2 + ((i + 1) * m) + j) = 1;
				*(matrix2 + ((i + 1) * m) + j + 1) = 1;

				pos[2 * k] = i + 1;
				pos[2 * k + 1] = j + 1;
				//j += 1;
				k += 1;
			}
		}
	}

	for(int i = 0; i < n; i += 2){
		for(int j = 0; j < m - 1; j++){
			not_equal += (*(matrix1 + (i * m) + j) != *(matrix2 + (i * m) + j));
		}
	}

	if(not_equal) printf("-1");
	else{
		printf("%d\n", k);
		for(int i = 0; i < k; i ++){
			printf("%d %d\n", pos[2 * i], pos[2 * i + 1]);
		}
	}

	free(matrix1);
	free(matrix2);

	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<stdlib.h>

int main(){
	int n, m;
	int pos[5000];
	int k = 0;
	int *matrix1, *matrix2;
	int not_equal = 0;

	scanf("%d %d", &n, &m);
	matrix1 = malloc(sizeof(int) * n * m);
	matrix2 = calloc(m * n, sizeof(int));

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			scanf("%d", matrix1 + (i * m) + j);
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(*(matrix1 + (i * m) + j) &&
				*(matrix1 + (i * m) + j + 1) &&
				*(matrix1 + ((i + 1) * m) + j) &&
				*(matrix1 + ((i + 1) * m) + j + 1)){

				*(matrix2 + (i * m) + j) = 1;
				*(matrix2 + (i * m) + j + 1) = 1;
				*(matrix2 + ((i + 1) * m) + j) = 1;
				*(matrix2 + ((i + 1) * m) + j + 1) = 1;

				pos[2 * k] = i + 1;
				pos[2 * k + 1] = j + 1;

				//j += 1;

				k += 1;
			}
		}
	}

	for(int i = 0; i < n; i += 2){
		for(int j = 0; j < m - 1; j++){
			not_equal += (*(matrix1 + (i * m) + j) != *(matrix2 + (i * m) + j));
		}
	}

	if(not_equal) printf("-1");
	else{
		printf("%d\n", k / 2);
		for(int i = 0; i < k; i ++){
			printf("%d %d\n", pos[2 * i], pos[2 * i + 1]);
		}
	}

	free(matrix1);
	free(matrix2);

	return EXIT_SUCCESS;
}


/* practice with Dukkha */
#include <stdio.h>

#define N	50
#define M	50

int main() {
	static int aa[N][M], bb[N][M], ii[N * M], jj[N * M];
	int n, m, i, j, cnt;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			scanf("%d", &aa[i][j]);
	cnt = 0;
	for (i = 1; i < n; i++)
		for (j = 1; j < m; j++)
			if (aa[i - 1][j - 1] && aa[i - 1][j] && aa[i][j - 1] && aa[i][j]) {
				bb[i - 1][j - 1] = bb[i - 1][j] = bb[i][j - 1] = bb[i][j] = 1;
				ii[cnt] = i, jj[cnt] = j;
				cnt++;
			}
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			if (aa[i][j] != bb[i][j]) {
				printf("-1\n");
				return 0;
			}
	printf("%d\n", cnt);
	while (cnt--)
		printf("%d %d\n", ii[cnt], jj[cnt]);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m;scanf("%d",&n);scanf("%d",&m);
    int impossible=0;
    int arr[n][m];
    int res[n][m];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            int in;
            scanf("%d", &in);
            arr[i][j] = in;
            res[i][j] = 0;
        }
    }

    int solution[(n-1)*(m-1)][2];
    int cnt = 0;
    for(int i = 0; i < n-1; i++){
        for(int j = 0; j < m-1; j++){
            if(arr[i][j] != 0 && arr[i+1][j] != 0 && arr[i][j+1] != 0 && arr[i+1][j+1] != 0){
                res[i][j] = 1;
                res[i+1][j] = 1;
                res[i][j+1] = 1;
                res[i+1][j+1] = 1;
                solution[cnt][0]=i+1;
                solution[cnt++][1]=j+1;
            }
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(arr[i][j] != res[i][j])
                impossible = 1;
        }
    }
    if(impossible == 1)
        printf("%d", -1);
    else{
        printf("%d\n", cnt);
        for(int i = 0 ; i<cnt;i++){
            printf("%d %d\n", solution[i][0], solution[i][1]);
        }

    }
}


#include <iostream>
#include <vector>

using namespace std;

typedef struct {
	int x, y;
} Point;

Point createNewPoint(int xx, int yy) {
	Point p;
	p.x = xx;
	p.y = yy;
	return p;
}

int main() {
	int m, n;
	cin >> m >> n;

	int** mat = new int* [m];
	for (int i = 0; i < m; i++)
		mat[i] = new int[n];

	for (int i = 0; i < m; i++)
		for (int j = 0; j < n; j++)
			cin >> mat[i][j];

	vector<Point> vec;

	bool can = true;

	for (int i = 0; i < m - 1; i++) {
		for (int j = 0; j < n - 1; j++) {
			if (mat[i][j] == 1 && mat[i + 1][j] == 1 && mat[i][j + 1] == 1 && mat[i + 1][j + 1]) {
				mat[i][j] = -1;
				mat[i + 1][j] = -1;
				mat[i][j + 1] = -1;
				mat[i + 1][j + 1] = -1;
				vec.push_back(createNewPoint(i+1, j+1));
			}
			else if (mat[i][j] == -1 && mat[i + 1][j] == -1) {
				if (mat[i][j + 1] == 1 && mat[i + 1][j + 1] == 1) {
					if (i + 2 == m || (mat[i][j + 2] != 1 || mat[i + 1][j + 2] != 1)) {
						mat[i][j] = -1;
						mat[i + 1][j] = -1;
						mat[i][j + 1] = -1;
						mat[i + 1][j + 1] = -1;
						vec.push_back(createNewPoint(i+1, j+1));
					}
				}
			}
			else if (mat[i][j] == -1 && mat[i][j + 1] == -1) {
				if (mat[i + 1][j] == 1 && mat[i + 1][j + 1] == 1) {
					if (j + 2 == n || (mat[i + 2][j] != 1 || mat[i + 2][j + 1] != 1)) {
						mat[i][j] = -1;
						mat[i + 1][j] = -1;
						mat[i][j + 1] = -1;
						mat[i + 1][j + 1] = -1;
						vec.push_back(createNewPoint(i+1, j+1));
					}
				}
			}
			else if (mat[i][j] != 0 || mat[i + 1][j] != 0 || mat[i][j + 1] != 0 || mat[i + 1][j + 1] != 0) {
				can = false;
				i = m;
				break;
			}
		}
	}

	if (!can)
		cout << -1;
	else {
		if (vec.size() == 0) {
			cout << 0;
		}
		else {
			cout << vec[0].x << " " << vec[0].y;
			for (int i = 1; i < vec.size(); i++)
				cout << endl << vec[i].x << " " << vec[i].y;
		}
	}

	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m;scanf("%d",&n);scanf("%d",&m);
    int impossible=0;
    int arr[n][m];
    int res[n][m];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            int in;
            scanf("%d", &in);
            arr[i][j] = in;
            res[i][j] = 0;
        }
    }
    int solution[(n-1)*(m-1)][2];
    int cnt = 0;
    for(int i = 0; i < n-1; i++){
        for(int j = 0; j < m-1; j++){
            if(arr[i][j] != 0 && arr[i+1][j] != 0 && arr[i][j+1] != 0 && arr[i+1][j+1] != 0){
                res[i][j] = 1;
                res[i+1][j] = 1;
                res[i][j+1] = 1;
                res[i+1][j+1] = 1;
                solution[cnt][0]=i+1;
                solution[cnt++][1]=j+1;
            }
        }
    }
    for(int i = 0; i < n-1; i++){
        for(int j = 0; j < m-1; j++){
            if(arr[i][j] != res[i][j])
                impossible = 1;
        }
    }
    if(impossible == 1)
        printf("%d", -1);
    else{
        printf("%d\n", cnt);
        for(int i = 0 ; i<cnt;i++){
            printf("%d %d\n", solution[i][0], solution[i][1]);
        }

    }
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m;scanf("%d",&n);scanf("%d",&m);
    int impossible=0;
    if(n<2 || m<2 || (n==2 && m==2))
        impossible = 1;
    int arr[n][m];
    int res[n][m];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            int in;
            scanf("%d", &in);
            arr[i][j] = in;
            res[i][j] = 0;
            if(in==0){
                if(i==1 || j==1 || n-i==2 || m-j==2)
                    impossible = 1;
            }
        }
    }
    int solution[(n-1)*(m-1)][2];
    int cnt = 0;
    for(int i = 0; i < n-1; i++){
        for(int j = 0; j < m-1; j++){
            if(arr[i][j] != 0 && arr[i+1][j] != 0 && arr[i][j+1] != 0 && arr[i+1][j+1] != 0){
                res[i][j] = 1;
                res[i+1][j] = 1;
                res[i][j+1] = 1;
                res[i+1][j+1] = 1;
                solution[cnt][0]=i+1;
                solution[cnt++][1]=j+1;
            }
        }
    }
    for(int i = 0; i < n-1; i++){
        for(int j = 0; j < m-1; j++){
            if(arr[i][j] != res[i][j])
                impossible = 1;
        }
    }
    if(impossible == 1)
        printf("%d", -1);
    else{
        printf("%d\n", cnt);
        for(int i = 0 ; i<cnt;i++){
            printf("%d %d\n", solution[i][0], solution[i][1]);
        }

    }
    return main();
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,m;scanf("%d",&n);scanf("%d",&m);
    int impossible=0;
    if(n<2 || m<2 || (n==2 && m==2))
        impossible = 1;
    int arr[n][m];
    int res[n][m];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            int in;
            scanf("%d", &in);
            arr[i][j] = in;
            res[i][j] = 0;
            if(in==0){
                if(i==1 || j==1 || n-i==2 || m-j==2)
                    impossible = 1;
            }
        }
    }
    int solution[(n-1)*(m-1)][2];
    int cnt = 0;
    for(int i = 0; i < n-1; i++){
        for(int j = 0; j < m-1; j++){
            if(arr[i][j] != 0 && arr[i+1][j] != 0 && arr[i][j+1] != 0 && arr[i+1][j+1] != 0){
                res[i][j] = 1;
                res[i+1][j] = 1;
                res[i][j+1] = 1;
                res[i+1][j+1] = 1;
                solution[cnt][0]=i+1;
                solution[cnt++][1]=j+1;
            }
        }
    }
    for(int i = 0; i < n-1; i++){
        for(int j = 0; j < m-1; j++){
            if(arr[i][j] != res[i][j])
                impossible = 1;
        }
    }
    if(impossible == 1)
        printf("%d", -1);
    else{
        printf("%d\n", cnt);
        for(int i = 0 ; i<cnt;i++){
            printf("%d %d\n", solution[i][0], solution[i][1]);
        }

    }
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{ char line[1000000];
  gets(line);
  int c=strlen(line);
  int m,i,element1,element2,b,e,j,x=0;

  scanf("%d",&m);

  for(i=0; i<m; i++)
  {
      scanf("%d%d",&element1,&element2);

     for(j=element1; j<element2; j++)
            {
    if(line[j-1]+line[j]==70 || line[j-1]+line[j]==92)
        {
            x++;
        }
            }
    printf("%d\n",x);
    x=0;
    }

    return 0;
}


#include <stdio.h>
#include <stdio.h>
#define N 300005
int main(int argc, char *argv[])
{
		char ch[N];
		int n,i,a[N]={0},pref[N]={0},sum=0,l,r;
		scanf("%s",ch);
		scanf("%d",&n);
		for(i=0;i<strlen(ch)-1;i++)
			if(ch[i]==ch[i+1]){
				a[i]++;
				a[i+1]++;
			}
		for(i=1;i<=strlen(ch);i++){
			sum+=a[i-1];
			pref[i]=sum;
		//	printf("%d\n",pref[i]);
		}
		for(i=0;i<n;i++){
			scanf("%d%d",&l,&r);
			printf("%d\n",(pref[r]-pref[l-1])/2);
		}
}


#include <stdio.h>
#include <stdio.h>
#define N 300005
int main(int argc, char *argv[])
{
		char ch[N];
		int n,i,a[N]={0},pref[N]={0},sum=0,l,r;
		scanf("%s",ch);
		scanf("%d",&n);
		for(i=0;i<strlen(ch)-1;i++)
			if(ch[i]==ch[i+1]){
				a[i]++;
				a[i+1]++;
			}
		for(i=1;i<=n;i++){
			sum+=a[i-1];
			pref[i]=sum;
		}
		for(i=0;i<n;i++){
			scanf("%d%d",&l,&r);
			printf("%d\n",(pref[l-1]-pref[r])/2);
		}
}


#include <stdio.h>
#include <stdio.h>
#define N 300005
int main(int argc, char *argv[])
{
		char ch[N];
		int n,i,a[N]={0},pref[N]={0},sum=0,l,r;
		scanf("%s",ch);
		scanf("%d",&n);
		for(i=0;i<strlen(ch)-1;i++)
			if(ch[i]==ch[i+1]){
				a[i]++;
			}
		for(i=1;i<=strlen(ch);i++){
			sum+=a[i-1];
			pref[i]=sum;
		//	printf("%d\n",pref[i]);
		}
		for(i=0;i<n;i++){
			scanf("%d%d",&l,&r);
			printf("%d\n",pref[r-1]-pref[l-1]);
		}
}


#include<stdio.h>
#include<string.h>

int main()
{
    char x[100001],y[100001]={};

    gets(x);

    int t,n=strlen(x),a,b,i;

    for(i=0;i<n-1;i++)
     if(x[i]==x[i+1]) y[i+1]++;

     scanf("%d",&t);

     while(t--)
     {
         int c=0;

         scanf("%d %d",&a,&b);

         for(i=a;i<b;i++)
            c+=y[i];

         printf("%d\n",c);
     }
}


#include<stdio.h>
#include<string.h>

int main()
{
    char x[100001];

    gets(x);

    int t,n=strlen(x),a,b,i;

     scanf("%d",&t);

     while(t--)
     {
         int c=0;

         scanf("%d %d",&a,&b);

         for(i=a;i<b;i++)
           if(x[i]==x[i-1]) c++;

         printf("%d\n",c);
     }
}


#include<stdio.h>
int main(){
    int n,m,i,c,ans=0,j,k;
    char str[100000];
    gets(str);
    scanf("%d", &m);
    int a,b;
    for(i=0;i<m;i++){
        c=0;
        scanf("%d %d", &a, &b);
        for(j=a-1;j<b-1;j++){
            if(str[j]==str[j+1])    c++;
        }
        printf("%d\n", c);
    }

}


#include<stdio.h>
#include<string.h>
int main(){
    int n,m,i,c=0,ans=0,j,k;
    char str[100000];
    gets(str);
    scanf("%d", &m);
    int x[m];
    for(i=0;str[i];i++){
        if(str[i]==str[i+1]){
            c++;
            x[i]=c;
        }
        else x[i]=x[i-1];
    }
    /*for(i=0;i<strlen(str);i++)  printf("%d",x[i]);
    return 0;*/
    int a,b;
    for(i=0;i<m;i++){
        c=0;
        scanf("%d %d", &a, &b);
        printf("%d\n", x[b-2]-((a-2>=0)?x[a-2]:0));
    }

}


#include<stdio.h>
#include<string.h>
int main(){
    int n,m,i,c=0,ans=0,j,k;
    char str[100000];
    gets(str);
    scanf("%d", &m);
    int x[m];
    for(i=0;str[i];i++){
        if(str[i]==str[i+1]){
            c++;
            x[i]=c;
        }
        else x[i]=c;
    }
    /*for(i=0;i<strlen(str);i++)  printf("%d",x[i]);
    return 0;*/
    int a,b;
    for(i=0;i<m;i++){
        c=0;
        scanf("%d %d", &a, &b);
        if(a-2>=0)  printf("%d\n", x[b-2]-x[a-2]);
        else printf("%d\n",x[b-2]);
    }

}


#include<stdio.h>
#include<string.h>
int main()
{       char ch[100010];
        long long int l, r, i, j, N[100011], q, ans,  ct=0;
        scanf("%s", ch);
        long long int x=strlen(ch);
        scanf("%lld", &q);
        N[0]=0;
        for(i=1; i<x; i++)
                {       if(ch[i]==ch[i-1])
                                N[i]=N[i-1]+1;
                        else    N[i]=N[i-1];
                }
        for(i=0; i<q; i++)
        {       scanf("%lld %lld", &l, &r);
                ans=N[r-1]-N[l-1];
                printf("%lld\n", ans);
        }
        return 0;
}


#include<stdio.h>
#include<string.h>
void main()
{	char ch[100010];
	long long int l, r, i, j, N[100011], q, ans,  ct=0;
	scanf("%s", ch);
	long long int x=strlen(ch);
	scanf("%lld", &q);
	N[0]=0;
	for(i=1; i<x; i++)
		{	if(ch[i]==ch[i-1])
				N[i]=N[i-1]+1;
			else	N[i]=N[i-1];
		}
	for(i=0; i<q; i++)
	{	scanf("%lld%lld", &l, &r);
		ans=N[r-1]-N[l-1];
		printf("%lld\n", ans);
	}
}


#include<stdio.h>
#include<string.h>
void main()
{	char ch[100010];
	long long int l, r, i, j, N[100011], q, ans,  ct=0;
	scanf("%s", ch);
	long long int x=strlen(ch);
	scanf("%lld", &q);
	N[0]=0;
	for(i=1; i<x; i++)
		{	if(ch[i]==ch[i-1])
				N[i]=N[i-1]+1;
			else	N[i]=N[i-1];
		}
	for(i=0; i<q; i++)
	{	scanf("%lld %lld", &l, &r);
		ans=N[r-1]-N[l-1];
		printf("%lld\n", ans);
	}
}


#include<stdio.h>
#include<string.h>
void main()
{	char ch[100010];
	long long int l, r, i, j, N[100010], q, ans,  ct=0;
	scanf("%s", ch);
	int x=strlen(ch);
	scanf("%lld", &q);
	N[0]=0;
	for(i=1; i<x; i++)
		{	if(ch[i]==ch[i-1])
				N[i]=N[i-1]+1;
			else	N[i]=N[i-1];
		}
	for(i=0; i<q; i++)
	{	scanf("%lld%lld", &l, &r);
		ans=N[r-1]-N[l-1];
		printf("%lld\n", ans);
	}
}


#include<stdio.h>
#include<string.h>
int dp[200000];
int main()
{
	char arr[200001];
	int m;
	int arr1[100001];
	int arr2[100001];

	scanf("%s",arr);
	scanf("%d",&m);
	for(int i=0;i<m;i++)
	{
		scanf("%d %d",&arr1[i],&arr2[i]);

	}
	int k=strlen(arr);
//	printf("%d",k);

	for(int i=0;i<k-1;i++)
	{
		if(i==0)
		{
			if(arr[i]==arr[i+1])
				dp[i]=1;
			else
				dp[i]=0;
		}
	   else	if(arr[i]==arr[i+1])
			dp[i]=dp[i-1]+1;
		else
		dp[i]=dp[i-1];
	}
	dp[k-1]=dp[k-2];

	for(int i=0;i<m;i++)
	{
		printf("%d\n", dp[arr2[i]-2]-dp[arr1[i]-2]);
	}

}


#include<stdio.h>
#include <string.h>
#include <math.h>

int main(){
    char str[100001];
    int str2[100001]={0};
    scanf("%s",str);
    int len=strlen(str),l,r;
    for(int i=0;i<strlen(str)-1;i++)
    {
       if(str[i]==str[i+1])
       {
           str2[i+1]++;
       }
    }
    for(int i=1;i<len;i++)
    {
        str2[i]+=str2[i-1];
    }
    int tests;
    scanf("%d",&tests);
    for(int i=0;i<tests;i++)
    {
        scanf("%d %d",&l,&r);
        if(l==1)
        {
            printf("%d\n",str2[r-1]);
        }
        else
        {
            printf("%d\n",(str2[r-1]-str2[l-1]));
        }
    }

}


#include<stdio.h>
#include <string.h>
#include <math.h>

int main(){
    char str[100001];
    int str2[100001]={0};
    scanf("%s",str);
    int l,r;
    for(int i=0;i<100001;i++)
    {
        if(str[i+1]=='\0')
        {
            break;
        }
       if(str[i]==str[i+1])
       {
           str2[i+1]++;
       }

    }
    for(int i=1;i<100001;i++)
    {
        str2[i]+=str2[i-1];
    }
    int tests;
    scanf("%d",&tests);
    while(tests--) {
        scanf("%d %d", &l, &r);
        printf("%d\n", (str2[r - 1] - str2[l - 1]));
    }

}


#include<stdio.h>
#include <string.h>
int main(){
    char str[100001];
    scanf("%s",str);
    int tests=0,i,l,r, cnt=0;
    scanf("%d",&tests);

    for(i=0;i<tests;i++)
    {
       scanf("%d %d",&l,&r);
       for(int j=(r-1);j>(l-1);j--)
       {
           if(str[j]==str[j-1])
           {
               cnt++;
           }
       }
       printf("%d\n",cnt);
       cnt=0;
    }

}


#include<stdio.h>
#include <string.h>
#include <math.h>

int main(){
    char str[100001];
    int str2[100001]={0};
    scanf("%s",str);
    int len=strlen(str),l,r;
    for(int i=0;i<strlen(str)-1;i++)
    {
       if(str[i]==str[i+1])
       {
           str2[i+1]++;
       }
    }
    for(int i=1;i<len;i++)
    {
        str2[i]+=str2[i-1];
    }
    int tests;
    scanf("%d",&tests);
    while(tests--) {
        scanf("%d %d", &l, &r);
        printf("%d\n", (str2[r - 1] - str2[l - 1]));
    }

}


#include <stdio.h>

int main() {
	char str[100005];
	//Input the string with length n
	scanf("%s", &str);
	int dp[100005];
	//memset(dp, 0, sizeof(int)*100005);
	dp[0] = 0;
	int i = 1;
	while (str[i] != 0) {
		if (str[i] == str[i-1]) dp[i] = dp[i-1]+1;
		else dp[i] = dp[i-1];
		i++;
	}

	int m,l,r;
	scanf("%d", &m);

	for (i = 0; i < m; i++) {
		scanf("%d %d", &l, &r);
		printf("%d\n", dp[r-1] - dp[l-1]);
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#define rep(i,a,b) for (long i=a; i<b; i++)
void generate(char *s, int *arr, long length) {
    rep (i,0,length-1) {
        if (s[i]==s[i+1]) {
            arr[i]=1;
        } else {
            arr[i]=0;
        }
    }
    arr[length-1]=0;
}
void accumulate(int *arro, long *arrt, long length) {
    arrt[0]=0;
    rep (i,1,length) arrt[i]=arrt[i-1]+arro[i-1];
}
int process(char *s, long m) {
    long len=strlen(s),l,r;
    int fill[len];
    long acc[len];
    generate(s,fill,len);
    accumulate(fill,acc,len);
    rep (i,0,m) {
        scanf("%ld %ld\n",&l,&r);
        printf("%ld\n",acc[r-1]-acc[l-1]);
    }
}
int main() {
    char str[100001];
    long queries;
    scanf("%s\n%ld\n",str,&queries);
    process(str,queries);
    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int L, D, i, j, N, dp[100000] = {0};
    char arr[100001];
    scanf("%s", arr);

        for (i = 0; i < strlen(arr) - 1; i++){
            if (i == 0 && arr[i] == arr[i + 1]) dp[0] = 1;
            else if (arr[i] == arr[i + 1]) dp[i] = dp[i - 1] + 1;
            else if (i != 0) dp[i] = dp[i - 1];
            printf("dp[%d] is %d\n", i , dp[i]);
        }
        dp[i] = dp[i - 1];
        printf("dp[%d] is %d\n", i, dp[i]);
    arr[i + 1] = 'a';
    scanf("%d", &N);
    int sol;
    for (i = 0; i < N; i++) {
        scanf("%d %d", &L, &D);
        if (arr[L - 1] == arr[L] && arr[D - 1] == arr[D] || arr[L - 1] != arr[L] && arr[D - 1] != arr[D]) sol = dp[D - 1] - dp[L - 1];
        else if (arr[L - 1] == arr[L] && arr[D - 1] != arr[D]) sol = dp[D - 1] - dp[L - 1] + 1;
        else sol = dp[D - 1] - dp[L - 1] - 1;

        printf("%d\n", sol);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int L, D, i, j, N, dp[100000] = {0};
    char arr[100000];
    scanf("%s", arr);

        for (i = 0; i < strlen(arr) - 1; i++){
            if (i == 0 && arr[i] == arr[i + 1]) dp[0] = 1;
            else if (arr[i] == arr[i + 1]) dp[i] = dp[i - 1] + 1;
            else if (i != 0) dp[i] = dp[i - 1];
            //printf("dp[%d] is %d\n", i , dp[i]);
    }
    if (arr[i - 1] == arr[i])
        dp[i] = dp[i - 1] + 1;
    else
        dp[i] = dp[i - 1];
        //printf("dp[%d] is %d\n", i, dp[i]);

    scanf("%d", &N);
    for (i = 0; i < N; i++) {
        scanf("%d %d", &L, &D);
        printf("%d\n", dp[D - 1] - dp[L - 1]);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int L, D, i, j, N, dp[100000] = {0};
    char arr[100000];
    scanf("%s", arr);

        for (i = 0; i < strlen(arr) - 1; i++){
            if (i == 0 && arr[i] == arr[i + 1]) dp[0] = 1;
            else if (arr[i] == arr[i + 1]) dp[i] = dp[i - 1] + 1;
            else if (i != 0) dp[i] = dp[i - 1];
           // printf("dp[%d] is %d\n", i , dp[i]);
        }
        dp[i] = dp[i - 1];
        //printf("dp[%d] is %d\n", i, dp[i]);

    scanf("%d", &N);
    for (i = 0; i < N; i++) {
        scanf("%d %d", &L, &D);
        if (D == strlen(arr)) printf("%d\n", dp[D - 1] - dp[L - 1] + 1);
        else printf("%d\n", dp[D - 1] - dp[L - 1]);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int L, D, i, j, N, dp[100000] = {0};
    char arr[100001];
    scanf("%s", arr);

        for (i = 0; i < strlen(arr) - 1; i++){
            if (i == 0 && arr[i] == arr[i + 1]) dp[0] = 1;
            else if (arr[i] == arr[i + 1]) dp[i] = dp[i - 1] + 1;
            else if (i != 0) dp[i] = dp[i - 1];
           // printf("dp[%d] is %d\n", i , dp[i]);
        }
        dp[i] = dp[i - 1];
       // printf("dp[%d] is %d\n", i, dp[i]);
    arr[i + 1] = 'a';
    scanf("%d", &N);
    int sol;
    for (i = 0; i < N; i++) {
        scanf("%d %d", &L, &D);
        if (arr[L - 1] == arr[L] && arr[D - 1] == arr[D]) sol = dp[D - 1] - dp[L - 1];
        else if (arr[L - 1] == arr[L] && arr[D - 1] != arr[D]) sol = dp[D - 1] - dp[L - 1] + 1;
        else if (arr[L - 1] != arr[L] && arr[D - 1] == arr[D]) sol = dp[D - 1] - dp[L - 1] - 1;
        else if (arr[L - 1] != arr[L] && arr[D - 1] != arr[D]) sol = dp[D - 1] - dp[L - 1];
        printf("%d\n", sol);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int L, D, i, j, N, dp[100000] = {0};
    char arr[100000];
    scanf("%s", arr);

        for (i = 0; i < strlen(arr) - 1; i++){
            if (i == 0 && arr[i] == arr[i + 1]) dp[0] = 1;
            else if (arr[i] == arr[i + 1]) dp[i] = dp[i - 1] + 1;
            else if (i != 0) dp[i] = dp[i - 1];
            printf("dp[%d] is %d\n", i , dp[i]);
    }
    if (arr[i - 1] == arr[i])
        dp[i] = dp[i - 1] + 1;
    else
        dp[i] = dp[i - 1];
        printf("dp[%d] is %d\n", i, dp[i]);

    scanf("%d", &N);
    for (i = 0; i < N; i++) {
        scanf("%d %d", &L, &D);
        printf("%d\n", dp[D - 1] - dp[L - 1]);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int L, D, i, j, N, dp[100000] = {0};
    char arr[100000];
    scanf("%s", arr);

        for (i = 0; i < strlen(arr) - 1; i++){
            if (i == 0 && arr[i] == arr[i + 1]) dp[0] = 1;
            else if (arr[i] == arr[i + 1]) dp[i] = dp[i - 1] + 1;
            else if (i != 0) dp[i] = dp[i - 1];
            //printf("dp[%d] is %d\n", i , dp[i]);
        }
        dp[i] = dp[i - 1];
        //printf("dp[%d] is %d\n", i, dp[i]);

    scanf("%d", &N);
    for (i = 0; i < N; i++) {
        scanf("%d %d", &L, &D);
        if (D == strlen(arr)) printf("%d\n", dp[D - 1] - dp[L - 1] + 1);
        else printf("%d\n", dp[D - 1] - dp[L - 1]);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int L, D, i, j, N, dp[100000] = {0};
    char arr[100000];
    scanf("%s", arr);

        for (i = 0; i < strlen(arr) - 1; i++){
            if (i == 0 && arr[i] == arr[i + 1]) dp[0] = 1;
            else if (arr[i] == arr[i + 1]) dp[i] = dp[i - 1] + 1;
            else if (i != 0) dp[i] = dp[i - 1];
            printf("dp[%d] is %d\n", i , dp[i]);
        }
        dp[i] = dp[i - 1];
        printf("dp[%d] is %d\n", i, dp[i]);

    scanf("%d", &N);
    for (i = 0; i < N; i++) {
        scanf("%d %d", &L, &D);
        if (D == strlen(arr)) printf("%d\n", dp[D - 1] - dp[L - 1] + 1);
        else printf("%d\n", dp[D - 1] - dp[L - 1]);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
    int L, D, i, j, N, dp[100000] = {0};
    char arr[100001];
    scanf("%s", arr);

        for (i = 0; i < strlen(arr) - 1; i++){
            if (i == 0 && arr[i] == arr[i + 1]) dp[0] = 1;
            else if (arr[i] == arr[i + 1]) dp[i] = dp[i - 1] + 1;
            else if (i != 0) dp[i] = dp[i - 1];
            printf("dp[%d] is %d\n", i , dp[i]);
        }
        dp[i] = dp[i - 1];
        printf("dp[%d] is %d\n", i, dp[i]);
    arr[i + 1] = 'a';
    scanf("%d", &N);
    int sol;
    for (i = 0; i < N; i++) {
        scanf("%d %d", &L, &D);
        if (arr[L - 1] == arr[L] && arr[D - 1] == arr[D]) sol = dp[D - 1] - dp[L - 1];
        else if (arr[L - 1] == arr[L] && arr[D - 1] != arr[D]) sol = dp[D - 1] - dp[L - 1] + 1;
        else if (arr[L - 1] != arr[L] && arr[D - 1] == arr[D]) sol = dp[D - 1] - dp[L - 1] - 1;
        else if (arr[L - 1] != arr[L] && arr[D - 1] != arr[D]) sol = dp[D - 1] - dp[L - 1];
        printf("%d\n", sol);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
typedef long long LL;

int main()
{
	char a[100005];
	char b[100005];

	scanf("%s",a);
	int len=strlen(a);
	b[0]=8;
	for(LL i=0;i<len;i++)
	{
		b[i+1]=a[i];
	}

	LL arr[len+1];
	arr[1]=0;
	for(LL i=2;i<=len;i++)
	{
		arr[i]=arr[i-1]+(b[i]==b[i-1]);
	}

	LL t,left,right;

	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld %lld",&left,&right);
		LL ans=arr[right]-arr[left];
		printf("%lld\n",ans);
	}
}


#include<stdio.h>
int main(){
    char str[100001];
    int DP[100000];
    scanf("%s",str);
    int count=0,m;
    int a,b;
    for(int i=1;str[i]!='\0';i++){
        if(str[i]==str[i-1]){
            count++;
        }
        DP[i]=count;
    }
    scanf("%d",&m);
    while(m--){
        scanf("%d%d",&a,&b);
        printf("%d\n",DP[b-1]-DP[a-1]);
    }
    return 0;
}


#include<stdio.h>
int main()
{
    char ch[1000000];
    scanf("%s",ch);
    int n;
    int a,b;
    scanf("%d",&n);
    int i,j,cnt=0;
    for(i=0;i<n;++i)
    {
        cnt=0;
        scanf("%d%d",&a,&b);
        for(j=a-1;j<b-1;++j)
        {
            if(ch[j]==ch[j+1])
            cnt++;
        }
        printf("%d\n",cnt);
    }
}


#include<stdio.h>
int main()
{
    char s[10000];
    scanf("%s",s);
    int k,j,m,c,a,b;
    scanf("%d",&m);
    for(j=0;j<m;j++)
    {
        c=0;
        scanf("%d %d",&a,&b);
        for(k=a;k<b;k++)
            if(s[k-1]==s[k])
            c++;
        printf("%d\n",c);
    }
    return 0;
}


#include<stdio.h>
void que(int , int, char*);
void que(int a,int b,char *p)
{
    int k,c=0;
    for(k=a;k<b;k++)
        if( *(p+k-1) == *(p+k))
            c++;
    printf("%d\n",c);
}
int main()
{
    char s[10000];
    scanf("%s",s);
    int j,m,a,b;
    scanf("%d",&m);
    for(j=0;j<m;j++)
    {
        scanf("%d %d",&a,&b);
        que(a,b,&s);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>

int main()
{
	char str[100000];
	scanf("%s",str);
	long long int q;
	scanf("%lld",&q);
	while(q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		int i;
		int count=0;
		for(i=l-1;i<r-1;i++)
		{
			if(str[i]==str[i+1])
				count++;
		}
		printf("%d\n",count);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>

int main()
{
	char str[2000001];
	scanf("%s",str);
	int n=strlen(str);
	int count[100000]={0};
	int i;
	int x=0;
	for(i=0;i<n-1;i++)
	{
		if(str[i]==str[i+1])
		{
			count[i]=x+1;
			x++;
		}
		else
		{
			count[i]=x;
		}
	}
	long long int q;
	scanf("%lld",&q);
	while(q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		int cn=count[r-2]-count[l-2];
		printf("%d\n",cn);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>

int main()
{
	char str[100000];
	scanf("%s",str);
	int n=strlen(str);
	int count[100000]={0};
	int i;
	int x=0;
	for(i=0;i<n-1;i++)
	{
		if(str[i]==str[i+1])
		{
			count[i]=x+1;
			x++;
		}
		else
		{
			count[i]=x;
		}
	}
	long long int q;
	scanf("%lld",&q);
	while(q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		int cn=count[r-2]-count[l-2];
		printf("%d\n",cn);
	}
	return 0;
}


/*  Bismillahir-Rahmanir-Rahim  */
/*
    Solved By:- MD. Hasibul Hossain Rezvi
    CSE 14th Batch
    Comilla University
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
#define ll long long int
#define db double
#define py printf("YES\n")
#define pn printf("NO\n")
#define nl printf("\n");

int main()
{
    int i,k,m,n,l,r,ci=0,cn=0;
    char in[100005],c;
    scanf("%s",&in);
    n=strlen(in);
    //in[n]=in[n-1];
    //printf("%d\n",n);
    /*for(i=0; i<n; i++)
    {
        if(in[i]=='.')
            ci++;
        else
            cn++;
    }
    if(ci>cn)
        c='.';
    else
        c='#';*/

    scanf("%d",&m);
    int out[m],j=m;
    while(m--)
    {
        k=0;
        scanf("%d%d",&l,&r);
        for(i=l; i<r; i++)
        {
            if(in[i]==in[i-1])
                k++;
        }
        //printf("%d\n",k);
        out[m]=k;
    }
    while(j--)
        printf("%d\n",out[j]);
    return 0;
}


/*  Bismillahir-Rahmanir-Rahim  */
/*
    Solved By:- MD. Hasibul Hossain Rezvi
    CSE 14th Batch
    Comilla University
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
#define ll long long int
#define db double
#define py printf("YES\n")
#define pn printf("NO\n")
#define nl printf("\n");

int main()
{
    int i,k,m,n,l,r,ci=0,cn=0;
    char in[100005],c;
    scanf("%s",&in);
    n=strlen(in);

    scanf("%d",&m);
    while(m--)
    {
        k=0;
        scanf("%d%d",&l,&r);
        for(i=l; i<r; i++)
        {
            if(in[i]==in[i-1])
                k++;
        }
        printf("%d\n",k);
    }

    return 0;
}


/*  Bismillahir-Rahmanir-Rahim  */
/*
    Solved By:- MD. Hasibul Hossain Rezvi
    CSE 14th Batch
    Comilla University
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
#define ll long long int
#define db double
#define py printf("YES\n")
#define pn printf("NO\n")
#define nl printf("\n");

int main()
{
    int i,k,m,n,l,r,ci=0,cn=0;
    char in[100005],c;
    scanf("%s",&in);
    n=strlen(in);
    //in[n]=in[n-1];
    //printf("%d\n",n);
    for(i=0; i<n; i++)
    {
        if(in[i]=='.')
            ci++;
        else
            cn++;
    }
    if(ci>cn)
        c='.';
    else
        c='#';

    scanf("%d",&m);
    int out[m],j=m;
    while(m--)
    {
        k=0;
        scanf("%d%d",&l,&r);
        for(i=l-1; i<r-1; i++)
        {
            if(in[i]==c)
                k++;
        }
        //printf("%d\n",k);
        out[m]=k;
    }
    while(j--)
        printf("%d\n",out[j]);
    return 0;
}


/*  Bismillahir-Rahmanir-Rahim  */
/*
    Solved By:- MD. Hasibul Hossain Rezvi
    CSE 14th Batch
    Comilla University
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
#define ll long long int
#define db double
#define py printf("YES\n")
#define pn printf("NO\n")
#define nl printf("\n");

int main()
{
    int i,k,m,n,l,r,ci=0,cn=0,a[100005];
    char in[100005],c;
    scanf("%s",&in);
    n=strlen(in);
    in[n]=in[n-1];
    scanf("%d",&m);
    a[0]=0;
    for(i=0; i<n; i++)
    {
        if(in[i]==in[i+1])
            a[i+1]=a[i]+1;
        else
            a[i+1]=a[i];
    }
    while (m--)
    {
        scanf("%d%d",&l,&r);
        printf("%d\n",a[r-1]-a[l-1]);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>

int main(){
    int length,n,l,r,count=0;
    char s[100000];
    gets(s);
    length=strlen(s);
    int arr[length-1];
    for(int i=0;i<length-1;++i){
        if(s[i]==s[i+1])
        count++;
        arr[i]=count;
    }
    scanf("%d",&n);
    while(n--){
        scanf("%d%d",&l,&r);
        if(l==1)
        printf("%d\n",arr[r-2]);
        else
        printf("%d\n",arr[r-2]-arr[l-2]);
    }
    return 0;
}


#include <stdio.h>

int main(){
    int n,l,r,count;
    char arr[100000];
    gets(arr);
    scanf("%d",&n);
    while(n--){
        count=0;
        scanf("%d%d",&l,&r);
        for(int i=l-1;i<r-1;i++){
            if(arr[i]==arr[i+1])
            count++;
        }
        printf("%d\n",count);
    }
    return 0;
}


#pragma GCC optimize("O2")
#include <stdio.h>
#include <string.h>
char data[100005];
int dp[100005];
int main()
{
    scanf("%s", data+1);
    data[0] = ' ';
    int len = strlen(data);
    dp[0] = 0;
    int i;
    for(i = 1; i < len; i++)
    {
        if(data[i] == data[i+1])
            dp[i] = dp[i-1] + 1;
        else
            dp[i] = dp[i-1];
    }
    dp[len] = dp[len-1];
    int q;
    scanf("%d", &q);
    while(q--)
    {
        int left, right;
        scanf("%d%d", &left, &right);
        printf("%d\n", dp[right-1] - dp[left-1]);
    }
}


#include <stdio.h>
#include <string.h>
char data[100005];
int dp[100005];
int main()
{
    scanf("%s", data+1);
    data[0] = ' ';
    int len = strlen(data);
    dp[0] = 0;
    for(int i = 1; i < len; i++)
    {
        if(data[i] == data[i+1])
            dp[i] = dp[i-1] + 1;
        else
            dp[i] = dp[i-1];
    }
    dp[len] = dp[len-1];
    int q;
    scanf("%d", &q);
    while(q--)
    {
        int left, right;
        scanf("%d%d", &left, &right);
        printf("%d\n", dp[right-1] - dp[left-1]);
    }
}


#include <stdio.h>
char data[100005];
int dp[100005];
int main()
{
    scanf("%s", data+1);
    data[0] = ' ';
//    int len = strlen(data);
    dp[0] = 0;
    int i;
    for(i = 1; data[i]; i++)
    {
        if(data[i] == data[i+1])
            dp[i] = dp[i-1] + 1;
        else
            dp[i] = dp[i-1];
    }
    //dp[len] = dp[len-1];
    dp[i-1] = dp[i-2];
    int q;
    scanf("%d", &q);
    while(q--)
    {
        int left, right;
        scanf("%d%d", &left, &right);
        printf("%d\n", dp[right-1] - dp[left-1]);
    }
}


#include <stdio.h>
char data[100005];
int dp[100005];
int main()
{
    scanf("%s", data+1);
    data[0] = ' ';
//    int len = strlen(data);
    dp[0] = 0;
    int i;
    for(i = 1; data[i]; i++)
    {
        dp[i] = (data[i] == data[i+1]) ? dp[i-1]+1 : dp[i-1];
/*        if(data[i] == data[i+1])
            dp[i] = dp[i-1] + 1;
        else
            dp[i] = dp[i-1];*/
    }
    //dp[len] = dp[len-1];
    dp[i-1] = dp[i-2];
    int q;
    scanf("%d", &q);
    while(q--)
    {
        int left, right;
        scanf("%d%d", &left, &right);
        printf("%d\n", dp[right-1] - dp[left-1]);
    }
}


#include <stdio.h>
char data[100005];
int dp[100005];
int main()
{
    scanf("%s", data+1);
    data[0] = ' ';
//    int len = strlen(data);
    dp[0] = 0;
    int i;
    for(i = 1; data[i]; i++)
    {
//        dp[i] = (data[i] == data[i+1]) ? dp[i-1]+1 : dp[i-1];
        dp[i] = dp[i-1] + (data[i] == data[i+1]);
/*        if(data[i] == data[i+1])
            dp[i] = dp[i-1] + 1;
        else
            dp[i] = dp[i-1];*/
    }
    //dp[len] = dp[len-1];
    dp[i-1] = dp[i-2];
    int q;
    scanf("%d", &q);
    while(q--)
    {
        int left, right;
        scanf("%d%d", &left, &right);
        printf("%d\n", dp[right-1] - dp[left-1]);
    }
}


#include <stdio.h>
#include <string.h>
#define MAX 100000

char string[MAX+1];

int main(){
	scanf("%s", string);
	//int len = strlen(string);

	int m;
	scanf("%d", &m);

	int l = 0, r = 0;
	int ans;
	while (m--){
		scanf("%d%d", &l, &r);
		ans = 0;
		for (int i = l-1; i < (r-1); i++){
			//check (si, s(i+1))
			if (string[i] == string[i+1]){
				ans++;
			}
		}
		printf("%d\n", ans);
	}

}


#include <stdio.h>
#include <string.h>
#define MAX 100000

char string[MAX+1];
int ans[MAX+1];

int main(){
	scanf("%s", string);
	int len = strlen(string);

	int count = 0;
	for (int i = 0 ; i < len-1; i++){		//loop: n times
		if (string[i] == string[i+1]){
			count++;
		}
		ans[i] = count;
		//printf("array:%d count:%d\n", ans[i], count);
	}

	int m;
	scanf("%d", &m);
	int l, r;
	for (int i = 0; i < m; i++){		//loop: m times
		scanf("%d%d", &l, &r);
		printf("%d\n", ans[(r-1)-1] - ans[(l-1)-1]);
	}
}


#include <stdio.h>
#include <string.h>
#define MAX 100000

char string[MAX+1];
int ans[MAX+1];

int main(){
	scanf("%s", string);
	int len = strlen(string);

	int count = 0;
	for (int i = 0 ; i < len-1; i++){		//loop: n times
		if (string[i] == string[i+1]){
			count++;
		}
		ans[i] = count;
		printf("array:%d count:%d\n", ans[i], count);
	}

	int m;
	scanf("%d", &m);
	int l, r;
	for (int i = 0; i < m; i++){		//loop: m times
		scanf("%d%d", &l, &r);
		printf("%d\n", ans[(r-1)-1] - ans[(l-1)-1]);
	}
}


#include<stdio.h>
#include<string.h>
int main()
{
    int m,i,p,len;
    char c[100005];
    scanf("%s",&c);
    getchar();
    len=strlen(c);
    scanf("%d",&m);
    for(i=0;i<m;i++){
            int count=0,j,a[100005],b[100005];
        scanf("%d %d",&a[i],&b[i]);
        for(j=a[i];j<b[i];j++){
            if(c[j-1]==c[j])
                count++;
        }
        printf("%d\n",count);
    }
}


#include "stdio.h"
#include "string.h"

int main() {
  char str[100005];
  scanf("%s",str);
  int len=strlen(str);
  int val[len-1];
  for(int i=0;i<len-1;i++){
    if(i==0){
        val[i]=(int)(str[i]==str[i+1]);
    }
    else{
      val[i]=val[i-1]+(int)(str[i]==str[i+1]);
    }
  }
  int n;
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    int l,r;
    scanf("%d %d",&l,&r);
    printf("%d\n",val[r-2]-val[l-1]+(int)(str[l-1]==str[l]));
  }

  return 0;
}


#include "stdio.h"
#include "string.h"

int main() {
  char str[100005];
  scanf("%s",str);
  int l=strlen(str);
  int val[l-1];
  for(int i=0;i<l-1;i++){
    val[i]=(int)(str[i]==str[i+1]);
  }
  int n;
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    int l,r;
    scanf("%d %d",&l,&r);
    int x=0;
    for(int j=l-1;j<r-1;j++){
      x+=val[j];
    }
    printf("%d\n",x);
  }

  return 0;
}


#include<stdio.h>
#include<string.h>
int main ()
{
	int m,n,l,r,i,count=0;
	char s[100000];
	scanf("%s",s);
	int a[100000]={0};
	//n is the length of the array of string
	n=strlen(s);
	scanf("%d",&m);
	for(i=0;i<n-1;i++)
	{
		if(s[i]==s[i+1])
			count++;
		a[i+1]=count;		//why i+1
	}
	int res=0;
	for(i=1;i<=m;i++)
	{
		//m is the number of cases
		scanf("%d%d",&l,&r);
		res=a[r-1]-a[l-1];
		printf("%d\n",res);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main ()
{
	//string with char only
	int m,n,l,r,i,count=0;
	char s[100000];
	scanf("%s",s);
	//n is the length of the array of string
	n=strlen(s);
	scanf("%d",&m);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&l,&r);
		while(l-1!=r-1)
		{
			//now here i have to do the string compare function
			if(s[l-1]==s[l])
				count++;
			l++;
		}
		printf("%d\n",count);
		count=0;
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main ()
{
	//string with char only
	int m,n,l,r,i,il,ir,count=0;
	char s[100000];
	scanf("%s",s);
	//n is the length of the array of string
	n=strlen(s);
	scanf("%d",&m);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&l,&r);
		il=l-1;
		ir=r-1;
		while(il!=ir)
		{
			//now here i have to do the string compare function
			if(s[il]==s[il+1])
				count++;
			il++;
		}
		printf("%d\n",count);
		count=0;
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main(){
    int arr[100005]={0},a,b,i,n,m,sum;
    char str[100005];
    scanf("%s",str);
    n=strlen(str);
    for(i=0;i<n-1;i++){
        if(str[i]==str[i+1]){
            arr[i]=1;
        }
    }
    scanf("%d",&m);
    while(m--){
        sum=0;
        scanf("%d %d",&a,&b);
        for(i=a-1;i<b-1;i++){
            sum+=arr[i];
        }
        printf("%d\n",sum);
    }

    return 0;
}


#include<stdio.h>
#include<string.h>
int main(){
    int arr[100005]={0},a,b,i,n,m,sum;
    char str[100005];
    scanf("%s",str);
    n=strlen(str);
    for(i=1;i<n;i++){
        if(str[i-1]==str[i]){
            arr[i]=arr[i-1]+1;
        }else{
            arr[i]=arr[i-1];
        }
    }

    scanf("%d",&m);
    while(m--){

        scanf("%d %d",&a,&b);

        printf("%d\n",arr[b-1]-arr[a-1]);
    }

    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	char a[100005];
	long len,l,r,i,m,b[100005],cnt=0;
	scanf("%s",a);
	scanf("%ld",&m);
	len=strlen(a);
	for(i=0;i<(len-1);i++)
	{
		if(a[i]==a[i+1])
			cnt++;
		b[i+1]=cnt;
	}
	while(m--)
	{
		scanf("%ld%ld",&l,&r);
		printf("%ld\n",b[r-1]-b[l-1]);
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#define MX 100005
int main()
{
	char s[MX];
	scanf("%s", s);
	int n = strlen(s), ps[MX] = {0}, j = 2, q;
	for (int i = 0; i < n; ++i, ++j) {
		ps[j] = ps[j-1];
		if (s[i] == s[i+1]) ps[j]++;
	}
	scanf("%d", &q);
	for (int i = 0; i < q; ++i) {
		int a, b;
		scanf("%d%d", &a, &b);
		printf("%d\n", ps[b]-ps[a]);
	}
}


#include <stdio.h>
#include <string.h>
#define MX 100005
int main()
{
	char s[MX];
	gets(s);
	int n = strlen(s), ps[MX] = {0}, j = 2, q;
	for (int i = 0; i < n; ++i, ++j) {
		ps[j] = ps[j-1];
		if (s[i] == s[i+1]) ps[j]++;
	}
	scanf("%d", &q);
	for (int i = 0; i < q; ++i) {
		int a, b;
		scanf("%d%d", &a, &b);
		printf("%d\n", ps[b]-ps[a]);
	}
}


#include <stdio.h>

int main(void){
  char query[100000];
  int queries;
  int pos1,pos2;
  int answers=0;

  scanf("%s", query);
  scanf("%d", &queries);

  for(int i=0; i<queries; i++){
    scanf("%d %d", &pos1, &pos2);
    for(int j=pos1-1;j<pos2-1;j++){
      if(query[j]==query[j+1]){
	answers+=1;
      }
    }
    printf("%d\n", answers);
    answers=0;
  }
}


#include <stdio.h>
#include <string.h>

int main(void){
  char query[1000000];
  int pos1,pos2, queries;
  int answers=0;
  int ans[1000000];

  scanf("%s", query);
  scanf("%d", &queries);

  for(int j=0;j<strlen(query)-1;j++){
    if(query[j]==query[j+1]){
      answers+=1;
      ans[j+1]=answers;
    }
    else{
      ans[j+1]=answers;
    }
  }

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

    scanf("%d %d", &pos1, &pos2);

    printf("%d\n", ans[pos2-1]-ans[pos1-1]);
  }
}


#include <stdio.h>

int main(void){
  char query[1000000];
  int queries;
  int pos1,pos2;
  int answers=0;

  scanf("%s", query);
  scanf("%d", &queries);

  for(int i=0; i<queries; i++){
    answers=0;
    scanf("%d %d", &pos1, &pos2);
    for(int j=pos1-1;j<pos2-1;j++){
      if(query[j]==query[j+1]){
	answers+=1;
      }
    }
    printf("%d\n", answers);
  }
}


#include <stdio.h>

int main(void){
  char query[150000];
  int queries;
  int pos1,pos2;
  int answers=0;

  scanf("%s", query);
  scanf("%d", &queries);

  for(int i=0; i<queries; i++){
    scanf("%d %d", &pos1, &pos2);
    for(int j=pos1-1;j<pos2-1;j++){
      if(query[j]==query[j+1]){
	answers+=1;
      }
    }
    printf("%d\n", answers);
    answers=0;
  }
}


#include <stdio.h>
#include <string.h>

int main(void){
  char s[100001];
  int pos1,pos2, queries;
  int answers=0;
  int ans[100001];

  scanf("%s", s);
  scanf("%d", &queries);

  int length = strlen(s);
  for(int j=0;j<length-1;j++){
    if(s[j]==s[j+1]){
      answers+=1;
    }
    ans[j+1]=answers;
  }

  for(int i=0; i<queries; i++){
    scanf("%d %d", &pos1, &pos2);
    printf("%d\n", ans[pos2-1]-ans[pos1-1]);
  }
}


#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<cctype>
#include <iomanip>
#include <string>
#include <sstream>
#include <functional>
#include <numeric>
#include <stack>
#include <climits>

using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define f(i,a,b) for(int i=a;i<b;i++)
#define F(i,a,b) for(int i=a;i>=    b;i--)
#define sz(a) ((int)a.size())
#define all(c) c.begin(), c.end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pair<int, int> > vpii;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef pair<int, int> pii;
typedef pair<string, int> psi;

template <class T> const T& max(const T& a, const T&b, const T&c) { return max(a, max(b, c)); }

template <class T> const T& min(const T& a, const T&b, const T&c) { return min(a, min(b, c)); }

ll stoi(string &str) { istringstream ss(str); ll i; ss >> i;  return i; }

string itos(ll i) { stringstream ss; ss << i;   return ss.str(); }

ll gcd(ll a, ll b){ if (b == 0) return a; return gcd(b, a % b); }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

ll poww(ll a, ll b) { ll res = 1;   for (int i = 1; i <= b; i++) res *= a;  return res; }

ll sumOfDigs(string s) { ll sum = 0; for (int i = 0; i < s.length(); i++) sum += s[i] - '0'; return sum; }

ll sumOfDigs(ll n) { return (n < 10 ? n : n % 10 + sumOfDigs(n / 10)); }

ll mod = poww(10, 9) + 7;

using namespace std;

#define PI 3.14159265358979323846

#define N 10

int cnt[10], modCount[3];

int main() {

	ios_base::sync_with_stdio(0); cin.tie(0);

//#ifndef ONLINE_JUDGE
//	freopen("test.txt", "r", stdin);
//#endif
//
	int n;
	cin >> n;

	ll sum = 0;

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

		int x;
		cin >> x;
		cnt[x]++;
		modCount[x % 3]++;
		sum += x;
	}

	/*cout << modCount[0] << " " << modCount[1] << " " << modCount[2] << endl;
	cout << "mod: " << sum % 3 << endl;
	*/
	if (!cnt[0] ) { return cout << -1 << endl, 0; }

	if (sum % 3 == 1 && !modCount[1] && modCount[2] < 1) { return cout << -1 << endl, 0; }

	else if (sum % 3 == 2 && !modCount[2] && modCount[1] < 2) {  return cout << -1 << endl, 0; }

	bool f = (sum % 3 == 0 ? 1 : 0);

	if (sum % 3 == 1) {

		(cnt[1] ? cnt[1]-- : cnt[4] ? cnt[4]-- : cnt[7] ? cnt[7]-- : cnt[2] ? cnt[2] -= 2 : cnt[5] ? cnt[5] -= 2 : cnt[8] -= 2);
		f = 1;
	}

	if (sum % 3 == 2) {

		(cnt[2] ? cnt[2]-- : cnt[5] ? cnt[5]-- : cnt[8] ? cnt[8]-- : cnt[1] ? cnt[1] -= 2 : cnt[4] ? cnt[4] -= 2 : cnt[7] -= 2);
		f = 1;
	}

	int c = 0;
	for (int i = 1; i <= 9; i++) c += cnt[i];

	if (!c) { return cout << 0 << endl, 0; }

	for (int i = 9; i >= 0; i--) {

		for (int j = 1; j <= cnt[i]; j++) cout << char(i + '0');

	}
	cout << endl;

	return 0;

}


#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<cctype>
#include <iomanip>
#include <string>
#include <sstream>
#include <functional>
#include <numeric>
#include <stack>
#include <climits>

using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define f(i,a,b) for(int i=a;i<b;i++)
#define F(i,a,b) for(int i=a;i>=    b;i--)
#define sz(a) ((int)a.size())
#define all(c) c.begin(), c.end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pair<int, int> > vpii;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef pair<int, int> pii;
typedef pair<string, int> psi;

template <class T> const T& max(const T& a, const T&b, const T&c) { return max(a, max(b, c)); }

template <class T> const T& min(const T& a, const T&b, const T&c) { return min(a, min(b, c)); }

ll stoi(string &str) { istringstream ss(str); ll i; ss >> i;  return i; }

string itos(ll i) { stringstream ss; ss << i;   return ss.str(); }

ll gcd(ll a, ll b){ if (b == 0) return a; return gcd(b, a % b); }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

ll poww(ll a, ll b) { ll res = 1;   for (int i = 1; i <= b; i++) res *= a;  return res; }

ll sumOfDigs(string s) { ll sum = 0; for (int i = 0; i < s.length(); i++) sum += s[i] - '0'; return sum; }

ll sumOfDigs(ll n) { return (n < 10 ? n : n % 10 + sumOfDigs(n / 10)); }

ll mod = poww(10, 9) + 7;

using namespace std;

#define PI 3.14159265358979323846

#define N 10

int cnt[10], modCount[3];

int main() {

	ios_base::sync_with_stdio(0); cin.tie(0);

//#ifndef ONLINE_JUDGE
//	freopen("test.txt", "r", stdin);
//#endif
//
	int n;
	cin >> n;

	ll sum = 0;

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

		int x;
		cin >> x;
		cnt[x]++;
		modCount[x % 3]++;
		sum += x;
	}

	/*cout << modCount[0] << " " << modCount[1] << " " << modCount[2] << endl;
	cout << "mod: " << sum % 3 << endl;
	*/
	if (!cnt[0] ) { return cout << -1 << endl, 0; }

	if (sum % 3 == 1 && !modCount[1] && modCount[2] < 1) { return cout << -1 << endl, 0; }

	else if (sum % 3 == 2 && !modCount[2] && modCount[1] < 2) {  return cout << -1 << endl, 0; }

	bool f = (sum % 3 == 0 ? 1 : 0);

	if (sum % 3 == 1) {

		cnt[1] ? cnt[1]--, f = 1 : cnt[4] ? cnt[4]--, f = 1 : cnt[7] ? cnt[7]--, f = 1 : f = 0;

		int c = 2;
		if (!f) {

			for (int i = 2; i <= 9 && c; i += 3) {

				if (c && cnt[i]) { cnt[i]--, c--; }
				if (c && cnt[i]) { cnt[i]--, c--; }

			}

		}

	}

	if (sum % 3 == 2) {

		(cnt[2] ? cnt[2]--, f = 1 : cnt[5] ? cnt[5]--  , f = 1: cnt[8] ? cnt[8]-- , f = 1 : f = 0);

		if (!f){

			int c = 2;
			for (int i = 1; i <= 9; i += 3) {

				if (c && cnt[i]) { cnt[i]--, c--; }
				if (c && cnt[i]) { cnt[i]--, c--; }
			}

		}

	}

	int c = 0;
	for (int i = 1; i <= 9; i++) c += cnt[i];

	if (!c) { return cout << 0 << endl, 0; }

	for (int i = 9; i >= 0; i--) {

		for (int j = 1; j <= cnt[i]; j++) cout << char(i + '0');

	}
	cout << endl;

	return 0;

}


#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<cctype>
#include <iomanip>
#include <string>
#include <sstream>
#include <functional>
#include <numeric>
#include <stack>
#include <climits>

using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define f(i,a,b) for(int i=a;i<b;i++)
#define F(i,a,b) for(int i=a;i>=    b;i--)
#define sz(a) ((int)a.size())
#define all(c) c.begin(), c.end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pair<int, int> > vpii;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef pair<int, int> pii;
typedef pair<string, int> psi;

template <class T> const T& max(const T& a, const T&b, const T&c) { return max(a, max(b, c)); }

template <class T> const T& min(const T& a, const T&b, const T&c) { return min(a, min(b, c)); }

ll stoi(string &str) { istringstream ss(str); ll i; ss >> i;  return i; }

string itos(ll i) { stringstream ss; ss << i;   return ss.str(); }

ll gcd(ll a, ll b){ if (b == 0) return a; return gcd(b, a % b); }

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

ll poww(ll a, ll b) { ll res = 1;   for (int i = 1; i <= b; i++) res *= a;  return res; }

ll sumOfDigs(string s) { ll sum = 0; for (int i = 0; i < s.length(); i++) sum += s[i] - '0'; return sum; }

ll sumOfDigs(ll n) { return (n < 10 ? n : n % 10 + sumOfDigs(n / 10)); }

ll mod = poww(10, 9) + 7;

using namespace std;

#define PI 3.14159265358979323846

#define N 10

int cnt[10], modCount[3];

int main() {

	ios_base::sync_with_stdio(0); cin.tie(0);

//#ifndef ONLINE_JUDGE
//	freopen("test.txt", "r", stdin);
//#endif
//
	int n;
	cin >> n;
	int sum = 0;
	for (int i = 1; i <= n; i++) {

		int x;
		cin >> x;
		cnt[x]++;

		sum += x;

	}

	if (!cnt[0]) { return cout << -1 << endl, 0; }
	bool f = (sum % 3 ? 0 : 1);
	for (int i = 1; i <= 9; i++) {

		if (sum % 3 == i % 3 && sum % 3 && cnt[i]) {  sum -= i, cnt[i]--, f = 1; break; }

	}

	if (!f) {

		for (int i = 1; i <= 2; i++) {

			for (int j = 1; j <= 9; j++) {

				if (j % 3 && cnt[j]) { sum -= j; cnt[j]--; break; }

			}

		}

	}

	if (sum == 0) { cnt[0] = 1; }

	if (!cnt[0] || sum % 3) { return cout << -1 << endl, 0; }

	//for (int i = 0; i <= 9; i++) cout << cnt[i] << " ";
	//cout << endl;

	for (int i = 9; i >= 0; i--) {

		while (cnt[i]--) cout << i;

	}

	return 0;

}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define MAXN 1000010

int dp[MAXN][30];
char memo[MAXN][30];
int n;
int s[MAXN];

int solve(int pos, int mod){
	if(pos == n || s[pos] == 0) return mod == 0 ? 0 : -1e9;
	if(memo[pos][mod]) return dp[pos][mod];
	memo[pos][mod] = 1;

	int ans = 0;
	ans = max(solve(pos+1, mod), 1 + solve(pos+1, (mod + s[pos])%3));

	return dp[pos][mod] = ans;
}

void path(int pos, int mod){
	if(pos == n || s[pos] == 0) return;

	if(dp[pos][mod] == 1 + solve(pos+1, (mod + s[pos])%3)){
		printf("%d", s[pos]);
		path(pos+1, (mod + s[pos])%3);
	}else{
		path(pos+1, mod);
	}
}

int main(){
	scanf("%d", &n);
	int cnt = 0;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &s[i]);
		if(s[i]==0) cnt++;
	}
	if(cnt == 0){
		cout << "-1\n";
		return 0;
	}
	sort(s, s+n);
	reverse(s, s+n);

	solve(0, 0);
	path(0, 0);
	for (int i = 0; i < cnt; i++)
	{
		cout << "0";
	}
	cout << "\n";

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define MAXN 1000010

int dp[MAXN][30];
char memo[MAXN][30];
int n;
int s[MAXN];

int solve(int pos, int mod){
	if(pos == n) return mod == 0 ? 0 : -1e9;
	if(memo[pos][mod]) return dp[pos][mod];
	memo[pos][mod] = 1;

	int ans = 0;
	ans = max(solve(pos+1, mod), 1 + solve(pos+1, (mod*10 + s[pos])%30));

	return dp[pos][mod] = ans;
}

void path(int pos, int mod){
	if(pos == n) return;

	if(dp[pos][mod] == 1 + solve(pos+1, (mod*10 + s[pos])%30)){
		cout << s[pos];
		path(pos+1, (mod*10 + s[pos])%30);
	}else{
		path(pos+1, mod);
	}
}

int main(){
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
	}
	sort(s, s+n);
	reverse(s, s+n);

	int aux = solve(0, 0);

	if(aux == 0) cout << "-1\n";
	else{
		path(0, 0);
		cout << "\n";
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define MAXN 1000010

int dp[MAXN][30];
char memo[MAXN][30];
int n;
int s[MAXN];

int solve(int pos, int mod){
	if(pos == n) return mod == 0 ? 0 : -1e9;
	if(memo[pos][mod]) return dp[pos][mod];
	memo[pos][mod] = 1;

	int ans = 0;
	ans = max(solve(pos+1, mod), 1 + solve(pos+1, (mod*10 + s[pos])%30));

	return dp[pos][mod] = ans;
}

string saida="";
void path(int pos, int mod){
	if(pos == n) return;

	if(dp[pos][mod] == 1 + solve(pos+1, (mod*10 + s[pos])%30)){
		saida += (s[pos]+'0');
		path(pos+1, (mod*10 + s[pos])%30);
	}else{
		path(pos+1, mod);
	}
}

int main(){
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
	}
	sort(s, s+n);
	reverse(s, s+n);

	int aux = solve(0, 0);

	if(aux == 0) cout << "-1\n";
	else{
		path(0, 0);
		if(saida[0] == '0') cout << "0\n";
		else cout << saida << "\n";

	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define MAXN 1000010
int n, x;
int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &x);
		if(x==0){
			cout << "0\n";
			return 0;
		}
	}
	cout << "-1\n";

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define MAXN 1000010

int dp[MAXN][30];
char memo[MAXN][30];
int n;
int s[MAXN];

int solve(int pos, int mod){
	if(pos == n) return mod == 0 ? 0 : -1e9;
	if(memo[pos][mod]) return dp[pos][mod];

	int ans = 0;
	ans = max(solve(pos+1, mod), 1 + solve(pos+1, (mod*10 + s[pos])%30));

	return dp[pos][mod] = ans;
}

void path(int pos, int mod){
	if(pos == n) return;

	if(dp[pos][mod] == 1 + solve(pos+1, (mod*10 + s[pos])%30)){
		cout << s[pos];
		path(pos+1, (mod*10 + s[pos])%30);
	}else{
		path(pos+1, mod);
	}
}

int main(){
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
	}
	sort(s, s+n);
	reverse(s, s+n);

	int aux = solve(0, 0);

	if(aux == 0) cout << "-1\n";
	else{
		path(0, 0);
		cout << "\n";
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define MAXN 1000010

int dp[MAXN][30];
char memo[MAXN][30];
int n;
int s[MAXN];

int solve(int pos, int mod){
	if(pos == n) return mod == 0 ? 0 : -1e9;
	if(memo[pos][mod]) return dp[pos][mod];
	memo[pos][mod] = 1;

	int ans = 0;
	ans = max(solve(pos+1, mod), 1 + solve(pos+1, (mod*10 + s[pos])%30));

	return dp[pos][mod] = ans;
}

string saida="";
void path(int pos, int mod){
	if(pos == n) return;

	if(dp[pos][mod] == 1 + solve(pos+1, (mod*10 + s[pos])%30)){
		saida += (s[pos]+'0');
		path(pos+1, (mod*10 + s[pos])%30);
	}else{
		path(pos+1, mod);
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
	}
	sort(s, s+n);
	reverse(s, s+n);

	int aux = solve(0, 0);

	if(aux == 0) cout << "-1\n";
	else{
		path(0, 0);
		if(saida[0] == '0') cout << "0\n";
		else cout << saida << "\n";

	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define MAXN 1000010

int dp[MAXN][30];
char memo[MAXN][30];
int n;
int s[MAXN];

int solve(int pos, int mod){
	if(pos == n) return mod == 0 ? 0 : -1e9;
	if(memo[pos][mod]) return dp[pos][mod];

	int ans = 0;
	ans = max(solve(pos+1, mod), 1 + solve(pos+1, (mod*10 + s[pos])%30));

	return dp[pos][mod] = ans;
}

void path(int pos, int mod){
	if(pos == n) return;

	if(dp[pos][mod] == 1 + solve(pos+1, (mod*10 + s[pos])%30)){
		printf("%d", s[pos]);
		path(pos+1, (mod*10 + s[pos])%30);
	}else{
		path(pos+1, mod);
	}
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &s[i]);
	}
	sort(s, s+n);
	reverse(s, s+n);

	int aux = solve(0, 0);

	if(aux == 0){
		printf("-1\n");
	}
	else{
		path(0, 0);
		printf("\n");
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define MAXN 1000010

int dp[MAXN][30];
char memo[MAXN][30];
int n;
int s[MAXN];

int solve(int pos, int mod){
	if(pos == n || s[pos] == 0) return mod == 0 ? 0 : -1e9;
	if(memo[pos][mod]) return dp[pos][mod];
	memo[pos][mod] = 1;

	int ans = 0;
	ans = max(solve(pos+1, mod), 1 + solve(pos+1, (mod + s[pos])%3));

	return dp[pos][mod] = ans;
}

bool printou = false;

void path(int pos, int mod){
	if(pos == n || s[pos] == 0) return;

	if(dp[pos][mod] == 1 + solve(pos+1, (mod + s[pos])%3)){
		printf("%d", s[pos]);
		printou = true;
		path(pos+1, (mod + s[pos])%3);
	}else{
		path(pos+1, mod);
	}
}

int main(){
	scanf("%d", &n);
	int cnt = 0;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &s[i]);
		if(s[i]==0) cnt++;
	}
	if(cnt == 0){
		cout << "-1\n";
		return 0;
	}
	sort(s, s+n);
	reverse(s, s+n);

	solve(0, 0);
	path(0, 0);

	if(!printou) cnt = 1;
	for (int i = 0; i < cnt; i++)
	{
		cout << "0";
	}
	cout << "\n";

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define MAXN 1000010

int dp[MAXN][30];
char memo[MAXN][30];
int n;
int s[MAXN];

int solve(int pos, int mod){
	if(pos == n || s[pos] == 0) return mod == 0 ? 0 : -1e9;
	if(memo[pos][mod]) return dp[pos][mod];

	int ans = 0;
	ans = max(solve(pos+1, mod), 1 + solve(pos+1, (mod + s[pos])%3));

	return dp[pos][mod] = ans;
}

void path(int pos, int mod){
	if(pos == n || s[pos] == 0) return;

	if(dp[pos][mod] == 1 + solve(pos+1, (mod + s[pos])%3)){
		printf("%d", s[pos]);
		path(pos+1, (mod + s[pos])%3);
	}else{
		path(pos+1, mod);
	}
}

int main(){
	scanf("%d", &n);
	int cnt = 0;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &s[i]);
		if(s[i]==0) cnt++;
	}
	if(cnt == 0){
		cout << "-1\n";
		return 0;
	}
	sort(s, s+n);
	reverse(s, s+n);

	solve(0, 0);
	path(0, 0);
	for (int i = 0; i < cnt; i++)
	{
		cout << "0";
	}
	cout << "\n";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0;i<n;i++)
#define for1(i, n) for(int i = 1;i<=n;i++)
const int N = 1e5 + 10;

int a[N];
int n, sum = 0;
vector<int> ans;

void input(){
    cin >> n;
    for1(i, n) scanf("%d",&a[i]);
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);

    int cnt = 1;
    forn(i, n) ans.push_back(a[cnt ++ ]);
}

bool solve(int rem){
    bool isfind = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            isfind = 1;
            ans.erase(ans.begin() + i);
            break;
        }
    }
    if(isfind == 1) return 1;
    rem = (rem * 2)%3;
    int cnt = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            cnt ++;
            ans.erase(ans.begin() + i);

            if(cnt == 2) break;
        }
    }
}
void getsum(){
    sum = 0;
    for1(i, n) sum += a[i];
    if(sum%3 == 0) return;
    int rem = ((sum %3) + 3)%3;
    solve(rem);
}
void print(vector<int> b){
    forn(i, b.size()) cout<< b[i];
}
bool check(){
    int s = 0;
    if(ans[0] == 0) return 0;
    forn(i, ans.size()) s += ans[i];
    if(ans.back()!= 0) return 0;
    if(s%3!=0)       return 0;
    return 1;
}
int main(){
    input();
    getsum();
    if(check()){
        print(ans);
    }
    else cout<<-1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0;i<n;i++)
#define for1(i, n) for(int i = 1;i<=n;i++)
const int N = 1e5 + 10;

int a[N];
int n, sum = 0;
vector<int> ans;

void input(){
    cin >> n;
    for1(i, n) scanf("%d",&a[i]);
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);

    int cnt = 1;
    forn(i, n) ans.push_back(a[cnt ++ ]);
}

bool solve(int rem){
    bool isfind = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            isfind = 1;
            ans.erase(ans.begin() + i);
            break;
        }
    }
    if(isfind == 1) return 1;
    rem = (rem * 2)%3;
    int cnt = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            cnt ++;
            ans.erase(ans.begin() + i);

            if(cnt == 2) break;
        }
    }
}
void getsum(){
    sum = 0;
    for1(i, n) sum += a[i];
    if(sum%3 == 0) return;
    int rem = ((sum %3) + 3)%3;
    solve(rem);
}
void print(vector<int> b){
    forn(i, b.size()) cout<< b[i];
}
bool check(){
    int s = 0;
    forn(i, ans.size()) s += ans[i];
    if(ans.back()!= 0) return 0;
    if(s%3!=0)       return 0;
    return 1;
}
int main(){
    input();
    getsum();
    if(check()){
        print(ans);
    }
    else cout<<-1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0;i<n;i++)
#define for1(i, n) for(int i = 1;i<=n;i++)
const int N = 1e5 + 10;

int a[N];
int n, sum = 0;
vector<int> ans;

void input(){
    cin >> n;
    for1(i, n) scanf("%d",&a[i]);
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);

    int cnt = 1;
    forn(i, n) ans.push_back(a[cnt ++ ]);
}

bool solve(int rem){
    bool isfind = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            isfind = 1;
            ans.erase(ans.begin() + i);
            break;
        }
    }
    if(isfind == 1) return 1;
    rem = (rem * 2)%3;
    int cnt = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            cnt ++;
            ans.erase(ans.begin() + i);

            if(cnt == 2) break;
        }
    }
}
void getsum(){
    sum = 0;
    for1(i, n) sum += a[i];
    if(sum%3 == 0) return;
    int rem = ((sum %3) + 3)%3;
    solve(rem);
}
void print(vector<int> b){
    forn(i, b.size()) cout<< b[i];
}
bool check(){
    int s = 0;
    if(ans.begin() == 0) return 0;
    forn(i, ans.size()) s += ans[i];
    if(ans.back()!= 0) return 0;
    if(s%3!=0)       return 0;
    return 1;
}
int main(){
    input();
    getsum();
    if(check()){
        print(ans);
    }
    else cout<<-1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0;i<n;i++)
#define for1(i, n) for(int i = 1;i<=n;i++)
const int N = 1e5 + 10;

int a[N];
int n, sum = 0;
vector<int> ans;

void input(){
    cin >> n;
    for1(i, n) scanf("%d",&a[i]);
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);

    int cnt = 1;
    forn(i, n) ans.push_back(a[cnt ++ ]);
}

bool solve(int rem){
    bool isfind = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            isfind = 1;
            ans.erase(ans.begin() + i);
            break;
        }
    }
    if(isfind == 1) return 1;
    rem = (rem * 2)%3;
    int cnt = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            cnt ++;
            ans.erase(ans.begin() + i);

            if(cnt == 2) break;
        }
    }
}
void getsum(){
    sum = 0;
    for1(i, n) sum += a[i];
    if(sum%3 == 0) return;
    int rem = ((sum %3) + 3)%3;
    solve(rem);
}
void print(vector<int> b){
    int st = b.size() - 1;
    for(int i = 0;i<b.size();i++) if(b[i] !=0){
        st = i;
        break;
    }
    for(int i = st;i<b.size();i++) printf("%d", b[i]);
}
bool check(){
    int s = 0;
    forn(i, ans.size()) s += ans[i];
    if(ans.back()!= 0) return 0;
    if(s%3!=0)       return 0;
    return 1;
}
int main(){
    input();
    getsum();
    if(check()){
        print(ans);
    }
    else cout<<-1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0;i<n;i++)
#define for1(i, n) for(int i = 1;i<=n;i++)
const int N = 1e5 + 10;

int a[N];
int n, sum = 0;
vector<int> ans;

void input(){
    cin >> n;
    for1(i, n) scanf("%d",&a[i]);
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);

    int cnt = 1;
    forn(i, n) ans.push_back(a[cnt ++ ]);
}

bool solve(int rem){
    bool isfind = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            isfind = 1;
            ans.erase(ans.begin() + i);
            break;
        }
    }
    if(isfind == 1) return 1;
    rem = (rem * 2)%3;
    int cnt = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            cnt ++;
            ans.erase(ans.begin() + i);

            if(cnt == 2) break;
        }
    }
}
void getsum(){
    sum = 0;
    for1(i, n) sum += a[i];
    if(sum%3 == 0) return;
    int rem = ((sum %3) + 3)%3;
    solve(rem);
}
void print(vector<int> b){
    while(b[0] == 0 && b.size() > 1) b.erase(b.begin());
    forn(i, b.size()) cout<< b[i];
}
bool check(){
    int s = 0;
    forn(i, ans.size()) s += ans[i];
    if(ans.back()!= 0) return 0;
    if(s%3!=0)       return 0;
    return 1;
}
int main(){
    input();
    getsum();
    if(check()){
        print(ans);
    }
    else cout<<-1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0;i<n;i++)
#define for1(i, n) for(int i = 1;i<=n;i++)
const int N = 1e5 + 10;

int a[N];
int n, sum = 0;
vector<int> ans;

void input(){
    cin >> n;
    for1(i, n) scanf("%d",&a[i]);
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);

    int cnt = 1;
    forn(i, n) ans.push_back(a[cnt ++ ]);
}

bool solve(int rem){
    bool isfind = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            isfind = 1;
            ans.erase(ans.begin() + i);
            break;
        }
    }
    if(isfind == 1) return 1;
    rem = (rem * 2)%3;
    int cnt = 0;
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            cnt ++;
            ans.erase(ans.begin() + i);

            if(cnt == 2) break;
        }
    }
}
void getsum(){
    sum = 0;
    for1(i, n) sum += a[i];
    if(sum%3 == 0) return;
    int rem = ((sum %3) + 3)%3;
    solve(rem);
}
void print(vector<int> b){
    int st = b.size() - 1;
    for(int i = 0;i<b.size();i++) if(b[i] !=0){
        st = i;
    }
    for(int i = st;i<b.size();i++) printf("%d", b[i]);
}
bool check(){
    int s = 0;
    forn(i, ans.size()) s += ans[i];
    if(ans.back()!= 0) return 0;
    if(s%3!=0)       return 0;
    return 1;
}
int main(){
    input();
    getsum();
    if(check()){
        print(ans);
    }
    else cout<<-1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0;i<n;i++)
#define for1(i, n) for(int i = 1;i<=n;i++)
const int N = 1e5 + 10;

int a[N];
int n, sum = 0;
vector<int> ans;

void input(){
    cin >> n;
    for1(i, n) scanf("%d",&a[i]);
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);

    int cnt = 1;
    forn(i, n) ans.push_back(a[cnt ++ ]);
}

bool solve(int rem){
    for(int i = ans.size() - 1;i>=0;i--){
        if(ans[i]% 3 == rem){
            ans.erase(ans.begin() + i);
            break;
        }
    }
}
void getsum(){
    for1(i, n) sum += a[i];
    if(sum%3 == 0) return;
    int rem = sum %3;
    solve(rem);
}
void print(vector<int> b){
    forn(i, b.size()) cout<< b[i];
}
bool check(){
    int s = 0;
    forn(i, ans.size()) s += ans[i];
    if(ans.back()!= 0) return 0;
    if(s%3!=0)       return 0;
    return 1;
}
int main(){
    input();
    getsum();
    if(check()){
        print(ans);
    }
    else cout<<-1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long  ll;
typedef unsigned long long ull;
typedef map <int, int> mii;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

int const maxn = int(1e5 + 12);
int const maxb = int(2e6 + 12);
int const inf = int(1e9 + 7);
ll const linf = ll(1e18 + 12);
double const eps = 1e-7;
double const pi = acos(-1);
#ifdef _WIN32
	#define I64 "%I64d"
#else
	#define I64 "%lld"
#endif
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define next MyLittleNext
//#define end MyLittleEnd
#define all(x) x.begin(), x.end()
//#define fn ""

template <typename T>
bool umax(T & a, T b)
{
	return a < b ? (a = b, 1) : 0;
}

template <typename T>
bool umin(T & a, T b)
{
	return a > b ? (a = b, 1) : 0;
}

int n, cnt[12];
int ans[12];

int rec(int cur, int bal)
{
//	printf("%d %d\n", cur, bal);
	if (cur == 0)
		return bal ? -inf : 0;
	int best = -inf, j = 0;
	for (int i = cnt[cur]; i >= 0; i--)
	{
//		printf("%d->%d(%d)\n", cur, cur - 1, i);
		if (umax(best, rec(cur - 1, (bal + i * cur) % 3) + i))
			j = i;
	}
	if (best > 0)
		ans[cur] += j;
	else
		best = -inf;
//	printf("[%d %d]=%d\n", cur, bal, best);
	return best;
}

int main()
{
	#ifdef fn
		freopen(fn ".in", "r", stdin);
		freopen(fn ".out", "w", stdout);
	#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		int x;
		scanf("%d", &x);
		cnt[x]++;
	}
	if (!cnt[0])
	{
		puts("-1");
		return 0;
	}
	for (int i = 0; i < 10; i++)
	{
		if (i % 3)
		{
			int last = cnt[i] % 3;
			ans[i] = cnt[i] - last;
			cnt[i] = last;
		}
		else
		{
			ans[i] = cnt[i];
			cnt[i] = 0;
		}
	}
//	for (int i = 9; i >= 0; i--)
//	{
//		for (int x = 0; x < ans[i]; x++)
//			printf("%d", i);
//	}
//	puts("");
	rec(9, 0);
	for (int i = 9; i >= 0; i--)
	{
		while (ans[i]--)
			printf("%d", i);
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long  ll;
typedef unsigned long long ull;
typedef map <int, int> mii;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

int const maxn = int(1e5 + 12);
int const maxb = int(2e6 + 12);
int const inf = int(1e9 + 7);
ll const linf = ll(1e18 + 12);
double const eps = 1e-7;
double const pi = acos(-1);
#ifdef _WIN32
	#define I64 "%I64d"
#else
	#define I64 "%lld"
#endif
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define next MyLittleNext
//#define end MyLittleEnd
#define all(x) x.begin(), x.end()
//#define fn ""

template <typename T>
bool umax(T & a, T b)
{
	return a < b ? (a = b, 1) : 0;
}

template <typename T>
bool umin(T & a, T b)
{
	return a > b ? (a = b, 1) : 0;
}

int n, cnt[12];
int ans[12];
vector <int> now, best;

bool better()
{
	int a = 0, b = 0;
	for (int x : now)
		a += x;
	for (int x : best)
		b += x;
	if (a != b)
		return a > b;
	for (int i = 9; i >= 0; i--)
		if (now[i] != best[i])
			return now[i] > best[i];
	return 0;
}

void rec(int cur, int bal)
{
	bal %= 3;
	if (cur == 0)
	{
		if (bal % 3)
			return;
//		for (int i = 0; i < 10; i++)
//			for (int y = 0; y < now[i]; y++)
//				printf("%d", i);
//		puts("");
		if (better())
			best = now;
		return;
	}
	for (int x = 0; x <= cnt[cur]; x++)
	{
		now[cur] = x;
		rec(cur - 1, bal + cur * x);
	}
}

int main()
{
	#ifdef fn
		freopen(fn ".in", "r", stdin);
		freopen(fn ".out", "w", stdout);
	#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		int x;
		scanf("%d", &x);
		cnt[x]++;
	}
	if (!cnt[0])
	{
		puts("-1");
		return 0;
	}
	for (int i = 0; i < 10; i++)
	{
		if (i % 3)
		{
			int last = cnt[i] % 3;
			ans[i] = cnt[i] - last;
			cnt[i] = last;
		}
		else
		{
			ans[i] = cnt[i];
			cnt[i] = 0;
		}
	}
//	for (int i = 9; i >= 0; i--)
//	{
//		for (int x = 0; x < ans[i]; x++)
//			printf("%d", i);
//	}
//	puts("");
	best.assign(10, 0);
	now.assign(10, 0);
	rec(9, 0);
	for (int i = 0; i < 10; i++)
		ans[i] += best[i];
	int ok = 0;
	for (int i = 9; i > 0; i--)
	{
		for (int x = 0; x < ans[i]; x++)
		{
			if (i)
				ok = 1;
			printf("%d", i);
		}
	}
	if (!ok)
		ans[0] = 1;
	for (int i = 0; i < ans[0]; i++)
		printf("0");
}


#include <bits/stdc++.h>

using namespace std;

typedef long long  ll;
typedef unsigned long long ull;
typedef map <int, int> mii;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

int const maxn = int(1e5 + 12);
int const maxb = int(2e6 + 12);
int const inf = int(1e9 + 7);
ll const linf = ll(1e18 + 12);
double const eps = 1e-7;
double const pi = acos(-1);
#ifdef _WIN32
	#define I64 "%I64d"
#else
	#define I64 "%lld"
#endif
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define next MyLittleNext
//#define end MyLittleEnd
#define all(x) x.begin(), x.end()
//#define fn ""

template <typename T>
bool umax(T & a, T b)
{
	return a < b ? (a = b, 1) : 0;
}

template <typename T>
bool umin(T & a, T b)
{
	return a > b ? (a = b, 1) : 0;
}

int n, x, cnt[12], bal;
vector <int> one = {1, 4, 7}, two = {2, 5, 8};

bool del(int f)
{
	auto & v = f == 1 ? one : two;
	for (int x : v)
		if (cnt[x])
		{
			cnt[x]--;
			return 1;
		}
	return 0;
}

int main()
{
	#ifdef fn
		freopen(fn ".in", "r", stdin);
		freopen(fn ".out", "w", stdout);
	#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &x);
		cnt[x]++;
		bal += x;
		bal %= 3;
	}
	if (!cnt[0])
	{
		puts("-1");
		return 0;
	}
	if (bal == 1)
	{
		if (!del(1))
		{
			del(2);
			del(2);
		}
	}
	if (bal == 2)
	{
		if (!del(2))
		{
			del(1);
			del(1);
		}
	}
	int ok = 0;
	for (int i = 9; i > 0; i--)
	{
		ok |= cnt[i];
		for (int k = 0; k < cnt[i]; k++)
			printf("%d", i);
	}
	if (!ok)
		cnt[0] = 1;
	for (int i = 0; i < cnt[0]; i++)
		printf("0");
}


#include <bits/stdc++.h>

using namespace std;

typedef long long  ll;
typedef unsigned long long ull;
typedef map <int, int> mii;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

int const maxn = int(1e5 + 12);
int const maxb = int(2e6 + 12);
int const inf = int(1e9 + 7);
ll const linf = ll(1e18 + 12);
double const eps = 1e-7;
double const pi = acos(-1);
#ifdef _WIN32
	#define I64 "%I64d"
#else
	#define I64 "%lld"
#endif
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define next MyLittleNext
//#define end MyLittleEnd
#define all(x) x.begin(), x.end()
//#define fn ""

template <typename T>
bool umax(T & a, T b)
{
	return a < b ? (a = b, 1) : 0;
}

template <typename T>
bool umin(T & a, T b)
{
	return a > b ? (a = b, 1) : 0;
}

int n, cnt[12];
int ans[12];

int rec(int cur, int bal)
{
	if (cur == 0)
		return !bal;
	int best = 0, j = 0;
	for (int i = cnt[cur]; i >= 0; i--)
	{
		if (umax(best, rec(cur - 1, (bal + i * cur) % 3) + i))
			j = i;
	}
	ans[cur] += j;
	return 0;
}

int main()
{
	#ifdef fn
		freopen(fn ".in", "r", stdin);
		freopen(fn ".out", "w", stdout);
	#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		int x;
		scanf("%d", &x);
		cnt[x]++;
	}
	if (!cnt[0])
	{
		puts("-1");
		return 0;
	}
	for (int i = 0; i < 10; i++)
	{
		if (i % 3)
			ans[i] = (cnt[i] / 3) * 3;
		else
			ans[i] = cnt[i];
		cnt[i] -= ans[i];
	}
	rec(9, 0);
	for (int i = 9; i >= 0; i--)
	{
		while (ans[i]--)
			printf("%d", i);
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long  ll;
typedef unsigned long long ull;
typedef map <int, int> mii;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

int const maxn = int(1e5 + 12);
int const maxb = int(2e6 + 12);
int const inf = int(1e9 + 7);
ll const linf = ll(1e18 + 12);
double const eps = 1e-7;
double const pi = acos(-1);
#ifdef _WIN32
	#define I64 "%I64d"
#else
	#define I64 "%lld"
#endif
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define next MyLittleNext
//#define end MyLittleEnd
#define all(x) x.begin(), x.end()
//#define fn ""

template <typename T>
bool umax(T & a, T b)
{
	return a < b ? (a = b, 1) : 0;
}

template <typename T>
bool umin(T & a, T b)
{
	return a > b ? (a = b, 1) : 0;
}

int n, cnt[12];
int ans[12];

bool rec(int cur, int bal)
{
	if (cur == 0)
		return !bal;
	for (int i = cnt[cur]; i >= 0; i--)
	{
		if (rec(cur - 1, (bal + cnt[cur] * cur) % 3))
		{
			ans[cur] += cnt[cur];
			return 1;
		}
	}
	return 0;
}

int main()
{
	#ifdef fn
		freopen(fn ".in", "r", stdin);
		freopen(fn ".out", "w", stdout);
	#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		int x;
		scanf("%d", &x);
		cnt[x]++;
	}
	if (!cnt[0])
	{
		puts("-1");
		return 0;
	}
	for (int i = 0; i < 10; i++)
	{
		if (i % 3)
			ans[i] = (cnt[i] / 3) * 3;
		else
			ans[i] = cnt[i];
		cnt[i] -= ans[i];
	}
	rec(9, 0);
	for (int i = 9; i >= 0; i--)
	{
		while (ans[i]--)
			printf("%d", i);
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long  ll;
typedef unsigned long long ull;
typedef map <int, int> mii;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

int const maxn = int(1e5 + 12);
int const maxb = int(2e6 + 12);
int const inf = int(1e9 + 7);
ll const linf = ll(1e18 + 12);
double const eps = 1e-7;
double const pi = acos(-1);
#ifdef _WIN32
	#define I64 "%I64d"
#else
	#define I64 "%lld"
#endif
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define next MyLittleNext
//#define end MyLittleEnd
#define all(x) x.begin(), x.end()
//#define fn ""

template <typename T>
bool umax(T & a, T b)
{
	return a < b ? (a = b, 1) : 0;
}

template <typename T>
bool umin(T & a, T b)
{
	return a > b ? (a = b, 1) : 0;
}

int n, cnt[12];
int ans[12];

int rec(int cur, int bal)
{
//	printf("%d %d\n", cur, bal);
	if (cur == 0)
		return bal ? -inf : 0;
	int best = -inf, j = 0;
	for (int i = cnt[cur]; i >= 0; i--)
	{
//		printf("%d->%d(%d)\n", cur, cur - 1, i);
		if (umax(best, rec(cur - 1, (bal + i * cur) % 3) + i))
			j = i;
	}
	if (best > 0)
		ans[cur] += j;
	else
		best = -inf;
//	printf("[%d %d]=%d\n", cur, bal, best);
	return best;
}

int main()
{
	#ifdef fn
		freopen(fn ".in", "r", stdin);
		freopen(fn ".out", "w", stdout);
	#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		int x;
		scanf("%d", &x);
		cnt[x]++;
	}
	if (!cnt[0])
	{
		puts("-1");
		return 0;
	}
	for (int i = 0; i < 10; i++)
	{
		if (i % 3)
		{
			int last = cnt[i] % 3;
			ans[i] = cnt[i] - last;
			cnt[i] = last;
		}
		else
		{
			ans[i] = cnt[i];
			cnt[i] = 0;
		}
	}
//	for (int i = 9; i >= 0; i--)
//	{
//		for (int x = 0; x < ans[i]; x++)
//			printf("%d", i);
//	}
//	puts("");
	rec(9, 0);
	int ok = 0;
	for (int i = 9; i > 0; i--)
	{
		for (int x = 0; x < ans[i]; x++)
		{
			if (i)
				ok = 1;
			printf("%d", i);
		}
	}
	if (!ok)
		ans[0] = 1;
	for (int i = 0; i < ans[0]; i++)
		printf("0");
}


#include <bits/stdc++.h>

using namespace std;

typedef long long  ll;
typedef unsigned long long ull;
typedef map <int, int> mii;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

int const maxn = int(1e5 + 12);
int const maxb = int(2e6 + 12);
int const inf = int(1e9 + 7);
ll const linf = ll(1e18 + 12);
double const eps = 1e-7;
double const pi = acos(-1);
#ifdef _WIN32
	#define I64 "%I64d"
#else
	#define I64 "%lld"
#endif
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define next MyLittleNext
//#define end MyLittleEnd
#define all(x) x.begin(), x.end()
//#define fn ""

template <typename T>
bool umax(T & a, T b)
{
	return a < b ? (a = b, 1) : 0;
}

template <typename T>
bool umin(T & a, T b)
{
	return a > b ? (a = b, 1) : 0;
}

int n, x, cnt[12], bal;
vector <int> one = {1, 4, 7}, two = {2, 5, 8};

int main()
{
	#ifdef fn
		freopen(fn ".in", "r", stdin);
		freopen(fn ".out", "w", stdout);
	#endif
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &x);
		cnt[x]++;
		bal += x;
		bal %= 3;
	}
	if (!cnt[0])
	{
		puts("-1");
		return 0;
	}
	if (bal == 1)
	{
		int ok = 0;
		for (int x : one)
		{
			if (cnt[x])
			{
				cnt[x]--, ok = 1;
				break;
			}
		}
		if (!ok)
		{
			for (int x : two)
			{
				if (cnt[x] > 1)
				{
					ok = 1;
					cnt[x] -= 2;
					break;
				}
			}
		}
	}
	if (bal == 2)
	{
		int ok = 0;
		for (int x : two)
		{
			if (cnt[x])
			{
				cnt[x]--, ok = 1;
				break;
			}
		}
		if (!ok)
		for (int x : one)
			if (cnt[x] > 1)
			{
				cnt[x] -= 2, ok = 1;
				break;
			}
	}
	int ok = 0;
	for (int i = 9; i > 0; i--)
	{
		ok |= cnt[i];
		for (int k = 0; k < cnt[i]; k++)
			printf("%d", i);
	}
	if (!ok)
		cnt[0] = 1;
	for (int i = 0; i < cnt[0]; i++)
		printf("0");
}


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


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


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