#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, c1 = 0, c2 = 0, x;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    if (x == 5)
      c1++;
    else
      c2++;
  }
  x = c1 / 9;
  x = x * 9;
  if (c2 == 0)
    cout << -1;
  else {
    if (x != 0) {
      for (i = 1; i <= x; i++) cout << 5;
      for (i = 1; i <= c2; i++) cout << 0;
    } else
      cout << 0;
  }
}
