#include <iostream>

using namespace std;

void fastIO()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main()
{
    fastIO();

    long long a, b, k;
    cin >> a >> b >> k;

    if (k == 2)
    {
        cout << (b - a) / 2 << endl;
        return 0;
    }

    long long x = a + k - a % k;
    long long y = b - b % k;

    if (x > y)
    {
        if ((b - a) % 2 == 0) cout << (b - a) / 2 << endl;
        else cout << (b - a) / 2 + 1 << endl;

        return 0;
    }

    long long step1 = (x - 1 - a) / 2;
    if ((x - 1 - a) % 2 == 1) step1++;

    long long step2 = k / 2;
    if (k % 2 == 1) step2++;

    long long step3 = (b - y + 1) / 2;
    if ((b - y + 1) % 2 == 1) step3++;

    cout << step1 + ((y - x) / k) * step2 + step3 << endl;

    return 0;
}
