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
				}
			}
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

bool allZero(vector<int> s, int cur) {
	/*for (int i = 0; i < ns.size(); i++) {
		if (ns[i].size() > 0) return false;
	}*/
	for (int i = 0; i < s.size(); i++) {
		if (s[i] > 0 && i != cur) return false;
	}
	
	return true;
}
int main()
{
	int n, p, cur;
	//vector<int> s;
	//vector<vector<int>> ns;
	int curP;
	vector<int> s; 
	
	while (cin >> n >> p) {
		if (n == 0 && p == 0) {
			break;
		}


		//cout << n << " " << p << endl;
		cur = 0;
		curP = p;
		s.clear();
		s.resize(n);
		/*ns.clear();
		ns.resize(0);
		for (int i = 0; i < n; i++) {
			ns[i].resize(0);
		}*/

		// 現在の候補者が碗から石を取り出す
		// 番号が降ってあるわけではない！
		//while (!(curP == 0 && isZero(s))) {
		while (true) {
			
			// 現在ターンである候補者が1つ取り出す
			//ns[cur].push_back(p[cur]);
			if (curP > 0) {
				s[cur]++;
				curP--;
			} else if (curP == 0 && s[cur] > 0) {// 手元に小石があれば全て碗の中に入れる
				/*for (int i = 0; i < ns[cur].size(); i++) {
					s.push_back(ns[cur][i]);
				}*/
				curP += s[cur];
				s[cur] = 0;
			}
			
			// 条件を満たして入れば終了
			if (curP == 0 && allZero(s, cur)) {
				cout << cur << endl;
				break;
			}

			// 次の候補者へ
			cur++;
			if (cur == n) cur = 0;
		}
	
		
	}

	return 0;
}

C++: #include <iostream>

using namespace std;

int main(){
    while(1){
        int n,m;
        cin>>m>>n;

        if(n==0&&m==0){
            return 0;
        }

        int a[m]={0};

        int owan=n;
        int now=0;
        
        for(int i=0;i<1000000;i++){
            now=i%m;

            if(owan==0){
                owan=a[now];
                a[now]=0;
            }else if(owan!=0){
                owan--;
                a[now]++;
            }

            // cout<<a[now]<<" "<<owan<<endl;
            if(a[now]==n){
                cout<<now<<endl;
                break;
            }   
        }
    }
}

C++: #include<stdio.h>
#include<iostream>
using namespace std;

