#include<iostream>

typedef unsigned long long ll;

int main(){
	ll n;
	while (1){
		std::cin >> n;
		if (n == 0)break;
		unsigned long long a = 0;
		ll ans;
		for (ll i = 1; i < 1000000001; ++i){
			
			ll i_ = i;
			bool f = false;
			while (i_>0){
				if (i_ % 10 == 4 || i % 10 == 6){
					f = true;
					break;
				}
				i_ /= 10;
			}
			if (f)continue;
			a++;
			if (n == a){
				ans = i;
				break;
			}
		}
		std::cout << ans << std::endl;
	}
	return 0;
}