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

int main(){
	while(1){
		int n, ans = 0;
		cin >> n;
		if(n == 0) break;
		for(int i = 0;i < n;i++){
			ans++;
			stringstream ss;
			ss << ans;
			string buf = ss.str();
			while(buf.find('4') != string::npos || buf.find('6') != string::npos){
				ans++;
				ss.str("");
				ss << ans;
				buf = ss.str();
			}
		}
		cout << ans << endl;
	}
	return 0;
}