#include<iostream>
const int MAX_N = 1000;
int N;
std::string dp[MAX_N+10][MAX_N+10];
/*
* compares two integers represented as strings
* if what>competitor, 'what' value copies 'competitor' value
* returns true if value was copied, false otherwise
*/
bool reduce(std::string& what, const std::string& competitor)
{
int wlen = what.length(), clen = competitor.length();
if (!wlen)
{
what = competitor;
return true;
}
bool competitorIsLesser = false;
if (wlen != clen)
competitorIsLesser = wlen>clen;
else
{
for (int i = 0; i < wlen; ++i)
if (what[i] != competitor[i])
{
competitorIsLesser = what[i]>competitor[i];
break;
}
}
if (competitorIsLesser)
{
what = competitor;
return true;
}
return false;
}
/*
* iterate through all possible last digits and adds them to end
* tries to improve dp values with obtained integer
*
* tricky case: when adding '0', we can refer to previous dp values
* in this case, function calls recursively on that previous cell if it was improved
*/
void proceed(int sum, int rem)
{
for (int lastDigit = 9; lastDigit; --lastDigit)
reduce(dp[sum+lastDigit][(rem*10+lastDigit)%N], dp[sum][rem]+char('0'+lastDigit));
if (reduce(dp[sum][rem*10%N], dp[sum][rem]+'0') && rem*10%N<rem)
proceed(sum, rem*10%N);
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
std::cin>>N;
//starting conditions
for (int lastDigit = 9; lastDigit >= 0; --lastDigit)
dp[lastDigit][lastDigit%N] = char('0'+lastDigit);
//dp itself
for (int sum = 1; sum <= N; ++sum)
for (int rem = 0; rem < N; ++rem)
if (dp[sum][rem].length())
proceed(sum, rem);
std::cout<<dp[N][0];
}
I2luY2x1ZGU8aW9zdHJlYW0+Cgpjb25zdCBpbnQgTUFYX04gPSAxMDAwOwppbnQgTjsKc3RkOjpzdHJpbmcgZHBbTUFYX04rMTBdW01BWF9OKzEwXTsKCi8qCiAqIGNvbXBhcmVzIHR3byBpbnRlZ2VycyByZXByZXNlbnRlZCBhcyBzdHJpbmdzCiAqIGlmIHdoYXQ+Y29tcGV0aXRvciwgJ3doYXQnIHZhbHVlIGNvcGllcyAnY29tcGV0aXRvcicgdmFsdWUKICogcmV0dXJucyB0cnVlIGlmIHZhbHVlIHdhcyBjb3BpZWQsIGZhbHNlIG90aGVyd2lzZQogKi8KYm9vbCByZWR1Y2Uoc3RkOjpzdHJpbmcmIHdoYXQsIGNvbnN0IHN0ZDo6c3RyaW5nJiBjb21wZXRpdG9yKQp7CiAgICBpbnQgd2xlbiA9IHdoYXQubGVuZ3RoKCksIGNsZW4gPSBjb21wZXRpdG9yLmxlbmd0aCgpOwogICAgaWYgKCF3bGVuKQogICAgewogICAgICAgIHdoYXQgPSBjb21wZXRpdG9yOwogICAgICAgIHJldHVybiB0cnVlOwogICAgfQoKICAgIGJvb2wgY29tcGV0aXRvcklzTGVzc2VyID0gZmFsc2U7CiAgICBpZiAod2xlbiAhPSBjbGVuKQogICAgICAgIGNvbXBldGl0b3JJc0xlc3NlciA9IHdsZW4+Y2xlbjsKICAgIGVsc2UKICAgIHsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHdsZW47ICsraSkKICAgICAgICAgICAgaWYgKHdoYXRbaV0gIT0gY29tcGV0aXRvcltpXSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgY29tcGV0aXRvcklzTGVzc2VyID0gd2hhdFtpXT5jb21wZXRpdG9yW2ldOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgIH0KCiAgICBpZiAoY29tcGV0aXRvcklzTGVzc2VyKQogICAgewogICAgICAgIHdoYXQgPSBjb21wZXRpdG9yOwogICAgICAgIHJldHVybiB0cnVlOwogICAgfQoKICAgIHJldHVybiBmYWxzZTsKfQoKLyoKICogaXRlcmF0ZSB0aHJvdWdoIGFsbCBwb3NzaWJsZSBsYXN0IGRpZ2l0cyBhbmQgYWRkcyB0aGVtIHRvIGVuZAogKiB0cmllcyB0byBpbXByb3ZlIGRwIHZhbHVlcyB3aXRoIG9idGFpbmVkIGludGVnZXIKICogCiAqIHRyaWNreSBjYXNlOiB3aGVuIGFkZGluZyAnMCcsIHdlIGNhbiByZWZ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