import Data.List

main = do
  li <- getLine
  let n = read li
  let ans = compute n
  print ans

compute :: Int -> Int
compute n = a + b - 2
  where
    (a,b) = loop 1 1 $ reverse $ primeFactors n

loop a b [] = (a,b)
loop a b (p:ps) = loop (min a1 b) (max a1 b) ps
  where
    a1 = a * p

getmin ps = snd $ minimum [ (abs (a-b),(a,b)) | (a,b) <- ps ]

primes = 2 : 3 : 5 : sieve [(9,3),(10,5)] [7,9..]

sieve ((q,p):qps) xxs@(x:xs) =
  case compare q x of
    LT -> sieve qps1 xxs
    EQ -> sieve qps1 xs
    GT -> x : sieve (insert (x+x,x) qps) xs
  where
    qps1 = insert (q+p,p) qps

primeFactors n = loop n primes
  where
    loop 1 _ = []
    loop n pps@(p:ps)
      | r == 0 = p : loop q pps
      | q < p  = [n]
      | True   = loop n ps
      where (q,r) = divMod n p