use std::io::Read; fn main() { let mut s = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let v: Vec = s.split_whitespace().flat_map(str::parse).collect(); for i in 0..v[0] * v[1] { if v[i % v[0] + 2] == v[i % v[1] + 2 + v[0]] { println!("{}", i + 1); return; } } println!("-1") }