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>
#include <algorithm>

using namespace std;

int main()
{
    int n, p;
    while (cin >> n >> p, n | p)
    {
        vector<int> stones_of_candidates(n, 0);
        int i = 0;
        while (true)
        {
            if (0 < p)
            {
                ++stones_of_candidates[i];
                --p;
            }
            else
            {
                p += stones_of_candidates[i];
                stones_of_candidates[i] = 0;
                if (all_of(stones_of_candidates.begin(), stones_of_candidates.end(), [](int x) { return x == 0; }))
                {
                    cout << i << endl;
                    break;
                }
            }


            if (i < n - 1)
            {
                ++i;
            }
            else
            {
                i = 0;
            }
        }
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <map>
#include <queue>
#include <numeric>
#include <climits>
#include <functional>
using namespace std;
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
using ll = unsigned long long;
const ll mod = 1000000007;
string al = "abcdefghijklmnopqrstuvwxyz";
//bool less_sec(const pair<int, int>&a, const pair<int, int>&b) {
//	return a.second > b.second;
//}
int n, p;
int arr[51];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (cin >> n >> p, n&&p) {
		int table = p;
		for (int i = 0; i < n; ++i) {
			arr[i] = 0;
		}
		int flag = 0;
		while (true) {
			if (table == 0) {
				if (arr[flag] != 0) {
					table = arr[flag];
					arr[flag] = 0;
				}

			}
			else {
				arr[flag]++;
				table--;
			}
			if (arr[flag] == p) break;
			flag++;
            if(flag == n) flag = 0;
		}
		cout << flag << endl;
	}
	return 0;
}

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

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

bool owari(vector<int> v, int c) {
    rep(i,v.size()) {
        if(i == c) continue;
        if(v[i] > 0) return false;
    }
    return true;
}

void solve(int n, int p) {
    int cup = p;
    vector<int> per(n,0);
    
    int i;
    while(1) {
        for(i=0; i<n; i++) {
            if(cup == 0) {
                if(owari(per, i)) goto out;
                cup += per[i]; per[i] = 0;
            }
            else {
                ++per[i]; --cup;
            }
        }
    }
out:
    cout << i << "\n";
    return ;
}

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

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

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

using namespace std;

bool check(vector<int> v) {
    rep(i,v.size()) {
        if(v[i] != 0) return false;
    }
    return true;
}

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

        int ans = 0;
        int id = 0;
        int sum = p;
        vector<int> v(n);
        rep(i,n) v[i] = 0;
        
        while(true) {
            if(sum) {
                v[id]++;
                sum--;
            }
            else {
                sum += v[id];
                v[id] = 0;
            }

            if(check(v)) break;

            id++;
            id %= n;
            ans++;
        }

        cout << id << endl;
    }  

    return 0;
}

C++: #include <iostream>

using namespace std;

int main() {
    int n, p;
    int bowl, id, score[51];
    while (cin >> n >> p) {
        if (!n && !p) { break; }
        bowl = p; id = 0;
        fill(score, score+51, 0);
        while (true) {
            if (bowl > 0) {
                bowl--;
                score[id]++;
                if (score[id] == p) {
                    cout << id << endl;
                    break;
                }
            }
            else {
                bowl += score[id];
                score[id] = 0;
            }
            id++;
            id %= n;
        }
    }
    return 0;
}

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

int main(){
	int mayors[50]{0};

	for(;;){
		int n, p;
		cin >> n >> p;
		if (n == 0) break;

		for (int i = 0; i < n; i++) mayors[i] = 0;
		int bowlp = p;
		int turn = 0;
		for(;;){
			if (bowlp == 1 && mayors[turn] == p - 1) break;
			else if (bowlp == 0){
				bowlp += mayors[turn];
				mayors[turn] = 0;
			}
			else{
				mayors[turn]++;
				bowlp--;
			}

			if (turn == n - 1) turn = 0;
			else turn++;
		}
		cout << turn << endl;
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <sstream>
#include <cmath>
#include <set>
#include <map>
using namespace std; 

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define len(x) ((int)(x).size())
#define tmax(a,b,c) max((a),max((b),(c)))
#define tmin(a,b,c) min((a),min((b),(c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<vector<int> > VVi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int main()
{
	int n, pp;
	while(1) {
		cin >> n >> pp;
		if (!n && !pp) break;
		map<int, int> have;
		int player = 0;
		int p = pp;
		while(1) {
			if(p) {
				have[player]++;
				p--;
				if (have[player] == pp) {
					cout << player << endl;
					break;
				}
			}
			else {
				p = have[player];
				have[player] = 0;
			}

			player++;
			if (player == n) player = 0;

		}
	}

 	return 0;
}

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

int main()
{
	int n,m;
	while(cin >> n >> m){
		if(n==0 && m == 0)
			break;
		int stone[50] = {0};
		int j = 0;
		int a = 100;
		int max_m = m;
		while(true){
			for(int i=0;i<n;i++){
				if(stone[i] == max_m)
					a = i;
			}
			if(a != 100)
				break;
		
			if(m > 0){
				stone[j]++;
				m--;
			}
			else{
				m = stone[j];
				stone[j] = 0;
			}
		
			j = (j+1)%n;
		}
		cout << a << endl;
	}
	return 0;
}

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

using namespace std;

int main(){

    int n,p;
    while(true){
	cin >> n >> p;
	vector<int> part(n);
	int peb = p;
	int winner=-1,count = 0;
	if(n == 0&& p==0)  break;
	while(true){
	    count++;
	    if(count > 1e6) break;
	    for(int i=0;i<n;i++){
		if(peb>0){
		    peb--;
		    part[i]++;
		}else{
		    peb = part[i];
		    part[i] = 0;
		}
		if(part[i] == p){
		    winner = i;
		    break;
		}
	    }
	    if(winner!=-1) break;
	}
	cout << winner << endl;
    }


    return 0;
}