#include #define rep(i, a, n) for(int i = a; i < (n); i++) using namespace std; using ll = long long; using P = pair; const int INF = 1001001001; const ll LINF = 1001002003004005006ll; //const int mod = 1000000007; //const int mod = 998244353; int main() { ll l, r, c; cin >> l >> r >> c; map mp; ll ans = LINF; for (ll i = l; i <= r; i++) { if (i*c <= 1000) { ans = min(ans, 1000 - i*c); } else if (mp[1000-i*c%1000]) break; else if (i*c%1000 == 0) { ans = 0; break; } else { mp[1000-i*c%1000] = 1; ans = min(ans, 1000-i*c%1000); } } cout << ans << endl; return 0; }