#include #define all(x) begin(x), end(x) #define times(x) for (int _ = 0 ; _ < (int)(x) ; _++) using i32 = int; using i64 = long long; using ld = long double; using usize = std::size_t; template inline bool chmax(T1 &a, const T2 &b) { return a < b and (a = b, true); } template inline bool chmin(T1 &a, const T2 &b) { return a > b and (a = b, true); } constexpr i64 supl = (std::numeric_limits::max() >> 1) - 100; constexpr i32 supi = (std::numeric_limits::max() >> 1) - 100; namespace zawa::input { template void in(T& res) { std::cin >> res; } template void in(Head& head, Tail&... tail) { in(head); in(tail...); } template void in(std::pair& res) { in(res.first); in(res.second); } template void in(std::vector& res) { for (auto& r : res) { in(r); } } } // namespace zawa::input using zawa::input::in; namespace zawa::output { void out() { std::cout << std::endl; } template void out(const T& a) { std::cout << a << std::endl; } template void out(const std::vector& as) { for (std::size_t i = 0 ; i < as.size() ; i++) { std::cout << as[i] << (i + 1 == as.size() ? '\n' : ' '); } } template void out(const Head& head, const Tail&... tail) { std::cout << head; if (sizeof...(tail)) { std::cout << ' '; } out(tail...); } void yesno(bool flag, std::string yes = "Yes", std::string no = "No") { std::cout << (flag ? yes : no) << std::endl; } } // namespace zawa::output using zawa::output::out; using zawa::output::yesno; namespace zawa::util { template std::vector sorted(std::vector A) { std::sort(A.begin(), A.end()); return A; } std::string sorted(std::string s) { std::sort(s.begin(), s.end()); return s; } } // namespace zawa::util namespace zawa::util { template std::vector reversed(std::vector A) { std::reverse(A.begin(), A.end()); return A; } std::string reversed(std::string s) { std::reverse(s.begin(), s.end()); return s; } } // namespace zawa::util using namespace zawa::util; // #include "atcoder/modint" // using mint = atcoder::modint998244353; // using mint = atcoder::modint1000000007; // #include "src/template/accum1d.hpp" // #include "src/template/binary-search.hpp" // #include "src/template/binary-search-ld.hpp" // #include "src/algorithm/compression.hpp" // #include "src/algorithm/RLE.hpp" // #include "src/graph/Read-Graph.hpp" // #include "src/graph/Read-Weighted-Graph.hpp" using namespace std; void main_() { i64 l, r, c; in(l, r, c); vector U(1000, false); for (i64 i = l ; i <= r ; i++) { if (U[(i * c) % 1000]) break; U[(i * c) % 1000] = true; } if (U[0]) out(0); else { for (i32 i = 999 ; i > 0 ; i--) if (U[i]) { out(1000 - i); break; } } } i32 main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); main_(); return 0; }