import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import qualified Data.Vector.Unboxed as VU

main :: IO ()
main = do
  k <- unsafeTextToInt <$> T.getLine :: IO Int
  print . fromLunlunToInt $ k

ls :: VU.Vector Int
ls = VU.constructN (succ $ 10 ^ 5) (encode . rec)
  where
    rec v | k < 10    = (0, k)
          | r <= min 8 r' = (k', succ r)
          | otherwise = (k'', max 0 (pred r''))
      where
        k = VU.length v
        (k', r) = decode $ VU.last v
        r' = snd . decode $ v VU.! k'
        k'' = succ k'
        r'' = snd . decode $ v VU.! k''

fromLunlunToInt :: Int -> Int
fromLunlunToInt 0 = 0
fromLunlunToInt k = 10 * fromLunlunToInt k' + r
  where
    (k', r) = decode $ ls VU.! k

encode :: (Int, Int) -> Int
encode (k, r) = 10 * k + r

decode :: Int -> (Int, Int)
decode = (`divMod` 10)

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n
