#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  std::vector<int> ar(n);
  for (int i = 0; i < n; i++) cin >> ar[i];
  int c_f = (int)count(ar.begin(), ar.end(), 5);
  int c_z = (int)count(ar.begin(), ar.end(), 0);
  int x = (int)c_f / 9;
  if (c_z == 0)
    cout << -1 << '\n';
  else if (x == 0)
    cout << 0 << '\n';
  else {
    for (int i = 0; i < x * 9; i++) cout << 5;
    for (int i = 0; i < c_z; i++) cout << 0;
  }
  return 0;
}
