#include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)
#define for_rev(i,a,b) for(int i=(a);i>=(b);--i)

typedef long long lint;

lint dp[20][2][2];

lint calc(lint N) {
	memset(dp, 0, sizeof(dp));
	
	lint div = 1;
	for_(i,0,18) div *= 10;
	
	dp[19][0][0] = 1;
	
	for_rev(i,19,1) {
		lint d = (N / div) % 10;
		
		for_(j,0,2) for_(k,0,2) {
			if (dp[i][j][k] == 0) continue;
			
			for_(a,0,10) {
				if (j == 0 && d < a) continue;
				int nj = (j == 1 || d > a) ? 1 : 0;
				int nk = (k == 1 || a == 4 || a == 6) ? 1 : 0;
				dp[i - 1][nj][nk] += dp[i][j][k];
			}
		}
		
		div /= 10;
	}
	
	return dp[0][1][1];
}

void solve(lint n) {
	lint lb = 1, ub = (lint)1e15;
	
	while (ub - lb > 1) {
		lint med = (ub + lb) / 2;
		lint c = calc(med);
		
		if (med - c > n) ub = med;
		else lb = med;
	}
	
	cout << lb << endl;
}

int main() {
	int n;
	while (cin >> n, n) solve(n);
}