#include <iostream>

bool func(unsigned long long int a);

int main(){
	unsigned long long int n;
	while(std::cin >> n, n){
		unsigned long long int ans = 0;
		unsigned long long int n_=n;
		while(n_--){
			ans++;
			while(func(ans))ans++;
		}
		std::cout << ans << std::endl;
	}
	return 0;
}

bool func(unsigned long long int a){
	while(a > 0){
		if(a % 10 == 4 || a % 10 == 6)return true;
		a /= 10;
	}
	return false;
}