#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  T = 1;
  while (T--) {
    int n;
    cin >> n;
    vector<int> v;
    int c = 0;
    for (int i = 0; i < n; i++) {
      int in;
      cin >> in;
      if (in == 5) {
        c++;
      }
      v.push_back(in);
    }
    int nn = c / 9;
    if (c == n) {
      cout << "-1" << endl;
    } else {
      string s = "";
      if (nn == 0) {
        cout << "0" << endl;
      } else {
        for (int i = 0; i < nn * 9; i++) {
          s = s + "5";
        }
        for (int i = 0; i < n - c; i++) {
          s = s + "0";
        }
        cout << s << endl;
      }
    }
  }
  return 0;
}
