use std::io::Read; fn main() { let mut s = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let l: Vec<_> = s.lines().collect(); let (n, a, b): (Vec, _, _) = ( l[0].split(' ').flat_map(str::parse).collect(), l[1].split(' ').flat_map(str::parse::), l[2].split(' ').flat_map(str::parse::), ); let p = a .cycle() .zip(b.cycle()) .take(n[0] * n[1]) .position(|(a, b)| a == b) .map_or(-1, |p| 1 + p as i64); println!("{p}") }