use std::collections::HashMap; const INF: usize = 1usize << 60; fn gcd(a: usize, b: usize) -> usize { if b == 0 { return a; } gcd(b, a%b) } fn main() { let mut nmp = String::new(); std::io::stdin().read_line(&mut nmp).ok(); let nmp: Vec = nmp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nmp[0]; let m = nmp[1]; let p = nmp[2]; let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let a = a.into_iter().filter(|&v| v % p > 0 && v > 1).collect::>(); if p == 1 || a.is_empty() { println!("-1"); return; } let mut dp = HashMap::new(); dp.insert(1usize, 0usize); for &v in a.iter() { let mut next_dp = HashMap::new(); for (&val, &cnt) in dp.iter() { next_dp.insert(val, cnt); for i in 1.. { let value = v.pow(i); let lgcdval = gcd(value, p); let rgcdval = gcd(val, p); if lgcdval == p || rgcdval == p || gcd(lgcdval*rgcdval, p) == p { break; } let target = if value >= m || value * val >= m { m } else { value * val }; let minval = (*next_dp.get(&target).unwrap_or(&INF)).min(i as usize + cnt); next_dp.insert(target, minval); if target == m { break; } } } dp = next_dp; } if dp.contains_key(&m) { println!("{}", dp.get(&m).unwrap()); } else { println!("-1"); } }