#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

string func(int x, int D, int a, int b) {
    if (a < b) swap(a, b);
    string res;
    REP (i, D) {
        if (x >> i & 1) res += string(1, a + '0');
        else res += string(1, b + '0');
    }
    reverse(res.begin(), res.end());
    return res;
}

string solve(int rem, int D, int d, int a) {
    for (int bb = 0; bb < 10; ++bb) {
        if (bb < a) {
            if (rem <= (1LL << (D - d))) {
                return string(1, bb + '0') + func(rem - 1, D - d, a, bb);
            }
            rem -= (1LL << (D - d));
        } else if (bb == a) {
            if (rem <= 9 * ((1LL << (D - d)) - 1)) {
                return string(1, a + '0') + solve(rem, D, d + 1, a);
            }
            rem -= 9 * ((1LL << (D - d)) - 1);
        } else {
            if (rem <= (1LL << (D - d))) {
                return string(1, bb + '0') + func(rem - 1, D - d, a, bb);
            }
            rem -= (1LL << (D - d));
        }
    }
    return "?";
}

void _main() {
    int N;
    while (cin >> N, N) {
        int D = 2, tmp = 0;
        while (N > 81 * ((1LL << D) - D - 1)) {
            tmp = 81 * ((1LL << D) - D - 1);
            D++;
        }
        int a = 1, sum = tmp;
        while (N > 9 * ((1LL << (D - 1)) - 1) + sum) {
            sum += 9 * ((1LL << (D - 1)) - 1);
            a++;
        }
        cout << string(1, a + '0') + solve(N - sum, D, 2, a) << endl;
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
    _main();
    return 0;
}
