#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, n, s5 = 0, s0 = -1, x;
  for (cin >> n; n; cin >> x, s5 += x == 5, s0 += x == 0, n--)
    ;
  if (s0 < 0)
    cout << -1;
  else {
    for (i = (s5 / 9) * 9; i; i--) cout << 5;
    for (i = s0 * (s5 / 9 > 0); i >= 0; i--) cout << 0;
  }
}
