#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> cnt(10, 0);
  for (int i = 1; i <= n; i++) {
    int inp;
    cin >> inp;
    cnt[inp]++;
  }
  if (cnt[0] == 0) {
    cout << "-1\n";
    return 0;
  }
  while ((cnt[5] * 5) % 9 != 0) cnt[5]--;
  if (cnt[5] == 0) {
    cout << "0\n";
    return 0;
  }
  while (cnt[5] > 0) {
    cout << "5";
    cnt[5]--;
  }
  while (cnt[0] > 0) {
    cout << "0";
    cnt[0]--;
  }
  cout << "\n";
  return 0;
}
