#include <bits/stdc++.h>
using namespace std;
int findLastOccurence(int A[], int n, int x) {
  int result, mid, high = n - 1, low = 0;
  while (low <= high) {
    mid = (high + low) / 2;
    if (A[mid] == x) {
      result = mid;
      low = mid + 1;
    } else if (x < A[mid])
      high = mid - 1;
    else
      low = mid + 1;
  }
  return result;
}
int main() {
  int A[1010], a, i, j, n;
  cin >> n;
  for (i = 0; i < n; i++) cin >> A[i];
  sort(A, A + n);
  if (A[0] == A[n - 1] && A[0] == 5)
    cout << "-1";
  else if (A[0] == A[n - 1] && A[0] == 0)
    cout << "0";
  else {
    int j = findLastOccurence(A, n, 0);
    if ((n - j - 1) >= 9) {
      a = n - j - 1 - ((n - j - 1) % 9);
      for (i = 0; i < a; i++) cout << "5";
      for (i = 0; i <= j; i++) cout << "0";
    } else
      cout << "0";
  }
  return 0;
}
