#include <bits/stdc++.h>
using namespace std;
void Solve() {
  int n;
  int numFive = 0, numZero = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    if (temp == 5) {
      numFive++;
    } else {
      numZero++;
    }
  }
  if (numZero == 0) {
    cout << -1 << endl;
    return;
  }
  if (numFive < 9) {
    cout << 0 << endl;
  } else {
    for (int i = 0; i < numFive / 9; i++) {
      for (int j = 0; j < 9; j++) {
        cout << 5;
      }
    }
    for (int i = 0; i < numZero; i++) {
      cout << 0;
    }
    cout << endl;
  }
}
int main() {
  Solve();
  return 0;
}
