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<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int main(){
  int n,p;
  while(cin>>n>>p, n!=0 || p!=0){
    int A[n];
    memset(A, 0, sizeof(A));
    int t=0;
    while(1){
      if(p==0){
        p=A[t];
        A[t]=0;
      } else {
        p-=1;
        A[t]+=1;
      }
      int f=1;
      rep(i,n) {
        if(t!=i && A[i]!=0) f=0;
      }
      if(p==0 && f) break;
      t=(t+1)%n;
    }
    cout<<t<<endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
int main() {
	int n, p;
	while (true) {
		cin >> n >> p;
		if (n == 0 && p == 0)break;
		vector<int> c(n, 0);
		int at = 0;
		int now = 0;
		while (true) {
			if (p != 0) {
				c[at]++; p--;
				if (c[at] == 1)now++;
				if (p == 0) {
					if (now == 1) {
						cout << at << endl;
						break;
					}
				}
			}
			else {
				p = c[at]; c[at] = 0;
				if (p != 0) now--;
			}
			at = (at + 1) % n;
		}
	}
}

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

vector<int> cand[50];
int peddle;
int n,p;
int count;
int who;

int main()
{
        while(cin >> n >> p){
                if(n==0 && p==0) break;
                count = 0;
                peddle = p;
                while(true){
                        who = count % n;
                        if(peddle>0){
                                cand[who].push_back(1);
                                peddle--;                                                                                                                                                       }else if(peddle==0){
                                peddle += cand[who].size();
                                cand[who].clear();
                        }

                        if(cand[who].size()==p){
                                cout << who << endl;
                                break;
                        }

                        count++;                                                                                                                                                        }
                for(int i=0;i<n;i++) cand[i].clear();
        }                                                                                                                                                               
        return 0;
}

C++: #include <cstdio>

int main(){
	int n, p;
	int x[50];
	while(scanf("%d%d", &n, &p)){
		if(n == 0 && p == 0) break;
		for(int i = 0; i < n; i++) x[i] = 0;
		int t = 0;
		int q = p;
		while(true){
			if(p > 0){
				x[t]++;
				if(x[t] == q){
					printf("%d\n", t);
					break;
				}
				p--;
			} else{
				p += x[t];
				x[t] = 0;
			}
			t++;
			t %= n;
		}
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

int n=0;
int p=0;
int have[55] = {};
int winner=0;
int judge=0;

int main()
{
    while(1){
        scanf("%d %d",&n,&p);
        if(n==0 && p == 0){
            break;
        }
        int pebble = p;
        for(int j=0;j<n;j++){
            have[j]=0;
        }
        while(1){
            for(int j=0;j<n;j++){
                if(pebble != 0){
                    have[j]++;
                    pebble--;
                    if(have[j] == p){
                        winner = j;
                        judge = 1;
                        break;
                    }
                }else{
                    pebble += have[j];
                    have[j] = 0;
                }
            }
            if(judge == 1){
                cout << winner << endl;
                judge = 0;
                break;
            }
        } 
    }

    return 0;
}

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

int main()
{
	// cinはscanfより基本的に遅い
	// sync_with_studio()を事前に書いておくとcinでも速度向上が期待出来る！

	bool flg = false;
	int n, p, cur;
	int curP;
	vector<int> s; 
	
	while (cin >> n >> p) {
		if (n == 0 && p == 0) {
			break;
		}
		// 初期化処理
		cur = 0;
		curP = p;
		s.clear();
		s.resize(n);

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

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

	return 0;
}

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

int main()
{
	int n,p,flag=0,mayor;
	while(cin>>n>>p , n!=0 && p!=0)
	{
		int stone[50]={0};
		while(1)
		{
			for(int i=0;i<n;i++)
			{
				if(p>0)//石があれば取る
				{
					stone[i]++;
					p--;
					if(p==0)//お椀の石がなくなったとき
					{
						flag=1;
						for(int j=0;j<i;j++)
							if(stone[j]!=0)//他に石を持っている人がいたら
							{
								flag=0;
								break;
							}
						for(int j=i+1;j<n;j++)
							if(stone[j]!=0)
							{
								flag=0;
								break;
							}
						if(flag==1)
						{
							mayor=i;
							break;
						}
					}
								
				}
				else//お椀の石がないとき
				{
					p=stone[i];//お椀に石を入れる
					stone[i]=0;
				}
			}
			if(flag==1)
			{
				cout<<mayor<<endl;
				flag=0;
				break;
			}
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>

using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;

int main() {
    int n, p;
    vi s(50, 0);

    while (cin >> n >> p) {
        if (n == 0 && p == 0) break;
        fill(s.begin(), s.end(), 0);

        int i = 0, all = p;
        while (true) {
            if (p != 0) {
                p--;
                s[i]++;
                if (s[i] == all) break;
            } else {
                p += s[i];
                s[i] = 0;
            }

            i = (i+1) % n;
        }
        cout << i << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int knum[50];

int main(){
	while(1){
		int n,p;
		cin >> n >> p;
		if(n==0&&p==0) break;
		REP(i,50) knum[i] = 0;
		int nowwan = p;
		int nowp = 0;
		int winner;
		while(1){
			if(nowwan==0){
				bool rst = true;
				REP(i,n) if(i!=nowp&&knum[i]!=0) rst = false;
				if(rst) {
					winner = nowp;
					break;
				}else{
					nowwan = knum[nowp];
					knum[nowp] = 0;
				}
			}else{
				nowwan--;
				knum[nowp]++;
			}
			if(nowp == n-1) nowp = 0;
			else nowp++;
		}
		cout << winner << endl;
	}
}