#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, five = 0, zero = 0, b, sum = 0;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> b;
    sum += b;
    if (b == 5) {
      five += 1;
    } else {
      zero += 1;
    }
  }
  if (zero == 0) {
    cout << -1;
    return 0;
  }
  if (five == 0) {
    cout << 0;
    return 0;
  }
  if (sum % 9 != 0 && sum < 45) {
    cout << 0;
    return 0;
  }
  int a = sum / 45;
  int d = (sum - (a * 45)) / 5;
  for (int i = 0; i < five - d; i++) {
    cout << 5;
  }
  for (int i = 0; i < zero; i++) {
    cout << 0;
  }
  return 0;
}
