def main(): N, M = map(int, input().split()) A = list(map(int,(input().split()))) * M B = list(map(int,(input().split()))) * N for i in range(N * M): if A[i] == B[i]: print(i + 1) return else: print(-1) if __name__ == '__main__': main()