#include #include #include #include using namespace std; int main(){ int l,r,c;cin>>l>>r>>c; set A; vector B; int nw = (l-1)*c; for(int i = l; r >= i; i++){ nw = (nw+c)%1000; int 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; }