#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main() {

	int i, n, count;
	char s1[64];
	char s2[2]="4";
	char s3[2]="6";

	while(cin >> n) {
		count=0;
		if(n==0)
			break;
		for(i=1; i<=n; i++) {
			count++;
			sprintf(s1, "%d", count);
			while((strpbrk(s1, s2)!=NULL)||(strpbrk(s1, s3)!=NULL)) {
				count++;
				sprintf(s1, "%d", count);
			}
		}
		cout << count << endl;
	}

	return 0;

}