N=gets.hex def q a=[] a[-N]?puts(a*' '):N.times{|i|z=a.size a.all?{|x|x!=i&&(x-i).abs!=1+z-=1}&&q(a+[i])}end q