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)

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};
#[allow(unused_imports)]
use std::io::*;
#[allow(unused_imports)]
use std::str::*;

#[allow(unused_macros)]
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)*}
    };
}

#[allow(unused_macros)]
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)*}
    };
}

#[allow(unused_macros)]
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")
    };
}

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let cin = stdin();
    let cin = cin.lock();
    let s: String = cin
        .bytes() // Bytes
        .map(|c| c.expect("failed reading char") as char)
        .skip_while(|c| c.is_whitespace()) // c が whitespace である限り skip
        .take_while(|c| !c.is_whitespace()) // c が whitespace でない限り要素を返す
        .collect();
    s.parse().ok().expect("failed parsing")
}

fn check(mut x: isize) -> bool {
    let mut a = x % 10;
    loop {
        x /= 10;
        if x == 0 { break; }
        let b = x % 10;
        if b != a && b != a - 1 && b != a + 1 {
            return false;
        }
        a = b;
    }
    true
}

fn precompute() {
    let mut j = 1;
    let mut f = false;
    for i in 1..1123456789012 {
        if j == 100001 {
            break;
        }
        if j % 101 == 0 {
            f = true;
        }
        if check(i) {
            if f {
                print!(",{}", i);
                f = false;
            }
            j+=1;
        } 
    }
}

