#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

long long int solve(long long int N) {
    string s = to_string(N);
    int len = s.length();
    
    long long int dp[15][2] = {};
    dp[0][0] = 1;
    for(int i=0; i<len; i++) {
        for(int j=0; j<2; j++) {
            int lim = j ? 9 : s[i] - '0';
            for(int x=0; x<lim+1; x++) {
                if(x == 4 || x == 6) continue;
                dp[i+1][j || x < lim] += dp[i][j];
            }
        }
    }
    
    long long int ret =  dp[len][0] + dp[len][1] - 1;
    return ret;
}


int main(void){
    // Your code here!
    int N;

    while(cin >> N, N) {
        long long int ub = 1LL << 40, lb = 0;
        while(ub - lb > 1) {
            long long int mid = (ub + lb) / 2;
            long long int val = solve(mid);
            
            if(val >= N) ub = mid;
            else lb = mid;
        }
        cout << ub << endl;
    }
    return 0;
}

