#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  int n, fives(0), zeros(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a(0);
    cin >> a;
    if (a == 5) {
      fives++;
    } else if (a == 0) {
      zeros++;
    }
  }
  if (zeros == 0) {
    cout << "-1";
  } else {
    if (fives < 9) {
      cout << 0;
    } else {
      fives -= fives % 9;
      for (int i = 0; i < fives; i++) {
        cout << 5;
      }
      for (int j = 0; j < zeros; j++) {
        cout << 0;
      }
    }
  }
  return 0;
}
