#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
int main(){
cin >> n;
ll a = 0, cnt = 0;
while(true){
if(n == cnt) break;
if(to_string(a).find("666") != string::npos) cnt++;
a++;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwpsbCBuOwppbnQgbWFpbigpewogICAgY2luID4+IG47CiAgICBsbCBhID0gMCwgY250ID0gMDsKICAgIHdoaWxlKHRydWUpewogICAgICAgIGlmKG4gPT0gY250KSBicmVhazsKICAgICAgICAKICAgICAgICBpZih0b19zdHJpbmcoYSkuZmluZCgiNjY2IikgIT0gc3RyaW5nOjpucG9zKSBjbnQrKzsKICAgICAgICBhKys7CiAgICB9Cn0=