#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, five = 0, zero = 0;
  cin >> n;
  while (n--) {
    cin >> k;
    if (k == 5)
      five += 1;
    else
      zero += 1;
  }
  if (five >= 9 && zero > 0) {
    for (size_t i = 0; i < five / 9; i++) cout << "555555555";
    for (size_t i = 0; i < zero; i++) cout << '0';
  } else if (zero > 0)
    cout << '0';
  else
    cout << "-1";
}
