#include<bits/stdc++.h>
using namespace std;
//int t[1e7+1], t[1e7+1], t[1e7+1];
int a, b, k;
long long ans;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>a>>b>>k;
    /*for(int i=1; i<k-1; i++){
        if(t[i+1]==0) t[i+1]=t[i]+1;
        else t[i+1]=min(t[i+1], t[i]+1);
        if(t[i+2]==0) t[i+2]=t[i]+1;
        else t[i+2]=min(t[i+2], t[i]+1);
    }
    for(int i=a; i%k!=0; i++){

    }*/
    if(b-a+1<k){
        cout<<(b-a)/2+(b-a)%2;
        return 0;
    }
    long long curr=k-a%k-1;
    b-=a;
    b+=a%k;
    ans=curr/2+curr%2+1;
    //cout<<curr<<endl;
    if(b-k-1>=k){
        curr=((b-k-1)/k)*(k-1)/2+(k-1)%2+1;
        ans+=curr;
        //cout<<curr<<endl;
    }
    curr=b%k-1;
    //cout<<curr<<endl;
    ans+=curr/2 + curr%2;
    cout<<ans;
    return 0;
}

