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 True :
    n, p = map(int, input().split())
    if n == 0 and p == 0 :
        break
    
    kouho = list([0] * n)
    wan = p
    
    while True :
        end = False
        for i in range(n) :
            if wan > 0 :
                kouho[i] += 1
                wan -= 1
                if wan == 0 and kouho[i] == p :
                    print(i)
                    end = True
                    break
            else :
                wan += kouho[i]
                kouho[i] = 0
        if end == True :
            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: #!/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: def main(n, p):
    player = [0] * n
    ans = p
    while 1:
        for i in range(n):
            if p > 0:
                player[i] += 1
                p -= 1
                if p == 0 and player[i] == ans:
                    print(i)
                    return
            else:
                if player[i] > 0:
                    p += player[i]
                    player[i] = 0
while 1:
    n, p = map(int, input().split())
    if n == p == 0:
        break
    main(n,p)

Python: """
Problem A: 次期町長
https://onlinejudge.u-aizu.ac.jp/problems/1159
 3 ≤ n ≤ 50, 2 ≤ p ≤ 50
"""

def solve(n, q):
    have = [0]*n
    now = 0
    stone = q

    while(True):
        #print(n)
        if stone != 0:
            have[now] += 1
            stone -= 1
        else:
            stone = have[now]
            have[now] = 0

        if q in have:
            return have.index(q)

        now = (now+1)%n


if __name__ == '__main__':
    ans = []
    while(True):
        n, q = map(int, input().split())
        if n == 0 and q == 0:
            break
        ans.append(solve(n, q))
    print(*ans, sep='\n')

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: 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]
        else:
            x[i]+=1
            p-=1
        if x[i]==n:
            print (i)
            break
        i=add(i,n)

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: def get_two_int():
    two_int = input().split()
    for i in range(2):
        two_int[i] = int(two_int[i])
    return two_int

def distribute_stone(candidate_num, stone_num):
    bowl_stone = stone_num
    candidate_list = [0] * candidate_num
    i = 0
    while True:
        order = i % candidate_num
        if bowl_stone != 0:
            candidate_list[order] += 1
            bowl_stone -= 1
        elif bowl_stone == 0 and candidate_list[order] == stone_num:
            return order
        else:
            bowl_stone = candidate_list[order]
            candidate_list[order] = 0
        i += 1


if __name__ == "__main__":
    while True:
        candidate_num, stone_num = get_two_int()
        if candidate_num == 0:
            break
        # print("candidate_num", candidate_num) # debug
        # print("stone_num", stone_num) # debug
        order = distribute_stone(candidate_num, stone_num)
        # print("order", order) # debug
        print(order)

Python: while True:
    n, p = map(int, input().split())
    if n == 0 and p == 0:
        break
    originalP = p
    stone = [0 for i in range(n)]
    i = 0
    while True:
        if p != 0:
            stone[i] += 1
            p -= 1
        elif p == 0 and stone[i] > 0:
            p += stone[i]
            stone[i] = 0
        if stone[i] == originalP:
            break
        i += 1
        if i >= n:
            i = 0
    print(i)

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

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;