#include <bits/stdc++.h>
using namespace std;
int a[10000];
int main() {
  int n;
  cin >> n;
  int zero = 0;
  int five = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 5)
      five++;
    else
      zero++;
  }
  for (int i = 0;; i++) {
    if (five % 9 == 0)
      break;
    else
      five--;
  }
  string a;
  if (five == 0 and zero == 0) cout << -1;
  if (five == 0 and zero != 0) cout << 0;
  if (five != 0 and zero == 0) cout << -1;
  if (five != 0 and zero != 0) {
    for (int i = 0; i < five; i++) a += '5';
    for (int i = 0; i < zero; i++) a += '0';
  }
  cout << a;
  return 0;
}
