#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  map<int, int> mp;
  int sm = 0;
  bool ok = false;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    mp[k]++;
    sm += k;
    if (k > 0) ok = true;
  }
  if (ok == false) {
    cout << "0\n";
  } else if (sm % 3 == 0 && mp[0] > 0) {
    string s = "";
    for (int i = 9; i >= 0; i--) {
      while (mp[i]--) {
        s += (char)('0' + i);
      }
    }
    if (s[0] == '0') s = "0";
    cout << s << endl;
  } else if (mp[0] > 0) {
    int d = -1;
    for (int i = 0; i < 10; i++) {
      if (mp[i] > 0 && ((sm - i + 3) % 3 == 0)) {
        d = i;
        break;
      }
    }
    if (d != -1) {
      mp[d]--;
      string s = "";
      for (int i = 9; i >= 0; i--) {
        while (mp[i]--) {
          s += (char)('0' + i);
        }
      }
      if (s.size() == 0)
        s = "-1";
      else if (s[0] == '0')
        s = "0";
      cout << s << endl;
      return 0;
    }
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < i + 1; j++) {
        if ((sm - i - j + 9) % 3 != 0) continue;
        if (i == j && mp[i] > 1) {
          mp[i] -= 2;
          d = i;
          break;
        } else if (i != j && mp[i] > 0 && mp[j] > 0) {
          d = i;
          mp[i]--;
          mp[j]--;
          break;
        }
      }
      if (d != -1) break;
    }
    if (d != -1) {
      string s = "";
      for (int i = 9; i >= 0; i--) {
        while (mp[i]--) {
          s += (char)('0' + i);
        }
      }
      if (s.size() == 0)
        s = "-1";
      else if (s[0] == '0')
        s = "0";
      cout << s << endl;
    } else {
      cout << "-1\n";
    }
  } else {
    cout << "-1\n";
  }
  return 0;
}
