#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 <<   n ;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   template <typename T>   using minpq = priority_queue<T, vector<T>, greater<T>>;   template <typename T>   using maxpq = priority_queue<T>;   inline long long CC(long long n, long long y) {     return (((n) + (y - 1) - 1) / (y - 1)) - (((n) + (y)-1) / (y));   }   inline long long FF(long long n, long long y) { return n / y - n / (y + 1); }   inline long long __gcd(long long a, long long b) {     return !b ? a : __gcd(b, a % b);   }   inline int add(int a, int b, int p = 1000000007) {     int c = a + b;     if (c >= p) c -= p;     return c;   }   inline int sub(int a, int b, int p = 1000000007) {     int c = a - b;     if (c < 0) c += p;     return c;   }   inline int mul(int a, int b, int p = 1000000007) { return (a * 1ll * b) % p; }   long long a, b, k, L;   long long lcm(long long a, long long b) { return a / __gcd(a, b) * b; }   long long bfs(long long src, long long dest) {     __f( src, dest , src, dest);     queue<long long> q;     vector<bool> vis(src - dest + 1);     q.push(src);     q.push(0);     vis[src - dest] = 1;     while (1) {       long long u = q.front();       q.pop();       long long d = q.front();       q.pop();       if (u == dest) return d;       if (!vis[u - 1 - dest]) q.push(u - 1), q.push(d + 1);       for (long long i = 2; i <= k; i++) {         long long v = u / i * i;         if (v >= dest && !vis[v - dest]) {           vis[v - dest] = 1;           q.push(v);           q.push(d + 1);         }       }     }   }   int32_t main() {     ios_base::sync_with_stdio(false), cin.tie(NULL);     clock_t clk = clock();     cerr <<  I will return... n ;     cin >> a >> b >> k;     if (k == 2) {       cout << a - b;       return 0;     }     L = 1;     for (long long i = (2); i < (k + 1); i++) L = lcm(L, i);     long long ans = 0;     long long cnt = a / L - (b - 1) / L;     __f( L, cnt , L, cnt);     if (cnt) {       long long m = bfs(L, 0);       ans = m * (cnt - 1);       __f( ans , ans);     }     if (!cnt) {       ans += bfs(a, b);       __f( ans , ans);     } else {       ans += bfs(a, a / L * L);       ans += bfs((((b) + (L)-1) / (L)) * L, b);       __f( ans , ans);     }     cout << ans <<   n ;     cerr <<  ...don t you ever hang your head. n ;     cerr <<  Time (in ms):   << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC          <<   n ;   }   