#include <iostream>
using namespace std;

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0208

bool hated(int x) {
	while (x > 0) {
		if (x % 10 == 4 || x % 10 == 6) return true;
		x /= 10;
	}
	return false;
}

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) break;
		int ans = 1;
		for (int i = 0; i < n - 1; i++) {
			ans++;
			while (hated(ans)) ans++;
		}
		cout << ans << endl;
	}
	return 0;
}