m;n;i;main(t){for(;i++?:~scanf("%d%d",&m,&n);printf(i/m?i=0,"%d\n":"%d ",t))for(t=n*i;t>m;t/=n-1)t=n*(t-m)-1;}