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 Common Lisp (SBCL 1.1.14) (judge verdict: Accepted)

;; D - Lunlun Number

(defparameter *max-n-digits* 10) ; 最大桁数 (入力例4を参照)

(defun next-lunlun (lun &optional (i 0))
  "i桁目以降を次のルンルン数に更新. lunは最下位桁から順に格納."
  (cond ((null (aref lun i))
          (setf (aref lun i) 1))
        ((or (>= (aref lun i) 9) ; i桁目が9
             (and (aref lun (1+ i)) ; i桁目がi+1桁目より大きい
                  (> (aref lun i) (aref lun (1+ i)))))
          (next-lunlun lun (1+ i))
          (setf (aref lun i) (max 0 (1- (aref lun (1+ i))))))
        (t ; 桁上がりなし
          (incf (aref lun i)))))

(defun lunlun-to-int (lun)
  "配列lun (先頭が最下位桁) を整数に変換"
  (loop for x across lun
        for i from 0
        for acc = x then (if x (+ acc (* (expt 10 i) x)) acc)
        finally (return acc)))

(defun lunlun (k)
  "k番目のルンルン数"
  (let ((lun (make-array (1+ *max-n-digits*) :initial-element nil)))
    ; lunは最下位桁から順に格納.
    (setf (aref lun 0) 0) ; 最下位桁を0に初期化
    (loop repeat k do (next-lunlun lun)
          finally (return (lunlun-to-int lun)))))

(let ((k (read)))
  (princ (lunlun k)))