#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n], count5 = 0, count0 = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] == 5) count5++;
    if (arr[i] == 0) count0++;
  }
  if (count5 >= 9 && count0 > 0) {
    for (int i = 0; i < count5 - count5 % 9; i++) {
      cout << 5;
    }
    for (int i = 0; i < count0; i++) {
      cout << 0;
    }
  }
  if (count0 == 0) {
    cout << "-1";
  }
  if (count5 < 9 && count0 > 0) {
    cout << "0";
  }
}
