#![allow(unused_imports)] use std::cmp::*; use std::collections::*; use std::io::Write; use std::ops::Bound::*; #[allow(unused_macros)] macro_rules! debug { ($($e:expr),*) => { #[cfg(debug_assertions)] $({ let (e, mut err) = (stringify!($e), std::io::stderr()); writeln!(err, "{} = {:?}", e, $e).unwrap() })* }; } fn main() { let n = read::(); let mut ans = n + 1; // debug!(with_p(2, 2)); for di in 1i64..1000001 { for i in 2.. { if (di + 1).pow(i) > n + 1 { break; } let criterion = |mid: i64| -> bool { if mid == 1 { return true; } if mid == n + 1 { return false; } let v = with_p(n, mid); if v.len() != i as usize { return v.len() > i as usize; } for num in v { if num < di { return false; } if num > di { return true; } } false }; let (_, ng) = binary_search(1, n + 1, criterion); /* if di == 1 && i == 3 { debug!(ng); debug!(criterion(1)); debug!(criterion(2)); } */ let criterion2 = |mid: i64| -> bool { let v = with_p(n, mid); if v.len() != i as usize { return false; } return v.iter().all(|&e| e == di); }; if criterion2(ng) && ng < ans { ans = ng; } } } println!("{}", ans); } type Input = i64; fn binary_search(lb: Input, ub: Input, criterion: F) -> (Input, Input) where F: Fn(Input) -> bool, { assert_eq!(criterion(lb), true); assert_eq!(criterion(ub), false); let mut ok = lb; let mut ng = ub; while ng - ok > 1 { let mid = (ng + ok) / 2; if criterion(mid) { ok = mid; } else { ng = mid; } } (ok, ng) } fn with_p(mut n: i64, p: i64) -> Vec { let mut v = vec![]; while n > 0 { v.push(n % p); n /= p; } v.reverse(); v } fn read() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec() -> Vec { read::() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() }