exec'''from itertools import*;I,F,Q=input(),[],2 while len(F)'0')for i in permutations(`g`))];Q+=1 print F[-1];'''*15