#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
int n;
vector<int> v;
int main() {
  scanf("%d", &n);
  int five = 0, zero = 0;
  for (int i = 0, x; i < n; i++) {
    scanf("%d", &x);
    if (x == 5) {
      five++;
    } else {
      zero++;
    }
  }
  if (five >= 9 && zero) {
    five = five / 9 * 9;
    for (int i = 0; i < five; i++) {
      cout << 5;
    }
    for (int i = 0; i < zero; i++) cout << 0;
  } else if (zero >= 1) {
    cout << 0;
  } else {
    cout << -1;
  }
  return 0;
}
