import Control.Monad
import qualified Data.ByteString.Char8 as B
import Data.Attoparsec.ByteString.Char8
import Data.List

main :: IO ()
main = do
  contents <- B.getContents
  case parseOnly inputParser contents of
    Right r -> print $ solve r
    Left  e -> error e

inputParser = decimal

solve :: Int -> Int
solve n = subtract 2 $ foldl' f maxBound [1 .. n_max]
  where
    n_max = floor $ sqrt $ fromIntegral n

    f m a
      | r > 0 = m
      | otherwise = m `min` (a + b)
      where (b, r) = n `divMod` a