#include #include using namespace std; using namespace atcoder; using ll = long long; void fast_io() { cin.tie(nullptr); ios_base::sync_with_stdio(false); } int main(void) { fast_io(); ll l, r, c; cin >> l >> r >> c; set st; for (ll i = l; i <= r; i++) { ll k = 1000 - i * c; k = (k + 1000) % 1000; if (st.count(k)) { cout << *st.begin() << endl; return 0; } else { st.insert(k); } } cout << *st.begin() << endl; return 0; }