#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int count0(0), count5(0);
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    if (a == 0) count0++;
    if (a == 5) count5++;
  }
  if (count0 == 0)
    cout << -1;
  else if (count5 < 9)
    cout << 0;
  else {
    for (int i = 0; i < floor(count5 / 9); i++) {
      cout << 555555555;
    }
    for (int i = 0; i < count0; i++) cout << 0;
  }
}
