#include <bits/stdc++.h>
int main() {
  unsigned long long five = 0, zero = 0, number = 0, max = 0, f = 10, n, num;
  unsigned long long i;
  scanf("%I64d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%I64d", &num);
    if (num == 5) {
      five++;
    } else
      zero++;
  }
  if (zero == 0)
    printf("-1\n");
  else if (five < 9)
    printf("0");
  else {
    for (i = 1; i <= five / 9; i++) printf("555555555");
    while (zero--) printf("0");
  }
}
