#include <bits/stdc++.h>
using namespace std;
int n;
void c_p_c() {}
int main() {
  int i;
  c_p_c();
  int n;
  cin >> n;
  map<int, int> m;
  int sum = 0, c = 0, mostdiv, zeros = 0;
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    if (k == 5) {
      sum += k;
      if (sum % 9 == 0) {
        mostdiv = sum;
        c++;
      }
    } else {
      zeros++;
    }
    m[k]++;
  }
  if (m.size() == 2 and c > 0 and zeros > 0) {
    for (int i = 0; i < mostdiv / 5; ++i) {
      cout << 5;
    }
    for (int i = 0; i < zeros; ++i) {
      cout << 0;
    }
  } else if (zeros > 0) {
    cout << 0 << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
