int main()
{
int t,n, k, total;
scanf("%d",&t);
while (t--)
scanf("%d %d", &n, &k);
total = 0;
int count=n;
while (count >= k)
total += count/k;
count/=k;
}
printf("%d\n", total+n);
return 0;