#include <iostream>
using namespace std;
unsigned long long num(unsigned long long n,unsigned long long k)
{
unsigned long long output=0,m=k;
while(n/m!=0)
{
output+=n/m;
m*=k;
}
return output;
}
unsigned long long taj(unsigned long long n,unsigned long long a[],unsigned long long p[])
{
unsigned long long i=2,j=0;
while(n>1)
{
if(n%i==0)
{
p[j]=0;
a[j]=i;
while(n%i==0)
{
p[j]++;
n/=i;
}
j++;
}
i++;
}
return j;
}
unsigned long long zero(unsigned long long x,unsigned long long y)
{
unsigned long long a[100],p[100],l,i,d[100];
int min=-1;
l=taj(y,a,p);
for(i=0;i<l;i++)
d[i]=0;
for(i=0;i<l;i++)
d[i]+=num(x,a[i]);
for(i=0;i<l;i++)
{
d[i]/=p[i];
if((d[i]<min)||(min==-1))
min=d[i];
}
return min;
}
int main()
{
unsigned long long x,y;
do{
cin >> x >> y;
if((x!=0)||(y!=0)) cout << zero(x,y) << endl;
} while((x!=0)||(y!=0));
return 0;
}
[I][I][I][I][I][I][I]