#include using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rep2(i, s, n) for (ll i = (s); i <= (ll)(n); i++) typedef long long ll; typedef vector vi; typedef vector> vvi; typedef vector>> vvvi; typedef vector vs; typedef vector> vvs; typedef vector>> vvvs; typedef vector vc; typedef vector> vvc; typedef vector>> vvvc; typedef vector vll; typedef vector> vvll; typedef vector>> vvvll; typedef vector vd; typedef vector> vvd; typedef vector>> vvvd; #define yes(ans) if(ans)cout << "yes"<< endl; else cout << "no" << endl #define Yes(ans) if(ans)cout << "Yes"<< endl; else cout << "No" << endl #define YES(ans) if(ans)cout << "YES"<< endl ;else cout << "NO" << endl #define all1(x) x.begin(),x.end() #define all2(x) x.rbegin(), x.rend() #define so(x) sort(all1(x)) #define re(x) reverse(all1(x)) #define rso(x) sort(all2(x)) #define vco(x, a) count(all1(x), a) #define per(x) next_permutation(all1(x)) int main(){ ll l, r, c; cin >> l >> r >> c; ll ans = 1000; if(r-l<=1000) rep2(i, l, r){ if(ans>1000-(i*c)%1000) ans = 1000-(i*c)%1000; if((i*c)%1000==0) ans = 0; } else rep(i, 1000){ if(ans>1000-(i*c)%1000) ans = 1000-(i*c)%1000; if((i*c)%1000==0) ans = 0; } cout << ans << endl; }