n,m = [int(nm) for nm in raw_input().split(" ")]
a = [int(ai) for ai in raw_input().split(" ")]
b = [int(bi) for bi in raw_input().split(" ")]
c = []
for ai in a:
	if ai in b:
		c += [ai]
for bi in b:
	if bi in a:
		c += [bi]
for ai in a:
	for bi in b:
		c += [int(str(ai)+str(bi))]
		c += [int(str(bi)+str(ai))]
print min(c)
