#include <iostream>
#include <string>
using namespace std;
int main(void)
{
int n, cnt = 0;
cin >> n;
for (int i = 666; ; i++)
{
if (to_string(i).find("666") != string::npos)
cnt++;
if (n == cnt)
{
cout << i;
break;
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKHZvaWQpCnsKCWludCBuLCBjbnQgPSAwOwoJY2luID4+IG47CgoJZm9yIChpbnQgaSA9IDY2NjsgOyBpKyspCgl7CgkJaWYgKHRvX3N0cmluZyhpKS5maW5kKCI2NjYiKSAhPSBzdHJpbmc6Om5wb3MpCgkJCWNudCsrOwoJCWlmIChuID09IGNudCkKCQl7CgkJCWNvdXQgPDwgaTsKCQkJYnJlYWs7CgkJfQoJfQoJcmV0dXJuIDA7Cn0=