#include <bits/stdc++.h>
using namespace std;
int a[2];
signed main() {
  int n;
  cin >> n;
  int x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x == 5)
      a[1]++;
    else
      a[0]++;
  }
  if (a[0] == 0) {
    cout << -1;
  } else if (a[1] < 9) {
    cout << 0;
  } else {
    a[1] = a[1] - (a[1] % 9);
    for (int i = 0; i < a[1]; i++) {
      cout << 5;
    }
    for (int i = 0; i < a[0]; i++) {
      cout << 0;
    }
  }
}
