#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int p = 0, q = 0;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  for (int i = 0; i < n; i++) {
    if (ar[i] == 5)
      p++;
    else
      q++;
  }
  int r = p / 9;
  if (q == 0)
    cout << "-1";
  else if (p < 9 && q != 0)
    cout << '0';
  else {
    for (int i = 1; i <= r; i++) {
      cout << "555555555";
    }
    for (int i = 1; i <= q; i++) {
      cout << '0';
    }
  }
  return 0;
}
