N,M = map(int,input().split())
s = [int(input()) for i in range(M)]
c = [int(input()) for i in range(M)]
first = []
second = []
third = []
for i in range(M):
  if s[i] == 1:
    first.append(c[i])
  elif s[i] == 2:
    second.append(c[i])
  elif s[i] == 3:
    third.append(c[i])
if first == []:
  print(-1)
elif second == []:
  second.append(0)
elif third == []:
  third.append(0)
l.append(min(first))
l.append(min(second))
l.append(min(third))
l = ''.join(str(n) for n in l)
print(l)