n,m,*z=map(int,open(0).read().split()) for i in range(n*m): if z[i%n]==z[n+i%m]: print(i+1) exit() print(-1)