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<vector>
using namespace std;


int main(){
    int N,P;
    while(cin>>N>>P, N != 0 && P != 0){
        vector<int> stone(N,0);
        int p = P-1;//ｐは椀の中の石の数
        stone[0]++;
        int i = 0;
        while(stone[i] != P){
            i = (i+1)%N;
            if(p > 0){
                stone[i]++;
                p--;
            }
            else if(stone[i] != 0){
                swap(p,stone[i]);
            }
        }
        cout<<i<<endl;
    }

}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

int main(){
    int n, p;
    
    while(cin >> n >> p, n && p){
        int wan = p;
        int* isi = new int[n]{0};
        bool game = true;
        while(game){
            for(int i=0; i<n; i++){
                if(wan == 0){
                    wan = isi[i];
                    isi[i] = 0;
                }else{
                    wan--;
                    isi[i]++;
                }
            }
            for(int i=0; i<n; i++){
                if(isi[i] == p){
                    cout << i << endl;
                    game = false;
                    break;
                }
            }
        }
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define MEM(a,n) memset(a,n,sizeof(a))
#define p(S) cout<<(S)<<endl
using namespace std;


int main(){
	int n,p;
	while(cin>>n>>p,n){
		int a[50],center=p;
		MEM(a,0);
		int ans;
		REP(i,0,n){
			if(center>0) {
				a[i]++;
				center--;
			}
			else{
				center+=a[i];
				a[i]=0;
			}
			if(center==0){
				bool flag=true;
				REP(j,0,n){
					if(i==j) continue;
					if(a[j]!=0){
						flag=false;
						break;
					}
				}
				if(flag) {
					ans=i;
					break;
				}
			}
			if(i==n-1) i=-1;
		}
		p(ans);
	}
	return 0;
}

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

using namespace std;

int main(){
  int n,p;
  
  while(1)
    {
      cin >> n >> p;
      if(n + p == 0) break;
      
      int man[n];
      for(int i=0;i < n;i++)
	{
	  man[i] = 0;
	}
      int stone = p;
      int i=0;

      while(1){
	
	if(stone == 0 && man[i] == 0)
	  {
	    i++;
	  }
	else if(stone == 0 && man[i] != 0)
	  {
	    stone = man[i];
	    man[i] = 0;
	    i++;
	  }
	else if(stone != 0)
	  {
	    man[i]++;
	    stone--;
	    i++;
	  }
	
	if(i == n) i = 0;

	if(man[i] == p)
	  {
	    cout << i << endl;
	    break;
	  }
      }
    }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,p;
  while(1){
    cin >> n >> p;
    if(n==0&&p==0){
      break;
    }
    int wan=p;
    int hito[10000]={0},num=0;
    while(1){
      //cout<<"hito[num]="<<hito[num]<<" wan="<<wan<<endl;
      if(wan==0){
	if(hito[num]==p){
	  cout<<num<<endl;
	  break;
	}
	else{
	  wan+=hito[num];
	  hito[num]=0;
	}
      }else{
	wan--;
	hito[num]++;
      }
      num++;
      if(num==n){
	num=0;
      }
    }

    /*while(1){
      wan++;
      if (p>0)break;
      }*/
  }
  return 0;
}

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

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;
using namespace std;
int mod = 1000000007;

int main(void){
    vector<int> ans;
    while(true){
        int N, P; cin >> N >> P;
        if(N == 0 && P == 0) break;

        vector<int> stone(N, 0);
        bool cont = true;
        int turn = 0;
        int rem = P;
        while(cont){
            if(rem == 0){
                rem += stone[turn];
                stone[turn] = 0;
            }
            else{
                stone[turn]++;
                rem--;
                if(rem == 0){
                    cont = false;
                    rep(i, N) if(i != turn && stone[i] > 0) cont = true;
                }
            }
            if(cont) turn = (turn+1) % N;
        }
        ans.push_back(turn);
    }
    rep(i, ans.size()) cout << ans[i] << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const long long INF = 1e7+1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i,r,n) for(ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i,r,n) for(ll i=(ll)(n-1);i>=r;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector < pair<ll, ll > > vp;
typedef vector <string> vs;
typedef vector <char> vc;
typedef list <ll> lst;

ll n,m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

/*--------------------template--------------------*/


int main()
{
	while (1) {
		cin >> n >> k;
		if (n == 0 && k == 0) {
			return 0;
		} else {
			vi v(n);
			fill(ALL(v), 0LL);
			ll q = k;
			ll i = 0;
			while (1) {
				if (q != 0) {
					v[i]++;
					q--;
					if (q == 0 && k == v[i]) {
						cout << i << endl;
						break;
					}
				} else {
					q += v[i];
					v[i] = 0;
				}
				i++;
				if (i == n) i = 0;
			}
		}
	}
}

C++: // comment
#include <iostream>
using namespace std;
 
int isFinish(int *human, int N) {
    int count = 0;
    int id = -1;
    for (int i = 0; i < N; i++) {
        if (human[i] > 0) {
            id = i; count++;
        }
    }
    if (count == 1) return id;
    else return -1;
}
int main() {
    while (true) {
        int N, P;
        int ans;
        cin >> N >> P;
        if (N == 0 && P == 0) break;
        int human[50] = {0};
        int i = 0;
        while (true) {
            if (P != 0) {
                human[i] += 1;
                P--;
                if (P == 0) {
                    ans = isFinish(human, N);
                    if (ans >= 0) break;
                }
            }
            else {
                P += human[i];
                human[i] = 0;
            }
            i = (i + 1) % N;
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

//constant
const int inf=1<<28;

//variable
int n,p;
int gor[52];

int main(){
    while(cin>>n>>p, n){
        for(int i=0; i<n; i++){
            gor[i]=0;
        }
        int p_t=p;
        for(int i=0; ; i++){
            if(p_t==0){
                p_t+=gor[i%n];
                gor[i%n]=0;
            }
            else if(p_t>0){
                gor[i%n]++;
                --p_t;
                if(gor[i%n]==p){
                    cout<<i%n<<endl;
                    break;
                }
            }
        }
    }

    return 0;
}