/*解题思路:分解为十进制的一位,每位求余一次。即设str[0--n]为大数(0为个位,1为十位,2为百位,...)
有
ans[0] = str[0] % b;
ans[1] = str[1] * 10 % b;
.
.
.
ans[n] = str[n]*10^n % b;
这里要解决对10的n次方求余的问题,其实很简单,设temp[k]=10^k%b,必有
temp[k] = temp[k] * 10 %b;
那么,得出最后的方程为
ans[k] = str[k] * temp[k] % b;
ans = ans[0]+...+ans[n];
其实就简单的求余操作。。。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char str[1050];
int n,i,j;
int b;
while(scanf("%s",str) != EOF)
{
scanf("%d",&b);
int ans = 0,temp = 1;
n = strlen(str);
for(i = n-1; i >= 0; i--)
{
ans += (((str[i] - '0') * temp) % b);
ans = ans % b;
temp = ((temp *10) %b);
}
printf("%d\n",ans);
}
return 0;
}