#include #include #include #include using namespace std; int main(){ long long l,r,c;cin>>l>>r>>c; set A; vector B; long long nw = ((l-1)*c)%1000; for(int i = l; r >= i; i++){ nw = (nw+c)%1000; long long rem = (1000-nw)%1000; if(A.count(rem)){ sort(B.begin(),B.end()); cout << B[0] << endl; return 0; } A.insert(rem); B.push_back(rem); } sort(B.begin(), B.end()); cout << B[0] << endl; }