#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  int x;
  int a;
  long long total;
  long long fives = 0;
  long long zeroes = 0;
  while (n--) {
    cin >> x;
    if (x == 5) {
      fives++;
    }
    if (x == 0) {
      zeroes++;
    }
  }
  if (zeroes > 0) {
    a = fives / 9;
    if (a == 0) {
      cout << 0;
      return 0;
    }
    if (a > 0) {
      a = 9 * a;
      while (a) {
        cout << 5;
        a--;
      }
      while (zeroes) {
        cout << 0;
        zeroes--;
      }
      return 0;
    }
  }
  if (zeroes == 0) {
    cout << -1;
    return 0;
  }
}
