def gcd(x, y): while y: x, y = y, x%y return x def main(): N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) for i in range(N*M//gcd(N, M)): if A[i%N] == B[i%M]: print(i+1) print("-1") if __name__ == "__main__": main()