# 
# Solution to Project Euler problem 79
# Philippe Legault
# 
# https://github.com/Bathlamos/Project-Euler-Solutions


# There seems to be no duplicate digit in the data
def compute():
	data = DATA.split()
	comes_after = {}
	for security in data:
		security = list(security)
		for i in range(0, len(security)):
			if security[i] not in comes_after:
				comes_after[security[i]] = set()
			comes_after[security[i]].update(security[i + 1:])

	return reduce(lambda x, y: y + x, sorted(comes_after, key=lambda x: len(comes_after[x])), '')


DATA =  '''
		319
		680
		180
		690
		129
		620
		762
		689
		762
		318
		368
		710
		720
		710
		629
		168
		160
		689
		716
		731
		736
		729
		316
		729
		729
		710
		769
		290
		719
		680
		318
		389
		162
		289
		162
		718
		729
		319
		790
		680
		890
		362
		319
		760
		316
		729
		380
		319
		728
		716
		'''

if __name__ == "__main__":
	print(compute())