int main(){

  int n,p,i;

  while(1){
    cin >> n >>p;
    if(n+p==0) break;
    int candidate[n],bowl;

    //init 
    for(i=0;i<n;i++) candidate[i]=0;
    bowl = p;

    for(i=0; i<1000000; i++){
      if(bowl > 0){
	candidate[i%n]++;
	bowl--;
      }else{
	bowl=candidate[i%n];
	candidate[i%n]=0;
      }
      if(candidate[i%n]==p) break;
    }
    printf("%d\n",i%n);
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int main() {
  int n, p, ans;
  while (cin >> n, cin >> p, n && p) {
    vector<int> has(n, 0);
    int stone = p;
    while (true) {
      for (int i = 0; i < n; i++) {
        if (stone > 0) {
          stone--;
          has[i]++;
          if (has[i] == p) {
            ans = i;
            goto FIN;
          }
        } else {
          stone += has[i];
          has[i] = 0;
        }
        //cerr << has[i] << " ";
      }
      //cerr << endl;
    }
  FIN:
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>

using namespace std;
int cnt[108000];
int n, p;
int main(){
	while(cin >> n >> p, n | p){
		fill(cnt, cnt + n, 0);
		int win = p;
		for(int i = 0;i <= 1000000;i++){
			if(p){
				p--;
				cnt[i%n]++;
				if(cnt[i%n] == win){
					cout << i%n << endl;
					break;
				}
			}
			else{
				p = cnt[i%n];
				cnt[i%n] = 0;
			}
		}
	}
	return 0;
}

C++: #include <iostream>
using namespace std;



int main()
{
	int N,P;
	while(cin >> N >> P, (N||P))
	{
		int n[100]={0}, p=0,cnt=0,np=P;

		while(1)
		{
			if(P==0)
			{
				P=n[p];
				n[p]=0;
			}
			else
			{
				P--;
				n[p]++;
			}

			cnt++;
			
			if(n[p]==np) break;
			p=(cnt%N);

		}

		cout << p << endl;
	}
}

C++: #include <iostream>
#include <vector>
using namespace std;

int main(){
  while(1){
    int n,p;
    cin >> n >> p;
    if(n == 0&& p == 0)break;
    int owan = p;
    vector<int> human(n,0);
    int current = 0;
    while(1){
      if(owan > 0){
	human[current]++;
	owan--;
      }else{
	if(human[current] == p){
	  
	  break;
	}else{ 
	  owan = human[current];
	  human[current] = 0;
	}
      }
      current++;
      if(current == n)current = 0;
      //      cout << current << " debug" << endl;
    }
    cout << current << endl;
  }
  return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
  int n,p,i,m,a=0,I;
  int box[100]={};
  while(1){
    cin>>n>>p;
    a=0;
    if(n==0&&p==0) break;
    else{
      while(1){

	for(i=0;i<n;i++){
	  m=i;
	 
	  if(p>0){
	    box[m]+=1;
	    p-=1;
	  }
	  else if(p==0){
	    for(I=0;I<n;I++){
	      if(box[I]==0) a++;
	    }
	    if(a==n-1) {
	      break;
	    }
	    else {
	      p=box[m];
	      box[m]=0;
	      a=0;
	    }
	  }
	}
	if(a==n-1) break;
      }
      if(m-1<0){
	m=n;
      }
      cout<<m-1<<endl;
          } 
    for(i=0;i<n;i++){
      box[i]=0;
    }
  }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
// 18:45 -
using namespace std;

int main(void)
{
    while(1){
        int n, p;
        scanf("%d %d", &n, &p);
        if(n == 0 & p == 0)
            break;
        int mine[n] = {0};
        int all = 0;
        int count = 0;
        while(1){
            all = 0;
            if(p > 0){
                p--;
                mine[count % n]++;
                for(int i = 0; i < n; i++) all += mine[i];
                all -= mine[count % n];
                if(all == 0 && p == 0){
                    break;
                }
            } else if(p == 0){
                p += mine[count % n];
                mine[count % n] = 0;
            }
            count++;
        }
        printf("%d\n", count % n);
    }

    return 0;
}

C++: #include <iostream>
using namespace std;

const int nmax = 50;

int main(){
	int n, p;
	int kouho[nmax];
	while(cin >> n >> p){
		if(n == 0 && p == 0) break;
		for(int i = 0; i < n; i++){
			kouho[i] = 0;
		}
		int num = p;
		int place = 0;
		while(true){
			if(num){
				kouho[place]++;
				if(kouho[place] == p) break;
				num--;
			}else{
				num += kouho[place];
				kouho[place] = 0;
			}
			place++;
			if(place == n) place = 0;
		}
		cout << place << endl;
	}
	return 0;
}

C++: #define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

int main(){
	int wan, n, winner;
	while(cin>>n>>wan, wan||n){
		int cnd[50]={0};
		for(int i = 0;;i=(i+1)%n){
			if(wan>0){
				wan--,cnd[i]++;
				if(wan==0){
					bool win=1;
					rep(j, n){
						if(i==j)continue;
						if(cnd[j]>0)win=0;
					}
					if(win){
						winner = i; break;
					}
				}
			}else wan+=cnd[i], cnd[i]=0;
		}
		cout<<winner<<endl;
	}
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

//n:候補者の人数,p:最初にお椀に入っている小石の数
int solve(int n,int p){
	int *pebbles = new int[n];//それぞれの候補者の持っている小石の数
	fill(pebbles,pebbles+n,0);
	int pos=n-1;//お椀の場所。スタートしたら0の人に渡す
	int inpebble = p;//お椀に入っている小石の数
	while(pebbles[pos] != p){
		pos= (pos+1)%n;
		if(inpebble==0){//お椀が空っぽ
			inpebble += pebbles[pos];
			pebbles[pos] = 0;
		}else{
			inpebble --;
			pebbles[pos]++;
		}
	}
	delete[] pebbles;
	return pos;
}

int main(){
	int n,p;
	while(true){
		cin >> n >> p;
		if(n==0 && p==0){
			break;
		}
		cout << solve(n,p) << endl;
	
	}

}