#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define inf (1<<29)
using namespace std;
typedef vector<pair<int, int> > vpii;

bool check(long n) {
    int digit;
    while(n > 0) {
        digit = n % 10;
        if(digit == 4 || digit == 6) {
            return false;
        }
        n /= 10;
    }
    return true;
}

int main() {
    long n, ans;
    while(true) {
        cin >> n;
        if(n == 0) {
             break;
        }
        ans = 0;
        while(n > 0) {
            while(!check(++ans)) {}
            n--;
        }
        cout << ans << endl;
    }
}