#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, a[10000];
  cin >> n;
  int cnt = 0, flag = 0, cnt1 = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0) {
      flag = 1;
      cnt1++;
    } else if (a[i] == 5) {
      cnt++;
    }
  }
  if (flag == 1) {
    int p = cnt / 9;
    for (int j = 0; j < p; j++) {
      cout << "555555555";
    }
    if (p != 0)
      for (int j = 1; j < cnt1; j++) {
        cout << "0";
      }
    cout << "0";
  } else
    cout << "-1" << endl;
}
