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