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 <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

void solve(int n, int p) {
    VI a(n);
    int owan = p;
    int id = 0;
    while (true) {
        bool f = (owan == 1);
        if (owan) {
            a[id]++;
            owan--;
        } else {
            owan = a[id];
            a[id] = 0;
        }
        if (f) {
            if (count(ALL(a), 0) == n - 1) {
                cout << id << endl;
                return ;
            }
        }
        id = (id + 1) % n;
    }
}

int main(void) {
    int n, p;
    while (cin >> n >> p) {
        if (n == 0 && p == 0) return 0;
        solve(n, p);
    }
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int const MOD = 1000000007;

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

signed main() {
    int n, p;
    while(cin >> n >> p) {
        if(!n && !p) break;
        int ord = p;
        int turn = 0;
        int array[60] = {};
        while(1) {
            if(p != 0) {
                array[turn]++;
                p--;
            }
            else if(p == 0) {
                p += array[turn];
                array[turn] = 0;
            }

            if(array[turn] == ord) {
                cout << turn << endl;
                break;
            }
            turn = (turn + 1) % n;
        }
    }
    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;
    vector<int> human;
    for(int i = 0;i < n;i++){
      human.push_back(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 <bits/stdc++.h>
using namespace std;

int main(int argc, char const *argv[])
{
	int n,p;
	while(cin>>n>>p,n+p)
	{
		int a[n];
		for (int i = 0; i < n; ++i) a[i]=0;
		int turn=0,s=p;
		while(1)
		{
			if(s>0)
			{
				++a[turn],--s;
				if(a[turn]==p) break;
				++turn;
				if(turn==n) turn=0;
			}
			else
			{
				if(a[turn]>0) s=a[turn],a[turn]=0;
				++turn;
				if(turn==n) turn=0;
			}
		}
		cout<<turn<<endl;
	}
	return 0;
}

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

using namespace std;

bool check(vector<int> v){
	int sum = 0;
	for (int i = 0; i < v.size(); i++){
		if (v[i] != 0) sum++;
	}
	if (sum == 1){
		return true;
	}else{
		return false;
	}
}


int main(){
	while(1){
		int n; cin >> n;
		int p; cin >> p;
		if (n == 0 and p == 0) break;
		int wan = p;
		vector<int> player_have(n);
		int player = 0;
		int idx = 0;
		while(wan != 0 or !check(player_have) ){
			player = idx%n;
			//cout << "wan = " << wan <<  endl;
			if (wan > 0){
				player_have[player] += 1;
				wan -= 1;
			}else if (wan == 0){
				wan = player_have[player];
				player_have[player] = 0;
			}
			idx++;;
		}
		cout << player << endl;
		player_have.clear();
	
	}

return 0;
}

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

int main() {
  int n, p;
  while (cin >> n >> p, n) {
    int a[50] = {0};

    bool f = false;
    for (int i = 0; ; ++i) {
      if (p) {
        p--;
        a[i % n]++;

        if (p == 0) {
          int cnt = 0;
          for (int j = 0; j < n; ++j) {
            if (a[j]) cnt++;
          }

          if (cnt == 1) {
            cout << i % n << endl;
            f = true;
          }
        }
      } else {
        p += a[i % n];
        a[i % n] = 0;
      }

      if (f) break;
    }
  }
}

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

int main() {
	long long N, P;
	while (cin >> N >> P) {
		if (N == 0 && P == 0) return 0;
		vector<long long> V(N, 0);
		long long NOW = P;
		for (int i = 0; ; i++) {
			if (NOW == 0) NOW = V[i % N], V[i % N] = 0;
			else NOW--, V[i % N]++;
			if (V[i % N] == P) {
				cout << i % N << endl;
				break;
			}
		}
	}
}