#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  long long int a[n];
  for (long long int i = 0; i < n; i++) cin >> a[i];
  long long int zer = 0, five = 0;
  for (long long int i = 0; i < n; i++) {
    if (a[i] == 0) zer++;
    if (a[i] == 5) five++;
  }
  if (zer == 0) {
    cout << -1;
    return;
  } else if (five < 9) {
    cout << 0;
    return;
  } else {
    for (long long int i = 0; i < (five / 9) * 9; i++) cout << 5;
    for (long long int j = 0; j < zer; j++) cout << 0;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long TESTS = 1;
  while (TESTS--) {
    solve();
  }
  return 0;
}
