#include <bits/stdc++.h>
using namespace std;
void display(vector<int> a) {
  for (int i = 0; i < a.size(); i++) cout << a[i] << " ";
  cout << endl;
}
int main() {
  long long int n, countF = 0, countZ = 0;
  vector<int> a(n, 0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 5) countF++;
    if (a[i] == 0) countZ++;
  }
  countF = (countF / 9) * 9;
  if (countZ == 0)
    cout << -1;
  else if (countF == 0)
    cout << 0;
  else {
    while (countF != 0) {
      cout << 5;
      countF--;
    }
    while (countZ != 0) {
      cout << 0;
      countZ--;
    }
  }
}
