while True:

    n, p = list(map(int, input().split()))

    pp = p

    if n == p == 0:

        break

    stones = [0] * n

    i = 0

    while True:

        er = i % n

        if p == 1 and stones[er] == pp - 1:

            print(er)

            break

        if p >= 1:

            stones[er] += 1

            p -= 1

        else:

            p += stones[er]

            stones[er] = 0

        i += 1
while True:

    n, p = list(map(int, input().split()))

    pp = p

    if n == p == 0:

        break

    stones = [0] * n

    for i in range(1000000):

        er = i % n

        if p >= 1:

            stones[er] += 1

            p -= 1

            if p == 0 and stones[er] == pp:

                print(er)

                break

        else:

            p = stones[er]

            stones[er] = 0
while True:
    n, p = list(map(int, input().split()))
    pp = p
    if n == p == 0:
        break
    stones = [0] * n
    i = 0
    while True:
        er = i % n
        if p == 1 and stones[er] == pp - 1:
            print(er)
            break
        if p >= 1:
            stones[er] += 1
            p -= 1
        else:
            p += stones[er]
            stones[er] = 0
        i += 1

while True:
    n, p = list(map(int, input().split()))
    pp = p
    if n == p == 0:
        break
    stones = [0] * n
    for i in range(1000000):
        er = i % n
        if p >= 1:
            stones[er] += 1
            p -= 1
            if p == 0 and stones[er] == pp:
                print(er)
                break
        else:
            p = stones[er]
            stones[er] = 0

-    i = 0 -    while True: +    for i in range(1000000): -        if p == 1 and stones[er] == pp - 1: -            print(er) -            break +            if p == 0 and stones[er] == pp: +                print(er) +                break -            p += stones[er] +            p = stones[er] -        i += 1