Problem:
Score : 400 points

Problem Statement

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

You are given a positive integer K. Find the K-th smallest lunlun number.

Constraints

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer.

Sample Input 1

15

Sample Output 1

23

We will list the 15 smallest lunlun numbers in ascending order:

1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
21,
22,
23.

Thus, the answer is 23.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

13

Sample Output 3

21

Sample Input 4

100000

Sample Output 4

3234566667

Note that the answer may not fit into the 32-bit signed integer type.

Solution in Haskell (GHC 7.10.3) (judge verdict: Accepted)

digit :: Int -> Int
digit n = length $ takeWhile (<= n) ( iterate (*10) 1 )


-- 各桁ペアごとのルンルン性リストを返す
checkDigits :: Int -> [Bool]
checkDigits n = reverse [ abs (a - b) <= 1 | (a,b) <- zip ms (tail ms) ]
    where
        ms = take (digit n) . map (`mod` 10) $ iterate (`div` 10) n


-- ダメな桁ペアの一番最初のもの
-- 例：97123 -> 1,2番目の要素について、 (|9-7| <= 1) を満たさないから
--     この場合 2番目の要素を +1, つまり +10^3 = 10^(digit - 2(=badIndex)) 
--     したい。よって戻り値 2。
badIndex :: [Bool] -> Int
badIndex ns = (2+) . length $ takeWhile (== True) ns


isLun :: Int -> Bool
isLun n = and $ checkDigits n


bitLun :: Int -> Int
bitLun n 
    | isLun n   = 1 
    | otherwise = 0


floorNum :: Int -> Int -> Int
floorNum n i  = n - mod n (10^i)


searchK :: Int -> Int
searchK n = f' (n+1) 0
    where
    f' 0 count = count - 1
    f' k count = f' (k - bitLun count) next
        where
        next
            | isLun count = count + 1
            | otherwise   = floorNum (count + (10 ^ pow)) pow
        pow = digit count - badIndex (checkDigits count)


main :: IO ()
main = do
    k <- read <$> getLine :: IO Int
    print $ searchK k