#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<int> v;
  int n, arr[1000], c1 = 0, c2 = 0, a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; i++) {
    if (arr[i] == 5) {
      c1++;
    } else if (arr[i] == 0) {
      c2++;
    }
  }
  if (c2 == 0) {
    cout << "-1";
    return 0;
  }
  while (c1 != 0) {
    a = c1 * 5;
    if (a % 9 == 0) {
      break;
    } else {
      c1--;
    }
  }
  if (c1 == 0) {
    cout << "0";
    return 0;
  }
  for (int i = 0; i < c1; i++) {
    v.push_back(5);
  }
  for (int j = c1; j < c1 + c2; j++) {
    v.push_back(0);
  }
  for (int k = 0; k < c1 + c2; k++) {
    cout << v[k];
  }
}
