#include "bits/stdc++.h" using namespace std; void solve() { int l, r, m; cin >> l >> r >> m; if (l+m<=r) { cout << m << endl; return; } l %= m; r %= m; cout << (r-l+1+m)%m << endl; } int main() { solve(); //cout << "yui(*-v・)yui" << endl; return 0; }