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 p+n<1: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: from itertools import cycle

while True:
    n, p = map(int, input().split())
    s = [0] * n

    if n == p == 0:
        break

    for i in cycle(range(n)):
        if p:
            p -= 1
            s[i] += 1
            if not p and sum(s) == s[i]:
                break
        else:
            p = s[i]
            s[i] = 0

    print(i)

Python: import sys
import math
input = sys.stdin.readline
##

n, p = input().rstrip().split()
n, p = int(n), int(p)
while n + p != 0:
    tegoma = [0] * n
    owan = p
    pivot = 0
    finish_flag = False
    while finish_flag is False:
        if owan != 0:
            owan = owan - 1
            tegoma[pivot] = tegoma[pivot] + 1
        else:
            if tegoma[pivot - 1] == p:
                finish_flag = True
            owan = tegoma[pivot]
            tegoma[pivot] = 0
        pivot = (pivot + 1) % n
    print((pivot - 2) % n)

    n, p = input().rstrip().split()
    n, p = int(n), int(p)

Python: from sys import stdin, stdout, stderr

a = [0] * 50

while 1:
    n, p = map(int, stdin.readline().split())

    if n + p == 0:
        break

    wan = p
    i = 0

    while a[i] < p:
        if wan > 0:
            a[i] += 1
            wan -= 1
        else:
            wan += a[i]
            a[i] = 0

        i = (i + 1) % n

    print(i)
    a[i] = 0

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

    wan = p
    candi = [0] * n
    cnt = 0
    while 1:
        turn = cnt % n
        if wan > 0:
            candi[turn] += 1
            wan -= 1
        else:
            wan += candi[turn]
            candi[turn] = 0

        if candi[turn] == p:
            break
        cnt += 1

    print(turn)

Python: def fun(n, p):
        stone = p
        stone_list = [0]*n
        index = 0
        while True:
                if stone != 0:
                        stone -= 1
                        stone_list[index] += 1
                        if stone == 0 and stone_list[index] == p:
                                print(index)
                                break
                else:
                        if stone_list[index] != 0:
                                stone += stone_list[index]
                                stone_list[index] = 0
                index += 1
                if index == n:
                        index = 0

n, p = map(int, input().split())
while n != 0:
        fun(n, p)
        n, p = map(int, input().split())

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

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

winner_number = []

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 having_amount.count(0) == len(having_amount) - 1:
                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 1:
    n, p = map(int, input().split())
    if n == 0 and p == 0:
        break

    member = [ 0 for i in range(n) ]
    stock  = p
    while 1:
        for index in range(len(member)):
            if stock > 0:
                stock -= 1
                member[index] += 1	
            elif stock == 0:
                stock = member[index]
                if p not in member:
                    member[index] = 0
        if p in member:
            break
    print(member.index(p))

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 and stones[er] == pp - 1:
            print(er)
            break
        if p >= 1:
            stones[er] += 1
            p -= 1
        else:
            p += stones[er]
            stones[er] = 0

Python: while True :
    n, p = map(int, input().split())
    if(n == 0 and p == 0) :
        break
    else :
        stone = [0 for i in range(n)]
        P = p
        cnt = 0
        for i in range(1000000) :
            cnt = cnt % n
            if(p == 0) :
                if(stone[cnt] == P) :
                    print(cnt)
                    break
                else :
                    p = stone[cnt]
                    stone[cnt] = 0
                    cnt += 1
            else :
                stone[cnt] += 1
                p -= 1
                cnt += 1

Python: 
while True:
	n, p = map(int, input().split())
	if (n, p) == (0, 0):
		break
	
	i = 0
	m = p;
	a = [0] * n

	while True:
		if p == 0:
			p = a[i]
			a[i] = 0
		else:
			p -= 1
			a[i] += 1
		if a[i] == m:
			print(i)
			break
		i += 1
		i %= n

Python: while True:
    n,p = map(int,input().split())
    if n==p==0: break
    s,c = [0]*n,p
    i = 0
    while True:
        if c:
            c-=1
            s[i%n]+=1
            if s[i%n]==p: break
        else:
            c+=s[i%n]
            s[i%n] = 0
        i+=1
    print(i%n)

Python: 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,man[a] =ball-1, man[a]+1
        elif ball == 0:ball,man[a] = man[a],ball
        a+=1
        if p in man:break
    print(man.index(max(man)))

Python: n=1
r=1
while n !=0 and r!=0:
    n,r=input().split()
    n,r=int(n),int(r)

    yama= list(0  for i in range(n))
    #print (yama)
    if n ==0 and r==0:
        break

    tern=0
    while r >-1 :
        kari=yama.copy()
        #print("a",kari)
        kari.pop(tern)
        #print("d",sum(kari))
        if r>0:
            r-=1
            yama[tern]+=1
            tern+=1
        elif r==0 and sum(kari)==0:
            #print("aa")
            r=-20
        elif r==0:
            r=yama[tern]
            yama[tern]=0
            tern+=1
        if tern > len(yama)-1:
            tern=0

    print(tern)