Jeff's got n cards, each card contains either digit 0, or digit 5. Jeff can choose several cards and put them in a line so that he gets some number. What is the largest possible number divisible by 90 Jeff can make from the cards he's got?

Jeff must make the number without leading zero. At that, we assume that number 0 doesn't contain any leading zeroes. Jeff doesn't have to use all the cards.


-----Input-----

The first line contains integer n (1 ≤ n ≤ 10^3). The next line contains n integers a_1, a_2, ..., a_{n} (a_{i} = 0 or a_{i} = 5). Number a_{i} represents the digit that is written on the i-th card.


-----Output-----

In a single line print the answer to the problem — the maximum number, divisible by 90. If you can't make any divisible by 90 number from the cards, print -1.


-----Examples-----
Input
4
5 0 5 0

Output
0

Input
11
5 5 5 5 5 5 5 5 0 5 5

Output
5555555550



-----Note-----

In the first test you can make only one number that is a multiple of 90 — 0.

In the second test you can make number 5555555550, it is a multiple of 90.

Source: codeforces | Difficulty: EASY
Tags: Mathematics

Implementation

Complete search


Solutions:
while 1:
	try:
		n = int(input())
		a = list(map(int, input().split()))
		count5 = a.count(5)
		count0 = a.count(0)
		if count5 // 9 == 0 and count0 != 0:
			print(0)
		elif count0 == 0:
			print(-1)
		else:
			print(count5 // 9 * 9 * '5' + count0 * '0')
	except EOFError:
		break


import sys
import itertools
import collections

def rs(x=''):
	return sys.stdin.readline().strip() if len(x) == 0 else input(x).strip()

def ri(x=''):
	return int(rs(x))

def rm(x=''):
	return map(str, rs(x).split())

def rl(x=''):
	return rs(x).split()

def rmi(x=''):
	return map(int, rs(x).split())

def rli(x=''):
	return [int(x) for x in rs(x).split()]

def println(val):
	sys.stdout.write(str(val) + '\n')

def solve(testCase):
	(n, a) = (ri(), rli())
	fives = a.count(5)
	zeros = n - fives
	if zeros == 0:
		print(-1)
	elif fives // 9 == 0:
		print(0)
	else:
		print(''.join(['5'] * (fives - fives % 9) + ['0'] * zeros))
for _ in range(ri() if 0 else 1):
	solve(_ + 1)


n = int(input())
s = list(map(int, input().split()))
k = 0
m = 0
for i in range(len(s)):
	if s[i] == 0:
		k += 1
	else:
		m += 1
if k == 0:
	print(-1)
elif m < 9:
	print(0)
else:
	x = m // 9
	print('5' * 9 * x + '0' * k)


import math
n = int(input())
l = list(map(int, input().split()))
c5 = l.count(5)
cc = math.floor(c5 / 9)
c0 = l.count(0)
if c0 == 0:
	print(-1)
elif cc == 0:
	print(0)
else:
	a = '5' * (9 * cc) + '0' * c0
	print(a)


n = int(input())
l = list(map(int, input().split()))
a = l.count(5)
b = n - a
if a >= 9 and b > 0:
	print(a // 9 * 9 * '5' + b * '0')
elif b == 0:
	print(-1)
else:
	print(0)


import sys
import math

def main():
	n = int(sys.stdin.readline().strip())
	l = sys.stdin.readline().strip().split()
	f = l.count('5')
	z = l.count('0')
	if z == 0:
		print(-1)
	elif f < 9:
		print(0)
	else:
		print((f - f % 9) * '5' + z * '0')
main()


import sys
from collections import Counter

def Ints():
	return map(int, sys.stdin.readline().strip().split())

def Strs():
	return map(str, sys.stdin.readline().strip().split())

def Array():
	return list(map(int, sys.stdin.readline().strip().split()))

def Str():
	return sys.stdin.readline().strip()

def Int():
	return int(sys.stdin.readline().strip())

def MOD():
	return 1000000007
N = Int()
arr = Array()
count = Counter(arr)
ans = ''
if 5 in count:
	ans += (count[5] - count[5] % 9) * '5'
if 0 in count:
	ans += count[0] * '0'
if ans == '' or 0 not in count:
	print(-1)
else:
	print(int(ans))


n = int(input())
l = list(map(int, input().split()))
if len(set(l)) == 1:
	if 0 in set(l):
		print(0)
	else:
		print(-1)
	exit()
ci = l.count(0)
c5 = l.count(5)
if c5 < 9:
	print(0)
else:
	print((c5 - c5 % 9) * '5' + '0' * ci)


n = int(input())
s = input()
i = 0
x = 0
z = 0
while i < len(s) - 1:
	if s[i] == ' ':
		s = s[:i] + s[i + 1:]
	if s[i] == '0':
		s = s[:i] + s[i + 1:]
		z = z + 1
	else:
		i += 1
if len(s) > 0:
	if s[len(s) - 1] == '0':
		s = s[:len(s) - 1]
		z = z + 1
for i in range(1, len(s) + 1):
	if i * 5 % 9 == 0:
		x = i
if x > 0 and z != 0:
	print('5' * x, end='')
if z >= 1 and x > 0:
	print('0' * z)
elif z >= 1:
	print('0')
if z == 0:
	print('-1')


def main():
	n = int(input())
	a = list(map(int, input().split()))
	f = a.count(5)
	z = n - f
	if z == 0:
		print(-1)
	elif f < 9:
		print(0)
	else:
		f = f // 9 * 9
		s = f * '5'
		s = s + z * '0'
		print(s)
main()


n = int(input())
l = list(map(int, input().split()))
a = l.count(5)
b = len(l) - a
if b == 0:
	print(-1)
	exit(0)
if a == 0:
	print(0)
	exit(0)
if a >= 9:
	print('5' * (a // 9 * 9) + '0' * b)
else:
	print(0)


n = int(input())
x = list(map(int, input().split()))
if 0 not in x:
	print(-1)
elif x.count(5) >= 9:
	print('5' * (x.count(5) // 9) * 9, end='0' * x.count(0))
else:
	print(0)


input()
a = input().split()
for i in range(a.count('5') + 1)[::-1]:
	if int('0' + i * '5') * 10 % 90 == 0 and a.count('0') > 0:
		print(int(i * '5' + a.count('0') * '0'))
		exit()
print(-1)


input()
s = input().split()
print(s.count('5') // 9 * (9 * '5') + ['0', s.count('0') * '0'][s.count('5') > 8] if '0' in s else -1)


def numbers(lst):
	(count5, count0) = (0, 0)
	for elem in lst:
		if elem == 5:
			count5 += 1
		else:
			count0 += 1
	if count0 < 1:
		return -1
	elif count5 < 9:
		return 0
	else:
		count5 -= count5 % 9
		s = ''
		for i in range(count5):
			s += '5'
		for i in range(count0):
			s += '0'
		return s
n = int(input())
b = [int(z) for z in input().split()]
print(numbers(b))


n = int(input())
l = list(map(int, input().strip().split()))
a = l.count(5)
b = l.count(0)
c = a // 9
d = c
c = c * 9
if b == 0:
	print(-1)
else:
	print(int('5' * c + '0' * b))


n = int(input())
mass = [int(i) for i in input().split()]
sm = 0
if 0 in mass:
	while True:
		if sum(mass) % 9 == 0:
			mass = [str(i) for i in sorted(mass, reverse=True)]
			ans = int(''.join(mass))
			break
		mass.remove(5)
else:
	ans = -1
print(ans)


n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
(f, z) = (0, 0)
s = ''
for x in l:
	if x == 5:
		f += 1
	else:
		z += 1
if f > 8 and z > 0:
	f = f // 9
	f = f * 9
	s = f * '5' + z * '0'
	print(int(s))
elif 0 in l:
	print(0)
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
c5 = 0
c0 = 0
for j in a:
	if j == 5:
		c5 += 1
	else:
		c0 += 1
if c5 < 9:
	if c0 > 0:
		print(0)
		return
	else:
		print(-1)
		return
elif c0 > 0:
	print(int('5' * (c5 - c5 % 9) + '0' * c0))
else:
	print(-1)


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math
from collections import defaultdict
n = ri()
l = ria()
x = l.count(5)
y = l.count(0)
z = x // 9
s = ''
s = '5' * (z * 9) + '0' * y
if y != 0:
	print(int(s))
else:
	print(-1)


n = int(input())
m = list(map(int, input().split()))
if 0 in m:
	if 5 in m:
		k = 0
		l = 0
		for i in range(n):
			k += m[i]
			if m[i] == 0:
				l += 1
		p = k // 9
		p1 = p // 5
		if p1 != 0:
			print('5' * p1 * 9 + '0' * l)
		else:
			print(0)
	else:
		print(0)
else:
	print(-1)


n = int(input())
arr = list(map(int, input().split()))
z = 0
f = 0
for i in arr:
	if i == 0:
		z += 1
	else:
		f += 1
s = f
while s * 5 % 9 != 0:
	if s == 0:
		break
	s = s - 1
if s != 0:
	if z > 0:
		ans = int(s * str(5) + z * str(0))
		print(ans)
	else:
		print(-1)
elif z > 0:
	print(0)
else:
	print(-1)


input()
vals = [*map(int, input().split())]
vals = sorted(vals)[::-1]
if 0 not in vals:
	print(-1)
else:
	while sum(vals) % 9 != 0:
		vals = vals[1:]
		if vals == []:
			break
	if vals == []:
		print(0)
	else:
		vals = [str(i) for i in vals]
		print(int(''.join(vals)))


l = input()
s = input()
(c0, c5) = (0, 0)
for i in range(len(s)):
	if s[i] == '0':
		c0 += 1
	elif s[i] == '5':
		c5 += 1
c5 = int(c5 / 9) * 9
if c0 == 0:
	print(-1, end='')
elif c5 == 0:
	print(0, end='')
else:
	for i in range(c5):
		print(5, end='')
	for i in range(c0):
		print(0, end='')


def maximum_number(n, a):
	d = {}
	for x in a:
		if x not in d.keys():
			d[x] = 1
		else:
			d[x] += 1
	if 5 not in d.keys():
		return 0
	elif d[5] < 9:
		if 0 not in d.keys():
			return -1
		else:
			return 0
	else:
		grp = d[5] // 9
		if 0 not in d.keys():
			return -1
		else:
			return str(5) * (grp * 9) + str(0) * d[0]
n = int(input())
a = list(map(int, input().split()))
print(maximum_number(n, a))


(a, b) = (int(input()), input())
c = b.count('5')
d = a - c
print(int('5' * (9 * (c // 9)) + '0' * d) if d else '-1')


from math import floor
l = input()
s = input()
(c0, c5) = (0, 0)
for i in range(len(s)):
	if s[i] == '0':
		c0 += 1
	elif s[i] == '5':
		c5 += 1
c5 = floor(c5 / 9) * 9
if c0 == 0:
	print(-1, end='')
elif c5 == 0:
	print(0, end='')
else:
	for i in range(c5):
		print(5, end='')
	for i in range(c0):
		print(0, end='')


n = int(input())
list1 = list(map(int, input().split()))
five = list1.count(5)
zero = list1.count(0)
if zero == 0:
	print(-1)
else:
	while not five * 5 % 9 == 0:
		five -= 1
	strx = ''
	strx += '5' * five + '0' * zero
	print(int(strx))


p = print
n = int(input())
a = input().split()
z = a.count('0')
f = (n - z) // 9 * 9
if z == 0:
	p(-1)
elif f >= 9:
	p('5' * f, end='')
	p('0' * z)
else:
	p(0)


n = int(input())
arr = [int(x) for x in input().split()]
count5 = 0
count0 = 0
for i in arr:
	if i == 5:
		count5 += 1
	else:
		count0 += 1
ans = ''
count5 = 9 * (count5 // 9)
for _ in range(count5):
	ans += '5'
for _ in range(count0):
	ans += '0'
if count5 == 0 and count0 > 0:
	ans = 0
elif count0 == 0 and count5 == 0:
	ans = '-1'
elif count0 == 0:
	ans = '-1'
print(ans)


n = int(input())
A = list(map(int, input().split()))
A.sort()
if A.count(0) == 0:
	print(-1)
else:
	ans = '0' * A.count(0)
	x = A.count(5)
	while x * 5 % 9 != 0:
		x -= 1
	ans = '5' * x + ans
	if '5' not in ans:
		print(0)
	else:
		print(ans)


n = int(input())
arr = list(map(int, input().split()))
five = arr.count(5)
zero = arr.count(0)
if zero == 0:
	print(-1)
else:
	ans = '5' * (five // 9 * 9) + '0' * zero
	print(int(ans))


n = int(input())
a = list(map(int, input().split()))
(c, d) = (0, 0)
for i in range(n):
	if a[i] == 0:
		d += 1
if d == 0:
	print(-1)
	quit()
for i in range(n):
	if a[i] == 5:
		c += 1
if c < 9:
	print(0)
	quit()
for i in range(n):
	if c % 9 == 0:
		print(c * str(5) + d * str(0))
		quit()
	else:
		c -= 1


a = int(input())
l = list(map(int, input().split()))
if 0 not in l:
	print(-1)
else:
	b = l.count(5) // 9
	if b == 0:
		print(0)
	else:
		c = l.count(0)
		print('5' * (b * 9) + '0' * c)


def __starting_point():
	inp = input()
	arr = input().split(' ')
	cz = 0
	cf = 0
	for a in arr:
		if int(a) == 0:
			cz += 1
		if int(a) == 5:
			cf += 1
	ans = '-1'
	if cz == 0:
		ans = '-1'
	else:
		ng = cf // 9
		ans = '555555555' * ng + '0' * cz
	print(int(ans))
__starting_point()


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp = lambda : list(map(int, sys.stdin.readline().rstrip('\r\n').split()))
mod = 10 ** 9 + 7
Mod = 998244353
INF = float('inf')
from math import *
from bisect import *
from heapq import *
from collections import defaultdict as dd
from collections import OrderedDict as odict
from collections import Counter as cc
from collections import deque
from itertools import groupby
sys.setrecursionlimit(20 * 20 * 20 * 20 + 10)
MAX = 10 ** 5

def solve():
	n = takein()
	arr = takeiar()
	dict_1 = counter_elements(arr)
	if 0 not in dict_1.keys():
		print(-1)
		return
	if 5 not in dict_1.keys():
		print(0)
		return
	summ = dict_1[5] * 5
	while summ > 0:
		if summ % 9 != 0:
			dict_1[5] -= 1
			summ -= 5
		else:
			break
	print(int(str('5' * dict_1[5]) + str('0' * dict_1[0])))

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	for tt in range(1, t + 1):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
		sys.stdout.close()

def takein():
	return int(sys.stdin.readline().rstrip('\r\n'))

def takesr():
	return sys.stdin.readline().rstrip('\r\n')

def takeiar():
	return list(map(int, sys.stdin.readline().rstrip('\r\n').split()))

def takesar():
	return list(map(str, sys.stdin.readline().rstrip('\r\n').split()))

def takeivr():
	return map(int, sys.stdin.readline().rstrip('\r\n').split())

def takesvr():
	return map(str, sys.stdin.readline().rstrip('\r\n').split())

def ispalindrome(s):
	return s == s[::-1]

def invert(bit_s):
	temp = int(bit_s, 2)
	inverse_s = temp ^ 2 ** (len(bit_s) + 1) - 1
	rslt = bin(inverse_s)[3:]
	return str(rslt)

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def factorial(n, m=1000000007):
	q = 1
	for i in range(n):
		q = q * (i + 1) % m
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b

def power_two(x):
	return x and (not x & x - 1)

def ceil(a, b):
	return -(-a // b)

def seive(n):
	a = [1]
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p ** 2, n + 1, p):
				prime[i] = False
		p = p + 1
	for p in range(2, n + 1):
		if prime[p]:
			a.append(p)
	return a

def isprime(n):
	if n > 2 and (not n % 2) or n == 1:
		return False
	for i in range(3, int(n ** 0.5 + 1), 2):
		if not n % i:
			return False
	return True
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


input()
s = input()
c0 = s.count('0')
c5 = s.count('5')
s = '5' * (c5 // 9 * 9)
if c0 == 0:
	print(-1)
elif len(s) == 0:
	print(0)
else:
	print(s + '0' * c0)


n = int(input())
nums = list(map(int, input().split()))
nums.sort(reverse=True)
if 5 not in nums:
	print(0)
elif 0 not in nums:
	print(-1)
else:
	fives = nums.index(0)
	if fives >= 9:
		for i in range(fives // 9):
			print(555555555, end='')
		for i in range(n - fives):
			print(0, end='')
		print()
	else:
		print(0)


n = int(input())
numbers = list(map(int, input().split()))
five = numbers.count(5)
zero = numbers.count(0)
if zero != 0:
	if five >= 9:
		print('5' * (five - five % 9) + '0' * zero)
	else:
		print('0')
else:
	print('-1')


def solve(arr, n):
	counts = {5: 0, 0: 0}
	for i in range(n):
		counts[arr[i]] += 1
	if counts[0] == 0:
		print(-1)
		return
	elif counts[5] < 9:
		print(0)
		return
	elif counts[5] >= 9:
		print('5' * (counts[5] // 9) * 9, end='')
		print('0' * counts[0], end='')
		return
n = int(input())
arr = list(map(int, input().split()))
solve(arr, n)
print()


n = int(input())
l = input()
a = l.count('0')
if a < 1:
	print('-1')
else:
	print(int('5' * (9 * (l.count('5') // 9)) + '0' * a))


n = int(input())
l = list(map(int, input().split()))
p = l.count(5)
p1 = l.count(0)
p = p - p % 9
S = ''
c = 0
for i in range(p):
	S = S + '5'
for i in range(p1):
	S = S + '0'
if p == 0 and p1 >= 1:
	print(0)
	c = 1
if p1 == 0:
	print(-1)
	c = 1
if c == 0:
	print(S)


input()
dic = {0: 0, 5: 0}
for i in map(int, input().split()):
	dic[i] += 1
ans = dic[5] // 9
if ans and dic[0]:
	print(int('5' * ans * 9 + '0' * dic[0]))
elif dic[0] == 0:
	print(-1)
else:
	print(0)


num = int(input())
cards = input().split(' ')
for i in range(num):
	cards[i] = int(cards[i])
num_5 = 0
num_0 = 0
result = ''
for i in range(num):
	if cards[i] == 5:
		num_5 += 1
	if cards[i] == 0:
		num_0 += 1
if num_0 == 0:
	print(-1)
elif num_5 // 9 < 0:
	print(-1)
else:
	result = '5' * (num_5 // 9 * 9) + '0' * num_0
	print(int(result))


n = int(input())
l = list(map(int, input().split()))
freq = {}
for i in l:
	if i in freq:
		freq[i] += 1
	else:
		freq[i] = 1
if 0 not in freq:
	print(-1)
elif 5 not in freq or freq[5] / 9 < 1:
	print('0')
else:
	t = int(freq[5] / 9)
	res = ['5' * (9 * t), '0' * freq[0]]
	r = ''.join(res)
	print(int(r))


foo = input()
string = input()
(count5, count0) = (string.count('5'), string.count('0'))
if count5 >= 9 and count0 > 0:
	if count5 % 9 == 0:
		aux = list(string)
		aux.sort(reverse=True)
		print(''.join(aux))
	else:
		print('5' * 9 * (count5 // 9) + '0' * count0)
elif count0 > 0:
	print(0)
else:
	print(-1)


n = int(input())
c = [int(_) for _ in input().split()].count(5)
if n - c > 0 and c >= 9:
	print('5' * (c // 9) * 9 + '0' * (n - c))
elif n - c > 0:
	print(0)
else:
	print(-1)


import math
n = int(input())
Line = input()
String = ''
if Line.count('0') != 0:
	if Line.count('5') >= 9:
		for i in range(math.floor(Line.count('5') / 9)):
			String += '5' * 9
		for i in range(Line.count('0')):
			String += '0'
		print(String)
	else:
		print(0)
else:
	print(-1)


n = int(input())
a = [int(x) for x in input().split()]
if a.count(0) == 0:
	print(-1)
else:
	ans = '5' * (a.count(5) // 9 * 9)
	if len(ans) == 0:
		ans = ans + '0'
	else:
		ans = ans + '0' * a.count(0)
	print(ans)


N = int(input())
A = list(map(int, input().split()))
five_cnt = A.count(5)
zero_cnt = A.count(0)
fiv = five_cnt // 9 * 9
if fiv == 0:
	if zero_cnt == 0:
		print(-1)
	else:
		print(0)
elif zero_cnt == 0:
	print(-1)
else:
	print('5' * fiv + '0' * zero_cnt)


n = int(input())
a = list(map(int, input().split()))
f = a.count(5)
z = a.count(0)
x = f // 9 * 9
p = 0
if x == 0 and z > 0:
	z = 1
elif x == 0 and z == 0:
	p = 1
	z = 0
elif x > 0 and z == 0:
	p = 1
	x = 0
ans = '5' * x + '0' * z + '-1' * p
print(ans)


n = int(input())
l = list(map(int, input().split()))
(a, b) = (0, 0)
s = ''
for i in l:
	if i == 5:
		a = a + 1
	if i == 0:
		b = b + 1
for i in range(a // 9):
	s = s + '555555555'
if a < 9:
	if b > 0:
		s = '0'
else:
	for i in range(b):
		s = s + '0'
if b > 0:
	print(s)
else:
	print('-1')


def main():
	n = int(input())
	nums = list(map(int, input().split(' ')))
	zeros = 0
	fives = 0
	for i in nums:
		if i == 0:
			zeros += 1
		elif i == 5:
			fives += 1
	if zeros == 0:
		return '-1'
	if fives < 9:
		return '0'
	return '5' * (fives // 9) * 9 + '0' * zeros
print(main())


def Sol(arr, n):
	cf = 0
	cz = 0
	for i in arr:
		if i == 5:
			cf += 1
	cz = n - cf
	nof = int(cf / 9) * 9
	if cz == 0:
		print(-1)
		return
	if nof == 0:
		if cz > 0:
			print(0)
			return
	else:
		ans = '5' * nof + '0' * cz
		print(ans)
		return
n = int(input())
arr = list(map(int, input().split()))
Sol(arr, n)


n = int(input())
l = list(map(int, input().rstrip().split()))
a = 0
b = 0
for i in range(0, n):
	if l[i] == 0:
		a = a + 1
	b = b + l[i]
c = (n - a) % 9
d = '5' * (n - (c + a)) + '0' * a
if n - a >= 9 and a >= 1:
	print(d)
elif a >= 1:
	print(0)
else:
	print(-1)


n = int(input())
m = [int(x) for x in input().split()]
if m.count(0) == 0:
	print(-1)
elif m.count(5) < 9:
	print(0)
else:
	t = int(m.count(5) / 9)
	print('5' * 9 * t + '0' * m.count(0))


n = int(input())
arr = list(input().strip().split())
f = 0
z = 0
for i in arr:
	if i == '0':
		z += 1
	else:
		f += 1
if z == 0:
	print(-1)
elif f // 9 == 0:
	print(0)
else:
	print('5' * int(f - f % 9) + '0' * z)


from collections import Counter
n = int(input())
l = list(map(int, input().split()))
c = Counter(l)
b = c[5] % 9
d = c[5] - b
if d > 0 and c[0] > 0:
	print('5' * d + '0' * c[0])
elif c[0] > 0:
	print('0')
else:
	print('-1')


n = int(input())
arr = list(map(int, input().split()))
t = arr.count(5)
p = arr.count(0)
if p == 0:
	print('-1')
elif t < 9:
	print('0')
else:
	t = t // 9
	t = t * 9
	q = str(5) * t
	q += str(0) * p
	print(q)


from sys import stdin
input = lambda : stdin.readline().strip()
n = int(input())
lst = list(map(int, input().split()))
n5 = lst.count(5)
n0 = lst.count(0)
n5 = n5 // 9
s = '5' * (n5 * 9) + '0' * n0
if n0 != 0:
	try:
		print(int(s))
	except:
		print(-1)
else:
	print(-1)


lmn = input()
b = list(map(int, input().split()))
c = {0: 0, 5: 0}
ans = ''
for i in b:
	c[i] = c[i] + 1
sumi = ''
for i in range(c[5]):
	sumi = sumi + '5'
	if int(sumi) % 9 == 0:
		ans = sumi
if ans == '' and c[0] != 0:
	print(0)
elif c[0] == 0:
	print(-1)
elif sumi != '':
	for i in range(c[0]):
		ans = ans + '0'
	print(ans)


n = int(input())
arr = [int(x) for x in input().split(' ')]
fives = arr.count(5)
zeros = n - fives
usableFives = fives - fives % 9
if not zeros:
	print(-1)
elif usableFives < 9:
	print(0)
else:
	print('5' * usableFives + '0' * zeros)


import sys
import math
import collections
from collections import deque
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))
T = 1
for _ in range(T):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	(fc, zc) = (a.count(5), a.count(0))
	if zc == 0:
		print(-1)
		continue
	if fc < 9 and zc > 0:
		print(0)
		continue
	if fc >= 9 and zc > 0:
		print(fc // 9 * 9 * '5' + zc * '0')


n = int(input())
l = [int(i) for i in input().split()]
if 0 in l:
	if l.count(5) % 9 == 0:
		print(int(''.join((str(i) for i in sorted(l, reverse=True)))))
	else:
		print(int('5' * (l.count(5) // 9 * 9) + '0' * l.count(0)))
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
m = {5: 0, 0: 0}
for i in a:
	m[i] = m[i] + 1
if m[5] >= 9 and m[0] > 0:
	print('5' * (m[5] - m[5] % 9) + '0' * m[0])
elif m[0] > 0:
	print(0)
else:
	print(-1)


n = int(input())
arr = input()
f = z = 0
for i in arr:
	if i == '5':
		f += 1
	if i == '0':
		z += 1
if not z:
	print(-1)
elif f < 9:
	print(0)
else:
	f -= f % 9
	print(f * '5' + z * '0')


def calc(n, arr):
	fives = 0
	zeros = 0
	for a in arr:
		if a == 5:
			fives += 1
		elif a == 0:
			zeros += 1
	if zeros == 0:
		return -1
	if fives < 9:
		return 0
	if len(arr) == zeros:
		return 0
	num = int('5' * fives + '0' * zeros)
	while num % 90 != 0:
		fives -= 1
		num = int('5' * fives + '0' * zeros)
	return num
n = int(input())
arr = list(map(int, input().split()))
print(calc(n, arr))


import math
n = int(input())
s = input()
li = []
li = s.split(' ')
n = li.count('5')
m = li.count('0')
if m == 0:
	print('-1')
elif n == 0 and m >= 1:
	print('0')
elif n < 9 and m >= 1:
	print('0')
else:
	t = int(n / 9)
	if t > 0:
		k = t * 9
		for i in range(0, k):
			print('5', end='')
		for i in range(0, m):
			print('0', end='')
	else:
		print('-1')


import math
from bisect import bisect_left, bisect, bisect_right
from itertools import accumulate

def index(a, x):
	i = bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	raise ValueError

def find_lt(a, x):
	i = bisect_left(a, x)
	if i:
		return a[i - 1]
	raise ValueError

def find_le(a, x):
	i = bisect_right(a, x)
	if i:
		return a[i - 1]
	raise ValueError

def find_gt(a, x):
	i = bisect_right(a, x)
	if i != len(a):
		return a[i]
	raise ValueError

def find_ge(a, x):
	i = bisect_left(a, x)
	if i != len(a):
		return a[i]
	raise ValueError
st = ''

def func(n, l1):
	z = l1.count(0)
	if (n - z) // 9 == 0 or z == 0:
		if z == 0:
			return -1
		return 0
	return '5' * (n - z - (n - z) % 9) + z * '0'
for _ in range(1):
	n = int(input())
	l1 = list(map(int, input().split()))
	st += str(int(func(n, l1))) + '\n'
print(st)


n = int(input())
a = list(map(int, input().split()))
nbr0 = 0
nbr5 = 0
for x in a:
	if x == 0:
		nbr0 += 1
	else:
		nbr5 += 1
if nbr0 == 0:
	print(-1)
else:
	b = nbr5 // 9
	s = int('5' * b * 9 + '0' * nbr0)
	print(s)


n = int(input())
L = list(map(int, input().split()))
c = L.count(5)
z = n - c
if z == 0:
	print(-1)
elif c // 9 == 0:
	print(0)
else:
	for i in range(c // 9 * 9):
		print('5', end='')
	for i in range(n - c):
		print('0', end='')


n = int(input())
a = list(map(int, input().split()))
(x, y) = (a.count(5), a.count(0))
x = x // 9 * 9
print('5' * x + '0' * y if x and y else 0 if y else -1)


n = int(input())
a = [int(x) for x in input().split()]
z = 0
f = 0
for e in a:
	if e == 5:
		f += 1
	else:
		z += 1
if z == 0:
	print(-1)
else:
	f -= f % 9
	s = ''
	for i in range(f):
		s += '5'
	if len(s) > 0:
		print(s + '0' * z)
	else:
		print(0)


n = int(input())
c = list(map(int, input().split()))
s = sum(c)
l = n - s // 5
if l == 0:
	print(-1)
else:
	k = s // 45
	s = '5' * 9 * k
	s += '0' * l
	print(int(s))


a = int(input())
b = list((int(x) for x in input().split()))
c = b.count(0)
d = b.count(5)
if d >= 9 and c >= 1:
	print((d - d % 9) * '5' + c * '0')
elif c > 0:
	print(0)
else:
	print(-1)


n = int(input())
arr = [int(x) for x in input().split()]
(f, o) = (0, 0)
for i in range(n):
	if arr[i] == '5':
		f += 1
	else:
		o += 1
x = n // 9
while x != 0:
	if x * 9 <= arr.count(5) and arr.count(0) >= 1:
		print('5' * (x * 9) + '0' * arr.count(0))
		break
	else:
		x -= 1
if x == 0 and arr.count(0) >= 1:
	print('0')
if x == 0 and arr.count(0) < 1:
	print('-1')


import collections
n = int(input())
a = [int(i) for i in input().split()]
b = []
d = collections.Counter(a)
c = d[5]
flag = 0
while c != 0:
	if c % 9 == 0:
		flag = 1
		break
	c -= 1
if flag == 1 and d[0] != 0:
	for i in range(0, c):
		b.append(5)
	for i in range(0, d[0]):
		b.append(0)
	print(*b, sep='')
elif d[0] != 0:
	print('0')
else:
	print('-1')


n = int(input())
a = [int(x) for x in input().split()]
flag = False
for i in range(0, n):
	if a[i] == 0:
		flag = True
		break
if flag == False:
	print('-1')
else:
	max = 0
	sum = 0
	for i in range(0, n):
		sum += a[i]
		if sum % 9 == 0:
			max = sum
	r = max // 5
	for i in range(0, r):
		print('5', end='')
	if r > 0:
		for i in range(0, n):
			if a[i] == 0:
				print('0', end='')
	else:
		print('0')


n = int(input())
a = list(map(int, input().split()))
count0 = a.count(0)
count5 = a.count(5)
if count5 % 9 == 0 and count0 > 0 and (count5 > 0):
	for i in range(0, count5):
		print(5, end='')
	for i in range(0, count0):
		print(0, end='')
elif count5 > 9 and count0 > 0:
	count5 = count5 - count5 % 9
	for i in range(0, count5):
		print(5, end='')
	for i in range(0, count0):
		print(0, end='')
elif count0 > 0:
	print(0)
else:
	print(-1)


n = int(input())
arr = list(map(int, input().split()))
fives = 0
if 5 in arr:
	fives = arr.count(5)
nulls = n - fives
fives -= fives % 9
if nulls == 0:
	print(-1)
else:
	print(int(str(5) * fives + str(0) * nulls))


n = int(input())
digits = [int(i) for i in input().split()]
fives = digits.count(5)
zeros = digits.count(0)
res = fives // 9
if fives == n:
	print('-1')
elif res == 0:
	print('0')
else:
	print('5' * res * 9 + '0' * zeros)


t = int(input())
l = [int(x) for x in input().split()]
c = 0
for i in l:
	if i == 0:
		c = c + 1
if c != 0:
	f = 0
	for i in l:
		if i == 5:
			f = f + 1
	if f < 9:
		print(0)
	else:
		for i in range(f - f % 9):
			print(5, end='')
		for i in range(c):
			print(0, end='')
else:
	print('-1')


n = int(input())
List = list(map(int, input().split()))
Sum = 0
zeroes = List.count(0)
index = 0
for i in range(List.count(5), -1, -1):
	Sum = 5 * i
	if Sum % 9 == 0 and zeroes > 0:
		index = i
		break
if index == 0 and zeroes == 0:
	print(-1)
	exit()
if index == 0:
	print('0')
	exit()
print(str(5) * index + '0' * zeroes)


n = int(input())
line = str(input()).split()
line = [int(it) for it in line]
num_5 = sum(line) // 5
num_0 = n - num_5
num_5 //= 9
num_5 *= 9
if num_0 == 0:
	print(-1)
elif num_5 > 0:
	print('5' * num_5 + '0' * num_0)
else:
	print(0)


n = int(input())
a = list(input().split(' '))
if a.count('0') == 0:
	print(-1)
else:
	f = a.count('5') // 9 * 9
	s = '5' * f + '0' * (1 if f == 0 else a.count('0'))
	print(s)


n = int(input())
l = list(map(int, input().split()))
c5 = 0
c0 = 0
for i in range(n):
	if l[i] == 5:
		c5 += 1
	else:
		c0 += 1
if c0 == 0:
	print(-1)
elif c5 >= 9:
	k = int(c5 / 9)
	s = '5' * (9 * k) + '0' * c0
	print(s)
else:
	print(0)


n = int(input())
arr = list(map(int, input().split()))
if arr.count(0) > 0:
	res = '5' * (arr.count(5) - arr.count(5) % 9) + '0' * arr.count(0)
	print(int(res))
else:
	print('-1')


n = int(input())
a = list(map(int, input().split()))
s = a.count(5)
if a.count(0) == 0:
	print('-1')
elif a.count(5) // 9 == 0:
	print('0')
else:
	print('5' * (9 * (s // 9)) + '0' * a.count(0))


def main():
	n = int(input())
	cartas = [int(x) for x in input().split()]
	cincos = cartas.count(5)
	zeros = cartas.count(0)
	if cincos >= 9 and zeros >= 1:
		print('{}{}'.format('5' * (cincos - cincos % 9), '0' * zeros))
	elif cincos < 9 and zeros > 0:
		print(0)
	else:
		print(-1)
main()


(N, Digitals) = (int(input()), list(map(int, input().split())))
if Digitals.count(0) == 0:
	print('-1')
	exit()
if Digitals.count(5) < 9:
	print('0')
	exit()
for i in range(Digitals.count(5) // 9 * 9):
	print('5', end='')
for i in range(Digitals.count(0)):
	print('0', end='')


x = int(input())
y = input()
z = y.split()
m = [0, 0]
q = []

def compose(alist1, alist2):
	for i in range(len(alist1)):
		if alist1[i] == '0':
			alist2[0] = alist2[0] + 1
		elif alist1[i] == '5':
			alist2[1] = alist2[1] + 1
compose(z, m)
if m[0] > 0:
	m[1] = m[1] // 9
	m[1] = m[1] * 9
	if m[1] < 9:
		print('0')
	else:
		for i in range(m[1]):
			q.append(5)
		for i in range(m[0]):
			q.append(0)
		for i in q:
			print(i, end='')
else:
	print('-1')


input()
numbers = [int(i) for i in input().split()]
res = numbers.count(5) // 9 * 9 * '5' + numbers.count(0) * '0'
if len(res) == 0 or res[-1] != '0':
	print(-1)
else:
	print(0 if res[0] == '0' else res)


n = int(input())
(five, zero) = (0, 0)
lis = []
k = list(map(int, input().split()))
for j in k:
	if j == 0:
		zero += 1
	elif j == 5:
		five += 1
if zero == 0:
	print('-1')
elif five < 9:
	print('0')
else:
	s = five % 9
	five = five - s
	for j in range(five):
		lis.append('5')
	for k in range(zero):
		lis.append('0')
	print(''.join(lis))


input()
A = list(map(int, input().split()))
if A.count(0) > 0:
	print('5' * (A.count(5) // 9) * 9, end='')
	if A.count(5) // 9 == 0:
		print(0)
	else:
		print('0' * A.count(0))
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
b = a.count(5)
c = a.count(0)
ans = '0'
if c == 0:
	ans = '-1'
elif b >= 9:
	if b == 9:
		ans = '5' * b + '0' * c
	elif b % 9 == 0:
		ans = b * '5' + c * '0'
	elif b % 9 != 0:
		ans = (b - b % 9) * '5' + c * '0'
print(ans)


(n, inp) = (int(input()), list(map(int, input().split())))
Five = inp.count(5)
Zero = n - Five
Five = int(Five / 9) * 9
if Five > 0 and Zero > 0:
	print('5' * Five + '0' * Zero)
elif Zero > 0:
	print('0')
else:
	print('-1')


def spliter():
	d = input()
	a = d.split()
	r = []
	for i in a:
		k = int(i)
		r.append(k)
	return r
n = spliter()
lst1 = spliter()
count5 = 0
count0 = 0
for i in lst1:
	if i == 0:
		count0 += 1
	else:
		count5 += 1
if count5 < 9 and count0 == 0:
	print(-1)
elif count0 > 0:
	for i in range(count5 // 9):
		for j in range(9):
			print(5, end='')
	if count5 < 9:
		print(0)
	else:
		for i in range(count0):
			print(0, end='')
else:
	print(-1)


from sys import stdin
n = int(input())
c = [int(i) for i in stdin.readline().split()]
(f, z) = (c.count(5), c.count(0))
if z == 0:
	print('-1')
elif f < 9 or z < 1:
	print('0')
else:
	print(''.join([str(i) for i in [5] * (f // 9) * 9 + [0] * z]))


n = int(input())
arr = list(map(int, input().split()))
f = 0
z = 0
for i in arr:
	if i == 5:
		f = f + 1
	else:
		z = z + 1
final = ''
if z == 0:
	print('-1')
elif f < 9:
	print('0')
else:
	f = f - f % 9
	for i in range(f):
		final = final + '5'
	for i in range(z):
		final = final + '0'
print(final)


from collections import Counter
n = int(input())
a = Counter(map(int, input().split()))
q0 = a[0]
q5 = a[5]
val = q5 // 9
if val == 0:
	if q0 == 0:
		print(-1)
	else:
		print(0)
else:
	if q0 == 0:
		print(-1)
		exit(0)
	while val > 0:
		print('5' * 9, end='')
		val -= 1
	print('0' * q0)


import re
from collections import Counter
from sys import stderr
from typing import Union, Generator, List, Tuple
import string
INF = 2e+18 + 3
EPS = 1e-10

def main():
	n = int(input())
	nums = Counter(input().split())
	if '0' not in nums:
		res = -1
	elif nums.get('5', 0) // 9:
		res = nums['5'] // 9 * 9 * '5' + nums['0'] * '0'
	else:
		res = '0'
	print(res)
main()


n = int(input())
l = list(map(int, input().split()))
count1 = 0
count2 = 0
for i in range(n):
	if l[i] == 5:
		count1 += 1
	else:
		count2 += 1
if count2 == 0:
	print('-1')
elif count1 < 9:
	print('0')
else:
	t = count1 // 9
	z = t * 9
	s = ''
	while z:
		s += '5'
		z -= 1
	while count2:
		s += '0'
		count2 -= 1
	print(s)


n = int(input())
s = input()
z = s.count('0')
f = s.count('5')
if z != 0 and (f % 9 == 0 and f != 0 or f > 9):
	f = f - f % 9
	print('5' * f + '0' * z)
elif z == 0 and f != 0:
	print(-1)
else:
	print(0)


def arr_inp():
	return [int(x) for x in input().split()]
(n, a) = (int(input()), arr_inp())
five = a.count(5)
zero = n - five
if zero == 0:
	print(-1)
elif five // 9 == 0:
	print(0)
else:
	print('5' * (9 * (five // 9)) + '0' * zero)


n = int(input(''))
num_0 = 0
num_5 = 0
lst = list(map(int, input('').split(' ')))
num_0 = lst.count(0)
num_5 = lst.count(5)
if num_0 == 0:
	print(-1)
elif num_5 < 9:
	print(0)
else:
	num_5 = num_5 - num_5 % 9
	for j in range(num_5):
		print(5, end='')
	for t in range(num_0):
		print(0, end='')


def go():
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	if not 0 in a:
		return -1
	else:
		return int('5' * (a.count(5) // 9 * 9) + '0' * a.count(0))
print(go())


n = int(input())
a = list(map(int, input().split()))
five = a.count(5)
zeros = a.count(0)
ans = five // 9
if ans == 0 and zeros == 0:
	print('-1')
elif ans == 0 and zeros != 0:
	print('0')
else:
	res = int('5' * (ans * 9))
	if res % 90 == 0:
		print(res)
	else:
		ans = False
		maxi = res
		while zeros > 0:
			res *= 10
			zeros -= 1
			if res % 90 == 0:
				maxi = max(maxi, res)
				ans = True
		if ans == True:
			print(maxi)
		else:
			print('-1')


input()
a = input().split()
(zero, five) = (a.count('0'), a.count('5'))
if zero == 0:
	print('-1')
elif five < 9:
	print('0')
else:
	for i in range(five - five % 9):
		print('5', end='')
	for i in range(zero):
		print('0', end='')
	print()


n = int(input().strip())
arr = list(map(int, input().strip().split()))
if 0 not in arr:
	print(-1)
else:
	fives = arr.count(5)
	ans = '5' * (fives // 9 * 9)
	if len(ans) == 0:
		print(0)
	else:
		ans += '0' * arr.count(0)
		print(ans)


a = int(input())
b = list(map(int, input().split()))
c = b.count(5)
e = b.count(0)
if e == 0:
	print(-1)
else:
	f = c % 9
	g = c - f
	if g == 0:
		print(0)
	else:
		h = '5' * g + '0' * e
		print(h)


n = int(input())
arr = list(map(int, input().split()))
zero = 0
for i in arr:
	if i == 0:
		zero = 1
		break
if zero == 0:
	print(-1)
else:
	five = 0
	zero = 0
	for i in arr:
		if i == 0:
			zero += 1
		else:
			five += 1
	a = []
	for i in range(five):
		a.append(5)
	for i in range(zero):
		a.append(0)
	ans = 0
	while five > 0:
		number = ''
		for i in a:
			number += str(i)
		number = int(number)
		if number % 90 == 0:
			ans = number
			break
		five -= 1
		a.remove(5)
	print(ans)


n = int(input())
s = input().split(' ')
f = 0
z = 0
a = ''
for i in s:
	if i == '5':
		f += 1
	else:
		z += 1
a = a + (f - f % 9) * '5' + z * '0'
if z != 0:
	print(int(a))
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
ans = '0' * a.count(0)
x = a.count(5)
ans = '5' * (x // 9 * 9) + ans
if len(ans) == 0:
	print(-1)
elif ans[-1] == '5':
	print(-1)
else:
	print(int(ans))


n = int(input())
s = ''.join(reversed(sorted(input().split())))
if s[-1] == '5':
	print(-1)
elif len(s) < 10 or s[8] == '0':
	print(0)
else:
	print(s[s.find('0') % 9:])


import collections
n = int(input())
l = list(map(int, input().split()))
s = ''
if 0 in l:
	c1 = collections.Counter(l)
	s += '0' * c1[0]
	x = c1[5] // 9 * 9
	s = '5' * x + s
	print(int(s))
else:
	print(-1)


a = int(input())
b = input()
c = b.split()
x = c.count('5')
xx = x // 9
y = c.count('0')
if y >= 1 and xx >= 1:
	print('5' * (xx * 9) + '0' * y)
elif y >= 1 and xx < 1:
	print('0')
else:
	print('-1')


n = int(input())
s = input()
s = s.replace(' ', '')
fives = 0
zeros = 0
for elem in s:
	if elem == '5':
		fives += 1
	else:
		zeros += 1
if fives == n:
	print(-1)
elif fives < 9:
	print(0)
else:
	remainder = fives % 9
	multiple_9 = fives - remainder
	max_num = []
	for i in range(multiple_9):
		max_num.append('5')
	for i in range(zeros):
		max_num.append('0')
	max_num = int(''.join(max_num))
	print(max_num)


n = int(input())
z = 0
f = 0
a = list(map(int, input().split()))
for i in range(n):
	if a[i] == 0:
		z += 1
	else:
		f += 1
if z == 0:
	print(-1)
elif f < 9:
	print(0)
else:
	f -= f % 9
	for i in range(f):
		print(5, end='')
	for i in range(z):
		print(0, end='')


n = int(input())
arr = list(map(int, input().split()))
countFive = 0
countZero = 0
for elm in arr:
	if elm == 5:
		countFive += 1
	else:
		countZero += 1
loop = countFive // 9
if countZero == 0:
	print(-1)
elif loop == 0:
	print(0)
else:
	for _ in range(9 * loop):
		print('5', end='')
	for _ in range(countZero):
		print('0', end='')


_ = int(input())
l_n = input().split()
d_n = {}
d_n['0'] = 0
d_n['5'] = 0
for n in l_n:
	d_n[n] += 1
if d_n['0'] == 0:
	print('-1')
elif d_n['5'] < 9:
	print('0')
else:
	print('%s%s' % ('5' * (d_n['5'] - d_n['5'] % 9), '0' * d_n['0']))


from collections import Counter
n = int(input())
c = Counter([int(i) for i in input().split()])
if c[0] == 0:
	print(-1)
	exit()
s = 0
mx = -1
for i in range(c[5] + 1):
	summa = s
	for j in range(c[0] + 1):
		if summa % 90 == 0:
			mx = max(summa, mx)
		summa *= 10
	s = s * 10 + 5
print(mx)


def check(sum0, sum5):
	if sum0 == 0:
		return -1
	if sum5 == 0:
		return 0
	sum = sum5 * 5
	while sum5 > 1 and sum % 9 != 0:
		sum -= 5
		sum5 -= 1
	if sum % 9 == 0:
		return '5' * sum5 + '0' * sum0
	else:
		return 0
n = int(input())
f = [int(x) for x in input().split()]
sum5 = 0
sum0 = 0
for i in f:
	if i == 0:
		sum0 += 1
	elif i == 5:
		sum5 += 1
ans = check(sum0, sum5)
print(ans)


n = int(input())
m = input()
count = 0
numbers = m.split()
for i in range(0, n):
	numbers[i] = int(numbers[i])
	if numbers[i] == 5:
		count += 1
numbers.sort(reverse=True)
if numbers[-1] == 0:
	if count < 9:
		print('0')
	else:
		extras = count % 9
		del numbers[0:extras]
		str1 = ''.join((str(e) for e in numbers))
		print(str1)
else:
	print('-1')


n = int(input())
cards = input().split()
card = []
for i in range(n):
	card.append(cards[i])
if '0' not in card:
	print(-1)
elif card.count('5') < 9:
	print(0)
else:
	d5 = card.count('5') // 9
	print('5' * (d5 * 9) + '0' * card.count('0'))


from collections import Counter
from sys import stdout
(n, c) = (input(), Counter(input().split()))
if c['0'] == 0:
	print(-1)
elif c['5'] // 9 == 0:
	print(0)
else:
	for _ in range(c['5'] // 9):
		stdout.write('555555555')
	for _ in range(c['0']):
		stdout.write('0')
	print()


class Code:

	def __init__(self):
		self.n = int(input())
		self.arr = list(map(int, input().split()))

	def process(self):
		(c_5, c_0) = (self.arr.count(5), self.arr.count(0))
		lis = [5] * (c_5 // 9 * 9) + [0] * c_0
		if lis.count(5) == 0 and lis.count(0) != 0:
			lis = [0]
		if len(lis) == 0 or lis.count(0) == 0:
			lis = [-1]
		print(*lis, sep='')
code = Code()
code.process()


from math import *
from random import *
n = int(input())
A = list(map(int, input().split()))
(z, f) = (0, 0)
for i in range(n):
	if A[i] == 0:
		z += 1
	else:
		f += 1
if z == 0:
	print(-1)
	exit(0)
if f // 9 == 0:
	print(0)
else:
	print('5' * (f - f % 9) + '0' * z)


n = int(input())
p = input().split()
zeros = p.count('0')
fives = p.count('5')
if not zeros:
	print(-1)
elif fives < 9:
	print(0)
else:
	print((fives - fives % 9) * '5' + zeros * '0')


try:
	n = int(input())
	a = list(map(int, input().split()))
	x = a.count(5) // 9
	s = ''
	z = a.count(0)
	if z == 0:
		print(-1)
	elif x == 0:
		print(0)
	else:
		for i in range(x * 9):
			s = s + '5'
		for i in range(z):
			s = s + '0'
		print(s)
except:
	pass


n = int(input())
num = sorted([int(x) for x in input().strip().split()], reverse=True)
tot = sum(num)
flag = False
if num[-1] == 0:
	while tot:
		if tot % 9 == 0:
			print(''.join([str(x) for x in num]))
			flag = True
			break
		else:
			num.pop(0)
			tot -= 5
	if not flag:
		print(0)
else:
	print(-1)


nt = int(input())
L = list(map(int, input().split()))
nf = len(list(filter(lambda x: x == 5, L)))
nz = nt - nf
print('5' * (nf - nf % 9) + '0' * nz if nz > 0 and nf // 9 > 0 else 0 if nz > 0 else -1)


n = int(input())
line = list(map(int, input().split()))
ans = ''
(count_5, count_0) = (0, 0)
for i in line:
	if i == 5:
		count_5 += 1
	elif i == 0:
		count_0 += 1
while count_5 % 9 != 0:
	count_5 -= 1
if count_5 > 0 and count_0 > 0:
	while count_5 != 0:
		ans += '5'
		count_5 -= 1
	while count_0 != 0:
		ans += '0'
		count_0 -= 1
	print(ans)
	exit()
elif count_0 > 0:
	print(0)
else:
	print(-1)


input()
a = list(map(int, input().split()))
n = int(a.count(5) / 9)
m = a.count(0)
if m == 0:
	print(-1)
else:
	for i in range(0, 9 * n):
		print(5, end='')
	if n == 0:
		m = 1
	for i in range(0, m):
		print(0, end='')


n = int(input())
a = list(map(int, input().split()))
if a.count(0) == 0:
	print(-1)
elif len(a) <= 9:
	print(0)
elif a.count(5) >= 9 and a.count(0) > 0:
	b = a.count(5) // 9
	print('555555555' * b, end='')
	print('0' * a.count(0))
else:
	print(0)


n = input()
a = list(map(str, input().split()))
a.sort(reverse=True)
s = ''
s = s.join(a)
if a.count('5') >= 9 and '0' in a:
	if a.count('5') % 9 == 0:
		print(s)
	else:
		print(s[a.count('5') % 9:])
elif a.count('5') < 9 and '0' in a:
	print('0')
else:
	print('-1')


def main():
	n = int(input())
	nulls = fives = 0
	for item in input().split():
		if int(item) == 0:
			nulls += 1
		else:
			fives += 1
	if nulls >= 1:
		if fives >= 9:
			print(int('5' * (fives - fives % 9) + '0' * nulls))
		else:
			print(0)
	else:
		print(-1)
main()


import os
import sys
import math
fin = sys.stdin
if os.path.exists('in.txt'):
	fin = open('in.txt')
n = int(fin.readline().split()[0])
x = [int(i) for i in fin.readline().split()]
c0 = len([i for i in x if i == 0])
c5 = len([i for i in x if i == 5])
c5 = int(c5 / 9) * 9
if c0 == 0:
	print(-1)
elif c5 == 0:
	print(0)
else:
	s = ''
	for i in range(c5):
		s += '5'
	for i in range(c0):
		s += '0'
	print(s)


n = int(input())
l = input().split()
x = l.count('5') // 9
s = '555555555' * x
s += '0' * l.count('0')
if l.count('0') == 0:
	print(-1)
else:
	print(int(s))


def check(n, arr):
	if sum(arr) >= 45 and 0 in arr:
		total = sum(arr)
		value = total // 45
		value = '5' * 9 * value
		zeroCount = n - total // 5
		value += '0' * zeroCount
		return value
	elif 0 in arr and sum(arr) < 45:
		return 0
	else:
		return -1
n = int(input().rstrip())
arr = list(map(int, input().rstrip().split()))
print(check(n, arr))


input()
a = input()
if a.count('5') // 9 and a.count('0'):
	print('555555555' * (a.count('5') // 9) + '0' * a.count('0'))
elif a.count('0'):
	print(0)
else:
	print(-1)


from math import floor
n = int(input())
l = [x for x in input().split()]
ans = [0, 0]
for i in l:
	if i == '5' and i != '0':
		ans[0] += 1
	else:
		ans[1] += 1
if floor(ans[0] / 9) > 0 and ans[1] > 0:
	print('5' * 9 * floor(ans[0] / 9) + '0' * ans[1])
elif ans[1] > 0:
	print(0)
else:
	print(-1)


n = int(input())
a = [i for i in input().split()]
a.sort()
a.reverse()
five = a.count('5')
zero = a.count('0')
if zero == 0:
	print('-1')
	exit(0)
if five < 9:
	print('0')
	exit(0)
five -= five % 9
print(str('5' * five + '0' * zero))


n = int(input())
arr = list(map(int, input().split()))
n0 = arr.count(0)
num = 0
ans = 0
for i in arr:
	if i != 0:
		num = num * 10 + i
		if num * 10 ** n0 % 90 == 0:
			ans = num * 10 ** n0
if ans == 0 and n0 == 0:
	print('-1')
else:
	print(ans)


n = int(input())
a = input()
arr = [int(y) for y in a.split()]
count5 = arr.count(5)
count0 = arr.count(0)
arr5 = [5]
arr0 = [0]
x = count5 // 9
if x >= 1 and count0 > 0:
	b = arr5 * (x * 9)
	c = arr0 * count0
	final_arr = b + c
	for i in final_arr:
		print(i, end='')
elif count0 == 0:
	print(-1)
else:
	print(0)


n = int(input())
l = list(map(str, input().split()))
count5 = 0
count0 = 0
for i in range(n):
	if l[i] == '5':
		count5 += 1
	else:
		count0 += 1
p = ''
if count0 == 0:
	print(-1)
elif count5 < 9:
	print(0)
else:
	c1 = count5 // 9
	if c1 > 0:
		p += '5' * (c1 * 9)
		p += '0' * count0
		print(p)
	else:
		print(0)


from sys import stdin

def main():
	n = int(stdin.readline().strip())
	l = stdin.readline().strip().split()
	fives = l.count('5') // 9 * 9
	zeros = l.count('0')
	if zeros:
		return '5' * fives + '0' * zeros if fives else '0'
	else:
		return '-1'
print(main())


a = int(input())
b = list(map(int, input().split()))
if 0 in b:
	flag = 0
	while 5 in b:
		if sum(b) % 9 == 0:
			flag = 1
			break
		else:
			b.remove(5)
	if flag == 1:
		print(int(b.count(5) * str(5) + b.count(0) * str(0)))
	else:
		print(0)
else:
	print(-1)


n = int(input())
cards = [int(c) for c in input().split()]
fives = cards.count(5)
ones = cards.count(0)
if ones == 0:
	print('-1')
elif fives < 9:
	print('0')
else:
	number = '5' * fives
	while True:
		if number == 0:
			break
		if int(number) % 9 == 0:
			end = number
			break
		else:
			number = number[1:]
	insg = end + '0' * ones
	print(insg)


n = int(input())
L = list(map(int, input().split()))
C0 = L.count(0)
C5 = L.count(5)
if C0 == 0:
	print(-1)
else:
	c51 = C5 // 9 * 9
	if c51 == 0:
		print(0)
	else:
		print('5' * c51 + '0' * C0)


(n, s) = (int(input()), input())
fives = s.count('5')
if fives == n:
	print(-1)
else:
	(div_, mod_) = divmod(fives, 9)
	print('5' * (fives - mod_) + '0' * (n - fives) if div_ else 0)


t = int(input())
a = list(map(int, input().split()))
fi = a.count(5)
ze = a.count(0)
no = fi // 9
if fi >= 9 and ze >= 1:
	print('5' * (no * 9), end='')
	print('0' * ze)
elif ze >= 1:
	print(0)
else:
	print(-1)


n = int(input())
A = list(map(int, input().split()))
cnt5 = sum(A) // 5
cnt0 = n - cnt5
if cnt0 == 0:
	print(-1)
else:
	ans = 0
	for i in range(cnt5 - cnt5 % 9):
		ans = 10 * ans + 5
	for i in range(cnt0):
		ans *= 10
	print(ans)


i = int(input())
n = [int(x) for x in input().split()]
five = n.count(5)
zero = n.count(0)
if zero == 0:
	print(-1)
elif five < 9:
	print(0)
else:
	five -= five % 9
	for i in range(five):
		print(5, end='')
	for i in range(zero):
		print(0, end='')


n = int(input())
lst = input().split()
count_zero = 0
count_five = 0
for i in lst:
	if i == '5':
		count_five += 1
	elif i == '0':
		count_zero += 1
if count_zero == 0:
	print('-1')
elif count_five // 9 == 0:
	print('0')
else:
	ans = '5' * (count_five // 9) * 9 + '0' * count_zero
	print(ans)


n = int(input().strip())
ll = input().split()
k0 = 0
k5 = 0
for i in ll:
	if i == '0':
		k0 += 1
	else:
		k5 += 1
if k0 == 0:
	print(-1)
else:
	print(int('5' * (k5 // 9) * 9 + '0' * k0))


x = int(input())
l = list(map(int, input().split()))
l = sorted(l)[::-1]
s = ''
if l[-1] == 0:
	while l != []:
		sum1 = sum(l)
		if sum1 % 9 == 0:
			for i in range(len(l)):
				s += str(l[i])
			if s[0] == '0':
				print(0)
			else:
				print(s)
			break
		else:
			l = l[1:]
else:
	print(-1)


x = int(input())
l = list(map(int, input().split()))
five = l.count(5)
k = five // 9
zero = x - five
if zero == 0:
	print(-1)
elif k == 0:
	print(0)
else:
	print(9 * k * '5' + zero * '0')


n = int(input())
l = list(map(int, input().split()))
c1 = l.count(5)
c2 = n - c1
if c2 == 0:
	print('-1')
elif c1 >= 9 and c2 > 0:
	print('5' * (c1 - c1 % 9) + '0' * c2)
elif c1 < 9 and c2 > 0:
	print('0')


n = int(input())
l = list(map(int, input().rstrip().split()))
f = 0
z = 0
for i in range(n):
	if l[i] == 5:
		f = f + 1
	elif l[i] == 0:
		z = z + 1
if z > 0 and f >= 9:
	s = f // 9
	print('5' * s * 9 + '0' * z)
elif z == 0:
	print(-1)
else:
	print(0)


n = int(input())
a = list(map(int, input().split()))
c5 = a.count(5)
c0 = a.count(0)
if c0 == 0:
	print(-1)
elif c5 < 9:
	print(0)
else:
	val = c5 // 9
	l = 9 * val
	for i in range(l):
		print(5, end='')
	for i in range(c0):
		print(0, end='')


n = int(input())
lst = list(map(int, input().split()))
z = 0
x = 0
for i in range(len(lst)):
	if lst[i] == 0:
		z += 1
	elif lst[i] == 5:
		x += 1
if z == 0:
	print(-1)
elif x < 9:
	print(0)
else:
	for i in range(x // 9):
		print(555555555, end='')
	for i in range(z):
		print(0, end='')


import sys
n = int(input())
ar = list(map(int, input().split()))
ar.sort()
x = ar.count(5)
y = ar.count(0)
z = x - x % 9
if y == 0:
	print(-1)
elif x >= 9:
	ans = z * '5' + y * '0'
	print(ans)
else:
	print(0)


n = int(input())
x = y = sum = 0
list = input().split()
for i in list:
	if i == '5':
		sum += 5
	else:
		x += 1
	if sum % 9 == 0:
		y = sum
if x == 0:
	print(-1)
elif y == 0 and x > 0:
	print(0)
else:
	while y > 0:
		y -= 5
		print(5, end='')
	while x > 0:
		x -= 1
		print(0, end='')


n = int(input())
l = list(map(str, input().split()))
su = 0
if '0' not in l:
	print(-1)
	exit(0)
for i in l:
	su += int(i)
if su >= 45:
	k = l.count('5')
	for i in range(k - k % 9):
		print('5', end='')
	for i in range(l.count('0')):
		print('0', end='')
else:
	print(0)


x = int(input())
y = [int(p) for p in input().split()]
x = a = y.count(5)
b = y.count(0)
while x * 5 % 9 != 0:
	x -= 1
if y.count(0) == 0:
	print(-1)
elif a != 0 and x != 0:
	print('5' * x + '0' * b)
elif x == 0:
	print(0)


import functools
import operator
n = int(input())
a = list(map(int, input().split(' ')))
fc = a.count(5)
zc = n - fc
if 0 not in a:
	print(-1)
elif fc < 9:
	print(0)
else:
	i = 1
	count = 0
	while i <= fc:
		if i * 5 % 9 == 0:
			count = i
		i += 1
	for i in range(count):
		print(5, end='')
	for j in range(zc):
		print(0, end='')


x = int(input())
y = [int(x) for x in input().split()]
s = 0
for i in y:
	if i == 5:
		s = s + 1
x = x - s
if x != 0:
	s = s - s % 9
	if s != 0:
		a = '5' * s + '0' * x
		print(a)
	else:
		print(0)
else:
	print(-1)


n = int(input())
li = [int(x) for x in input().split()]
c0 = li.count(0)
c5 = li.count(5)
c5 = c5 // 9 * 9
if c0 == 0:
	print(-1)
elif c5 == 0:
	print(0)
else:
	print('5' * c5 + '0' * c0)


def largestnum(lst):
	strfornum = ''
	count5 = lst.count(5)
	count0 = len(lst) - count5
	for item in range(count5):
		strfornum += '5'
	for item in range(count0):
		strfornum += '0'
	return strfornum
n = int(input())
lst = [int(item) for item in input().split()]
count = lst.count(0)
count5 = n - count
possible = False
while count5 != 0 and count != 0:
	sumofdigits = sum(lst)
	if sumofdigits % 9 == 0 and sumofdigits != 0:
		possible = True
	if possible:
		print(largestnum(lst))
		break
	rem = count5 % 9
	count5 -= rem
	for _ in range(rem):
		lst.remove(5)
if not possible and count != 0:
	print(0)
elif not possible and count == 0:
	print(-1)


a = input()
b = input().split(' ')
f = 0
z = 0
for element in b:
	if int(element) == 5:
		f += 1
	else:
		z += 1
if z == 0:
	print(-1)
elif f <= 8:
	print(0)
else:
	c = f % 9
	print((f - c) * '5' + z * '0')


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007
n = I()
lis = L()
a = lis.count(0)
b = lis.count(5)
if a == 0:
	print(-1)
elif b < 9:
	print(0)
else:
	print(str('5' * (b // 9) * 9) + '0' * a)


n = input()
s = input()
cnt5 = s.count('5')
cnt0 = s.count('0')
if cnt5 >= 9 and cnt0 > 0:
	print('5' * (cnt5 // 9 * 9) + '0' * cnt0)
	exit()
if cnt0:
	print(0)
	exit(0)
print(-1)


n = int(input())
nums = [int(x) for x in input().split()]
fives = 0
zeros = 0
for n in nums:
	if n == 5:
		fives += 1
	elif n == 0:
		zeros += 1
if zeros < 1:
	print(-1)
else:
	actfives = fives // 9 * 9
	if actfives > 0:
		print('5' * actfives + '0' * zeros)
	else:
		print(0)


x = int(input())
l = list(map(int, input().split()))

def func(r5, r0):
	(s, i) = (0, 0)
	r5 = r5 - r5 % 9
	while i < r5:
		s = s * 10 + 5
		i += 1
	s = s * 10 ** r0
	return s
ans = -1
d = {}
if 0 not in l:
	ans = -1
else:
	d[0] = l.count(0)
	d[5] = l.count(5)
	if l.count(5) < 9:
		ans = 0
	else:
		ans = func(d[5], d[0])
print(ans)


n = int(input())
a = [i for i in input().split()]
cnt_5 = a.count('5')
cnt_0 = a.count('0')
if cnt_5 // 9 > 0 and cnt_0 > 0:
	print(''.join(['5'] * (cnt_5 // 9 * 9) + ['0'] * cnt_0))
elif cnt_0 > 0:
	print(0)
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
z1 = a.count(5)
z2 = a.count(0)
l = []
if z1 >= 9 and z2 >= 1:
	x = z1 // 9
	for i in range(9 * x):
		l.append('5')
	for i in range(z2):
		l.append('0')
	print(''.join(l))
elif z2 >= 1:
	print(0)
elif z2 == 0:
	print(-1)
else:
	print(-1)


n = int(input())
v = list(map(int, input().split()))
five = v.count(5)
zero = v.count(0)
if zero == 0:
	print(-1)
else:
	flag = 0
	for i in range(int(five / 9) * 9):
		print(5, end='')
		if int(five / 9) > 0:
			flag = 1
	if flag:
		for i in range(zero):
			print(0, end='')
	else:
		print(0)


n = int(input())
s5 = 0
s0 = 0
a = [int(i) for i in input().split()]
for i in a:
	if i == 5:
		s5 += 1
	if i == 0:
		s0 += 1
d = s5 // 9
t = '555555555' * d
if s0 == 0:
	print(-1)
elif t == '':
	print(0)
else:
	t = t + '0' * s0
	print(t)


n = int(input())
digits = [int(x) for x in input().split()]
number = {0: 0, 5: 0}
for d in digits:
	number[d] += 1
if number[0] == 0:
	print(-1)
elif number[5] < 9:
	print(0)
else:
	j = int(number[5] / 9)
	for i in range(j):
		print(555555555, end='')
	for i in range(number[0]):
		print(0, end='')


import math
from collections import defaultdict, Counter, deque
INF = float('inf')

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i ** 2 <= n:
		if n % i == 0:
			return i
		i += 1
	return n

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def main():
	n = int(input())
	arr = array()
	five = arr.count(5)
	five = five // 9 * 9
	zero = arr.count(0)
	if zero > 0 and five > 0:
		ans = '5' * five
		ans += '0' * zero
		print(ans)
	elif zero > 0:
		print('0')
	else:
		print('-1')
t = 1
for _ in range(t):
	main()


n = int(input())
cs = list(map(int, input().split()))
c = sum(cs)
if 0 in cs:
	print(int('5' * ((c - c % 45) // 5) + '0' * cs.count(0)))
else:
	print('-1')


num = int(input())
cards = [int(x) for x in input().split()]
if cards.count(0) < 1:
	print(-1)
	exit()
ans = []
for f in range(9, cards.count(5) + 1, 9):
	ans.append('555555555')
if len(ans) == 0:
	ans.append('0')
else:
	ans.append('0' * cards.count(0))
print(''.join(ans))


n = int(input())
arr = input().split()
five = arr.count('5')
zero = arr.count('0')
five = five // 9
if zero == 0:
	print(-1)
elif five == 0:
	print(0)
else:
	print('5' * (five * 9) + '0' * zero)


input()
a = list(map(int, input().split()))
d = a.count(5)
if any((i == 0 for i in a)):
	print(int(9 * (d // 9) * '5' + a.count(0) * '0'))
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
(num_f, num_z) = (0, 0)
for x in a:
	if x == 5:
		num_f += 1
	else:
		num_z += 1
if num_z == 0:
	print(-1)
else:
	num_f = num_f // 9 * 9
	res = ['5'] * num_f
	if len(res):
		res.extend(['0'] * num_z)
	else:
		res = ['0']
	print(''.join(res))


n = int(input())
digits = list(map(int, input().split()))
five_count = sum(digits) // 5
zero_count = n - five_count
maxi = -1
if zero_count > 0:
	s = '0'
	maxi = 0
	zero_count -= 1
	for _ in range(five_count):
		s = '5' + s
		if int(s) % 90 == 0:
			maxi = max(maxi, int(s))
	if maxi > 0 and zero_count > 0:
		maxi = str(maxi) + '0' * zero_count
print(maxi)


n = int(input())
a = [int(d) for d in input().split()]
f = a.count(5)
z = a.count(0)
if z == 0:
	print(-1)
elif z != 0 and f < 9:
	print(0)
else:
	x = f // 9 * 9
	ans = [5] * x
	ans.extend([0] * z)
	print(*ans, sep='')


n = int(input())
a = list(map(int, input().split()))
if 0 not in a:
	print(-1)
else:
	f = 0
	while a.count(5) > 1:
		if sum(a) / 9 % 1 == 0:
			f = 1
			c5 = a.count(5)
			c0 = a.count(0)
			print(int('5' * c5 + '0' * c0))
			break
		a.remove(5)
	if f == 0:
		print(0)


import math
n = int(input())
l = [int(i) for i in input().split()]
c5 = l.count(5)
c0 = l.count(0)
c5 = math.floor(c5 / 9) * 9
if c0 == 0:
	print(-1, end='')
elif c5 == 0:
	print(0, end='')
else:
	for i in range(c5):
		print(5, end='')
	for i in range(c0):
		print(0, end='')


n = int(input())
cards = list(map(int, input().split()))
total = sum(cards)
cards.sort(reverse=True)
if cards[-1] == 0:
	if total % 9 != 0:
		while total != 0 and total % 9 != 0:
			total -= 5
		if total == 0:
			print(0)
		else:
			result = '5' * (total // 5)
			result = result + '0' * cards.count(0)
			print(result)
	elif total % 9 == 0:
		if total == 0:
			print(0)
		else:
			result = '5' * (total // 5)
			result = result + '0' * cards.count(0)
			print(result)
else:
	print(-1)


def jeff_digits():
	total = int(input())
	cards = list(map(int, input().split()))
	table = {5: 0, 0: 0}
	for i in range(len(cards)):
		table[cards[i]] += 1
	if table[5] < 9 and table[0] > 0:
		return 0
	elif table[5] < 9 and table[0] == 0:
		return -1
	elif table[0] == 0:
		return -1
	times = int(table[5] / 9)
	temp = '5' * 9 * times
	temp_cero = '0' * table[0]
	return int(temp + temp_cero)
A = jeff_digits()
print(A)


n = int(input())
a = [int(i) for i in input().split()]
if 0 not in a:
	print(-1)
else:
	s = sum(a)
	x = a.count(5)
	y = a.count(0)
	z = []
	for i in range(n):
		if s % 9 != 0 and x >= 0:
			x -= 1
			s -= 5
		elif s % 9 == 0 and x >= 0:
			z.append(int(x * '5' + y * '0'))
	if len(z) != 0:
		print(z[0])
	else:
		print(-1)


n = int(input())
l = sorted(input().split())
if set(l) == {'5'}:
	print(-1)
elif l.count('5') < 9:
	print(0)
else:
	print('5' * (l.count('5') // 9 * 9) + '0' * l.index('5'))


n = int(input())
l = input().split()
t = l.count('5')
k = l.count('0')
if t >= 9 and k > 0:
	t = t - t % 9
	print('5' * t + '0' * k)
elif k > 0:
	print(0)
else:
	print(-1)


trash = int(input())
firstLine = [int(n) for n in input().split()]
zCount = 0
fCount = 0
bestFives = 0
for i in firstLine:
	if i == 0:
		zCount += 1
	else:
		fCount += 1
for i in range(0, fCount + 1):
	if i * 5 % 9 == 0:
		bestFives = i
if zCount == 0:
	print(-1)
elif bestFives == 0:
	print(0)
else:
	print(str('5' * bestFives) + str('0' * zCount))


n = int(input())
c = input().split()
res = -1
f = c.count('5')
z = n - f
q = f // 9
res = '5' * (9 * q) + '0' * z
print(int(res) if z else -1)


n = int(input())
lst = list(map(int, input().split()))
if 0 in lst:
	no_of_5 = lst.count(5)
	no_of_0 = lst.count(0)
	if no_of_5 >= 9:
		no = no_of_5 // 9
		print('5' * 9 * no, end='')
		print('0' * no_of_0)
	else:
		print('0')
else:
	print('-1')


n = int(input())
l = [int(x) for x in input().split()]
zeroc = fivec = 0
for i in l:
	if i == 5:
		fivec += 1
	else:
		zeroc += 1
if fivec < 9:
	if zeroc == 0:
		print('-1')
	else:
		print('0')
elif zeroc == 0:
	print('-1')
else:
	print('5' * (fivec // 9 * 9) + '0' * zeroc)


def readln():
	return tuple(map(int, input().split()))
(n,) = readln()
c0 = c5 = 0
for a in readln():
	if a:
		c5 += 1
	else:
		c0 += 1
if c0 == 0:
	print(-1)
elif c0 and c5 < 9:
	print(0)
else:
	print('5' * (c5 // 9) * 9 + '0' * c0)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict, Counter
from os import path
import bisect as bi
import heapq
mod = 10 ** 9 + 7

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def inp():
		return int(input())

	def minp():
		return map(int, input().split())
else:

	def inp():
		return int(stdin.readline())

	def minp():
		return map(int, stdin.readline().split())
n = inp()
a = list(minp())
five = a.count(5)
zero = a.count(0)
if zero == 0:
	print(-1)
	exit(0)
n5 = five // 9
for i in range(n5):
	for j in range(9):
		print(5, end='')
if n5 == 0:
	print(0)
	exit(0)
for i in range(zero):
	print(0, end='')
print()


n = int(input())
bt = list(map(int, input().split()))
if 0 in bt:
	c5 = 0
	c0 = 0
	for a in bt:
		if a == 5:
			c5 += 1
		else:
			c0 += 1
	if c5 < 9:
		print(0)
	else:
		print(int(c5 / 9) * 9 * '5' + c0 * '0')
else:
	print(-1)


n = int(input())
arr = list(map(int, input().split()))
z = arr.count(5)
x = n - z
y = z % 9
arr.sort(reverse=True)
for _ in range(y):
	arr.remove(5)
if len(arr) == 0 or x == 0:
	print(-1)
elif set(arr) == {0}:
	print(0)
else:
	arr = list(map(str, arr))
	print(''.join(arr))


n = int(input())
l = [int(i) for i in input().split()]
a = l.count(0)
b = l.count(5)
if a == 0:
	print(-1)
elif b < 9:
	print(0)
else:
	print('5' * (b // 9 * 9) + a * '0')


c = {0: 0, 5: 0}
n = int(input())
a = map(int, input().split())
s = 0
for i in a:
	c[i] += 1
if c[0] == 0:
	print(-1)
elif c[5] < 9:
	print(0)
else:
	print('5' * (9 * (c[5] // 9)) + '0' * c[0])


from collections import Counter
n = int(input())
nums = list(map(int, input().split()))
cnt = dict(Counter(nums))
if 5 in cnt and 0 in cnt:
	if 5 * cnt[5] % 9 == 0 and cnt[0] >= 1:
		res = '5' * cnt[5] + '0' * cnt[0]
		print(int(res))
	elif 5 * cnt[5] % 9 != 0 and cnt[0] >= 1:
		while True:
			if 5 * cnt[5] % 9 == 0 or cnt[5] == 0:
				break
			cnt[5] -= 1
		if cnt[5] != 0:
			res = '5' * cnt[5] + '0' * cnt[0]
		elif cnt[5] == 0:
			res = '0'
		print(int(res))
elif 5 in cnt and 0 not in cnt:
	res = -1
	print(int(res))
else:
	print(0)


a = int(input())
l = list(map(int, input().split()))
five = 0
zero = 0
ans = ''
for i in l:
	if i == 5:
		five += 5
		if five % 9 == 0:
			ans += '5' * (five // 5)
			five = 0
	else:
		zero += 1
if zero == 0:
	print(-1)
else:
	print(int(ans + '0' * zero))


n = int(input())
mas = list(map(int, input().split()))
if sum(mas) // 45 >= 1 and 0 in mas:
	print('5' * (sum(mas) // 45 * 9) + '0' * mas.count(0))
elif 0 in mas:
	print(0)
else:
	print(-1)


n = int(input())
l = list(map(int, input().split()))
cnt5 = l.count(5)
cnt0 = l.count(0)
s = sum(l)
t = n - 1
if cnt0 == 0:
	print('-1')
	exit(0)
flag = False
while t != 0:
	if s % 9 == 0:
		flag = True
		break
	cnt5 -= 1
	s -= 5
if flag and s != 0:
	ans = ''
	for i in range(cnt5):
		ans += '5'
	for i in range(cnt0):
		ans += '0'
	print(ans)
else:
	print('0')


from collections import Counter
n = int(input())
arr = list(map(int, input().split()))
if arr.count(5) >= 9 and arr.count(0) >= 1:
	mapp = Counter(arr)
	f = mapp[5] // 9 * 9
	five = '5' * f
	zero = '0' * mapp[0]
	res = five + zero
	print(int(res))
elif arr.count(5) < 9 and arr.count(0) >= 1:
	print(0)
else:
	print(-1)


n = int(input())
l = sorted(list(input().split()))
s = ''
for i in l:
	s += i
s = s[::-1]
p = s.count('5')
o = s.count('0')
j = 1
for i in range(p):
	if int(s) % 90 == 0:
		print(s)
		exit(0)
	s = s[j:]
if o >= 1:
	print(0)
else:
	print(-1)


n = int(input())
arr = input()
a = 0
b = 0
for ch in arr:
	if ch == '5':
		a += 1
	if ch == '0':
		b += 1
if b == 0:
	print('-1')
else:
	a -= a % 9
	str = ''
	flag = 0
	if a == 0:
		flag = 1
	while a > 0:
		str += '5'
		a -= 1
	while b > 0:
		str += '0'
		b -= 1
	if flag == 1:
		str = '0'
	print(str)


n = int(input().strip())
cards = list(map(int, input().strip().split()))
cards.sort(reverse=True)
if cards[-1] == 5:
	print(-1)
	exit()
while sum(cards) % 9 != 0:
	cards.pop(0)
print(int(''.join([str(card) for card in cards])))


n = int(input())
digits = input().split()
c0 = digits.count('0')
c5 = digits.count('5')
if c0 == 0:
	print(-1)
elif c5 >= 9:
	print(''.join(['5'] * (c5 // 9 * 9) + ['0'] * c0))
else:
	print(0)


n = int(input())
lis = input().split()
count = [0, 0]
for k in lis:
	if k == '0':
		count[0] += 1
	if k == '5':
		count[1] += 1
if count[1] < 9 and count[0] == 0:
	print(-1)
elif count[1] < 9 and count[0] > 0:
	print(0)
elif count[1] >= 9 and count[0] == 0:
	print(-1)
else:
	m = count[1] // 9
	string = ''
	for i in range(m * 9):
		string += '5'
	for i in range(count[0]):
		string += '0'
	print(int(string))


n = int(input())
lst = list(map(int, input().split()))
arr = []
ans = 0
s = lst.count(5)
y = n - s
print(int('5' * (s - s % 9) + '0' * y) if y else -1)


a = int(input())
b = [int(s) for s in input().split()]
c = b.count(5)
cc = b.count(0)
if c // 9 > 0 and cc > 0:
	print('5' * (c // 9 * 9) + '0' * cc)
elif cc > 0:
	print(0)
else:
	print(-1)


n = int(input())
l = list(map(int, input().split()))
r = 0
for i in l:
	if i == 5:
		r += 1
k = int(r / 9)
if n - r > 0:
	for i in range(k * 9):
		print(5, sep='', end='')
	if k == 0:
		print(0)
	else:
		for i in range(n - r):
			print(0, sep='', end='')
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
if sum(a) < 45 and 0 in a:
	print(0)
elif sum(a) < 45 and 0 not in a:
	print(-1)
elif sum(a) >= 45 and 0 in a:
	x = sum(a) // 45
	y = a.count(0)
	print('5' * (9 * x) + '0' * y)
else:
	print(-1)


n = int(input())
cards = [int(x) for x in input().split()]
zeros = 0
fives = 0
for digit in cards:
	if digit == 0:
		zeros += 1
	else:
		fives += 1
if zeros >= 1:
	if fives / 9 >= 1:
		answer = ''
		for i in range(fives - fives % 9):
			answer += '5'
		for i in range(zeros):
			answer += '0'
		print(answer)
	else:
		print('0')
else:
	print('-1')


input()
X = input()
(Five, Zero) = (X.count('5') // 9 * 9, X.count('0'))
print('5' * Five + '0' * Zero if 0 not in [Five, Zero] else 0 if Zero != 0 else -1)


p = int(input())
n = input().split()
count_5 = n.count('5')
count_0 = n.count('0')
ans = ''
if count_0 > 0:
	if count_5 >= 9:
		ans += '5' * (count_5 // 9 * 9)
	ans += '0' * count_0
else:
	ans += '-1'
ans = int(ans)
print(ans)


input()
A = input().split()
count5 = 0
for elem in A:
	if elem == '5':
		count5 += 1
count0 = len(A) - count5
if count0 == 0:
	print(-1)
else:
	print(int('5' * (count5 // 9 * 9) + '0' * count0))


def scan(type):
	return list(map(type, input().split()))
(n,) = scan(int)
arr = scan(int)
f = z = 0
for x in arr:
	if x == 0:
		z += 1
	else:
		f += 1
if z == 0:
	print(-1)
elif f < 9:
	print(0)
else:
	f -= f % 9
	print('5' * f + '0' * z)


n = int(input())
a = list(map(int, input().split()))
s = a.count(5)
f = 555555555
if n - s == 0:
	print('-1')
elif s // 9 < 1:
	print('0')
else:
	for i in range(0, s // 9):
		print(f, end='')
	for i in range(0, n - s):
		print(0, end='')


n = int(input())
a = list(map(int, input().split()))
fives = a.count(5)
zeros = a.count(0)
if zeros == 0:
	print(-1)
elif fives // 9 == 0:
	print(0)
else:
	print('5' * (9 * (fives // 9)) + zeros * '0')


a = int(input())
b = list(map(int, input().split()))
z = b.count(5)
if a - z > 0:
	if z < 9:
		print(0)
	else:
		print('5' * (z // 9 * 9) + '0' * b.count(0))
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
z = a.count(0)
if z == 0:
	print(-1)
else:
	test = ['5'] * (n - z) + ['0'] * z
	while len(test) > 1:
		if int(''.join(test)) % 90 == 0:
			break
		elif z > 1:
			del test[len(test) - 1]
			z -= 1
		else:
			del test[0]
			if z < a.count(0):
				z = a.count(0)
				test += ['0'] * (z - 1)
	if int(''.join(test)) == 0:
		test = ['0']
	print(''.join(test))


import sys, os.path
if os.path.exists('in_py.txt'):
	sys.stdin = open('in_py.txt', 'r')
	sys.stdout = open('out_py.txt', 'w')
input = sys.stdin.readline

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
n = inp()
v = inlist()
cnt5 = 0
cnt0 = 0
for i in range(n):
	if v[i] == 0:
		cnt0 += 1
	else:
		cnt5 += 1
if cnt5 % 9 == 0 and cnt0 > 0 and (cnt5 > 0):
	for i in range(cnt5):
		print(5, end='')
	for i in range(cnt0):
		print(0, end='')
	print()
elif cnt5 % 9 != 0 and cnt0 > 0 or (cnt5 == 0 and cnt0 > 0):
	if cnt5 < 9 and cnt0 > 0:
		print(0)
	else:
		for i in range(9 * int(cnt5 / 9)):
			print(5, end='')
		for i in range(cnt0):
			print(0, end='')
		print()
else:
	print(-1)


num_cards = int(input())
cards = list(input().split())
num_zeros = 0
num_fives = 0
for card in cards:
	if card == '0':
		num_zeros += 1
	else:
		num_fives += 1
if num_zeros == 0:
	print(-1)
elif num_fives < 9:
	print(0)
else:
	solution = ['5'] * (num_fives // 9 * 9)
	solution.extend(['0'] * num_zeros)
	print(''.join(solution))


n = input()
l = list(map(int, input().split()))
c5 = l.count(5)
c0 = l.count(0)
if c5 >= 9 and c0 >= 1:
	while c5 * 5 % 9 != 0:
		c5 -= 1
	a5 = ['5'] * c5 + ['0'] * c0
	print(''.join(a5))
elif c0 == 0:
	print(-1)
else:
	print(0)


import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from itertools import accumulate, combinations, combinations_with_replacement, groupby, permutations, product
from math import acos, asin, atan, ceil, cos, degrees, factorial, gcd, hypot, log2, pi, radians, sin, sqrt, tan
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
n = inp()
a = inlist()
f = a.count(5)
z = a.count(0)
if z >= 1:
	if f >= 9:
		print('5' * 9 * (f // 9) + '0' * z)
	else:
		print(0)
else:
	print(-1)


(n, a) = (input(), list(sorted(input().split(), reverse=True)))
if '0' not in a:
	exit(print(-1))
if a.count('5') < 9:
	exit(print(0))
print('5' * (a.count('5') // 9 * 9) + '0' * a.count('0'))


n = int(input())
digits = [int(x) for x in input().split(' ')]
zeroes = digits.count(0)
fives = n - zeroes
if zeroes == 0:
	print(-1)
elif fives < 9:
	print(0)
else:
	print('5' * (9 * (fives // 9)) + '0' * zeroes)


n = input()
s = input()
if s.count('5') // 9 >= 1 and s.count('0') > 0:
	print('5' * (9 * (s.count('5') // 9)) + '0' * s.count('0'), sep='')
elif s.count('0') > 0:
	print(0)
else:
	print(-1)


n = int(input())
l = [int(i) for i in input().split(' ')]
c5 = l.count(5)
c0 = n - c5
if c0 == 0:
	print(-1)
	exit(0)
ans = '5' * (c5 - c5 % 9) + '0' * c0
print(int(ans))


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
c1 = c = a.count(5)
if c == n:
	print(-1)
else:
	while s >= 0 and s % 9 != 0:
		s -= 5
		c -= 1
	if c == 0:
		print(0)
	else:
		print(c * '5' + (n - c1) * '0')


n = int(input())
cards = [int(i) for i in input().split()]
fives = 0
zeroes = 0
for i in cards:
	fives += int(i == 5)
	zeroes += int(i == 0)
result = 0
if fives >= 9 and zeroes > 0:
	result = fives // 9 * 9 * '5' + zeroes * '0'
elif zeroes == 0:
	result = -1
print(result)


n = int(input())
a = [int(x) for x in input().split()]
mulofnine = [0]
x = int(1)
a = sorted(a)
k = int(0)
ctr5 = a.count(5)
if 0 not in a:
	print(-1)
elif ctr5 < 9:
	print(0)
elif ctr5 >= 9:
	for i in range(0, ctr5):
		mulofnine.append(9 * i)
	while mulofnine[k] <= ctr5:
		k += 1
	print('5' * mulofnine[k - 1] + '0' * a.count(0))


n = int(input())
num = list(map(int, input().split()))
if n < 10:
	if 0 in num:
		print(0)
	else:
		print(-1)
else:
	f = num.count(5)
	z = num.count(0)
	if z == 0:
		print(-1)
	elif f < 9:
		print(0)
	else:
		a = f // 9
		s = []
		for i in range(9 * a):
			s.append('5')
		for i in range(z):
			s.append('0')
		st = ''.join(s)
		print(st)


n = int(input())
a = list(map(int, input().split()))
cnt5 = 0
cnt0 = 0
for i in range(n):
	if a[i] == 0:
		cnt0 += 1
	else:
		cnt5 += 1
if cnt0 == 0:
	print(-1)
elif cnt5 < 9:
	print(0)
else:
	print((cnt5 - cnt5 % 9) * '5' + cnt0 * '0')


n = input()
cards = input().split()
c5 = 0
c0 = 0
for i in cards:
	if i == '5':
		c5 += 1
	if i == '0':
		c0 += 1
if c5 >= 9:
	if c0 > 0:
		cad = '5' * (c5 // 9) * 9
		cad += '0' * c0
	else:
		cad = '-1'
elif c0 > 0:
	cad = '0'
else:
	cad = '-1'
print(cad)


a = int(input())
l = list(map(int, input().split()))
zero = l.count(0)
five = l.count(5)
fiveRepetition = five // 9
if zero == 0:
	print(-1)
elif fiveRepetition == 0:
	print(0)
else:
	for i in range(fiveRepetition * 9):
		print(5, end='')
	for i in range(zero):
		print(0, end='')


n = int(input())
arr = list(map(int, input().split()))
srt = sorted(arr, reverse=True)
ans = 0
srt = [str(i) for i in srt]
while srt:
	if srt[0] == 0:
		print(0)
		exit()
	if srt[0] != '0' and int(''.join(srt)) % 90 == 0:
		print(''.join(srt))
		exit()
	else:
		check = srt.pop(0)
		if check == '0':
			print(0)
			exit()
print(-1)


x = int(input())
q = list(map(int, input().split()))
if q.count(5) >= 9 and q.count(0) >= 1:
	print(9 * int(q.count(5) / 9) * '5' + q.count(0) * '0')
elif q.count(0) > 0:
	print(0)
else:
	print(-1)


n = int(input())
l = [int(i) for i in input().split()]
if 0 not in l:
	print(-1)
else:
	p = l.count(5)
	p -= p % 9
	s = '5' * p
	s += '0' * l.count(0)
	print(int(s))


num = int(input())
str = input().split(' ')
five_count = str.count('5')
zero_count = str.count('0')
while five_count > 0:
	temp = five_count * 5
	if temp % 9 == 0:
		break
	five_count = five_count - 1
if five_count == 0 and zero_count > 0:
	print(0)
elif zero_count == 0:
	print(-1)
else:
	print('5' * five_count + '0' * zero_count)


n = int(input())
num = input()
f = num.count('5')
zero = num.count('0')
if zero == 0:
	print(-1)
elif f < 9:
	print('0')
else:
	print('5' * (f // 9 * 9) + '0' * (n - f))


n = int(input())
a = list(map(int, input().split()))
ans = 0
if a.count(0) == 0:
	print(-1)
else:
	for i in range(a.count(5)):
		if (a.count(5) - i) % 9 == 0:
			ans = a.count(5) - i
			break
	if ans == 0:
		print(0)
	else:
		print('5' * ans + a.count(0) * '0')


import sys
import math
n = int(sys.stdin.readline())
an = [int(x) for x in sys.stdin.readline().split()]
cfive = 0
czero = 0
for i in an:
	if i == 5:
		cfive += 1
	if i == 0:
		czero += 1
t = int(cfive / 9)
if czero == 0:
	print(-1)
elif t == 0:
	print(0)
else:
	for k in range(t * 9):
		sys.stdout.write(str(5))
	for k in range(czero):
		sys.stdout.write(str(0))
	sys.stdout.write('\n')


N = int(input())
Numbers = list(map(int, input().split()))
(Zeros, Fives) = (0, 0)
for Num in Numbers:
	if Num == 0:
		Zeros += 1
	else:
		Fives += 1
if Zeros == 0:
	print(-1)
elif Fives < 9:
	print(0)
else:
	print('5' * (Fives // 9 * 9) + '0' * Zeros)


def max_number():
	n = int(input())
	lst = [x for x in input().split()]
	num = ''
	num_zeros = 0
	num_fives = 0
	for i in lst:
		if i == '0':
			num_zeros += 1
		if i == '5':
			num_fives += 1
	if num_zeros:
		if num_fives // 9 == 0:
			return 0
		else:
			num = 9 * (num_fives // 9) * '5' + num_zeros * '0'
			return num
	else:
		return -1
print(max_number())


n = int(input())
a = list(map(int, input().split()))
c = a.count(5)
c1 = n - c
if c == n:
	print(-1)
else:
	print(int((c - c % 9) * '5' + c1 * '0'))


def main():
	n = int(input())
	cards = map(int, input().split(' '))
	cards2 = ''
	found = False
	ceros = 0
	highest = 0
	for card in cards:
		if card == 5:
			cards2 += str(card)
		else:
			ceros += 1
	for i in range(ceros):
		cards2 += str(0)
	for i in range(n):
		if int(cards2[i:n]) % 90 == 0 and int(cards2[i:n]) > highest:
			highest = int(cards2[i:n])
			found = True
			break
	for i in range(n):
		if int(cards2[0:n - i]) % 90 == 0 and int(cards2[0:n - i]) > highest:
			highest = int(cards2[0:n - i])
			found = True
			break
	if not found:
		if ceros > 0:
			print(0)
		else:
			print(-1)
	else:
		print(highest)
main()


n = int(input())
l = list(map(int, input().split()))
no5 = 0
no0 = 0
for i in range(n):
	if l[i] == 5:
		no5 = no5 + 1
	else:
		no0 = no0 + 1
if no5 < 9:
	if no0 >= 1:
		print(0)
	else:
		print(-1)
elif no0 >= 1:
	p = no5 // 9
	r = p * 9
	q = '5'
	print(q * r, end='')
	print('0' * no0)
else:
	print('-1')


n = int(input())
a = list(input().split())
y = []
z = []
for i in range(len(a)):
	if a[i] == '5':
		y.append(int(a[i]))
	else:
		z.append(int(a[i]))
if len(z) > 0:
	k = True
	i = len(y) - 1
	d = 0
	while k == True and i >= 0:
		c = []
		for j in range(i + 1):
			c.append(str(y[j]))
		b = ''.join(c)
		b = int(b)
		if b % 9 == 0:
			k = False
			d = b
		i = i - 1
	if d > 0:
		print(d, end='')
		for i in range(len(z)):
			print(z[i], end='')
	else:
		print(0)
else:
	print(-1)


n = int(input())
l = [int(i) for i in input().split()]
five_count = l.count(5)
zero_count = l.count(0)
count = five_count // 9
if count > 0 and zero_count > 0:
	print('5' * (count * 9) + '0' * l.count(0))
elif zero_count > 0:
	print(0)
else:
	print(-1)


n = int(input())
zeroes = 0
fives = 0
arr = list((int(x) for x in input().split()))
for i in range(n):
	temp = arr[i]
	if temp == 0:
		zeroes += 1
	else:
		fives += 1
if zeroes > 0:
	bunch = fives // 9
	for i in range(bunch):
		print('555555555', end='')
	if bunch > 0:
		for i in range(zeroes):
			print('0', end='')
	else:
		print(0)
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
count_5 = a.count(5) // 9
count_0 = a.count(0)
if count_0 <= 0:
	print(-1)
else:
	print(int('5' * (count_5 * 9) + '0' * count_0))


n = int(input())
arr = list(map(int, input().split()))
five_count = arr.count(5)
zero_count = n - five_count
x = five_count // 9
if x == 0:
	if zero_count != 0:
		print(0)
	else:
		print(-1)
elif zero_count == 0:
	print(-1)
else:
	print('5' * 9 * x + '0' * zero_count)


n = int(input())
a = list(map(int, input().split()))
nines = a.count(5)
zeros = a.count(0)
if nines < 9:
	if 0 in a:
		print(0)
	else:
		print(-1)
elif zeros == 0:
	print(-1)
else:
	pref = nines // 9 * 9
	print('5' * pref + '0' * zeros)


n = int(input())
A = [int(i) for i in input().split()]
zeros = sum([a == 0 for a in A])
fives = n - zeros
numgroups = fives // 9
if zeros == 0:
	print('-1')
	exit()
if numgroups > 0:
	s = ''
	for i in range(numgroups):
		s += '555555555'
	for i in range(zeros):
		s += '0'
	print(s)
	exit()
print('0')


from functools import reduce
n = int(input())
d = reduce(lambda x, y: (x[0] + y[0], x[1] + y[1]), [(1, 0) if x == '5' else (0, 1) for x in input().split()], (0, 0))
d = (d[0] // 9 * 9, d[1])
d = (d[0], 1 if d[0] == 0 and d[1] > 0 else d[1])
if d[1] > 0:
	print('5' * d[0] + '0' * d[1])
else:
	print(-1)


n = int(input())
temp = ''
li2 = []
li = list(map(int, input().split()))
if li.count(5) < 9:
	if li.count(0) > 0:
		print(0)
	else:
		print(-1)
else:
	li1 = [li[x] for x in range(n) if li[x] == 5]
	for i in range(len(li1)):
		temp += str(li1[i])
		if int(temp) % 9 == 0:
			li2.append(len(temp))
	temp1 = ''
	for i in range(max(li2)):
		temp1 += '5'
	for i in range(li.count(0)):
		temp1 += '0'
	if int(temp1) % 90 == 0:
		for i in range(max(li2)):
			print(5, end='')
		for i in range(li.count(0)):
			print(0, end='')
	else:
		print(-1)


N = int(input())
A = list(map(int, input().split()))
f = A.count(5)
z = A.count(0)
if z < 1:
	print(-1)
else:
	S = '5' * (f - f % 9) + '0' * z
	print(int(S))


n = int(input())
arr = list(map(int, input().split()))
temp = arr.count(5)
temp1 = arr.count(0)
if temp >= 9 and temp1 > 0:
	for i in range(0, temp // 9):
		print('555555555', end='')
	for i in range(0, temp1):
		print('0', end='')
elif temp1 > 0:
	print('0')
else:
	print(-1)


int(input())
ab = list(input().split())
ab = sorted(ab, reverse=True)
ab1 = ''
for i in ab:
	ab1 = ab1 + str(i)
ab1 = ab1.strip()
while int(ab1) % 90 != 0:
	ab1 = ab1[1:]
	if ab1 == '':
		break
if ab1 == '':
	print(-1)
elif ab1[0] == '0':
	print(0)
else:
	print(ab1)


n = int(input())
l = list(map(int, input().split()))
x = l.count(5)
y = l.count(0)
if x >= 9 and y > 0:
	print(''.join(['5' for i in range(x // 9 * 9)] + ['0' for i in range(y)]))
elif y > 0:
	print(0)
else:
	print(-1)


n = int(input())
l = list(map(int, input().split()))
f = l.count(5)
z = l.count(0)
if f // 9 > 0 and z > 0:
	print('5' * (9 * (f // 9)) + '0' * z)
elif z > 0:
	print(0)
else:
	print(-1)


def main():
	n = int(input())
	c = [int(i) for i in input().split()]
	fives = 0
	zeros = 0
	for i in c:
		if i == 0:
			zeros += 1
		else:
			fives += 1
	fives = fives // 9 * 9
	if fives > 0 and zeros > 0:
		ans = ''.join(['5' for _ in range(fives)])
		ans += ''.join(['0' for _ in range(zeros)])
	else:
		ans = '0' if zeros > 0 else '-1'
	print(ans)
main()


n = int(input())
t = 0
i = list(map(int, input().split()))
i.sort(reverse=True)
c = i.count(5)
p = i.count(0)
o = c % 9
if i[-1] == 0:
	for i in range(c - o):
		t = 10 * t + 5
	print(t * 10 ** p)
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
d = a.count(5)
z = a.count(0)
c = int(d / 9)
if d < 9:
	if z == 0:
		print(-1)
	else:
		print(0)
elif z == 0:
	print(-1)
else:
	for i in range(0, c * 9):
		print(5, end='')
	for i in range(0, z):
		print(0, end='')
	print()


n = int(input())
a = list(map(int, input().split()))
if a.count(0) == 0:
	print(-1)
else:
	s = int(a.count(5) / 9)
	if s == 0:
		print(0)
	else:
		w = ['5' for i in range(s * 9)]
		print(''.join(w) + '0' * a.count(0))


n = int(input())
l = input().split()
l1 = l.count('5')
if l1 >= 9 and n - l1 > 0:
	l2 = l1 % 9
	l3 = l1 - l2
	for i in range(l3):
		print('5', end='')
	for i in range(n - l1):
		print('0', end='')
elif n - l1 > 0:
	print('0')
else:
	print('-1')


import itertools
n = int(input())
lis = list(map(int, input().split()))
fives = lis.count(5)
zeros = lis.count(0)
if zeros == 0:
	print(-1)
elif fives < 9:
	print(0)
elif fives >= 9:
	res = fives // 9
	ans = ''
	for i in range(res * 9):
		ans += '5'
	for i in range(zeros):
		ans += '0'
	print(ans)
else:
	print(0)


n = int(input())
l = list(map(int, input().split()))
f_5 = l.count(5)
f_0 = l.count(0)
if f_5 / 9 < 1 and f_0 >= 1:
	print('0')
elif f_5 / 9 >= 1 and f_0 >= 1:
	res = '5' * (9 * int(f_5 / 9)) + '0' * f_0
	print(res)
else:
	print('-1')


(n, s) = (int(input()), input())
five = s.count('5')
one = n - five
if one:
	print(int('5' * (five - five % 9) + '0' * one))
else:
	print(-1)


import math
n = int(input())
l = list(map(int, input().split()))
zero = l.count(0)
if zero == 0:
	print(-1)
	exit(0)
five = l.count(5)
io = math.floor(five / 9)
io = io * 9
ans = '0'
for j in range(io):
	ans += '5'
for i in range(zero):
	ans += '0'
if int(ans) == 0 or len(ans) == 1:
	print(0)
else:
	print(int(ans))


n = int(input())
a = [int(i) for i in input().split()]
zero = 0
five = 0
for x in range(len(a)):
	if a[x] == 0:
		zero += 1
	elif a[x] == 5:
		five += 1
if zero == 0:
	print(-1)
else:
	v = five // 9
	if v > 0:
		print(''.join(map(str, [5] * (v * 9))), end='')
		print(''.join(map(str, [0] * zero)))
	else:
		print(0)


n = int(input())
li = list(map(int, input().split()))
su = sum(li)
tr = True
c = li.count(5)
while su > 0:
	t = su
	s = 0
	while t > 0:
		s += t % 10
		t = t // 10
	if s % 9 == 0 and 0 in li:
		print('5' * c + '0' * li.count(0))
		tr = False
		break
	su -= 5
	c -= 1
if tr and 0 in li:
	print(0)
elif tr:
	print(-1)


(n, a) = (int(input()), [int(i) for i in input().split()])
(n0, n5) = (a.count(0), a.count(5))
if n0 > 0:
	print(int('5' * (n5 // 9) * 9 + '0' * n0))
else:
	print(-1)


n = int(input())
fives = 0
zeros = 0
l = list(map(int, input().split()))
for i in l:
	if i == 0:
		zeros += 1
	else:
		fives += 1
prefix = fives // 9
if prefix == 0 and zeros > 0:
	print(0)
elif prefix == 0:
	print(-1)
elif zeros == 0:
	print(-1)
else:
	print('5' * prefix * 9 + '0' * zeros)


n = int(input())
l = list(map(int, input().split()))[:n]
a = l.count(0)
b = l.count(5)
c = 0
d = -1
while b >= 1 and a >= 1:
	c = int(str(5) * b + str(0))
	if c % 90 == 0:
		d = c
		break
	b -= 1
if d > 0:
	if d > 0 and a > 1:
		d = int(str(c) + '0' * (a - 1))
		print(d)
	else:
		print(d)
elif a > 0 and d == -1:
	print(0)
else:
	print(d)


n = int(input())
p = input()
p = list(map(int, p.split()))
c = p.count(5)
if c >= 9 and p.count(0) > 0:
	d = ''
	for i in range(c // 9 * 9):
		d += '5'
	for i in range(n - c):
		d += '0'
	print(d)
elif p.count(0) == 0:
	print(-1)
else:
	print(0)


import string
n = int(input())
str = input()
L = str.split()
S = 0
Szero = 0
Sfive = 0
while n > 0:
	S += int(L[n - 1])
	if int(L[n - 1]) == 0:
		Szero += 1
	else:
		Sfive += 1
	n -= 1
newstr = ''
if Sfive >= 9 and Szero > 0:
	i = int(Sfive / 9)
	i = i * 9
	while i > 0:
		newstr += '5'
		i -= 1
	i = Szero
	while i > 0:
		newstr += '0'
		i -= 1
	print(newstr)
elif Sfive < 9 and Szero > 0:
	print('0')
else:
	print('-1')


import sys

def main():
	n = int(input())
	numberOfFives = 0
	numberOfZeros = 0
	inputs = str(input()).split()
	for i in range(n):
		if int(inputs[i]) == 5:
			numberOfFives = numberOfFives + 1
		else:
			numberOfZeros = numberOfZeros + 1
	if numberOfZeros == 0:
		print(-1)
	elif numberOfFives < 9:
		print(0)
	else:
		numberOfFives = GetMaxFives(numberOfFives)
		for i in range(numberOfFives):
			sys.stdout.write('5')
		for i in range(numberOfZeros):
			sys.stdout.write('0')
	print()

def GetMaxFives(numberOfFives):
	i = numberOfFives
	while i > 8:
		if int('5' * i) % 9 == 0:
			return i
		i = i - 1
main()


(n, a) = (int(input()), input())
u = a.count('5')
v = n - u
if not v:
	print(-1)
else:
	print(int('5' * (u // 9 * 9) + '0' * v))


def fn(noOfFives, noOfZeros):
	s = ''
	for i in range(noOfFives):
		s = s + '5'
	if noOfZeros != 0:
		for i in range(noOfZeros):
			s = s + '0'
	return int(s)
n = input()
lst = input().split(' ')
noOfZeros = lst.count('0')
noOfFives = lst.count('5')
if noOfZeros == 0:
	print('-1')
else:
	noOfZeros = noOfZeros - 1
	s = 5 * noOfFives
	b = False
	while s >= 0:
		if s % 9 == 0:
			print(fn(noOfFives, noOfZeros + 1))
			b = True
			break
		s = s - 5
		noOfFives = noOfFives - 1
	if b == False:
		print('0')


n = int(input())
l = list(map(int, input().split()))
a = l.count(5)
b = l.count(0)
if a >= 9 and b > 0:
	a = a // 9
	m = 9 * a * [5] + b * [0]
	print(''.join(map(str, m)), end='')
elif b > 0:
	print('0')
else:
	print('-1')


n = int(input())
b = 0
a = list(map(int, input().split()))
for i in range(n):
	if a[i] == 5:
		b += 1
if b < 9 and n != b:
	print(0)
	exit(0)
if n == b:
	print(-1)
else:
	print(int('5' * (b // 9 * 9)) * 10 ** (n - b))


n = int(input())
a = list(map(int, input().split()))
if a.count(0) == 0:
	print(-1)
elif a.count(0) > 0 and a.count(5) < 9:
	print(0)
elif a.count(0) > 0 and a.count(5) >= 9:
	n5 = a.count(5) // 9 * 9
	n0 = a.count(0)
	for i in range(n5):
		print(5, end='')
	for i in range(n0):
		print(0, end='')


i = int
cin = input
le = len
s = str
n = i(cin())
arr = list(map(i, cin().split()))
cn = arr.count(5)
cn0 = arr.count(0)
if cn0 == 0:
	print(-1)
	exit()
for k in range(cn, 0, -1):
	r = '5' * k
	if i(r) % 9 == 0:
		print(r + '0' * cn0)
		exit()
print(0)


n = int(input())
t = list(map(int, input().split()))
zeros = 0
fives = 0
for i in t:
	if i == 5:
		fives += 1
	else:
		zeros += 1
if zeros == 0:
	print(-1)
elif fives < 9:
	print(0)
else:
	while sum(t) % 9 != 0:
		t.remove(5)
	for i in sorted(t, reverse=True):
		print(i, end='')


n = int(input())
numbers = input().split()
fives = 0
zeroes = 0
for n in numbers:
	if n == '5':
		fives += 1
	else:
		zeroes += 1
if zeroes == 0:
	print('-1')
	exit()
if fives < 9:
	print('0')
else:
	print('555555555' * int(fives / 9) + '0' * zeroes)


n = int(input())
numbers = list(map(int, input().split()))
count_5 = 0
count_0 = 0
for i in numbers:
	if i == 5:
		count_5 += 1
	else:
		count_0 += 1
num = ''
for i in range(count_5, 0, -1):
	num = '5' * i + str('0' * count_0)
	if int(num) % 90 == 0:
		print(num)
		exit()
if count_0 > 0:
	print(0)
else:
	print(-1)


n = int(input())
l = input().split(' ')
c_5 = l.count('5')
c_0 = l.count('0')
if c_0 == 0:
	print('-1')
else:
	only_5 = '5' * c_5
	k = ''
	while c_5 > 0:
		k = only_5[:c_5]
		if int(k) % 9 == 0:
			break
		c_5 -= 1
	if c_5 == 0:
		k = ''
	k += '0' * c_0
	print(int(k))


from collections import Counter
n = int(input())
a = [int(i) for i in input().split()]
d = Counter(a)
max5 = 0
for i in range(d[5], 0, -1):
	if 5 * i % 9 == 0:
		max5 = i
		break
ans = ''
if max5 != 0 and d[0] >= 1:
	for i in range(0, max5):
		ans += '5'
	for i in range(0, d[0]):
		ans += '0'
if ans == '':
	if d[0] >= 1:
		ans = '0'
	else:
		ans = -1
print(ans)


import sys
n = int(input())
an = list(map(int, sys.stdin.readline().split()))
zeros = 0
fives = 0
for i in range(n):
	if an[i] == 0:
		zeros += 1
	else:
		fives += 1
if zeros is 0:
	print(-1)
elif fives < 9:
	print(0)
else:
	ans = '5' * (fives - fives % 9)
	ans += '0' * zeros
	print(ans)


I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
n = I()
a = LI()
c1 = 0
c2 = 0
for i in range(n):
	if a[i] == 5:
		c1 += 1
	else:
		c2 += 1
c3 = c1 - c1 % 9
if c2 == 0:
	print(-1)
elif c3 == 0:
	print(0)
else:
	print('5' * c3 + '0' * c2)


n = int(input())
arr = []
count = 0
found = False
for i in input().split():
	arr.append(int(i))
	if i == '5':
		count += 1
if n == count:
	print(-1)
elif count < 9:
	print(0)
else:
	print('5' * (count - count % 9) + '0' * (n - count))


n = int(input(''))
arr = list(map(int, input().split()))
b = 0
c = 0
for i in arr:
	if i == 5:
		b = b + 1
	else:
		c = c + 1
e = b * 5
if c == 0:
	print(-1)
	exit()
while e % 9 != 0:
	e = e - 5
	b = b - 1
if b == 0 and c > 0:
	print(0)
	exit()
if b == 0 and c == 0:
	print(-1)
	exit()
for i in range(0, b):
	print('5', end='')
for i in range(0, c):
	print('0', end='')


n = int(input())
a = [int(i) for i in input().split()]
z = a.count(0)
f = a.count(5)
if z == 0:
	print(-1)
elif f < 9:
	print(0)
else:
	x = f % 9
	f -= x
	b = [5] * f
	c = [0] * z
	y = ''
	b = b + c
	for i in b:
		y += str(i)
	print(y)


n = int(input())
m = list(map(str, input().split()))
s = m.count('5')
t = m.count('0')
if t > 0:
	if s > 8:
		print(int('5' * (9 * (s // 9)) + '0' * t))
	else:
		print(0)
else:
	print(-1)


n = int(input())
a = [int(i) for i in input().split()]
fives = a.count(5)
ans = fives // 9
if n == fives:
	print(-1)
elif ans == 0:
	print(0)
else:
	print('5' * ans * 9 + (n - fives) * '0')


n = int(input())
a = sorted(list(map(int, input().split())))[::-1]
i = 0
if a[-1] == 5:
	print(-1)
else:
	while a[i] == 5:
		i += 1
	if i < 9:
		if i < n:
			print(0)
		else:
			print(-1)
	else:
		for j in range(i - i % 9):
			print('5', end='')
		for j in range(n - i):
			print('0', end='')


(n, t) = (int(input()), input())
c5 = t.count('5')
c0 = n - c5
print(int('5' * (c5 - c5 % 9) + '0' * c0) if c0 else -1)


n = int(input())
a = list(map(int, input().split()))
x = a.count(5)
y = a.count(0)
if x >= 9 and y > 0:
	b = x % 9
	b = x - b
	l = [5] * b + [0] * y
	print(*l, sep='', end='')
	exit()
elif x < 9 and y >= 1:
	print(0)
	exit()
else:
	print(-1)


n = int(input())
num = list(map(int, input().split()))
num = sorted(num, reverse=True)
cntr5 = 0
cntr0 = 0
res2 = ''
res3 = []
for i in range(0, len(num)):
	if num[i] == 0:
		cntr0 += 1
	elif num[i] == 5:
		cntr5 += 1
		res2 += str(num[i])
		if int(res2) % 9 == 0:
			res3.append(res2)
if len(res3) > 0:
	for i in range(0, cntr0):
		res3[-1] += '0'
if cntr0 == 0:
	print(-1)
elif cntr5 < 9:
	print(0)
else:
	print(res3[-1])


n = int(input())
a = [int(x) for x in input().split()]
k0 = 0
k5 = 0
for i in a:
	if i == 5:
		k5 += 1
	else:
		k0 += 1
if k0 == 0:
	print(-1)
	exit()
d = 0
while d * 9 <= k5:
	d += 1
d -= 1
x = 0
for i in range(d * 9):
	x = x * 10 + 5
for i in range(k0):
	x = x * 10
print(x)


def cards(n, a):
	f = a.count(5)
	z = a.count(0)
	ans = []
	if z == 0:
		return -1
	g = [5] * (f // 9 * 9)
	ans += g
	ze = [0] * z
	if ans == []:
		return 0
	else:
		ans += ze
		return ''.join(map(str, ans))
n = int(input())
a = list(map(int, input().split()))
print(cards(n, a))


n = int(input())
l = list(map(int, input().split()))
j = l.count(5)
k = l.count(0)
p = j - j % 9
if k == 0:
	print(-1)
elif j >= 9:
	print(p * '5' + k * '0')
else:
	print(0)


z = int(input())
x = list(input())
if '0' not in x:
	print(-1)
elif x.count('5') < 9:
	print(0)
else:
	s = x.count('5') % 9
	print('5' * (x.count('5') - s) + '0' * x.count('0'))


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
n = iinput()
a = list(map(int, input().split()))
l = a.count(5)
l1 = int(l / 9)
b = []
s = a.count(0)
if s == 0:
	print('-1')
elif l1 != 0:
	for i in range(l1 * 9):
		b.append(5)
	for j in range(s):
		b.append(0)
	for k in range(len(b)):
		print(b[k], end='')
else:
	print('0')


a = int(input())
lists = list(map(int, input().split()))
countnine = 0
countzero = 0
for i in lists:
	if i == 5:
		countnine += 1
	else:
		countzero += 1
if countnine >= 9 and countzero >= 1:
	val = int(countnine / 9)
	calc = val * 9
	for i in range(calc):
		print(5, end='')
	for i in range(countzero):
		print(0, end='')
elif countzero == 0:
	print(-1)
else:
	print(0)


n = int(input())
C = list(map(int, input().split()))
five = C.count(5)
zero = n - five
s = ''
if five // 9 > 0 and zero > 0:
	for i in range(0, five // 9):
		s = s + '555555555'
	for k in range(0, zero):
		s = s + '0'
elif zero > 0:
	s = s + '0'
else:
	s = s + '-1'
print(s)


n = int(input())
q = [int(x) for x in input().split()]
ans = -1
if q.count(0):
	ans = 0
	if q.count(5) >= 9:
		ans = '5' * (q.count(5) // 9 * 9) + '0' * q.count(0)
print(ans)


n = int(input())
(number_0s, number_5s) = (0, 0)
for number in input().split(' '):
	if number == '0':
		number_0s += 1
	else:
		number_5s += 1
if number_0s == 0:
	print('-1')
elif number_5s < 9:
	print('0')
else:
	max_numbers5 = number_5s // 9 * 9
	for i in range(max_numbers5):
		print('5', end='')
	for i in range(number_0s):
		print('0', end='')
	print('')


n = int(input())
a = list(map(int, input().split()))
x = a.count(5)
y = a.count(0)
if x // 9 == 0 and y != 0:
	print('0')
elif y == 0:
	print('-1')
else:
	for i in range(x // 9):
		print('555555555', end='')
	for i in range(y):
		print('0', end='')


n = int(input())
a = list(map(int, input().split()))
f = a.count(5)
z = a.count(0)
aa = f // 9
if aa >= 1 and z >= 1:
	ans = ''
	for i in range(aa):
		ans += '555555555'
	for i in range(z):
		ans += '0'
	print(ans)
elif z > 0:
	print(0)
else:
	print(-1)


n = int(input())
l = [int(x) for x in input().split()]
a = l.count(5)
b = 0
c = 0
for i in range(n):
	if l[i] == 0:
		b = 1
		c = l.count(0)
		break
if b == 0:
	print(-1)
elif a // 9 >= 1:
	print('5' * 9 * (a // 9) + '0' * c)
else:
	print(0)


n = int(input())
arr = list(map(int, input().split()))
five = 0
zero = 0
for _ in range(0, n):
	if arr[_] == 5:
		five = five + 1
	else:
		zero = zero + 1
if zero == 0:
	print('-1')
elif five >= 9:
	count = five // 9
	while count > 0:
		for _ in range(0, 9):
			print('5', sep='', end='')
		count = count - 1
	for _ in range(0, zero):
		print('0', sep='', end='')
	print('')
else:
	print('0')


n = int(input())
a = list(map(int, input().split()))

def chrnum(s, a):
	ans = 0
	for i in s:
		if i == a:
			ans += 1
	return ans

def list_to_str(a):
	y = ''
	for i in range(len(a)):
		y = y + str(a[i])
	return y
if chrnum(a, 5) // 9 > 0 and 0 in a:
	ans = chrnum(a, 5) // 9 * 9 * '5' + chrnum(a, 0) * '0'
	print(ans)
elif 0 in a:
	print(0)
else:
	print(-1)


(_, num) = (input(), list(input().split()))
if num.count('5') < 9 and num.count('0') >= 1:
	print(0)
elif num.count('0') < 1 or num.count('5') < 9:
	print(-1)
else:
	print('5' * 9 * int(num.count('5') / 9) + '0' * num.count('0'))


n = int(input())
num = list(map(int, input().split()))
import collections
c = collections.Counter(num)
x = int(c[5] / 9)
if c[0] == 0:
	print('-1')
elif c[5] < 9 or c[0] < 0:
	print('0')
else:
	res = ['5' for _ in range(1, int(9 * x) + 1)]
	for i in range(1, c[0] + 1):
		res.append('0')
	print(''.join(res))


n = int(input())
(f, z) = (0, 0)
for x in input().split():
	if x == '5':
		f += 1
	else:
		z += 1
if z == 0:
	print(-1)
elif f // 9 == 0:
	print(0)
else:
	print('5' * (f // 9 * 9) + '0' * z)


n = int(input())
l = list(map(int, input().split()))
if l.count(0) == 0:
	print('-1')
elif l.count(5) < 9:
	print('0')
else:
	a = l.count(5)
	while True:
		b = ''
		for i in range(a):
			b = b + '5'
		if int(b) % 9 == 0:
			break
		a -= 1
	for i in range(a):
		print('5', end='')
	for j in range(l.count(0)):
		print('0', end='')


n = int(input())
seq = list(map(int, input().split()))
five = 0
zero = 0
for i in seq:
	if i == 5:
		five += 1
	elif i == 0:
		zero += 1
if five >= 9:
	mod = five % 9
	if zero >= 1:
		print((five - mod) * '5' + zero * '0')
	else:
		print(-1)
elif zero > 0:
	print(0)
else:
	print(-1)


n = int(input())
arr = list(map(int, input().split()))
count_0 = 0
count_5 = 0
for i in range(n):
	if arr[i] == 0:
		count_0 += 1
	if arr[i] == 5:
		count_5 += 1
if count_0 == 0:
	print(-1)
elif count_5 == 0 and count_0 != 0:
	print(0)
elif count_5 * 5 % 9 == 0:
	print(str(5) * count_5 + str(0) * count_0)
else:
	count_5 -= 1
	while count_5 != 0:
		if count_5 * 5 % 9 == 0:
			print(str(5) * count_5 + str(0) * count_0)
			break
		count_5 -= 1
	if count_5 == 0 and count_0 != 0:
		print(0)


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	if arr.count(5) >= 9 and arr.count(0) >= 1:
		ans = '5' * (arr.count(5) // 9) * 9 + '0' * arr.count(0)
		print(ans)
	elif arr.count(0) == 0:
		print('-1')
	else:
		print('0')
main()


n = input()
list = input().split()
n5 = list.count('5')
n0 = list.count('0')
if n0 == 0:
	print('-1\n')
elif n5 < 9:
	print('0\n')
else:
	print5 = ('{:5<' + str(n5 - n5 % 9) + '}').format('')
	print0 = ('{:0<' + str(n0) + '}').format('')
	print(print5 + print0)


n = int(input())
a = list(map(int, input().split()))
n5 = a.count(5)
n0 = n - n5
if n0 == 0:
	print(-1)
elif n5 < 9:
	print('0')
else:
	print('5' * (9 * (n5 // 9)) + '0' * n0)


def ans(l, a):
	l.sort()
	l.reverse()
	f = 0
	t = 0
	s = ''
	for i in range(0, a, 1):
		s += str(l[i])
	while a > 0:
		d = int(s[t:len(s)])
		if d % 90 == 0:
			f = 1
			break
		else:
			f = 0
		t += 1
		a -= 1
	if f:
		print(d)
	else:
		print('-1')
a = int(input())
l = list(map(int, input().split()))
ans(l, a)


n = int(input())
lst = list(map(str, input().split()))
if '0' in lst:
	lst.sort()
	lst.reverse()
	num = 0
	while lst[0] != 0:
		if int(''.join(lst)) % 90 == 0:
			num = int(''.join(lst))
			break
		else:
			lst.pop(0)
	print(num)
else:
	print(-1)


from bisect import *
n = int(input())
arr = list(map(int, input().split()))
if 0 in arr:
	arr.sort()
	c0 = bisect_right(arr, 0) - bisect_left(arr, 0)
	c5 = bisect_right(arr, 5) - bisect_left(arr, 5)
	for i in range(c5 // 9):
		for j in range(9):
			print('5', end='')
	if c5 // 9 > 0:
		for j in range(c0):
			print('0', end='')
	else:
		print('0')
else:
	print('-1')


n = int(input())
s = len([1 for i in input() if i == '5'])
print(-1 if s == n else 0 if s < 9 else ''.join(['5'] * (s // 9) * 9 + ['0'] * (n - s)))


I = input
I()
s = I()
(q, w) = (0, 0)
for i in s:
	if i == '5':
		q += 1
	elif i == '0':
		w += 1
print(int('5' * (q // 9) * 9 + '0' * w) if w else -1)


n = int(input())
l = list(map(int, input().split()))
l.sort()
l = l[::-1]
if l[-1] != 0:
	print(-1)
else:
	zero = l[l.index(0):]
	l = l[:l.index(0)]
	i = len(l)
	if i == 0:
		print(0)
	while i > 0:
		if sum(l) % 9 == 0:
			l = l + zero
			print(''.join([str(x) for x in l]))
			break
		i -= 1
		l.pop()
		if i == 0:
			print(0)


n = int(input())
a = [int(x) for x in input().split()]
if a.count(5) >= 9 and a.count(0) > 0:
	print('5' * (9 * (a.count(5) // 9)) + '0' * a.count(0))
elif a.count(0) > 0:
	print(0)
else:
	print(-1)


s = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
ans = ''
if l.count(0) >= 1:
	ans += l.count(0) * '0'
	ans = l.count(5) // 9 * '5' * 9 + ans
	ans = int(ans)
	print(ans)
else:
	print(-1)


x = int(input())
a = list(str(input().replace(' ', '')))
a.sort(reverse=True)
y = a.count('0')
flag = 0
if y == 0:
	print(-1)
else:
	while a.count('5') > 0:
		if a.count('5') * 5 % 9 == 0:
			print(''.join(a))
			flag += 1
			break
		else:
			a.pop(a.count('5') - 1)
	if flag == 0:
		print(0)


n = int(input())
a = list(map(int, input().split()))
if 0 not in a:
	print(-1)
elif a.count(5) < 9:
	print(0)
else:
	res = '0' * a.count(0)
	for i in range(a.count(5) // 9):
		res = '5' * 9 + res
	print(res)


a = int(input())
mass = list(map(int, input().split()))
mass_5 = []
mass_0 = []
string = ''
for i in range(a):
	if mass[i] == 5:
		mass_5.append(mass[i])
	else:
		mass_0.append(mass[i])
l = int(len(mass_5) / 9)
for i in range(l * 9):
	string += str(mass_5[i])
if not mass_0:
	print(-1)
elif l != 0:
	string += ''.join(map(str, mass_0))
	print(string)
elif mass_0:
	print(0)


n = int(input())
arr = [int(x) for x in input().split(' ')]
cnt_5 = arr.count(5)
cnt_0 = arr.count(0)
while cnt_5 != 0:
	if 5 * cnt_5 % 9 == 0:
		break
	cnt_5 -= 1
if cnt_5 == 0:
	if cnt_0 == 0:
		print('-1')
	else:
		print('0')
elif cnt_0 != 0:
	print('5' * cnt_5 + '0' * cnt_0)
else:
	print(-1)


a = int(input())
t = list(map(int, input().split()))
p = sum(t) // 5
if t.count(0) > 0:
	h = 0
	for k in range(p, 0, -1):
		if 5 * k % 9 == 0:
			print(k * '5' + '0' * t.count(0))
			h += 1
			break
	if h == 0:
		print(0)
else:
	print(-1)


n = int(input())
s = list(map(int, input().split()))
count5 = 0
count0 = 0
op = '0'
for i in s:
	if i == 5:
		count5 += 1
	if i == 0:
		count0 += 1
if count0 == 0:
	print(-1)
elif count5 < 9:
	print(0)
else:
	for i in range(1, count5 - count5 % 9 + 1):
		print(5, end='')
	for i in range(0, count0):
		print(0, end='')


n = input()
b1 = list(input())
a = b1.count('5')
b = b1.count('0')
b1.sort()
b1.reverse()
t = 0
r = ''
if b != 0:
	while t == 0:
		for i in b1:
			r = r + i
		if int(r) % 90 == 0:
			t = 1
		else:
			b1.reverse()
			b1.pop()
			b1.reverse()
			r = ''
	print(int(r))
else:
	print('-1')


n = int(input())
a = [int(a) for a in input().split()]
count = 0
for i in a:
	if i == 5:
		count += 1
zero = n - count
extra = count % 9
ans = ''
if zero == 0:
	print(-1)
elif count < 9 and zero == 0:
	print(-1)
elif count < 9 and zero != 0:
	print(0)
else:
	print(str(5) * (count - extra) + str(0) * zero)


import sys
n = int(input())
a = list(map(int, input().split()))
f = 0
for i in range(n):
	if a[i] == 5:
		f += 1
z = n - f
f = f // 9
if z == 0:
	print('-1')
elif f == 0 and z > 0:
	print('0')
else:
	f = 9 * f
	for i in range(f):
		sys.stdout.write('5')
	for i in range(z):
		sys.stdout.write('0')


n = int(input())
l = input().split()
a = l.count('0')
b = n - a
if a == 0:
	print(-1)
else:
	b = b // 9 * 9
	if b == 0:
		print(0)
	else:
		ans = ['5'] * b + ['0'] * a
		print(''.join(ans))


n = int(input())
arr = list(map(int, input().split()))
c0 = arr.count(0)
c5 = n - c0
if c5 >= 9 and c0 > 0:
	str = c5 // 9 * 9 * '5'
	str += c0 * '0'
elif c0 >= 1:
	str = '0'
else:
	str = '-1'
print(str)


def f(l):
	n = len(l)
	n0 = sum([i == 0 for i in l])
	if n0 == 0:
		return -1
	n5 = n - n0
	n5 = n5 // 9 * 9
	if n5 == 0:
		return 0
	return ''.join(['5'] * n5 + ['0'] * n0)
_ = input()
l = list(map(int, input().split()))
print(f(l))


n = int(input())
a = map(int, input().split())
n0 = 0
n5 = 0
for i in a:
	if i == 0:
		n0 += 1
	else:
		n5 += 1
if n0 == 0:
	print(-1)
	exit()
x = n5 // 9
if x == 0:
	print(0)
	exit()
for i in range(9 * x):
	print(5, end='')
for i in range(n0):
	print(0, end='')
print()


n = int(input())
cnt = input().count('5')
if n == cnt:
	print('-1')
elif cnt < 9:
	print('0')
else:
	print('5' * (cnt // 9 * 9) + '0' * (n - cnt))


n = int(input())
l = list(map(int, input().split()))
c0 = 0
c5 = 0
for i in range(n):
	if l[i] == 0:
		c0 += 1
	else:
		c5 += 1
if c0 == 0:
	print(-1)
else:
	print(int('555555555' * (c5 // 9) + '0' * c0))


import sys
n = int(input())
arr = list(map(int, input().split()))
no_of_five = no_of_zeros = 0
for num in arr:
	if num:
		no_of_five += 1
	else:
		no_of_zeros += 1
if no_of_five >= 9:
	limit = 0
	if no_of_five % 9 != 0 and no_of_zeros:
		limit = no_of_five - no_of_five % 9
	elif no_of_five % 9 == 0 and no_of_zeros:
		limit = no_of_five
	else:
		print('-1', end='')
		sys.exit()
	for _ in range(limit):
		print('5', end='')
	if no_of_zeros:
		for _ in range(no_of_zeros):
			print('0', end='')
elif no_of_zeros:
	print('0', end='')
else:
	print('-1', end='')


n = int(input())
l = list(map(int, input().strip().split()))
f = l.count(5)
zer = l.count(0)
if f == 0:
	print(0)
elif zer == 0:
	print(-1)
else:
	r = f // 9
	if r == 0:
		print(0)
	else:
		print('5' * (r * 9) + '0' * zer)


input()
a = input().split()
b = c = 0
for i in a:
	if i == '5':
		b += 1
	else:
		c += 1
if c == 0:
	print(-1)
elif b < 9:
	print(0)
else:
	print('5' * (b // 9 * 9) + '0' * c)


n = int(input())
nums = [int(x) for x in input().split(' ')]
count = nums.count(5)
countZeros = len(nums) - count
if countZeros == 0:
	print(-1)
elif count // 9 >= 1:
	count = count // 9 * 9
	ans = '5' * count + '0' * countZeros
	print(ans)
else:
	print(0)


five = 0
zero = 0
n = int(input())
a = input()
m = len(a)
for i in range(m):
	if a[i] == '0':
		zero += 1
	elif a[i] == '5':
		five += 1
if zero == 0:
	print(-1)
elif five < 9:
	print(0)
else:
	temp = ''
	five -= five % 9
	for i in range(five):
		temp += '5'
	for i in range(zero):
		temp += '0'
	print(temp)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import bisect as bi
import heapq

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def main():
	try:
		n = I()
		l = list(In())
		(a, b) = (0, 0)
		for x in l:
			if x == 5:
				a += 1
		b = n - a
		if b == 0:
			print(-1)
		elif a // 9 > 0:
			a -= a % 9
			ans = ['5'] * a
			for i in range(b):
				ans.append('0')
			print(''.join(ans))
		else:
			print(0)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(1):
	main()


a = int(input())
r = input().split(' ')
r = ''.join(r)
m = 0
count = r.count('0')
r = r.replace('0', '')
z = '0' * count
for i in range(1, len(r) + 1):
	if int(r[:i] + z) % 90 == 0:
		m = int(r[:i] + z)
	if m != 0:
		exit
if m == 0 and count == 0:
	print(-1)
else:
	print(m)


import math

def ost(a, b):
	if a % b != 0:
		return 1
	return 0
n = int(input())
data = list(map(int, input().split()))
a = data.count(5)
b = data.count(0)
if a // 9 == 0:
	if b > 0:
		print(0)
	else:
		print(-1)
elif b >= 1:
	ans = [5] * (a // 9 * 9) + [0] * b
	print(''.join(map(str, ans)))
else:
	print(-1)


n = int(input())
d = dict()
d[5] = 0
d[0] = 0
a = list(map(int, input().split()))
for i in range(n):
	d[a[i]] += 1
d[5] -= d[5] % 9
if d[5] == 0 and d[0] == 0:
	print(-1)
elif d[5] == 0:
	print(0)
elif d[0] == 0:
	print(-1)
else:
	for i in range(d[5]):
		print(5, sep='', end='')
	for i in range(d[0]):
		print(0, sep='', end='')


def list_to_int(numbers):
	return int(''.join(['%d' % x for x in numbers]))
n = int(input())
line = list(map(int, input().split()))
fives = 0
zeros = 0
for i in line:
	if i == 5:
		fives += 1
	elif i == 0:
		zeros += 1
ans = ''
if fives >= 9 and zeros > 0:
	for _ in range(fives // 9):
		ans += '555555555'
	for _ in range(zeros):
		ans += '0'
elif zeros > 0:
	print('0')
else:
	print('-1')
print(ans)


count = 0

def func(s):
	if s == '5':
		global count
		count = count + 1
	return int(s)
n = int(input())
L = map(func, input().split())
L = list(L)
L.sort()
ans = ''
if count == n:
	print(-1)
elif count < n and count % 9 == 0 and (count != 0):
	ans = '5' * count
	ans += '0' * (n - count)
	print(ans)
elif count < n and count // 9 > 0 and (count != 0):
	ans = ans + '0' * (n - count)
	newCount = count
	while newCount % 9 != 0:
		newCount -= 1
	ans = '5' * newCount + ans
	print(ans)
elif count < n and count // 9 <= 0:
	print(0)


from sys import stdin, stdout
input = stdin.readline
n = map(int, input().split())
a = input().split()
zer = fiv = 0
for i in a:
	if i == '0':
		zer += 1
	else:
		fiv += 1
if zer == 0:
	stdout.write('-1')
else:
	ans = '0'
	for i in range(1, fiv + 1):
		if i * 5 % 9 == 0:
			ans = '5' * i
	ans += '0' * zer
	if zer > 1 and (not fiv < 9):
		stdout.write(ans)
	elif fiv < 9:
		stdout.write('0')
	else:
		stdout.write(ans)


n = int(input())
ais = list(map(int, input().split()))
s5 = '5' * (ais.count(5) // 9 * 9)
s0 = '0' * ais.count(0)
if s0 == '':
	s = '-1'
else:
	s = s5 + s0
print(int(s))


def main(N, arr):
	cnt5 = 0
	cnt0 = 0
	for i in range(N):
		if arr[i] == 5:
			cnt5 += 1
		else:
			cnt0 += 1
	if cnt0 == 0:
		print(-1)
	elif cnt5 // 9 < 1:
		print(0)
	else:
		res = '5' * (cnt5 - cnt5 % 9) + '0' * cnt0
		print(res)

def text():
	with open('input.txt', 'r') as f:
		t = int(f.readline().rstrip())
		while t:
			N = int(f.readline().rstrip())
			arr = list(map(int, f.readline().rstrip().split()))
			main(N, arr)
			t -= 1

def normal():
	N = int(input())
	arr = list(map(int, input().split()))
	main(N, arr)
normal()


n = int(input())
l = list(map(int, input().split()))
c = l.count(5)
z = n - c
if z == 0:
	print(-1)
elif c < 9:
	print(0)
else:
	while True:
		temp = sum([5] * c)
		if temp % 9 == 0:
			break
		else:
			c -= 1
	print(''.join(['5'] * c + ['0'] * z))


a = int(input())
b = input().split()
l = []
for i in range(a):
	l.append(int(b[i]))
if 0 not in l:
	print(-1)
else:
	p = l.count(5)
	if p < 9:
		print(0)
	else:
		n = p - p % 9
		m = l.count(0)
		s = ''
		for i in range(n):
			s = s + str(5)
		for i in range(m):
			s = s + str(0)
		print(s)


n = int(input())
l1 = [int(x) for x in input().split()]
if 0 not in l1:
	print(-1)
elif l1.count(5) < 9:
	print(0)
else:
	while l1.count(5) % 9:
		l1.remove(5)
	l1.sort(reverse=True)
	print(''.join([str(x) for x in l1]))


n = int(input())
A = list(map(int, input().split()))
if 0 not in A:
	print(-1)
elif A.count(5) < 9:
	print(0)
else:
	print((A.count(5) - A.count(5) % 9) * str(5) + A.count(0) * str(0))


n = int(input())
ls = list(map(str, input().split()))
ls.sort(reverse=True)
divided = False
while len(ls) > 0:
	num = int(''.join(ls))
	if num % 90 == 0:
		divided = True
		break
	ls.remove(ls[0])
if not divided:
	print(-1)
else:
	print(num)


n = int(input())
five = 0
zero = 0
for x in map(int, input().split()):
	if x == 5:
		five += 1
	else:
		zero += 1
five //= 9
five *= 9
if zero:
	for _ in range(five):
		print(5, end='')
	if five:
		for _ in range(zero):
			print(0, end='')
	else:
		print(0)
else:
	print(-1)


n = input()
a = input().split()
a_5 = a.count('5') // 9
a_1 = a.count('0')
if a_1 == 0:
	print(-1)
elif a_5 == 0:
	print(0)
else:
	print('5' * 9 * a_5 + '0' * a_1)


import sys, threading, os.path
import collections, heapq, math, bisect
import string
from platform import python_version
import itertools
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)

def main():
	if os.path.exists('input.txt'):
		input = open('input.txt', 'r')
	else:
		input = sys.stdin
	n = int(input.readline())
	lis = list(map(int, input.readline().split()))
	count = [0] * 2
	for x in lis:
		if x == 0:
			count[0] += 1
		else:
			count[1] += 1
	if count[0] == 0:
		output = -1
	else:
		(str1, ans) = ('', '')
		for i in range(count[1]):
			str1 += '5'
			if len(str1) % 9 == 0:
				ans = str1 + '0'
		if count[0] > 1:
			for i in range(count[0] - 1):
				ans += '0'
		if count[0] == 1 and len(ans) == 0 or ans[0] == '0':
			ans = '0'
		output = ans
	if os.path.exists('output.txt'):
		open('output.txt', 'w').writelines(str(output))
	else:
		sys.stdout.write(str(output))
main()


n = int(input())
arr = [int(x) for x in input().split()]
d = dict()
for a in arr:
	if a in d:
		d[a] += 1
	else:
		d[a] = 1
if 5 in d and d[5] >= 9:
	if 0 in d:
		print(d[5] // 9 * 9 * '5' + d[0] * '0')
	else:
		print(-1)
elif 0 in d:
	print(0)
else:
	print(-1)


n = int(input())
lis = list(map(int, input().split()))
su = lis.count(5) * 5
nof = su // 45
if lis.count(0) is 0:
	print(-1)
elif lis.count(0) > 0 and nof is 0:
	print(0)
else:
	print('5' * (9 * nof), end='')
	print('0' * lis.count(0))


import math
n = int(input())
a = list(map(int, input().split()))
five = a.count(5)
zero = a.count(0)
m = 0
for i in range(1, five + 1):
	if 5 * i % 9 == 0:
		m = i
if m == 0:
	if zero == 0:
		print(-1)
	else:
		print(0)
elif zero == 0:
	print(-1)
else:
	print('5' * m + '0' * zero)


(n, t) = (int(input()), input())
a = t.count('5')
b = n - a
print(int('5' * (a - a % 9) + '0' * b) if b else -1)


n = int(input())
z = 0
o = 0
a = list(map(int, input().split()))
for i in a:
	if i == 0:
		z += 1
	else:
		o += 1
if o / 9 >= 1:
	if z > 0:
		o -= o % 9
		while o > 0:
			print('5', end='')
			o -= 1
		while z > 0:
			print('0', end='')
			z -= 1
	else:
		print('-1')
elif z > 0:
	print('0')
else:
	print('-1')


n = int(input())
a = list(map(int, input().split(' ')))
a.sort(reverse=True)
a = [str(x) for x in a]
c = 0
while len(a) != 0:
	s = ''.join(a)
	t = int(s)
	if t % 90 == 0:
		if t == 0:
			print('0')
		else:
			print(t)
		c = 1
		break
	else:
		v = a.pop(0)
if c == 0:
	print('-1')


def jeffAndDigits():
	n = int(input())
	s = [int(x) for x in input().split()]
	fivec = 0
	zeroc = 0
	fiven = 0
	number = ''
	for i in s:
		if i == 5:
			fivec = fivec + 1
		if i == 0:
			zeroc = zeroc + 1
	if fivec < 9 and zeroc > 0:
		print(0)
		return
	elif zeroc == 0:
		print(-1)
		return
	for i in range(fivec, 8, -1):
		if i % 9 == 0:
			fiven = i
			break
	for i in range(fiven):
		number = number + '5'
	for i in range(zeroc):
		number = number + '0'
	print(number)
jeffAndDigits()


n = int(input())
A = list(map(int, input().split()))
c0 = A.count(0)
c5 = A.count(5)
if not c0 > 0:
	print(-1)
else:
	print('5' * (c5 // 9) * 9 + '0' * [1, c0][c5 >= 9])


N = int(input())
A = list(map(int, input().split()))
N0 = A.count(0)
N5 = A.count(5)
if N0 == 0:
	print(-1)
elif N5 < 9:
	print(0)
else:
	T = N5 // 9
	print('555555555' * T + '0' * N0)


def getIn():
	return input().strip()
N = int(getIn())
cards = list(map(int, getIn().split(' ')))
tot = sum(cards)
cnt = cards.count(5)
if cnt == N:
	print(-1)
else:
	rem = tot - tot // 45 * 45
	if rem % 5 == 0:
		if tot // 45 * 45 // 5 == 0:
			print(0)
		else:
			print(tot // 45 * 45 // 5 * '5' + (N - cnt) * '0')
	else:
		print(0)


import sys
input = sys.stdin.readline
n = int(input())
arr = list(map(int, input().split()))
c_zero = arr.count(0)
c_five = n - c_zero
result = '-1'
if c_five / 9 >= 1 and c_zero > 0:
	result = '5' * (c_five // 9 * 9) + '0' * c_zero
elif c_zero > 0:
	result = '0'
print(result)


n = int(input())
arr = list(map(int, input().split()))
c5 = 0
c0 = 0
for i in arr:
	if i == 5:
		c5 += 1
	else:
		c0 += 1
k = c5 // 9
if k > 0 and c0 > 0:
	print(9 * k * '5' + c0 * '0')
elif c0 > 0:
	print(0)
else:
	print(-1)


import sys
f = sys.stdin
n = int(f.readline())
a = f.readline().strip().split(' ')
b = a.count('5')
c = a.count('0')
print(int('5' * (b - b % 9) + '0' * c) if c else -1)


n = int(input())
a = list(map(int, input().split()))
if a.count(0) == 0:
	print('-1')
elif a.count(5) < 9:
	print('0')
else:
	res = a.count(5)
	res //= 9
	print(str(res * 9 * '5') + '0' * a.count(0))


a = input()
b = input()
if not b.count('0'):
	print(-1)
elif b.count('5') >= 9:
	cont = b.count('5')
	print(cont // 9 * 9 * '5' + b.count('0') * '0')
else:
	print('0')


import sys

def main():
	n = int(sys.stdin.readline().strip())
	m = sys.stdin.readline().strip().split()
	cz = 0
	cf = 0
	res = ''
	for i in range(len(m)):
		m[i] = int(m[i])
		if m[i] == 0:
			cz += 1
		else:
			cf += 1
	if cz == 0:
		print(-1)
		return
	elif cf < 9:
		print(0)
		return
	else:
		for i in range(cf // 9):
			res += '555555555'
		while cz != 0:
			cz -= 1
			res += '0'
	print(res)
	return
main()


a = input()
fives = 0
zeros = 0
b = input().split()
for i in b:
	i = int(i)
	if i == 5:
		fives += 1
	else:
		zeros += 1
if zeros == 0:
	print(-1)
elif fives < 9:
	print(0)
else:
	while fives / 9 >= 1:
		print(555555555, end='')
		fives -= 9
	while zeros:
		print(0, end='')
		zeros -= 1


from math import gcd
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def prog_name():
	n = int(input())
	l = list(map(int, input().split()))
	if 0 not in l:
		print(-1)
	else:
		f = l.count(5)
		if f < 9:
			print(0)
		else:
			print('5' * (f // 9 * 9) + '0' * l.count(0))

def main():
	t = 1
	for unique in range(t):
		prog_name()
main()


n = int(input())
z = input().split().count('0')
f = n - z
a = (f // 9 * 9 * '5' + '0' * z, -(z < 1))[f < 9 or z < 1]
print(a)


n = int(input())
a = [int(i) for i in input().split()]
fives = a.count(5)
zeros = n - fives
if zeros == 0:
	print(-1)
	exit(0)
if fives < 9:
	print(0)
	exit(0)
print('5' * (fives // 9 * 9) + '0' * zeros)


n = int(input())
l = [int(x) for x in input().split()]
a = l.count(5)
b = n - a
if b > 0:
	ans = 0
	c = a // 9
	c = c * 9
	s = '5' * c
	if c > 0:
		print(s + '0' * b)
	else:
		print('0')
else:
	print(-1)


n = int(input())
y = [int(m) for m in input().split()]
c5 = y.count(5)
c0 = y.count(0)
if c0 == 0:
	print(-1)
elif c5 < 9:
	print(0)
else:
	print('5' * (9 * (c5 // 9)) + '0' * c0)


n = int(input())
cards = list(map(int, input().split()))
five = cards.count(5) - cards.count(5) % 9
zero = cards.count(0)
if zero == 0:
	print(-1)
elif five == 0:
	print(0)
else:
	print('5' * five + zero * '0')


n = int(input())
a = list(map(int, input().split()))
(five, zero) = (0, 0)
for item in a:
	if item == 5:
		five += 1
	if item == 0:
		zero += 1
if zero == 0:
	print(-1)
elif five < 9:
	print(0)
else:
	ans = []
	no_of_five = five - five % 9
	for i in range(no_of_five):
		ans.append('5')
	for i in range(zero):
		ans.append('0')
	print(''.join(ans))


n = int(input())
m = list(map(int, input().split()))
a0 = [i for i in m if i == 0]
b0 = len(a0)
a5 = [i for i in m if i == 5]
b5 = len(a5)
if b0 < 1:
	print(-1)
elif sum(a5) == 0:
	print(0)
elif b5 // 9 > 0:
	d5 = [5] * (b5 // 9 * 9)
	for i in range(b0):
		d5.append(0)
	print(''.join(map(str, d5)))
elif b0 > 0:
	print(0)
else:
	print(-1)


n = int(input())
a = input().split()
ans = ''
if a.count('5') > 8 and a.count('0'):
	ans = '5' * (a.count('5') - a.count('5') % 9) + '0' * a.count('0')
elif a.count('0') == 0:
	ans = -1
else:
	ans = '0'
print(ans)


n = int(input())
a = list(map(int, input().split()))
num5 = a.count(5)
num0 = a.count(0)
if num0 == 0:
	print(-1)
elif num5 < 9:
	print(0)
else:
	num5 -= num5 % 9
	ans = '5' * num5 + '0' * num0
	print(ans)


n = int(input())
five = int(input().count('5'))
zero = n - five
print(int('5' * (five - five % 9) + '0' * zero) if zero else -1)


n = int(input())
lst = list(map(int, input().split()))
z = 0
five = 0
for i in lst:
	if i == 0:
		z += 1
	elif i == 5:
		five += 1
if z == 0:
	print(-1)
elif five < 9:
	print(0)
else:
	i = 9
	j = 0
	tmp = '5' * i + '0' * z
	while i <= five:
		tmp = '5' * i + '0' * z
		if int(tmp) % 90 == 0:
			j = i
		i += 1
	print('5' * j + '0' * z)


n = int(input())
cards = list(map(int, input().split()))
fives = cards.count(5)
zeros = cards.count(0)
if zeros > 0 and fives > 8:
	f = [5] * (fives - fives % 9)
	z = [0] * zeros
	f.extend(z)
	print('5' * (fives - fives % 9) + '0' * zeros)
elif zeros > 0:
	print('0')
else:
	print('-1')


n = int(input())
m = input().split().count('5')
print(int('5' * (m // 9 * 9) + '0' * (n - m)) if n > m else '-1')


def is9(s, t):
	m = 0
	v = 0
	for i in range(s.count(5)):
		v += 5
		if v % 9 == 0:
			m = max(m, v)
	zero = '' if s.count(0) == 0 else s.count(0) * '0'
	lead = m // 5 * '5' if m % 5 == 0 and m % 9 == 0 else '0'
	if s.count(0) == 0 or (len(lead) == 0 and len(zero) == 0):
		return -1
	return int(lead + zero)
t = int(input())
s = list(map(int, input().split()))
print(is9(s, t))


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	n = I()
	a = LI()
	c = collections.Counter(a)
	if c[0] < 1:
		return -1
	if c[5] < 9:
		if c[0] == 0:
			return -1
		return 0
	return '5' * (c[5] // 9 * 9) + '0' * c[0]
print(main())


n = int(input())
num = list(map(int, input().split()))
c5 = num.count(5)
c0 = num.count(0)
if c0 > 0 and c5 > 0:
	k = c5 // 9
	if k > 0:
		ans = [5] * (k * 9) + [0] * c0
		print(*ans, sep='')
	else:
		print(0)
elif c0 > 0:
	print(0)
else:
	print(-1)


x = int(input())
list1 = input().split(' ')
list1 = [int(a) for a in list1]
sum1 = 0
flag = 0
flag2 = 0
if not 0 in list1:
	print(-1)
	flag2 = 1
elif list1.count(5) < 9:
	print(0)
	flag2 = 1
else:
	for i in range(list1.count(5), -1, -1):
		if i * 5 % 9 == 0:
			flag = i
			break
if flag2 != 1:
	if flag != 0:
		for i in range(0, flag + list1.count(0)):
			if i >= flag:
				print('0', end='')
			else:
				print('5', end='')
	else:
		for i in range(0, list1.count(0)):
			print('0', end='')


n = input()
lst = list(input().split())
(zeros, five) = (lst.count('0'), lst.count('5'))
if zeros < 1:
	print(-1)
elif five < 9:
	print(0)
else:
	print('5' * (five - five % 9) + '0' * zeros)


n = int(input())
l = input()
l = l.split()
if l.count('5') < 9 and l.count('0') != 0:
	print(0)
elif l.count('5') < 9 and l.count('0') == 0:
	print(-1)
elif l.count('5') >= 9 and l.count('0') != 0:
	a = l.count('5') // 9
	b = l.count('0')
	s = '5' * (a * 9)
	s1 = '0' * b
	print(s + s1)
else:
	print(-1)


N = int(input())
A = list(map(int, input().split()))
c5 = c0 = 0
for i in A:
	if i == 5:
		c5 += 1
	else:
		c0 += 1
if c5 < 9:
	if c0 == 0:
		print(-1)
	else:
		print(0)
elif c0 == 0:
	print(-1)
else:
	res = c5 // 9
	res *= 9
	ans = [5] * res
	ans1 = [0] * c0
	ans += ans1
	print(*ans, sep='')


n = int(input())
(c, z) = (0, 0)
line = input()
v = [int(h) for h in line.split()]
for i in range(len(v)):
	if v[i] == 5:
		c += 1
	else:
		z += 1
if z == 0:
	print('-1')
elif c <= 8:
	print('0')
else:
	d = int(c / 9)
	d = d * 9
	(cincos, zeros) = ('', '')
	for k in range(d):
		cincos = cincos + str('5')
	for g in range(z):
		zeros = zeros + str('0')
	print(cincos + zeros)


n = int(input())
f = input().split().count('5')
z = n - f
if z < 1:
	print('-1')
elif f < 9:
	print('0')
else:
	print(f // 9 * 9 * '5' + z * '0')


n = int(input())
L = list(map(int, input().split()))
cinq = 0
zero = 0
for i in range(n):
	if L[i] == 5:
		cinq = cinq + 1
	else:
		zero = zero + 1
if zero == 0:
	print(-1)
elif cinq == 0:
	print(0)
else:
	a = cinq // 9
	res = a * 9
	s = 0
	k = 1
	for i in range(res):
		s = s + 5 * k
		k = k * 10
	s = s * 10 ** zero
	print(s)


import sys, os, io, time, copy
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
import math

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	count_0 = 0
	count_5 = 0
	for a in arr:
		if a == 0:
			count_0 += 1
		if a == 5:
			count_5 += 1
	if count_0 == 0:
		print(-1)
	else:
		count_5 = count_5 // 9 * 9
		for _ in range(count_5):
			print(5, end='')
		if count_5 >= 1:
			for _ in range(count_0):
				print(0, end='')
		else:
			print(0)
		print('')
main()


import sys
inf = float('inf')
from math import ceil, floor, log, sqrt, factorial, pow, pi, gcd
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
(mod, MOD) = (1000000007, 998244353)
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getvals(n):
	while n % 2 == 0:
		n //= 2
	while n % 3 == 0:
		n //= 3
	return n

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
n = int(input())
Arr = get_array()
five = Arr.count(5)
zero = Arr.count(0)
x = five // 9
if zero == 0:
	print(-1)
	exit()
if x == 0:
	if zero >= 1:
		print(0)
	else:
		print(-1)
else:
	number = '5' * (x * 9) + zero * '0'
	print(number)


n = int(input())
s = input()
k = s.count('5')
l = s.count('0')
if k == 0 and l != 0:
	print('0')
elif k < 9 and l != 0:
	print('0')
elif k < 9 and l == 0:
	print('-1')
elif k >= 9 and l == 0:
	print('-1')
elif k >= 9:
	p = k - k % 9
	print('5' * p + '0' * l)


n = int(input())
arr = list(map(int, input().split()))
five = arr.count(5)
zero = arr.count(0)
if 0 not in arr:
	print(-1)
elif five < 9:
	print(0)
else:
	q = five // 9
	print('5' * (q * 9) + zero * '0')


n = int(input())
s5 = 0
s0 = 0
ar = list(input().split())
for i in range(n):
	if ar[i] == '5':
		s5 += 1
	else:
		s0 += 1
while s5 % 9 and s5:
	s5 -= 1
if s5 and s0:
	print(s5 * '5' + '0' * s0)
elif s0:
	print(0)
else:
	print(-1)


def gint():
	return int(input())

def gint_arr():
	return list(map(int, input().split()))

def gfloat():
	return float(input())

def gfloat_arr():
	return list(map(float, input().split()))

def pair_int():
	return map(int, input().split())
INF = 1 << 31
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
input()
inp = input()
fives = inp.count('5')
zeroes = inp.count('0')
if not zeroes:
	print('-1')
else:
	fives = int(fives / 9)
	print('555555555' * fives, end='')
	if fives:
		print('0' * zeroes)
	else:
		print('0')


n = int(input())
s = [int(i) for i in input().split()]
from collections import Counter
s = Counter(s)
output = -1
if s[0] > 0:
	output = 0
	if s[5] > 8:
		output = '5' * (s[5] - s[5] % 9)
		output += '0' * s[0]
print(output)


n = int(input())
L = []
l = input()
lung = len(l)
nr5 = nr0 = 0
for i in range(lung):
	if l[i].isnumeric():
		if int(l[i]) == 5:
			nr5 += 1
		else:
			nr0 += 1
if nr0 == 0:
	print('-1')
elif nr5 < 9:
	print('0')
else:
	for i in range(nr5 - nr5 % 9):
		print(5, end='')
	for i in range(nr0):
		print(0, end='')


def main():
	numberOfFive = 0
	numberOfZero = 0
	line = input()
	line = input()
	for c in line:
		if c == '5':
			numberOfFive = numberOfFive + 1
		elif c == '0':
			numberOfZero = numberOfZero + 1
	if numberOfFive >= 9 and numberOfZero != 0:
		numberOfFive = numberOfFive // 9 * 9
		print('5' * numberOfFive + '0' * numberOfZero)
	elif numberOfZero != 0:
		print(0)
	else:
		print(-1)
main()


n_cards = int(input())
cards = list(map(int, input().split()))
if 0 not in cards:
	print(-1)
elif cards.count(5) < 9:
	print(0)
else:
	number = [5] * (9 * (cards.count(5) // 9))
	number += [0] * cards.count(0)
	print(''.join(map(str, number)))


n = input()
a = input()
if '0' not in a:
	print(-1)
elif a.count('5') < 9:
	print(0)
else:
	x = a.count('5')
	while x % 9 != 0:
		x -= 1
	print('5' * x + '0' * a.count('0'))


nos = int(input())
fives = list(map(int, input().split()))
fives.sort(reverse=True)
ans = ''
zerocnt = 0
for i in fives:
	if i == 0:
		zerocnt += 1
if zerocnt == 0:
	print(-1)
else:
	while True:
		if sum(fives) % 9 == 0 and fives[nos - 1] == 0:
			break
		else:
			fives.pop(0)
			nos -= 1
	ans = ''
	for i in range(len(fives)):
		if i == 0 and fives[i] == 0:
			ans = '0'
			break
		else:
			ans += str(fives[i])
	print(ans)


n = int(input())
a = [int(i) for i in input().split()]
z = a.count(5) // 9
if 0 in a:
	if z == 0:
		print(0)
	else:
		t = ''
		t = t + '5' * z * 9
		t = t + a.count(0) * '0'
		print(t)
else:
	print(-1)


n = int(input())
a = [int(i) for i in input().split()]
t = a.count(5)
if t == n:
	print(-1)
elif t < n and t >= 9:
	temp = int(t // 9 * 9 * '5' + (n - t) * '0')
	if temp % 90 == 0:
		print(temp)
	else:
		print(0)
elif t < n and 0 in a:
	print(0)
else:
	print(-1)


n = int(input())
a = list(map(int, input().split()))
if a.count(0) >= 1:
	s = a.count(0)
	t = ''
	i = 0
	ans = 0
	x = 0
	while i < n - s:
		x = 1
		t += '5'
		i += 1
	if x == 1:
		t = int(t)
		while t > 0:
			if t % 9 == 0:
				ans = 1
				print(t * pow(10, s))
				break
			else:
				t = t // 10
	if ans == 0:
		print(0)
else:
	print(-1)


n = int(input())
l = list(map(int, input().split()))
x = l.count(0)
y = l.count(5) // 9 * 9
if x == 0:
	print(-1)
elif y == 0:
	print(y * '5' + '0')
else:
	print('5' * y + '0' * x)


n = int(input())
b = list(map(int, input().split()))
f = z = 0
for x in range(n):
	if b[x] == 5:
		f += 1
	else:
		z += 1
if f >= 9 and z > 0:
	print('5' * (f - f % 9) + '0' * z)
elif f < 9 and z > 0:
	print(0)
else:
	print(-1)


input()
temp = input()
(n0, n5) = (temp.count('0'), temp.count('5'))
n5 = n5 // 9 * 9
if n0 <= 0:
	print(-1)
elif n5 > 0:
	print('5' * n5 + '0' * n0)
else:
	print(0)


import sys
n = int(input())
a = list(map(int, sys.stdin.readline().split()))
nb0 = 0
nb5 = 0
for i in a:
	if i == 5:
		nb5 += 1
	else:
		nb0 += 1
if nb5 <= 8:
	if nb0 >= 1:
		print(0)
	else:
		print(-1)
elif nb0 == 0:
	print(-1)
else:
	r = nb5 % 9
	print('5' * (nb5 - r) + '0' * nb0)


n = int(input())
if n == 1:
	a = int(input())
	if a % 90 == 0:
		print(a)
	else:
		print(-1)
	exit()
A = [int(x) for x in input().split()]
A.sort(reverse=True)
cnt0 = A.count(0)
cnt5 = A.count(5)
cnt5 = 9 * (cnt5 // 9)
C = ''
for i in range(cnt5):
	C += '5'
for i in range(cnt0):
	C += '0'
if cnt0:
	if C.count('0') == len(C):
		print(0)
	else:
		print(C)
else:
	print(-1)


import math
n = int(input())
l = list(map(int, input().split()))
c1 = l.count(5)
c2 = l.count(0)
if c2 == 0:
	print(-1)
elif c1 >= 9:
	z = math.floor(c1 / 9)
	z1 = z * 9
	for i in range(z1):
		print(5, end='')
	for i in range(c2):
		print(0, end='')
else:
	print(0)


n = int(input())
l = list(map(int, input().split()))
a = 0
b = 0
for i in range(n):
	if l[i] == 5:
		a += 1
	else:
		b += 1
if b == 0:
	print('-1')
elif a < 9:
	print('0')
elif a >= 9:
	x = a // 9 * 9
	print('5' * x + '0' * b)


n = int(input())
arr = [int(i) for i in input().split(' ')]
cnt = arr.count(5)
cnt0 = arr.count(0)
if cnt0 == 0:
	print(-1)
elif cnt < 9:
	print(0)
elif cnt % 9 == 0:
	print('5' * cnt + '0' * cnt0)
else:
	print('5' * (cnt // 9) * 9 + '0' * cnt0)


t = int(input())
l = list(map(int, input().split()))
x = l.count(5)
if l.count(0) == 0:
	print(-1)
elif x < 9:
	print(0)
elif x % 9 == 0:
	print('5' * x + '0' * l.count(0))
else:
	v = x // 9
	print('5' * 9 * v + '0' * l.count(0))


n = int(input())
lst = [int(i) for i in input().split()]
num0 = 0
num5 = 0
for i in range(len(lst)):
	if lst[i] == 0:
		num0 += 1
	elif lst[i] == 5:
		num5 += 1
a = num5 // 9
if a == 0:
	if num0 == 0:
		ans = -1
	else:
		ans = 0
elif num0 == 0:
	ans = -1
else:
	ans = a * '555555555' + num0 * '0'
print(ans)


n = int(input())
a = list(map(int, input().split()))
five = a.count(5)
zero = a.count(0)
if zero == 0:
	print(-1)
	exit()
if five < 9:
	print(0)
	exit()
print('5' * (five // 9 * 9) + '0' * zero)


a = int(input())
b = input()
sum = 0
count = b.count('0')
flag = 0
if '0' in b:
	flag = 1
b = b.replace('0', '')
b = b.replace(' ', '')
sum1 = 0
for i in b:
	sum1 += int(i)
for i in range(len(b)):
	if sum1 % 9 == 0:
		break
	b = b[1:]
	sum1 -= 5
if sum1 != 0 and sum1 % 9 == 0 and flag:
	print(str(b) + '0' * count)
elif flag:
	print(0)
else:
	print(-1)


length = int(input())
nums = list(map(int, input().split()))
zeros = nums.count(0)
fives = nums.count(5)
if zeros == 0:
	print(-1)
elif fives < 9:
	print(0)
else:
	while fives % 9 != 0:
		fives -= 1
	print('5' * fives + '0' * zeros)


n = int(input())
digits = list(map(int, input().split()))
dic = {5: 0, 0: 0}
for i in digits:
	dic[i] += 1
if dic[0] == 0:
	print(-1)
elif dic[5] < 9:
	print(0)
else:
	dic[5] = dic[5] - dic[5] % 9
	print('5' * dic[5] + '0' * dic[0])


t = int(input())
s = input()
s = s.split()
for i in range(t):
	s[i] = int(s[i])
n5 = 0
for i in range(t):
	if s[i] == 5:
		n5 += 1
num = 0
delta = n5 // 9
u5 = delta * 9
u0 = t - n5
temp = u0
while u5:
	num = num * 10 + 5
	u5 -= 1
while u0:
	num = num * 10
	u0 -= 1
if temp == 0:
	print(-1)
else:
	print(num)


n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
k = a.count(5)
o = a.count(0)
b = []
if o == 0:
	print(-1)
else:
	k = k // 9
	b = ['5'] * (k * 9)
	if k != 0:
		b = b + ['0'] * o
	else:
		b = b + ['0']
	print(''.join(b))


n = int(input())
arr = [int(x) for x in input().split()]
myansw = []
arr.sort()
flag = 0
if arr.count(0) == 0:
	print(-1)
	flag = 1
if arr.count(5) < 9 and flag == 0:
	print(0)
	flag = 1
elif flag == 0:
	store = arr.count(5)
	zcount = arr.count(0)
	z = '0' * zcount
	while store > 8:
		mystr = '5' * store
		mystr += z
		temp = int(mystr)
		if temp % 90 == 0:
			myansw.append(temp)
		store -= 1
	print(max(myansw))


(n, cards) = (int(input()), sorted(input().split()))
print(int(max(('555555555' * (cards.count('5') // 9) + '0' * cards.count('0')) * (cards.count('0') != 0), '-1')))


n = int(input())
l = list(map(int, input().split()))
tot = int(sum(l) / 5)
t = tot
if t == len(l):
	print(-1)
else:
	while 5 * t % 9 != 0:
		t = t - 1
	print('5' * t + '0' * (len(l) - tot) if t != 0 else 0)


_ = input()
a = input()
x = a.count('5')
y = a.count('0')
if y == 0:
	ans = '-1'
elif x < 9:
	ans = '0'
else:
	ans = '5' * (x // 9 * 9) + '0' * y
print(ans)


in1 = input()
in2 = input()
ls = in2.split()
c5 = ls.count('5')
c0 = ls.count('0')
d = int(c5 / 9)
i = 0
l = []
flag = 0
if c0 == 0:
	print(-1)
else:
	if d == 0:
		flag = 1
	while i < d * 9:
		l.append(5)
		i += 1
	if flag == 1:
		print(0)
	else:
		i = 0
		while i < c0:
			l.append(0)
			i += 1
		b = int(''.join(map(str, l)))
		print(b)


import sys
size = int(input())
list = list(map(int, input().split()))
fives = list.count(5)
zeros = list.count(0)
if zeros == 0:
	print('-1')
elif fives < 9:
	print('0')
else:
	for i in range(fives, 0, -1):
		string = '5' * i
		buffer = int(string)
		if buffer % 9 == 0:
			string2 = '0' * zeros
			print(string + string2)
			sys.exit()


t = int(input())
inp = list(map(int, input().split()))
zero = 0
five = 0
for item in inp:
	if item == 5:
		five = five + 1
	else:
		zero = zero + 1
if zero == 0:
	print('-1')
else:
	k = five - five % 9
	if k > 0:
		for i in range(k):
			print('5', end='')
		for i in range(zero):
			print('0', end='')
	else:
		print('0')


n = int(input())
A = [int(i) for i in input().split()]
(zero_count, five_count) = (0, 0)
for i in A:
	if i == 0:
		zero_count += 1
	else:
		five_count += 1
k = int(five_count / 9)
if zero_count == 0:
	print(-1)
elif k == 0:
	print(0)
else:
	ans = '5' * (k * 9) + '0' * zero_count
	print(ans)