#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int five = 0;
  for (int i = 0; i < n; i++) {
    int c;
    cin >> c;
    if (c == 5) {
      five++;
    }
  }
  int five1 = five - five % 9;
  if (five1 == 0) {
    if (five == n) {
      cout << -1;
    } else {
      cout << 0;
    }
    return 0;
  }
  if (five == n) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < five1; i++) {
    cout << 5;
  }
  for (int i = 0; i < n - five; i++) {
    cout << 0;
  }
}
