#include <iostream>
#include <string>

using namespace std;

long long a[11]={0, 797380500, 1392951000, 2190321500, 2987902000, 3783282500, 5380853000, 7178223500, 7973805000, 8771185500, 9358757000};
char b[100000]={};

long long int number(int n){
  long long int h=0;
  int p=0;

  //if(n>=100000000 && n<200000000){p=100000000;h=a[1]}
  p=(n/100000000)*100000000;
  h=a[p/100000000];

  while(p<n){
    h++;
    if(b[h%100000]==1 && b[h/100000]==1)
      p++;
    }

  return h;
}
  


int main() {
    ios::sync_with_stdio(false);
    int s;
    int m=0;
    while(m<=99999){
      int t=m;
      while(t>0){
	if(t%10==4 || t%10==6)
	  {b[m]--;break;}
	else{
	  t=t/10;      
	}
      }
      b[m]++;
      m++;
    }
    
    int p,q;
    
    while (1) {
      cin >> s;
      if(s != 0){
	cout << number(s) << endl;
      }
      else
	break;
    }

    return 0;
}