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

int main(){
	int n,i;
	int count;
	while(1){
		cin >> n;
		count = 0;
		if(n==0){
			break;
		}
		i = 0;
		count = 1;
		while(1){
			if(std::to_string(count).find("4",0) == -1 && std::to_string(count).find("6",0) == -1){
				i++;
			}
			if(i==n){
				cout << count << endl;
				break;
			}

			count++;
		}

	}
}