N,M = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) A = A * M B = B * N import sys for i in range(N*M): if A[i] == B[i]: print(i+1) exit() print(-1)