#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++;
    }
}