#include<stdio.h>
int main(void)
{
	int n,p,son[50]={0},cou=0,count = 0,i,save=0;
	scanf("%d %d",&n, &p);
	while(n != 0 && p != 0){
	for(i = 0;i < n;i++){
	son[i]=0;
	}
	cou = 0;
	while(1){
		if(cou == n)
			cou = 0;
		if(p!=0){
			son[cou]++;
			p--;
		}else{
			p = son[cou];
			son[cou]=0;
		}
		cou++;
		if(p == 0){
			count =0; 
			for(i = 0;i < n;i++){
				if(son[i] != 0){
				count++;
				save = i;
				}
			}
			if(count == 1){
			
				break;
			}
		}
		}
		printf("%d\n",save);
		scanf("%d %d",&n, &p);
	}
	return 0;
}