#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  long long int a[n];
  long long int c5 = 0, c0 = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0) {
      c0++;
    } else {
      c5++;
    }
  }
  if (c0 == 0) {
    cout << -1 << "\n";
    ;
    return;
  }
  if (c0 > 0 && c5 < 9) {
    cout << 0 << "\n";
    ;
    return;
  }
  string ans = "";
  long long int x = (c5 / 9) * 9;
  for (long long int i = 0; i < x; i++) {
    ans += '5';
  }
  for (long long int i = 0; i < c0; i++) {
    ans += '0';
  }
  cout << ans << "\n";
  ;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}
