import sys N, M = map(int, sys.stdin.readline().split()) A = list(map(int, sys.stdin.readline().split())) B = list(map(int, sys.stdin.readline().split())) if(len(A) <= len(B)): bx = (M - N) * N ax = (bx // N) + 2 else: ax = (N - M) * M bx = (ax // M) + 2 b = B * bx a = A * ax #print(a) #print(b) #fois = min(len(a), len(b)) #print(ax, bx, fois) for i in range(0, fois): if a[i] == b[i]: print(i+1) exit() print(-1)