M,P=lambda:map(int,input().split()),print n,m=M() a=list(M()) b=list(M()) for i in range(n*m): if a[i%n]==b[i%m]:exit(P(i+1)) P(-1)