Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var (
	sc = bufio.NewScanner(os.Stdin)
)

func main() {
	for sc.Scan() {
		if sc.Text() == "0 0" {
			break
		}
		chunks := strings.Split(sc.Text(), " ")
		n, _ := strconv.Atoi(chunks[0])
		p, _ := strconv.Atoi(chunks[1])
		cnts := make([]int, n, n)
		for i := 0; true; i++ {
			j := i % n
			if p == 0 {
				p, cnts[j] = cnts[j], 0
			} else {
				cnts[j]++
				p--
			}

			if p == 0 {
				noneCnt := 0
				winner := 0
				for j, c := range cnts {
					if c == 0 {
						noneCnt++
					} else {
						winner = j
					}
				}
				if noneCnt == n-1 {
					fmt.Println(winner)
					break
				}
			}
		}
	}
}

Python: while 1:
    n,p = map(int,input().split())
    if n==0:break
    man = [i for i in [0]*n]
    ball,a = p,0
    while p not in man:
        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
    print(man.index(max(man)))

Python: while True:
    n,p = map(int,input().split())
    if n == p == 0: break
    stones = [0 for i in range(n)]
    i = 0
    wan = p
    while True:
        if wan > 0:
            wan -= 1
            stones[i] += 1
        else:
            wan += stones[i]
            stones[i] = 0
        if stones[i] == p:
            print(i)
            break
        i = (i+1) % n

Python: import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**3
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []
    while True:
        n,p = LI()
        mp = p
        if n == 0:
            break
        a = [0] * n
        i = 0
        while True:
            if p == 0:
                p = a[i]
                a[i] = 0
            else:
                a[i] += 1
                p -= 1
                if a[i] == mp:
                    break
            i = (i+1) % n
        rr.append(i)

    return '\n'.join(map(str, rr))


print(main())

Python: def add(i,n):
    if i==n-1:
        return 0
    else:
        return i+1
while 1:
    n,p=map(int,input().split())
    if n+p==0:
        break
    x=[0]*n
    i=0
    while 1:
        if p==0:
            p+=x[i]
            x[i]=0
        else:
            x[i]+=1
            p-=1
        if x[i]==n:
            print (i)
            break
        i=add(i,n)

Python: # Next Mayor

ans = []

while True:
    n, p = map(int, input().split())
    if n == 0 and p == 0:
        break

    stone = p
    person = [0] * n
    i = 0
    while True:
        i = i % n
        if stone > 1:
            person[i] += 1
            stone -= 1
            i += 1

        elif stone == 1:
            stone -= 1
            person[i] += 1
            if person[i] == p:
                ans.append(i)
                break
            else:
                i += 1

        else:
            stone += person[i]
            person[i] = 0
            i += 1

for i in ans:
    print(i)

Python: import numpy as np

while True:
    try:
        l = list(map(int, input().split()))
        S = l[0]
        p = np.zeros(S)
        M = l[1]
        i = 0
        N = M

        while True:
            if N == M and i != 0:
                ans = i % S - 1
                if ans < 0:
                    ans = S + ans
                print(ans)
                break

            if N == 0:
                N = p[i % S]
                p[i % S] = 0
                i += 1

            else:
                N -= 1
                p[i % S] += 1
                i += 1

    except:
        break;

Python: ans = []

while True:
    n, p = map(int, input().split())
    if n == 0 and p == 0:
        break
    player = [0 for i in range(n)]
    turn = 0
    max_p = p
    while True:
        if p == 0:
            p = player[turn]
            player[turn] = 0
        else:
            p -= 1
            player[turn] += 1

        if player[turn] == max_p:
            ans.append(turn)
            break
        if turn == n - 1:
            turn = 0
        else:
            turn += 1

for i in ans:
    print(i)

Python: import numpy as np

while True:
    try:
        l = list(map(int, input().split()))
        S = l[0]
        p = np.zeros(S)
        M = l[1]
        i = 0
        N = M
        if M == 0 and S == 0:
            break
        
        while True:
            if p[i] == M:
                print(i)
                break

            if N == 0:
                N = p[i]
                p[i] = 0
                i += 1
                i %= S

            else:
                N -= 1
                p[i] += 1
                i += 1
                i %= S

    except:
        break

Python: import numpy as np

while True:
    l = list(map(int, input().split()))
    S = l[0]
    p = np.zeros(S)
    M = l[1]
    i = 0
    N = M
    if M == 0 and S == 0:
        break

    while True:
        if p[i] == M:
            print(i)
            break

        if N == 0:
            N = p[i]
            p[i] = 0
            i += 1
            i %= S

        else:
            N -= 1
            p[i] += 1
            i += 1
            i %= S

Python: #!/usr/bin/python
# -*- coding: utf-8 -*-

# having_amount[i] represents the amount of pebbles the
# candidate numbered i is having in the "turn"

def is_winner(turn, having_amount, mayors):
    zero_condition = True
    for i in range(mayors):
        if i != turn:
            zero_condition = zero_condition and having_amount[i] == 0
    return zero_condition


while True:
    mayors, pebbles_in_bowl = map(int, input().split())

    # action of a candidate
    if mayors == 0 and pebbles_in_bowl == 0:
        break

    else:
        turn = 0
        having_amount = [0 for i in range(mayors)]

        while True:
            if pebbles_in_bowl == 0:
                pebbles_in_bowl += having_amount[turn]
                having_amount[turn] = 0
            elif pebbles_in_bowl == 1:
                # if the candidate in the turn wins
                if is_winner(turn, having_amount, mayors):
                    print(turn)
                    break
                else:
                    having_amount[turn] += 1
                    pebbles_in_bowl -= 1
            else:
                having_amount[turn] += 1
                pebbles_in_bowl -= 1

            # go to the next turn
            if turn == mayors - 1:
                turn = 0
            else:
                turn += 1

Python: while True:
    n, p = 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

Python: def election(a,b):
    candidate=[0 for i in range(a)]
    index=0
    bowl=b
    while True:
        if bowl==0:
            bowl+=candidate[index]
            candidate[index]=0
        else:
            bowl-=1
            candidate[index]+=1

        if candidate[index]==b:
            break

        if index==a-1:
            index=0
        else:
            index+=1

    return index


while True:
    n,p=[int(i) for i in input().split(" ")]
    if n==0 and p==0:
        break
    else:
        print(election(n,p))