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: #!/usr/bin/python
# -*- coding: utf-8 -*-

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



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

winner_number = []

# initialize
turn = 0

while True:
    mayors, pebbles_in_bowl = map(int, input().split())
    having_amount = [0 for i in range(mayors)]

    # action of a candidate
    if mayors == 0 and pebbles_in_bowl == 0:
        break
    elif pebbles_in_bowl == 0:
        pebbles_in_bowl += having_amount[turn]
        having_amount[turn] = 0
    elif pebbles_in_bowl == 1:
        if is_winner(turn):
            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:
        turn = 0
    else:
        turn += 1

    print(*having_amount)

Python: from copy import copy
while True:
    n,p=map(int,input().split())
    if n==0:break
    l=[0 for _ in range(n)]
    a=-1
    p1=copy(p)
    while True:
        a+=1
        if a==n:
            a=0
        if p>0:
            if l[a]+1==p1:
                print(a)
                break
            l[a]+=1
            p-=1
            continue
        if p==0:
            p=l[a]
            l[a]=0

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] = man[a%n],ball
        if ball >0:
            ball,man[a%n] =ball-1, man[a%n]+1
        a+=1
    print(man.index(max(man)))

Python: #23:16

while True:
    candidate,max_stones = map(int,input().split())
    if candidate == 0 and max_stones ==0 :
        break
    # 候補者がもっている石の数
    have_stones = [0] * candidate
    i = 0
    stones = max_stones
    while True:
        if not stones == 0 :
            stones = stones -1
            have_stones[i] = have_stones[i] + 1
        else :
            stones = stones + have_stones[i]
            have_stones[i] = 0
        
        if stones == 0 and have_stones[i]  == max_stones:
            print(i)
            break
        i = i+1 
        i = i % candidate

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

Python: while True:
    (n, p) = [int(x) for x in input().split()]
    if n==0 and p==0:
        exit()
    s = [0 for _ in range(n)]
    i = 0
    q = p
    while True:
#        print(i,q,s)
        if q == 0:
            q = s[i]
            s[i] = 0
        else:
            s[i] += 1
            q -= 1
            if q==0 and s[i]==p:
                print(i)
                break
        i = (i+1)%n

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

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

import collections
import itertools


BOWL = -1


def simulation(n, p):
    status = collections.defaultdict(int)
    status[BOWL] = p
    for candidate in itertools.cycle(range(n)):
        if status[BOWL] > 0:
            status[candidate] += 1
            status[BOWL] -= 1
            if status[candidate] == p:
                return candidate
        else:
            status[BOWL] += status[candidate]
            status[candidate] = 0


if __name__ == "__main__":
    while True:
        n, p = map(int, input().split())
        if n == 0 and p == 0:
            break
        else:
            print(simulation(n, p))

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 collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

while True:
    n,p = inpl()
    if n == 0:
        break
    else:
        x = 0
        S = p
        temoto = [0]*n
        while True:
            if p != 0:
                p -= 1
                temoto[x] += 1
                if p == 0 and temoto[x] == S:
                    print(x)
                    break
            else:
                p += temoto[x]
                temoto[x] = 0

            x += 1
            if x == n:
                x = 0

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

Python: #2009_A
def solve(n,p):
    a = [0]*n
    for _ in range(1000000):
        for i in range(n):
            if p:
                p -= 1
                a[i] += 1
                if p == 0:
                    if sum(a)-a[i] == 0:
                        return i
            else:
                p += a[i]
                a[i] = 0

while 1:
    n,p = [int(x) for x in input().split()]
    if n == 0:
        break
    print(solve(n,p))

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

Python: while True:
    n, p = map(int, input().split())
    if n == 0 and p == 0:
        break
    kouho = [0 for i in range(n)]
    i = 0
    motokazu = p
    while True:
        if p == 0 and kouho[i] == motokazu:
            print(i)
            break
        elif not p == 0:
            kouho[i] += 1
            p -= 1
        elif p == 0 and kouho[i] >= 1:
            p += kouho[i]
            kouho[i] = 0
        else:
            pass
        i = (i + 1) % n