public void solve() throws IOException {
    int zeroes = 0;
    int fives = 0;
    int n = nextInt();
    int[] arr = new int[n];
    for (int x = 0; x < n; x++) {
      arr[x] = nextInt();
      if (arr[x] == 0)
        zeroes++;
      else
        fives++;
    }
    Arrays.sort(arr);

    char[] beg = new char[fives - (fives % 9)];
    for (int x = 0; x < beg.length; x++)
      beg[x] = '5';

    char[] end = new char[zeroes];
    for (int x = 0; x < zeroes; x++)
      end[x] = '0';

    String ans = new String(beg) + new String(end);
    if (beg.length == 0)
      ans = "0";
    if (zeroes == 0)
      ans = "-1";

    System.out.println(ans);

  }