// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
    std.range, std.container, std.bigint, std.ascii, std.typecons;

// }}}
// nep.scanner {{{
class Scanner
{
    import std.stdio : File, stdin;
    import std.conv : to;
    import std.array : split;
    import std.string : chomp;

    private File file;
    private char[][] str;
    private size_t idx;

    this(File file = stdin)
    {
        this.file = file;
        this.idx = 0;
    }

    private char[] next()
    {
        if (idx < str.length)
        {
            return str[idx++];
        }

        char[] s;
        while (s.length == 0)
        {
            s = file.readln.chomp.to!(char[]);
        }

        str = s.split;
        idx = 0;

        return str[idx++];
    }

    T next(T)()
    {
        return next.to!(T);
    }

    T[] nextArray(T)(size_t len)
    {
        T[] ret = new T[len];

        foreach (ref c; ret)
        {
            c = next!(T);
        }

        return ret;
    }

    void scan()()
    {
    }

    void scan(T, S...)(ref T x, ref S args)
    {
        x = next!(T);
        scan(args);
    }
}
// }}}

void main()
{
    auto cin = new Scanner;

    while (true)
    {
        int n, p;
        cin.scan(n, p);
        if (n == 0 && p == 0) break;
        int[] ar = new int[n];
        int sum;
        for (int i = 0; i < n; i = (i + 1) % n)
        {
            if (p == 0)
            {
                p = ar[i];
                sum -= ar[i];
                ar[i] = 0;
            }
            else
            {
                ++sum;
                ++ar[i];
                --p;
                if (p == 0 && sum == ar[i])
                {
                    writeln(i);
                    break;
                }
            }
        }
    }
}

