fn get_primes(n: usize) -> Vec { let mut flgs = vec![true; n+1]; flgs[0] = false; flgs[1] = false; for i in 2..=(n as f64).sqrt().ceil() as usize { if !flgs[i] { continue; } for j in i..=n/i { flgs[i*j] = false; } } flgs } fn get_hashed(n: usize) -> usize { let mut ret = n; while ret > 9 { ret = ret.to_string().chars().map(|c| c as usize - '0' as usize).sum::(); } ret } fn main() { let mut k = String::new(); std::io::stdin().read_line(&mut k).ok(); let k: usize = k.trim().parse().unwrap(); let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let primes = get_primes(n); let primes = (k..=n).filter(|&i| primes[i]).collect::>(); let hashed = primes.iter().map(|&v| get_hashed(v)).collect::>(); let mut maxval = 0usize; let mut result = 0usize; let mut used = vec![false; 10]; let mut idx = 0usize; let mut cnt = 0usize; for i in 0..primes.len() { while used[hashed[i]] { used[hashed[idx]] = false; idx += 1; cnt -= 1; } used[hashed[i]] = true; cnt += 1; if cnt >= maxval { maxval = cnt; result = primes[idx]; } } println!("{}", result); }