#include #include using namespace std; using ll = long long; template using V = vector; template using VV = V >; const ll inf = 1000000000000000000; VV prime_factorize(ll n) { VV prime = {}; ll f = 2; while (f * f <= n) { if (n % f == 0) { n /= f; ll cnt = 1; while (n % f == 0) { n /= f; cnt++; } prime.push_back({ f,cnt }); } f++; } if (n != 1) { prime.push_back({ n,1 }); } return prime; } ll digit_sum(ll n, ll p) { ll res = 0; while (n) { res += n % p; n /= p; } return res; } ll kummer(ll N, ll K, V& p) { ll cnt = (digit_sum(N - K, p[0]) + digit_sum(K, p[0]) - digit_sum(N, p[0])) / (p[0] - 1); return cnt / p[1]; } int main(void) { ll N, K, M; cin >> N >> K >> M; VV prime = prime_factorize(M); ll ans = inf; for (auto& p : prime) { ans = min(ans, kummer(N, K, p)); } cout << ans << endl; return 0; }