#include <iostream>

using namespace std;

int main () {
    long int l, n, m;
    while (cin >> n, n) {
          m = 1;
          for (long int i = 1; i < n; ++i) {
              ++m;
              l = m;
              for (int j = 1; l; j *= 10) {
                  long int x;
                  x = (l % (j * 10)) / j;
                  l -= x;
                  if (x == 6 || x == 4) m += j;
              }
          }
          cout << m << endl;
    }
}