N,M=map(int,input().split()) L=list(map(int,input().split())) R=list(map(int,input().split())) K=L*M S=R*N for i in range(N*M): if K[i]==S[i]: print(i+1) exit() print(-1)