{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Data.List
import           Control.Monad
import           Debug.Trace
import qualified Data.ByteString.Char8         as BC
import           Data.Maybe                     ( fromJust )
import qualified Data.Set                      as S
import qualified Data.IntMap.Strict            as IM
import qualified Data.Map.Strict               as M
import           Data.Char


readTuple2 :: IO (Int, Int)
readTuple2 = do
    [a, b] <- map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine
    return (a, b)

readTuple3 :: IO (Int, Int, Int)
readTuple3 = do
    [a, b, c] <- map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine
    return (a, b, c)

readInt :: IO Int
readInt = fst . fromJust . BC.readInt <$> BC.getLine

readInts :: IO [Int]
readInts = map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine

split :: (a -> Bool) -> [a] -> [[a]]
split f [] = []
split f as =
    let (fo, la) = span f as
        las      = case la of
            [] -> []
            xs -> split f $ tail xs
    in  case fo of
            [] -> las
            xs -> xs : las

isLun :: Int -> Bool
isLun n 
    | n < 1 = True
    | otherwise = let
        nums = digitToInt <$> show n
        diffPair = zip nums $ tail nums 
        checker (a, b) = abs (a - b) < 2
        in
        all checker diffPair

availableNums :: Integer -> [Integer]
availableNums 0 = [0, 1]
availableNums 9 = [8, 9]
availableNums n = [n - 1, n, n + 1]

search :: [Integer] -> Integer -> [Integer]
search availNums 1 = availNums
search availNums nokoriKeta = 
    let 
        nextNokori = nokoriKeta - 1
    in
    do
        useNum <- availNums
        let nextAvail = availableNums useNum
        nextAns <- search nextAvail nextNokori
        pure $ useNum * (10^nextNokori) + nextAns

ketaRekkyo :: Integer -> [Integer]
ketaRekkyo n = search [1..9] n

allSearch :: [Integer]
allSearch = do
    keta <- [1..]
    ketaRekkyo keta

solve :: Integer -> Integer
solve k = genericIndex allSearch (k - 1)

main = do
    k <- readInt
    print $ solve $ fromIntegral k 



