#include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace atcoder; typedef long long ll; #define rep(i,n) for (int i = 0; i < int(n);i++) int main(){ ll l,r,c; cin >> l >> r >> c; ll ans = 1000; for (ll i = l; i <= min(l + 2000,r);i++){ if ((i*c)%1000 == 0) { ans = 0; break; } ans = min(ans,1000-(i*c)%1000); } cout << ans << endl; return 0; }