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