#include using namespace std; int main() { long long l, r, m; cin >> l >> r >> m; long long d = (r - l) / m; l %= m; r %= m; if (d) cout << m << endl; else { if (l < r) cout << r - l << endl; else cout << (m - l) + r + 1 << endl; } return 0; }