#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 1;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, a[maxn];
  long long n0 = 0, n5 = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 0)
      n0++;
    else
      n5++;
  }
  if (n0 == 0)
    cout << "-1";
  else {
    if (n5 < 9)
      cout << "0";
    else {
      if (n5 % 9 == 0) {
        for (int i = 1; i <= n5; i++) cout << "5";
        for (int i = 1; i <= n0; i++) cout << "0";
      } else {
        while (n5 % 9 != 0) n5--;
        for (int i = 1; i <= n5; i++) cout << "5";
        for (int i = 1; i <= n0; i++) cout << "0";
      }
    }
  }
}
