import Control.Monad.State

type Memo = State [(Int, Int)] Int


memo :: (Int -> Memo) -> Int -> Memo 
memo f n =
  state $ \s ->
    case lookup n s of
      Nothing -> 
        let 
          (a, s') = runState (f n) $ s
        in 
          (a, (n, a) : s')
      Just a ->
        (a, s)

ret :: Int -> Memo
ret n = 
  if n == 1 then
    return 1
  else
    (memo ret (n `div` 2) >>= (\a -> return (2 * a + 1)))








main = do
  h <- read <$> getLine
  putStrLn $ show $ evalState (ret h) []
