use std::cmp::max; use std::io::Read; fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let a: Vec = (0..n) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); // 1 <= K <= 5000000 let mut need: Vec = vec![false; 10]; for &i in a.iter() { need[i] = true; } let mut check = vec![false; 5000010]; let mut prime: Vec = Vec::new(); for i in 2..5000001 { if !check[i] { prime.push(i); let mut j = i * 2; while j <= 5000000 { check[j] = true; j += i; } } } prime.push(5000001); // 足りないチェックと余分チェックが必要。 let mut l: usize = 1; let mut state: Vec = vec![false; 10]; let mut ans: i32 = -1; for i in 0..prime.len() - 1 { let mut tmp = prime[i]; while tmp > 0 { state[tmp % 10 as usize] = true; tmp /= 10; } let mut tarinai = false; let mut yobun = false; for j in 0..10 { if state[j] && !need[j] { yobun = true; } if !state[j] && need[j] { tarinai = true; } } if yobun { l = prime[i] + 1; for j in 0..10 { state[j] = false; } continue; } if tarinai { continue; } ans = max(ans, (prime[i + 1] - 1 - l) as i32); } println!("{}", ans); }