#include <stdio.h>
#define N 51
int main(int argc, const char * argv[]) {
    int n,p,wan,candidate[N][2],i;
    //candidate 0 ??£??¢ 1 ????????°
    while (1) {
        
        scanf("%d %d",&n,&p);
        
        if (n == 0 && p == 0) {
            break;
        }
        
       
        for (i = 0; i < n; i++) {
            candidate[i][0] = i;
            candidate[i][1] = 0;
        }
        
        
        wan = p;
        
        i = 0;
        
        while (1) {
            
            if (i > n-1) {
                i = 0;
            }
            
            if (wan > 0) {
                wan--;
                candidate[i][1]++;
                if (candidate[i][1] == p) {
                    printf("%d\n",candidate[i][0]);
                    break;
                }
                
                i++;
                continue;
            }
            
            if (wan <= 0) {
                wan = candidate[i][1];
                candidate[i][1] = 0;
                i++;
            }
            
        }
        
        
    }
    return 0;
}