f=lambda x:(x and(x%6**8==549305)+f(x//6)[0],x) for x in sorted(map(int,open(0)),key=f):print(x)