#include <bits/stdc++.h>
using namespace std;
int main() {
  long five = 0, zero = 0;
  long a, n, d;
  while (cin >> n) {
    while (n--) {
      cin >> a;
      if (a == 0)
        zero++;
      else
        five++;
    }
    d = five / 9;
    if (zero <= 0)
      cout << -1 << endl;
    else if (d == 0 && zero > 0)
      cout << 0 << endl;
    else if (d == 0 && zero <= 0)
      cout << -1 << endl;
    else if (d > 0 && zero <= 0)
      cout << -1 << endl;
    else if (d > 0 && zero > 0) {
      d = d * 9;
      while (d--) cout << 5;
      while (zero--) cout << 0;
      cout << endl;
    }
  }
  return 0;
}
