def calculate(x):
res = 0
s = str(x)
for c in s:
if c == ‘0’ or c == ‘4’ or c == ‘6’ or c == ‘9’:
res += 1
if c == ‘8’:
res += 2
return res
n = int(input())
data = list(map(int,input().split()))
data = sorted(data,key=lambda x:(calculate(x),x))
print(*data)