Problem A: Next Mayor

One of the oddest traditions of the town of Gameston is that the next town mayor is chosen through a game. As the current mayor's term approaches expiration, at least three candidates, including the current mayor, play a game of pebbles, and the winner becomes the next mayor.

The game of pebbles involves a round table, a bowl, and pebbles. The number of pebbles in the bowl is determined by a secret stochastic process. Candidates, numbered from 0 to n-1, sit around the table in a counterclockwise order. The serving mayor initially holds the bowl.

During the game, a candidate takes a pebble from the bowl if there are any, and if not, puts all the pebbles they have into the bowl before passing it to the next candidate. The game continues until a winner is decided.

The game always ends within a finite number of steps, although it may require a large number of steps.

Input:
The input consists of a sequence of datasets, each containing two integers n and p separated by a space. n represents the number of candidates, including the current mayor, and p represents the initial number of pebbles in the bowl. The game will end within one million steps.

Output:
The output should contain the winner's candidate number for each input dataset, with no other characters.

Sample Input:
3 2
3 3
3 50
10 29
31 32
50 2
50 50
0 0

Output for the Sample Input:
1
0
1
5
30
1
13

# Buggy version:
# n= 人数　p = 最初に入っている個数
n, p = map(int,input().split())

def solve(n, p):
    tmp = p
    i = 0
    person = [0] * n
    while(1):
        if tmp != 0:
            person[i] += 1
            tmp -= 1
            if person[i] == p:
                return i
        else:
            tmp = person[i]
            person[i] = 0

        i += 1
        if i == n:
            i = 0

        if p == 0:
            for i in range(n):
                if person[i] > 0:
                    return i + 1
ans = []
while(1):
    n, p = map(int,input().split())
    if n == 0 and p == 0:
        for i in ans:
            print(i)
        break
    else:
        ans.append(solve(n, p))



# Fixed version:
# n= 人数　p = 最初に入っている個数

def solve(n, p):
    tmp = p
    i = 0
    person = [0] * n
    while(1):
        if tmp != 0:
            person[i] += 1
            tmp -= 1
            if person[i] == p:
                return i
        else:
            tmp = person[i]
            person[i] = 0

        i += 1
        if i == n:
            i = 0

ans = []
while(1):
    n, p = map(int,input().split())
    if n == 0 and p == 0:
        for i in ans:
            print(i)

        break
    else:
        ans.append(solve(n, p))

# Buggy version:
while 1:
    n,p = map(int,input().split())
    if n==0:break
    man =  [0]*n
    ball,a = p,0
    while 1:
        if ball >0:ball,man[a%n] =ball-1, man[a%n]+1
        elif ball == 0:ball,man[a%n] = man[a%n],ball
        a+=1
        if p in man:break
    print(man.index(max(man)))        )            

# Fixed version:
while 1:
    n,p = map(int,input().split())
    if n==0:break
    man =  [0]*n
    ball,a = p,0
    while 1:
        a%=n
        if ball >0:
            ball-=1
            man[a]+=1
        elif ball == 0:ball,man[a] = man[a],ball
        if man[a]==p:break
        a+=1
    print(a)