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)

pub struct Scanner{
    line : Vec<String> ,
    idx : usize
}

#[allow(dead_code)]
impl  Scanner{
   fn new() -> Scanner{
       return Scanner{ line: vec![], idx : 0}
   }
   fn next<T: std::str::FromStr> (&mut self) -> T{
       if self.idx == self.line.len() {
           let mut buffer = String::new();
           std::io::stdin().read_line(&mut buffer).ok();

           let a : Vec<&str>  = buffer.trim().split(' ').collect();
           self.line = a.iter().map(|&s| String::from(s)).collect();
           self.idx = 0;
       }
       let ret : T = self.line[self.idx].parse::<T>().ok().expect("Parse error.");
       self.idx += 1;
       ret
   }
   fn next_string(&mut self) -> Vec<char> {
       let s : String = self.next();
       return s.chars().collect::<Vec<char>>();
   }
   fn next_vec<T: std::str::FromStr>  (&mut self, n: usize) -> Vec<T>{
       let mut v : Vec<T>= Vec::with_capacity(n);
       for _i in 0..n {
           v.push(self.next());
       }
       v
   }
}


fn main() {
    let mut scan = Scanner::new();
    let mut k:i32 = scan.next();

    let mut ans : i64 = 0;

    let mut p3 = 1;
    let mut n = 0;
    loop {
        for st in 1..10{
            for pp in 0..p3{
                let mut x = st;
                let mut p = pp;
                let mut ok = true;
                let mut num : i64 = st;
                let mut dif : Vec<i64> = Vec::with_capacity(n);
                for _ in 0..n{
                    dif.push(p %3 - 1);
                    p /= 3;
                }
                dif.reverse();

                for i in 0..n{
                    x += dif[i];
                    if x < 0 || x==10{
                        ok = false;
                        break;
                    }
                    num = num*10 + x;
                }
                if ok {
                    k -= 1;
                }
                if k==0{
                    ans = num;
                    break;
                }
            }
            if k==0 {
                break;
            }
        }
        if k==0 {
            break;
        }
        n += 1;
        p3 *= 3;
    }
    
    println!("{}", ans);
}