def seek(prefix, l): for a in l[::-1]: for b in l[::-1]: if a != b: yield int(prefix + a + b) def test(): for x in seek('93', '24567'): num = str(x) + str(x * 2) if '123456789' == ''.join(sorted(list(num))): return num for x in seek('92', '34567'): num = str(x) + str(x * 2) if '123456789' == ''.join(sorted(list(num))): return num print(test())