n,m = map(int,input().split())
listA = [list(map(int, list(input()))) for i in range(n)]
listB = [list(map(int, list(input()))) for j in range(m)]
listC=[]
for k in range(n):
  for l in range(m):
    listC.append(abs(listA[k][0]-listB[l][0])+abs(list[k][1]-listB[l][1]))
  listC.sort()
  print(listC[0])
