import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T

main :: IO ()
main = do
  (k : _) <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    ls = iterate nextLunlun 0 :: [Int]
  print $ ls !! k

nextLunlun :: Int -> Int
nextLunlun = nextLunlun' . (`divMod` 10)
  where
    nextLunlun' (q, r) | q == 0 || r <= min 8 r' = 10 * q + succ r
                       | r'' == 0 = 10 * l'
                       | otherwise = 10 * l' + pred r''
      where
        r' = q `mod` 10
        l' = nextLunlun q
        r'' = l' `mod` 10

unsafeDigitToChar :: Int -> Char
unsafeDigitToChar = toEnum . (+ 48)

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