#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, sum = 0, cnt = 0, ans = 0, a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (a) sum += 1;
    if (sum % 9 == 0) ans = sum;
    if (!a) cnt++;
  }
  if (!cnt) {
    cout << -1;
  } else if (!ans)
    cout << 0;
  else {
    for (int i = 1; i <= ans + cnt; i++) {
      if (i <= ans)
        cout << 5;
      else
        cout << 0;
    }
  }
}
