#include<iostream>
#include <bits/stdc++.h>
#define ll unsigned long long
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define MAX 100000
#define mod 1000000007
#define zr 0
ll n,k,M;
using namespace std;
ll xyp(ll x,ll y){
if(y == 1) return x;
if(y == 0) return 1;
if(y % 2 == 0){
ll p = xyp(x,y / 2);
return (p * p) % mod;
}
ll p = xyp(x,(y - 1));
return (x * (p) % mod) % mod;
}
ll invmod(ll x){
return xyp(x,mod - 2);
}
class fraction{
public:
ll num,den;
fraction(ll x,ll y){
num = x;
den = y;
}
fraction operator *(fraction f){
ll NUM = num * f.num,DEN = den * f.den;
fraction ans(NUM / __gcd(NUM,DEN),DEN / __gcd(NUM,DEN));
return ans;
}
fraction operator +(fraction f){
ll NUM = num * f.den + f.num * den,DEN = f.den * den;
fraction ans(NUM / __gcd(NUM,DEN),DEN / __gcd(NUM,DEN));
return ans;
}
fraction operator -(fraction f){
ll NUM = num * f.den - f.num * den,DEN = f.den * den;
fraction ans(NUM / __gcd(NUM,DEN),DEN / __gcd(NUM,DEN));
return ans;
}
bool operator >(fraction f){
return ((num * f.den) > (f.num * den));
}
ll moda(){
return (num * invmod(den)) % mod;
}
void print(){
//cout << num << " / " << den << "\n";
}
};
fraction rec(ll m,ll count){
if(count == M){
fraction g(1,n + (m) * k);
return g;
}
if(count > M){
fraction q(0,1);
return q;
}
fraction g(1,n + (m - 1) * k),p(n + (m - 1) * k - 1,n + (m - 1) * k);
fraction h(1,n + m * k),j(n + m * k - 1,n + m * k);
p = p * rec(m,count + 2);
//cout << "p = ";
//p.print();
g = g + p;
//cout << "g = ";
//g.print();
j = j * rec(m + 1,count + 1);
h = h + j;
if(g > h) return g;
return h;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(NULL);
ll n,m;
cin >> n >> m;
ll mini = LLONG_MAX,su = 0,p = -1;
vector <pair <ll,ll> > v;
while(m % 2 == 0){
m = m / 2;
su++;
}
if(su > 0){
v.push_back(mk(2,su));
}
for(int i = 3;i <= sqrt(m);i += 2){
if(m % i == 0){
ll sum = 0;
while(m % i == 0){
m = m / i;
sum = sum + 1;
}
v.push_back(mk(i,sum));
}
}
if(m > 2) v.push_back(mk(m,1));
for(auto x : v){
ll st = 0;
ll d = 1,e = x.f;
while(d > 0){
d = n / e;
st = st + d;
// It gets over flow So be careful with way you check conditions
if(e <= n / x.f)
e = e * x.f;
else
break;
}
st = st / x.s;
mini = min(st,mini);
}
cout << mini << "\n";
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgojZGVmaW5lIGxsIHVuc2lnbmVkIGxvbmcgbG9uZwojZGVmaW5lIGYgZmlyc3QKI2RlZmluZSBzIHNlY29uZAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1rIG1ha2VfcGFpcgojZGVmaW5lIE1BWCAxMDAwMDAKI2RlZmluZSBtb2QgMTAwMDAwMDAwNwojZGVmaW5lIHpyIDAKbGwgbixrLE07CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmxsIHh5cChsbCB4LGxsIHkpewoJaWYoeSA9PSAxKSByZXR1cm4geDsKCWlmKHkgPT0gMCkgcmV0dXJuIDE7CglpZih5ICUgMiA9PSAwKXsKCQlsbCBwID0geHlwKHgseSAvIDIpOwoJCXJldHVybiAocCAqIHApICUgbW9kOwoJfQoJbGwgcCA9IHh5cCh4LCh5IC0gMSkpOwoJcmV0dXJuICh4ICogKHApICUgbW9kKSAlIG1vZDsKfQpsbCBpbnZtb2QobGwgeCl7CglyZXR1cm4geHlwKHgsbW9kIC0gMik7Cn0KY2xhc3MgZnJhY3Rpb257CnB1YmxpYzoKCWxsIG51bSxkZW47CglmcmFjdGlvbihsbCB4LGxsIHkpewoJCW51bSA9IHg7CgkJZGVuID0geTsKCX0KCWZyYWN0aW9uIG9wZXJhdG9yICooZnJhY3Rpb24gZil7CgkJbGwgTlVNID0gbnVtICogZi5udW0sREVOID0gZGVuICogZi5kZW47CgkJZn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