fn main() {
    //precompute();
    //return;
    let k : isize = read();
    let p = vec![-1,887,4455,8877,21212,33343,45456,65433,76777,89876,111100,123322,222101,233445,323444,343344,433322,445443,456778,554455,566654,656554,667889,765667,777789,876654,888876,988889,1010121,1111010,1123233,1221233,1233434,2111232,2123454,2222233,2234433,2332211,2344332,3212233,3232233,3322232,3334432,3432110,3443445,3455566,4323343,4343321,4433221,4444556,4456677,4554432,4566545,5433454,5445567,5543322,5555443,5566778,5656678,5676656,6544433,6556554,6567889,6665567,6677767,6767766,6787766,7655667,7667788,7765566,7777766,7876545,7888767,8766565,8778766,8876766,8888989,8989878,9878767,9898998,9999887,10012333,10121000,11001210,11101223,11122232,11221001,11233211,12111000,12123223,12221223,12233432,12323345,12343323,21001223,21110001,21122323,21221101,21233232,22111100,22123322,22222101,22233445,22323444,22343344,23212101,23232101,23322100,23333444,23345565,23443321,23455434,32111001,32123232,32221232,32233433,32323432,32343332,33211232,33223454,33321223,33333432,33345545,33443223,33454566,34322343,34334543,34432221,34444334,34455677,34545654,34565545,43211232,43223454,43321223,43333432,43345545,43443223,43454566,44322343,44334543,44432221,44444334,44455677,44545654,44565545,45433232,45444567,45456766,45554443,45566556,45655678,45667877,54321232,54333433,54345554,54443232,54454567,54544544,54556665,55432122,55444321,55455656,55545555,55565454,55655432,55666767,55678898,56554444,56566565,56656543,56667878,56765656,56777778,65432122,65444321,65455656,65545555,65565454,65655432,65666767,65678898,66554444,66566565,66656543,66667878,66765656,66777778,67654343,67665678,67677878,67767877,67787877,67878787,67899878,76554432,76566545,76655667,76667788,76765566,76777766,77654323,77665666,77677788,77767787,77787787,77878767,77898998,78776565,78788787,78878989,78987656,87654344,87666543,87677887,87767878,87787878,87878788,87899887,88776656,88788878,88887656,88987677,89876665,89888887,89988987,98766776,98778899,98877677,98889999,98998777,99878987,99987654,100000100,100100121,100121212,101010101,101110122,101123221,101221221,101233344,110011212,110111233,110123455,111100011,111121111,111211234,111223456,112101110,112122111,112212234,112232234,112322233,112334433,121000011,121100111,121121210,121212112,121232112,122101123,122122210,122212333,122232333,122322332,122334454,123210111,123223210,123234554,123332332,123344445,123434345,123454322,210001111,210101210,210122211,211011112,211111211,211123433,211222212,211234334,212112211,212210112,212223211,212234555,212332333,212344454,221011211,221111232,221123454,221222233,221234433,222112232,222211001,222223232,222321000,222333210,222344545,223221101,223233232,223323223,223334567,223432323,223444444,223456565,232112212,232210121,232223212,232234556,232332334,232344455,233221010,233233212,233323211,233334555,233432233,233444432,233456545,234332101,234343444,234433344,234445543,234543212,234554555,234566676,321011212,321111233,321123455,321222234,321234434,322112233,322211010,322223233,322321001,322333211,322344554,323221110,323233233,323323232,323343210,323432332,323444445,323456566,332112221,332210122,332223221,332234565,332332343,332344456,333221011,333233221,333323212,333334556,333432234,333444433,333456554,334332110,334343445,334433345,334445544,334543221,334554556,334566677,343221011,343233221,343323212,343334556,343432234,343444433,343456554,344332110,344343445,344433345,344445544,344543221,344554556,344566677,345434434,345454333,345543454,345555567,345567766,345665444,345676788,432111011,432123234,432221234,432233443,432323434,432343334,433211234,433223456,433321233,433333434,433345555,433443233,433455432,434322345,434334545,434432223,434444344,434456543,434545656,434565555,443211234,443223456,443321233,443333434,443345555,443443233,443455432,444322345,444334545,444432223,444444344,444456543,444545656,444565555,445433234,445445433,445456776,445554445,445566566,445656544,445667887,454321234,454333443,454345556,454443234,454455433,454544554,454556667,455432210,455444323,455455666,455545565,455565456,455655434,455666777,455678987,456554454,456566567,456656545,456667888,456765666,456777788,543210111,543223210,543234554,543332332,543344445,543434345,543454322,544321234,544333443,544345556,544443234,544455433,544544554,544556667,545432210,545444323,545455666,545545565,545565456,545655434,545666777,545678987,554332333,554344454,554434432,554454323,554543444,554555565,554567678,555443221,555454556,555544455,555556654,555654332,555665667,555677789,556545566,556565543,556655443,556666778,556678988,556776678,556788987,565443221,565454556,565544455,565556654,565654332,565665667,565677789,566545566,566565543,566655443,566666778,566678988,566776678,566788987,567665434,567676778,567766777,567778987,567877678,567898765,654323432,654343332,654433232,654444567,654456766,654554443,654566556,655434321,655445656,655543333,655555454,655566789,655656767,655676667,656544444,656556565,656654321,656665656,656677778,656767777,656787777,665434321,665445656,665543333,665555454,665566789,665656767,665676667,666544444,666556565,666654321,666665656,666677778,666767777,666787777,667655678,667667877,667765655,667777777,667876556,667888778,676544432,676556545,676567888,676665566,676677766,676767765,676787765,677655666,677667787,677765565,677777765,677876544,677888766,678766556,678778765,678876765,678888988,678989877,765434322,765445665,765543334,765555455,765567654,765656776,765676676,766544445,766556566,766654322,766665665,766677787,766767778,766787778,767656543,767667878,767765656,767777778,767876565,767888787,776544433,776556554,776567889,776665567,776677767,776767766,776787766,777655667,777667788,777765566,777777766,777876545,777888767,778766565,778778766,778876766,778888989,778989878,787656544,787667887,787765665,787777787,787876566,787888788,788766656,788778787,788876787,788889878,788989899,789878788,789899887,789999987,876554454,876566567,876656545,876667888,876765666,876777788,877654345,877666544,877677888,877767887,877787887,877878789,877899888,878776665,878788887,878887665,878987678,887654444,887666565,887678766,887776543,887788765,887878888,887899988,888776678,888788987,888887678,888987777,889876765,889888988,889989877,898767654,898787654,898877777,898898778,898998877,899887655,899987676,987654434,987666555,987677899,987767898,987787898,987878878,987899899,988776676,988788898,988887676,988987767,989876677,989888899,989988999,998766788,998778998,998877767,998898776,998998789,999878999,999987666,1000000112,1000101001,1000121232,1001010121,1001111010,1001123233,1001221233,1001233434,1010011232,1010112110,1011000010,1011100110,1011121123,1011212111,1011232111,1012101122,1012122123,1012212332,1012232332,1012322323,1012334445,1100000110,1100100123,1100121222,1101010111,1101111000,1101123223,1101221223,1101233432,1110011222,1110112100,1111000000,1111100100,1111121121,1111212101,1111232101,1112101112,1112122121,1112212322,1112232322,1112322321,1112334443,1121000100,1121100121,1121121212,1121212122,1121232122,1122101211,1122122212,1122212343,1122232343,1122322334,1122334456,1123210121,1123223212,1123234556,1123332334,1123344455,1123434433,1123454332,1210001121,1210101212,1210122221,1211011122,1211111221,1211123443,1211222222,1211234344,1212112221,1212210122,1212223221,1212234565,1212332343,1212344456,1221011221,1221111234,1221123456,1221222321,1221234443,1222112234,1222211011,1222223234,1222321010,1222333212,1222344555,1223221111,1223233234,1223323233,1223343211,1223432333,1223444454,1223456567,1232112222,1232210123,1232223222,1232234566,1232332344,1232344543,1233221012,1233233222,1233323221,1233334565,1233432321,1233444434,1233456555,1234332111,1234343454,1234433432,1234445545,1234543222,1234554565,1234566678,2100011222,2100112100,2101000000,2101100100,2101121121,2101212101,2101232101,2110001112,2110101211,2110122212,2111011121,2111111212,2111123434,2111222221,2111234343,2112112212,2112210121,2112223212,2112234556,2112332334,2112344455,2121011212,2121111233,2121123455,2121222234,2121234434,2122112233,2122211010,2122223233,2122321001,2122333211,2122344554,2123221110,2123233233,2123323232,2123343210,2123432332,2123444445,2123456566,2210012221,2210112234,2211001011,2211101111,2211122112,2211212321,2211232321,2212110011,2212122333,2212221111,2212233234,2212323233,2212343211,2221001111,2221101210,2221122211,2221212334,2221232334,2222110111,2222123210,2222221210,2222233333,2222323332,2222343232,2223211210,2223223432,2223321123,2223333332,2223345445,2223434567,2223454544,2232110012,2232122334,2232221112,2232233321,2232323234,2232343212,2233211112,2233223334,2233321111,2233333234,2233345433,2233434555,2233454454,2234322223,2234334345,2234432101,2234443444,2234455565,2234545456,2234565433,2321001112,2321101211,2321122212,2321212343,2321232343,2322110112,2322123211,2322221211,2322233334,2322323333,2322343233,2323211211,2323223433,2323321210,2323333333,2323345454,2323443210,2323454545,2332110100,2332122343,2332221121,2332233322,2332323321,2332343221,2333211121,2333223343,2333321112,2333333321,2333345434,2333434556,2333454455,2334322232,2334334432,2334432110,2334443445,2334455566,2334545543,2334565434,2343211121,2343223343,2343321112,2343333321,2343345434,2343434556,2343454455,2344322232,2344334432,2344432110,2344443445,2344455566,2344545543,2344565434,2345432343,2345444456,2345456655,2345554332,2345565667,2345655567,2345667766,3210000121,3210101010,3210121233,3211010122,3211111011,3211123234,3211221234,3211233443,3212111233,3212123455,3212222234,3212234434,3212332212,3212344333,3221011011,3221111111,3221123333,3221222112,3221233456,3222112111,3222210011,3222222333,3222234455,3222332233,3222344432,3223212333,3223232333,3223322332,3223334454,3223432210,3223444323,3223455666,3232111234,3232123456,3232222321,3232234443,3232332221,3232344334,3233212321,3233232321,3233322234,3233334434,3233432112,3233443455,3233455654,3234323345,3234343323,3234433223,3234444566,3234456765,3234554434,3234566555];

    let mut j = 1;
    let h = k / 101;
    let mut s = 1;
    if h != 0 {
       s = p[h as usize];
       j = h * 101; 
    }
    for i in s..1123456789012 {
        if check(i) {
            if j == k {
                println!("{}", i);
                break;
            }
            j+=1;
        } 
    }
}