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: import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline()[:-1]
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def MF(): return map(float, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LF(): return list(map(float, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
dij = [(0, 1), (1, 0), (0, -1), (-1, 0)]

def main():
    while 1:
        n,p0=MI()
        if n==0:break
        i=0
        s=[0]*n
        p=p0
        while 1:
            if p:
                s[i]+=1
                if s[i]==p0:
                    break
                p-=1
            else:
                p+=s[i]
                s[i]=0
            i=(i+1)%n
        print(i)

main()

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

def is_winner(having_amount):
    if having_amount.count(0) == len(having_amount) - 1:
        return True
    else:
        return False


def game(mayors, pebbles_in_bowl):
    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 is_winner(having_amount):
                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

# 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:
        game(mayors, pebbles_in_bowl)

Python: # AOJ 1159: Next Mayor
# Python3 2018.7.14 bal4u

while True:
	n, p = map(int, input().split())
	if n == 0: break
	ans, m = 0, p
	x = [0]*53
	while True:
		m -= 1
		if m >= 0:
			x[ans] += 1
			if x[ans] == p: break
		else: m = x[ans]; x[ans] = 0
		ans += 1
		if ans == n: ans = 0
	print(ans)

Python: #終了判定は　持ちこまがp-1であれば終われる。

"""
アルゴリズム

handstone : それぞれ候補者の持ちコマ リストで候補者はindexで member = 0~n-1まで存在
allstone : 現在のお椀のコマ

作るべき機能:

1. 
まず、現在のトリ役の手持ちをチェック　もし、お椀の中身が１個で、かつ持っている個数がp-1 であれば、breakして終了



お椀の中身をチェック
"""

while(True):

	n, p= input().split()
	
	if n =='0' and p =='0':
		break
	n = int(n) #n人
	p = int(p)
	allstone = int(p) #椀の石の数
	
	handstone = []
	for i in range(n):
		handstone.append(0)
	
	member = 0
	
	while(True):
			
		if member == n:
			member = 0
			
		if handstone[member] == p-1 and allstone == 1: #ほかの人が全く石を持っていない
				print(member)
				break
		
	
		if allstone == 0: #椀の中に石がない場合、戻そう
			allstone += handstone[member] 
			handstone[member] = 0
		else:
			handstone[member] += 1
			allstone -= 1
			
		member += 1

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: #!/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 True:
    n, p = map(int, input().split())
    if n == p == 0:
        quit()
    have = [0 for i in range(n)]
    pool = p
    flag = True
    while flag:
        for i in range(n):
            if pool > 0:
                have[i] += 1
                pool -= 1
                if pool == 0 and max(have) == p:
                    print(i)
                    flag = False
            else:
                pool = have[i]
                have[i] = 0

Python: # 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))

Python: from sys import stdin, stdout, stderr

a = [0] * 50
ans = []

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
 
    ans.append(i)
    a[i] = 0

print(*ans, sep='\n')

Python: ans_list = []

while True:
    n,p = map(int,input().split())
    if (n,p) == (0,0):
        break
    table = p
    a = [0]*n
    turn = 0
    while True:
        if table != 0:
            a[turn] += 1
            table -= 1
        else:
            table += a[turn]
            a[turn] = 0
        if a[turn] == p:
            ans = turn
            break
        turn += 1
        turn %= n
    ans_list.append(ans)

for ans in ans_list:
    print(ans)