import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int[] b = new int[m]; for (int i = 0; i < m; i++) { b[i] = sc.nextInt(); } sc.close(); int nm = n * m; for (int i = 0; i < nm; i++) { if (a[i % n] == b[i % m]) { System.out.println(i + 1); return; } } System.out.println(-1); } }