from sys import stdin N,M = map(int,stdin.readline().split()) A = list(map(int,stdin.readline().split())) B = list(map(int,stdin.readline().split())) for i in range(N*M): if A[i%N] == B[i%M]: print (i+1) break else: print (-1)