#include <stdio.h>

int main(void){
    int n,p;
    while(1){
        int map[50]={0},sign=0;
        scanf("%d %d",&n,&p);
        int stone=p;
        if(n==0 && p==0)
            break;
        while(map[sign] != p-1 || stone != 1){
            if(stone == 0){
                stone=map[sign];
                map[sign]=0;
            }
            else{
                stone--;
                map[sign]++;
            }
            sign++;
            if(sign == n)
                sign=0;
        }
        printf("%d\n",sign);
    }
    return 0;
}