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