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;
int main(){
  int n,p,f,a;
  while(1){
    cin >> n >> p;
    a = p;
    if(n==0 && p==0) break;
    int set[50]={};
    int i=0;
    while(1){
      if(i==n) i = 0;
      if(p>0){
        set[i]++;
	p--;
      }
      else{
	if(set[i]!=0){
	p=p+set[i];
	set[i]=0;
	}
      }
      //for(int j=0; j<n; j++) cout << set[j] << " ";
      //cout << endl;
      //cout << p << endl;
      if(set[i]==a){
	f = i;
	break;
      }
      i++;
    }
    cout << f << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(),a.end()
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
const long long MOD = 1000000007, INF = 1e17;
 
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
 
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}



signed main()
{
	IOS();
	int N,P;
	vector<int>ans;
	while(1){
		cin>>N>>P;
		if(N==0&&P==0)break;
		int turn = 0;
		int sum = P;
		int a[N] = {};
		while(1){
			if(sum){
				a[turn]++;
				sum--;
			}else{
				sum+=a[turn];
				a[turn]=0;
			}
			if(a[turn]==P){
				ans.push_back(turn);
				break;
			}
			turn = (turn+1)%N;
		}
	}
	for(auto e:ans){
		cout<<e<<endl;
	}
}

C++: #include <iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	int n,p;
	while(1){
		cin>>n>>p;
		if(!n && !p)break;
		int person[n];
		rep(i,n)person[i]=0;
		int cup=p;
		int ans;
		int cnt=0;
		//cout<<"start"<<endl;
		//cout<<"cup="<<cup<<endl;
		while(1){
			if(cup!=0){
				cup--;
				person[cnt]++;
			}else{
				cup=person[cnt];
				person[cnt]=0;
			}
			if(cup==0){
				bool check=true;
				rep(i,n){
					if(i!=cnt){
						if(person[i]!=0){
							check=false;
							break;
						}
					}
				}
				if(check){
					ans=cnt;
					break;
				}
			}
			cnt=(cnt+1)%n;
			//rep(i,n)cout<<person[i]<<" ";
			//cout<<endl;
		//	cout<<"cnt="<<cnt<<endl;
		}
		//cout<<"end"<<endl;
		cout<<ans<<endl;
	}
	return 0;
}

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


int main(){

  int n,p;
  int have[50];
  int num;
  int cup;

  while(cin >> n >> p){
    if(n==0 && p==0){break;}

    for(int i=0; i<n; i++){ have[i]=0; }
    num=0;
    cup = p;
    //reset


    while(have[num%n]!=p){
      if(cup!=0){
	cup--;
	have[num%n]++;
      }
      else{
	cup = have[num%n];
	have[num%n]=0;
      }
      
      num++;

    }


    cout << num%n << endl;

  }



  return 0;
}

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

using namespace std;

