#!/usr/bin/env python3 import sys def main(): N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) for i in range(M * N): if A[i % N] == B[i % M]: print(i + 1) return print(-1) return if __name__ == '__main__': main()