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())
    pp = p
    if n == p == 0:
        break
    stones = [0] * n
    i = 0
    while True:
        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
        i += 1

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

Python: from sys import stdin, stdout, stderr

a = [0] * 50
lim = 10**6

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

    if n + p == 0:
        break

    wan = p
    i = 0

    for i in range(lim):
        pos = i % n

        if wan > 0:
            a[pos] += 1
            wan -= 1

            if a[pos] == p:
                print(pos)
                a[pos] = 0
                break
        else:
            wan += a[pos]
            a[pos] = 0

Python: while True:
  n,p = map(int,input().split())
  if n*p == 0:
    break
  a = [0 for _ in range(n)]
  stone = p
  ans = 0
  while True:
    for i in range(n):
      if stone==0:
        stone = a[i]
        a[i] = 0
      elif stone==1:
        a[i]+=1
        stone-=1
        if a[i]==p:
          stone = -1
          ans = i
          break
      else:
        a[i]+=1
        stone-=1
    if stone==-1:
      print(ans)
      break

Python: if __name__ == "__main__":
    while True:
        n,p = list(map(int,input().strip().split()))
        if n == 0 and p ==0:break
        cup = p
        candidates = [0 for i in range(n)]
        current = 0
        while True:
            if cup == 0:
                cup = candidates[current]
                candidates[current] = 0

            else:
                cup = cup -1
                candidates[current] += 1
                if cup == 0 and candidates[current] == p:break
            if current ==n-1: current = 0
            else: current +=1
        print(current)

Python: while True:
    l = list(map(int, input().split()))
    S = l[0]
    p = [0 for i in range(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: line = input()
while line != '0 0':
    line_split = line.split(' ')
    no_candidate = int(line_split[0])
    no_stone_begin = int(line_split[1])
    no_stone = no_stone_begin
    candidate_list = [0]*no_candidate
    i = 0
    position = 0
    while True:
        if no_stone > 0:
            candidate_list[position] += 1
            no_stone -= 1
        else:
            if candidate_list[position] != no_stone_begin:
                no_stone = candidate_list[position]
                candidate_list[position] = 0
            else:
                print(str(position))
                break
        i += 1
        position = i % no_candidate
    line = input()

Python: while 1:
  n,p=map(int,input().split())
  if not n and not p:break
  l=[0 for _ in range(n)]
  np=p
  s=0
  while 1:
    if np>0:
      l[s]+=1
      np-=1
      if l[s]==p:
        print(s)
        break
    elif np==0:
      np+=l[s]
      l[s]=0
    s+=1
    if s>n-1:s=0

Python: def main():
    while True:
        data = input()
        data = data.split(" ")
        
        for i in range(len(data)):
            data[i] = int(data[i])

        if data == [0, 0]:
            return 

        
        man = data[0]
        donst = data[1]
        print(game(man, donst))

def game(man, donst):
    manst = [0 for j in range(man)]

    while True:
        for j in range(len(manst)):
            if donst != 0:
                donst -= 1
                manst[j] += 1

                if donst == 0:
                    zerocount = 0
                    for item in manst:
                        if item == 0:
                            zerocount += 1

                    if zerocount == man - 1:
                        return j

            else:
                donst = manst[j]
                manst[j] = 0
main()

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

    holding = [0]*n
    ctr = 0
    wan = p
    for _ in range(1000000):
        if wan != 0:
            holding[ctr] += 1
            wan -= 1
            if wan == 0 and holding[ctr] == p:
                print(ctr)
                break
        else:
            wan = holding[ctr]
            holding[ctr] = 0
        ctr += 1
        if ctr >= n: ctr -= 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-=1
            man[a]+=1
        elif ball == 0:ball,man[a] = man[a],ball
        if man[a]==p:break
        a+=1
        
    print(a)

Python: #!/usr/bin/env python3
#AOJ A
while 1:
    n,p = map(int,input().split())
    if n == 0 and p == 0:
        break
    s = [0]*n
    w = p
    while 1:
        for i in range(n):
            flg = False
            if w > 0:#碗が空でない
                w -= 1
                s[i] += 1
            else:#碗が空
                if s[i - 1] == p:
                    flg = True
                    if i > 0:
                        ans = i-1
                    else:
                        ans = n-i-1
                    break
                w += s[i]
                s[i] = 0
        if flg:
            print(ans)
            break

Python: while True:
    n, p = [int(s) for s in input().strip().split()]

    if n == 0 and p == 0:
        break

    wan = p
    cands = [0] * n

    is_decided = False
    while not is_decided:
        for i in range(n):
            if wan == 1 and (cands[i] + 1) == p:
                print(i)
                is_decided = True
                break

            if wan == 0:
                wan += cands[i]
                cands[i] = 0
            else:
                wan -= 1
                cands[i] += 1

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
        a+=1
        if p in man:break
    print(man.index(max(man)))

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