#include <bits/stdc++.h>
using namespace std;
int f = 0;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    if (x == 5) {
      f++;
    }
  }
  if (n == f) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i <= f - f % 9; i++) {
    cout << 5;
  }
  if (f - f % 9 == 0) {
    cout << 0 << endl;
  } else {
    for (int i = 1; i <= n - f; i++) {
      cout << 0;
    }
  }
  cout << endl;
}
