#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n];
  int cf = 0;
  int co = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] == 5)
      cf++;
    else
      co++;
  }
  if (co == 0) {
    cout << "-1";
  } else if (cf < 9) {
    cout << "0";
  } else {
    int r = cf / 9;
    r = r * 9;
    for (int i = 0; i < r; i++) {
      cout << "5";
    }
    for (int i = 0; i < co; i++) {
      cout << "0";
    }
  }
  return 0;
}