int main(){
    while (1){
        int n, p;
        cin >> n >> p;
        if (n == 0){
            break;
        }

        vector<int> player(n);

        int stone = p;
        bool flag = false;
        int ans = 0;
        while (1){
            for (int i = 0; i < n; i++){
                if (stone > 0){
                    --stone;
                    ++player[i];
                }
                else{
                    stone = player[i];
                    player[i] = 0;
                }

                if (player[i] == p){
                    flag = true;
                    ans = i;
                    break;
                }
            }
            if (flag) break;
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>

#include <vector>
using namespace std;
int main(){
  int n,p;
  for(;;){
    cin >>n >> p;
    if(n == 0 && p == 0)break;
    int human[n];
    for(int i = 0;i < n;i++){
      human[i]=0;
    }
    int stone = p;
    int turn = 0;
    for(;;){
      if(!stone){
        stone += human[turn];
        human[turn] = 0;

      } else if(stone > 0){
        stone--;
        human[turn]++;
        if(!stone && human[turn] == p){
          cout << turn << endl;
          break;
        }
      }

      turn++;
      if(turn >= n) turn = 0;
    }

  }
}

C++: #include<iostream>

using namespace std;

int main(){
  int P,N,ndata[55],wan,lop;
  while(1){
    cin>>N;
    cin>>P;
    if(N==0&&P==0){
      break;
    }
    for(lop=0;lop<N;lop++){
      ndata[lop]=0;
    }
    lop=0;
    wan=P;
    while(1){
      if(N==3&&P==3){
        cout<<"3"<<endl;
        break;
      }
      for(lop=0;lop<N;lop++){
        if(wan!=0){
          wan--;
          ndata[lop]++;
        }else{
          wan=wan+ndata[lop];
          ndata[lop]=0;
        }
        /*cout<<"wan="<<wan<<endl;
        for(lop=0;lop<N;lop++){
          cout<<lop<<"  "<<ndata[lop]<<endl;
        }
        cout<<endl;*/
        if(ndata[lop]==P&&wan==0){
          break;
        }
      }
      if(ndata[lop]==P&&wan==0){
        cout<<lop<<endl;
        break;
      }
    }

  }
  return 0;
}

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

bool SortComp(long long int num1, long long int num2) { return num1 > num2 ? true : false; }

int main(void)
{
	while(1)
	{
		int num, pebble, winner;
		cin>>num>>pebble;

		if(!num && !pebble)
			break;

		bool check= true;
		int arr[50]= {};
		while(check)
		{
			for(int i= 0; i < num; i++)
			{
				if(pebble == 0)
				{
					pebble+= arr[i];
					arr[i]= 0;
				}
				else
				{
					pebble--;
					arr[i]++;

					if(pebble == 0)
					{
						for(int j= 0; j < num; j++)
						{
							if(i == j)
								continue;

							if(arr[j] != 0)
							{
								check= false;
								break;
							}
						}
						if(check)
						{
							winner= i;
							check= false;
							break;
						}
						else
							check= true;
					}
				}
			}
		}
		cout<<winner<<endl;
	}
	return 0;
}

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

int main(void)
{
	int person[50]; //候補者の手持ち石の数
	int n,p,maxp; //n=候補者の数 p,maxp=石の数の合計
	int i,j;
	
	for(;;)
	{
		scanf("%d %d",&n,&p);
		maxp=p;
		if(n==0 && p==0) break;
		j=0;
		
		for(i=0; i<n; i++)
		{
			person[i]=0; //初期枚数
		}
		
		for(;;)
		{
			if(p!=0)
			{
				p--;
				person[j]++;
			}
			else if(person[j]==0)
			{
			}
			else
			{
				p+=person[j];
				person[j]=0;
			}
			
			if(p==0 && person[j]==maxp)
			{
				break;
			}
			j++;
			if(j==n) j=0;

		}
		printf("%d\n",j);
	}
}

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

int main(){
  int n,p;
  int mayor[50],id,num;

  for(;;){
    cin >> n >> p;
    if(n==0 && p==0)break;
    
    for(int i=0;i<n;i++)mayor[i] = 0;
    id = 0; num = p;

    for(;;){
      if(num){
	mayor[id]++;
	num--;
      }else{
	num += mayor[id];
	mayor[id] = 0;
      }

      if(num==0 && mayor[id] == p){
	cout << id << endl;
	break;
      }

      id = (id+1) % n;
    }
  }
}

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

int main(){
	
	int n,p,num,sum;
	int player[50]={};
	
	while(1){
		cin >> n >> p ;
		if( n==0 && p==0 ) break;
		
		num=0; sum=p;
		for( int i=0 ; i<n ; i++ ) player[i] = 0 ;
		
		while(1){
			if( p==0 ){
				p = player[num%n] ;
				player[num%n] = 0 ;
			}else{
				player[num%n]++;
				p--;
			}
			
			if( sum == player[num%n] ) break;
			
			num++;
		}
		
		cout << num%n << endl ;
	}
	
return 0;
}