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;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;

void solve(int n, int p) {
    vector<int> have(n);
    int now = p;
    int index = 0;
    while(true) {
        if(now > 0) {
            have[index]++;
            now--;
        } else {
            now = have[index];
            have[index] = 0;
        }
        if(have[index] == p) {
            cout << index << endl;
            return;
        }
        index = (index + 1) % n;
    }
}

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        int n, p;
        cin >> n >> p;
        if(n == 0 && p == 0) break;
        solve(n, p);
    }
    return 0;
}

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

int main(){
	int people;
	int stone;
	int hand[55];
	while(1){
		int j=0;
		for(int i=0; i < 54; i++) hand[i] = 0;

		cin >> people >> stone;

		if(people == 0 && stone == 0) break;

		int box = stone;

		while(1){
			if(box == 1){
				int cont = 0;
				for(int i=0; i < people; i++){
					if(hand[i] == 0) cont++;
				}
				if(people == cont+1) break;
				else{
					hand[j % people]++;
					box--;
				}
			}
			else if(box != 0){
				hand[j % people]++;
				box--;
			}
			else if(box == 0){
				box += hand[j % people];
				hand[j % people] = 0;
			}
			j++;
		}
		cout << j % people << endl;
	}
	return 0;
}

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

#define PI 4*atan(1)
#define INF 1e8

int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

int main(){
  int n, p;
  while(cin >> n >> p, n || p){
    vector<int> MayorS(n);
    int bowlS = p;
    int player = 0;
    while(1){
      if(bowlS > 1){      //??¨?????????
        MayorS[player]++;
        bowlS--;
      }else {
        if(bowlS == 1){
          MayorS[player]++;
          bowlS--;
          if(MayorS[player] == p){  //??????
            cout << player << endl;
            break;
          }
        }else {         //????????????or????????????
          if(MayorS[player] > 0){
            bowlS += MayorS[player];
            MayorS[player] = 0;
          }
        }
      }
      player++;
      if(player == n)player = 0;
    }
  }  
}

C++: #include <iostream>

using namespace std;

int main(){
  while(true){
    int n,p,i,wan;
    cin >> n >> p;
    if (n==0&&p==0)
      break;
    wan = p;
    int people[n];
    for(int j=0;j<n;j++)
      people[j] = 0;
    for(i=0;;i++){
      if (wan > 0){
	people[i%n]++;
	wan--;
	if (people[i%n] == p)
	  break;
      }
      else{
	wan += people[i%n];
	people[i%n] = 0;
      }
    }
    cout << i%n << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
    int n, p;
    while (cin >> n >> p, n || p) {
        vector<int> v(n, 0);
        int bowl = p;
        int k = 0;
        while (1) {
            if (0 < bowl) {
                bowl --;
                v[k] ++;
                if (v[k] == p) {
                    cout << k << endl;
                    break;
                }
            } else {
                bowl = v[k];
                v[k] = 0;
            }
            k = (k + 1) % n;
        }
    }
    return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define rer(i, a, b) for (int i = (int)a; i <= (int)b; ++i)
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define all(v) v.begin(), v.end()
#define mset(a, n) memset(a, n, sizeof(a))

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int> > vvi;
typedef vector<pair<int, int> > vpii;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int inf = 1000000000;
const int mod = 1000000007;
const double EPS = 1e-9;
const int dx[] = { -1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int n, p;

int main() {
	// cin.tie(0); // tie
	// ios::sync_with_stdio(false); // sync
	while (cin >> n >> p, n || p) {
		int tmp = p;
		int a[n] = {0};
		for (int i = 0; i < n; ++i) {
			if (p != 0) {
				a[i]++;
				p--;
				if (p == 0 && a[i] == tmp) {
					cout << i << endl;
					break;
				}
			} else {
				p = a[i];
				a[i] = 0;
			}
			if (i == n - 1) i = -1;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
int main(){
	int task,data[50],n;
	while(cin>>n>>task,n,task){
		for(int i=0;i<n;i++){
			data[i]=0;
		}
		while(1){
			for(int i=0;i<n;i++){
				if(task>0){
					data[i]++;
					task--;
				}else if(data[i]>0){
					task+=data[i];
					data[i]=0;
				}
			}
			int cnt=0,ans;
			for(int i=0;i<n;i++){
				if(data[i]>0){
					ans=i;
					cnt++;
				}
			}
			if(cnt==1&&task==0){
				cout<<ans<<endl;
				break;
			}
		}
	}
}

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

int main(){
    int n,p,st[MAX];
    
    while(cin >> n >> p){
        bool flg = false;
        if(!n && !p) break;
        fill(st,st+n,0);
        
        int k = 0 , t = p;
        while(true){
            if(t){
                t--;
                st[k]++;
            }
            else{
                t += st[k];
                st[k] = 0;
            }
            k++;
            if(k == n) k = 0;
            
            for(int i = 0 ; i < n ; i++){
                if(st[i]==p){
                    cout << i << endl;
                    flg = true;
                    break;
                }
            }
            if(flg) break;
        }
    }
    
    return 0;
}

C++: #include <iostream>

using namespace std;

int main() {
	int n, p;
	while (cin >> n >> p) {
		if (n == 0 && p == 0) {
			break;
		}
		int kouho[50] = {0};
		int owan = p;
		int counter = 0;
		while (1) {
			//cout << owan << endl;
			if (owan > 0) {
				kouho[counter]++;
				owan--;
				if (owan == 0) {
					bool hantei = true;
					for (int i = 0; i < n; i++) {
						if (i != counter && kouho[i] != 0) {
							hantei = false;
						}
					}
					if (hantei) {
						break;
					}
				}
			} else {
				owan = kouho[counter];
				kouho[counter] = 0;
			}
			counter++;
			if (counter >= n) {
				counter = 0;
			}
		}
		cout << counter << endl;
	}
	return 0;
}