#include <bits/stdc++.h>
using namespace std;
signed main() {
  signed long long int T, n;
  signed long long int k, x, m, p = 0, min = 10000000000;
  float d = 0;
  cin >> n;
  signed long long int a[n];
  for (signed long long int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 5) p++;
  }
  if (p >= 9)
    m = p / 9;
  else
    m = 0;
  if (n - p >= 1)
    x = n - p;
  else
    x = 0;
  if (x == 0)
    cout << -1 << endl;
  else {
    for (signed long long int i = 1; i <= m * 9; i++) cout << 5;
    if (m != 0)
      for (signed long long int i = 1; i <= x; i++) cout << 0;
    else
      cout << 0 << endl;
  }
}
