#include<bits/stdc++.h>
#define FOR(i,a,b,k) for (ll i=a;i<=b;i+=k)
#define FORD(i,a,b,k) for (ll i=a;i>=b;i-=k)
#define pb(i) push_back(i)
#define ll long long int
#define MOD 1000000007
#define lim 1e16
using namespace std;
ll a,b,c,res=0;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin>>a>>b>>c;
if(c%2==0)
{
if(a%2==0) {res++;a++;}
if(b%2==0) {res++;b--;}
res+=(b-a)/2;
}
else
{
ll them_a=c-1-a%c;
ll chuky=(c+1)/2;
ll tanso=(b-a)/c;
a+=them_a+tanso*c;
res+=them_a/2+them_a%2+tanso*chuky;
res+=(b-a)/2+(b-a)%2;
}
cout<<res;
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBGT1IoaSxhLGIsaykgZm9yIChsbCBpPWE7aTw9YjtpKz1rKQojZGVmaW5lIEZPUkQoaSxhLGIsaykgZm9yIChsbCBpPWE7aT49YjtpLT1rKQojZGVmaW5lIHBiKGkpIHB1c2hfYmFjayhpKQojZGVmaW5lIGxsIGxvbmcgbG9uZyBpbnQKI2RlZmluZSBNT0QgMTAwMDAwMDAwNwojZGVmaW5lIGxpbSAxZTE2CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmxsIGEsYixjLHJlcz0wOwoKaW50IG1haW4oKQp7CiAgICAJaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICAgICAgY2luLnRpZShOVUxMKTtjb3V0LnRpZShOVUxMKTsKICAgIGNpbj4+YT4+Yj4+YzsKICAgIAogICAgaWYoYyUyPT0wKQoJewoJCWlmKGElMj09MCkge3JlcysrO2ErKzt9CgkJaWYoYiUyPT0wKSB7cmVzKys7Yi0tO30KCQkKCQlyZXMrPShiLWEpLzI7Cgl9CgllbHNlIAoJewoJCWxsIHRoZW1fYT1jLTEtYSVjOwoJCWxsIGNodWt5PShjKzEpLzI7CgkJbGwgdGFuc289KGItYSkvYzsKCQkKCQlhKz10aGVtX2ErdGFuc28qYzsKCQlyZXMrPXRoZW1fYS8yK3RoZW1fYSUyK3RhbnNvKmNodWt5OwoJCQoJCXJlcys9KGItYSkvMisoYi1hKSUyOwoJfQoJCgljb3V0PDxyZXM7CiAgICByZXR1cm4gMDsKfQ==