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 Rust (1.15.1) (judge verdict: Accepted)

// 入力マクロ
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}


/// `n`を十進法で表したときの桁数
pub fn scale_dight(n: usize) -> usize {
    if n == 0 {
        return 1;
    }
    let mut count = 0;
    let mut n = n;
    while n >= 1 {
        n /= 10;
        count += 1;
    }
    count
}
pub fn dight_vec(
    n: usize,
) -> Vec<usize> {
    let mut idx = scale_dight(n) - 1;
    let mut ret = Vec::new();
    loop {
        ret.push(n / 10usize.pow(idx as u32) % 10);
        if idx == 0 {
            break;
        }
        idx -= 1;
    }
    ret
}

use std::collections::VecDeque;

fn main() {
    input! {
        n: usize
    }

    let mut stack = VecDeque::new();
    let mut lunlun = vec![];
    for i in 1u64..10u64 {
        stack.push_back(i);
        lunlun.push(i);
    }
    let mut count = 9usize;

    while count < n {
        let p = stack.pop_front().unwrap();
        match p % 10 {
            0 => {
                let a = p * 10;
                let b = p * 10 + 1;
                count += 2;
                stack.push_back(a);
                stack.push_back(b);
                lunlun.push(a);
                lunlun.push(b);
            }
            9 => {
                let a = p * 10 + 8;
                let b = p * 10 + 9;
                count += 2;
                stack.push_back(a);
                stack.push_back(b);
                lunlun.push(a);
                lunlun.push(b);
            }
            r => {
                let a = p * 10 + r - 1;
                let b = p * 10 + r;
                let c = p * 10 + r + 1;
                count += 3;
                stack.push_back(a);
                stack.push_back(b);
                stack.push_back(c);
                lunlun.push(a);
                lunlun.push(b);
                lunlun.push(c);
            }
        }
    }

    // println!("{:?}", lunlun);

    println!("{}", lunlun[n-1]);


    // // dp[桁数][上の桁がnと等しいか][上の桁までがルンルン]
    // let mut dp = vec![(0, 0); d];

    // if d == 1 {
    //     dp[0].0 = 1;
    //     dp[0].1 = n;
    // } else {
    //     dp[0].0 = 0;
    //     dp[0].1 = 9;
    // }

    // for i in 1..d {
    //     if dp[i-1].0 == 1 {}
    // }
}