#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <random>
#include <cmath>
#include <iomanip>
#include <climits>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <deque>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>
#include <bitset>
#include <sstream>
#include <new>
#include <typeinfo>

typedef  long long ll;
#define mop 1000000007
using namespace std;

long long fastPow(long long a, long long b, long long mod=0){
    if(a == 0 and b == 0){
        return -1;
    }
    if(a == 0){
        return 0;
    }
    if(b == 0){
        return 1;
    }
    long long tmp = fastPow(a,b/2,mod);
    if(mod != 0) {
        if (b % 2 == 0) {
            return (tmp * tmp) % mod;
        } else {
            return ((tmp*tmp) % mod * (a % mod)) % mod;
        }
    }else{
        if (b % 2 == 0) {
            return (tmp*tmp);
        } else {
            return tmp * tmp * a;
        }
    }
}
ll ans = 0;


ll calc_lunlun(ll n, ll pre_dig, ll digit, bool ex0, bool wapos){
//    cout << n << " " << pre_dig << " " << digit << " " << ex0 << " " << wapos << " " << ans << endl;
    if(!digit){
        if(ex0){
            ans++;
        }
        return 0;
    }
    if(wapos){
        ll max_dig = n;
        for(ll i=0;i<digit-1;i++){
            max_dig /= 10;
        }
        max_dig %= 10;
        if(ex0){
            if(pre_dig+1 <= 9){
                if(pre_dig+1 == max_dig){
                    calc_lunlun(n, pre_dig+1, digit-1, true, true);
                }else if(pre_dig+1 < max_dig){
                    calc_lunlun(n, pre_dig+1, digit-1, true, false);
                }
            }
            {
                if(pre_dig == max_dig){
                    calc_lunlun(n, pre_dig, digit-1, true, true);
                }else if(pre_dig < max_dig){
                    calc_lunlun(n, pre_dig, digit-1, true, false);
                }
            }
            if(pre_dig-1 >= 0){
                if(pre_dig-1 == max_dig){
                    calc_lunlun(n, pre_dig-1, digit-1, true, true);
                }else if(pre_dig-1 < max_dig){
                    calc_lunlun(n, pre_dig-1, digit-1, true, false);
                }
            }
        }else{
            calc_lunlun(n, pre_dig, digit-1, false, false);
            for(ll i=1;i<=9;i++){
                if(i == max_dig){
                    calc_lunlun(n, i, digit-1, true, true);
                }else if(i < max_dig){
                    calc_lunlun(n, i, digit-1, true, false);
                }
            }
        }
    }else{
        if(ex0){
            if(pre_dig - digit >= 0 and pre_dig + digit <= 9){
                ans += fastPow(3, digit);
            }else{
                if(pre_dig+1 <= 9)calc_lunlun(n, pre_dig+1, digit-1, true, false);
                calc_lunlun(n, pre_dig, digit-1, true, false);
                if(pre_dig-1 >= 0)calc_lunlun(n, pre_dig-1, digit-1, true, false);
            }
        }else{
            calc_lunlun(n, pre_dig, digit-1, false, false);
            for(ll i=1;i<=9;i++){
                calc_lunlun(n, i, digit-1, true, false);
            }
        }
    }

}

ll digit(ll k){
    ll kmax = k;
    ll dig_def = 0;
    while(kmax > 0){
        kmax /= 10;
        dig_def++;
    }
    return dig_def;
}
int main() {
    

    ll k = 3234566667;
    ll kmax = k;
    ll dig_def = 0;
    while(kmax > 0){
        kmax /= 10;
        dig_def++;
    }
    ll idx;
    cin >> idx;
    ll l,r;
    l = 1, r = k;
    ll cnt;
    while(true){
        ll mid = (l+r)/2;
        ans = 0;
        calc_lunlun(mid, 0, digit(mid), false, true);
        if(ans < idx){
            l = mid+1;
        }else if(ans >= idx){
            if(l == r){
                cnt = l;
                break;
            }
            r = mid;
        }
    }
    cout << cnt << endl;
}

Measured memory_bytes: 256