#include <iostream>

using namespace std;

bool odd(long long a)
{
    return a % 2 == 1;
}

bool even(long long a)
{
    return a % 2 == 0;
}

long long PathQ(long long length)
{
    if (odd(length))
    {
        return length / 2 + 1;
    }
    return length / 2;
}

long long PathQ(long long from, long long to)
{
    if (odd(to - from))
    {
        return (to - from) / 2 + 1;
    }

    return (to - from) / 2;
}

int main()
{
    long long N, M, K, Ans = 0;

    cin >> N >> M >> K;

    long long FirstK = N / K * K + K;
    long long LastK = M / K * K;
    long long Interval = (LastK - FirstK) / K;

    if (FirstK > M)
    {
        cout << PathQ(N, M);
        return 0;
    }

    cout << PathQ(N, FirstK - 1) + PathQ(K) * Interval + PathQ(LastK - 1, M);
}
