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