import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

void main() {
    while (true) {
        int n, p;
        scan(n, p);

        if (n == 0 && p == 0) return;

        auto m = new int[](n);
        int pos;
        int wan = p;

        while (true) {
            if (wan == 0) {
                wan += m[pos];
                m[pos] = 0;
            }
            else {
                wan--;
                m[pos]++;
            }

            if (m[pos] == p) break;
            (++pos) %= n;
        }

        writeln(pos);
    }
}



void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}