function Main(input){
    input = input.split("\n");
    var k = 0;
    while(1){
        input[k] = input[k].split(" ");
        var n = parseInt(input[k][0],10);
        var p = parseInt(input[k][1],10);
        if(n == 0 && p == 0)
            break;

        var temoto = [];

        for(var i = 0;i < n;i++){
            temoto[i] = 0;
        }

        aaa = n;
        for(var i = 0;;i = (i + 1) % n){
            if(p > 0){
                if(temoto[i] == 0)
                    aaa--;
                temoto[i]++;p--;
                if(aaa == n - 1 && p == 0){
                    console.log(i);
                    break;
                }
            }else{
                if(temoto[i] > 0)
                    aaa++;
                p += temoto[i];
                temoto[i] = 0;
            }
        }
        k++;
    }
}
Main(require("fs").readFileSync("/dev/stdin","utf8"));