#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int num_zero = 0, num_five = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a == 0)
      num_zero++;
    else
      num_five++;
  }
  if (num_zero == 0) {
    cout << -1 << endl;
    return 0;
  }
  if (num_five < 9) {
    cout << 0 << endl;
    return 0;
  }
  num_five -= (num_five % 9);
  for (int i = 0; i < num_five; i++) cout << 5;
  for (int i = 0; i < num_zero; i++) cout << 0;
  cout << endl;
  return 0;
}
