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 <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;



int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	while (1) {
		ll n, p;
		scanf("%lld %lld", &n, &p);
		if (n == 0 && p == 0) break;
		ll ini = p;
		vector<ll> a(n);
		ll res = 0;
		bool flag = false;
		while (1) {
			for (ll i = 0; i < n; i++) {
				if (p == 0) {
					p += a[i];
					a[i] = 0;
				}
				else {
					a[i]++;
					p--;
					if (p == 0 && a[i] == ini) {
						res = i;
						flag = true;
						break;
					}
				}
			}
			if (flag)break;
		}
		//puts("a");
		printf("%lld\n", res);
	}
	return 0;
}

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

int main(void){
  while(1){
    int stone[50];
    for(int k = 0 ; k < 50 ; k++){stone[k] = 0;}

    int bowl;
    int n, p;  // n：候補者人数，p：小石の総数
    cin >> n;
    cin >> p;

    if(n == 0 && p == 0){return 0;}

    bowl = p;

    bool flg = true;
    int ans;

    for(int i = 0 ; flg ; i = (i+1) % n){

      if(bowl == 0){
        bowl = bowl + stone[i];
        stone[i] = 0;
      }else{
        stone[i]++;
        bowl--;
        if(bowl == 0 && stone[i] == p){ans = i; flg = false;}
      }
    }

    for(int i = 0 ; i < 1000000 ; i++){
        for(int j = 0 ; j < 1000000 ; j++){
            for(int k = 0 ; k < 1000000 ; k++){
                int K = i + j + k;
            }
        }    
    }


    cout << ans << endl;
  }
}

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

int main(){

  int n, p;

  while(cin >> n >> p, n|p){
    int kouho[55];
    memset(kouho, 0, sizeof(kouho));
    int i = 0;
    int full = p;
    while(1){
      if(i == n) i = 0;
      if(p != 0){
        kouho[i]++;
        p--;
        if(kouho[i] == full) break;
      }else{
        p = kouho[i];
        kouho[i] = 0;
      }
      i++;
    }
    cout << i << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,s,e) for (int i=s;i<(e);i++)
#define All(v) (v).begin(),(v).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define int long long
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18 * 5;

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, p;
    cin >> n >> p;
    while(!(n == 0 && p == 0)){
        vector<int> st(n, 0);
        int rem = 0;
        bool game = true;
        while(game){
            REP(i, n){
                if(p > 0){
                    if(st[i]++ == 0) rem++;
                    if(--p == 0 && rem == 1){
                        game = false;
                        break;
                    }
                }else{      // p == 0
                    p = st[i];
                    if(st[i] > 0) rem--;
                    st[i] = 0;
                }
            }
        }
        REP(i, n){
            if(st[i] > 0){
                cout << i << "\n";
                break;
            }
        }
    
        cin >> n >> p;
    }
    return 0;
}

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

int main(){
	int n, p;

	while (cin >> n >> p, n + p){
		int a[1000] = { 0 };
		int i = 0;
		int b = p;

		while (true){
			if (b == 0){
				b = a[i];
				a[i] = 0;
			}
			else{
				b--;
				a[i]++;
			}
			if (a[i] == p){
				cout << i << endl;
				break;
			}
			i++;
			if (i == n) i = 0;

		}
	}
	return 0;
}

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

using namespace std;

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

int main(){
    int n,p;
    while(cin >> n >> p && n){
        int const P = p;
        int ans = -1;
        int k = 0;
        int have[55] = {};
        int cup = p;
        while(1){
            if(cup){
                have[k]++;
                cup--;
                if(cup == 0){
                    if(have[k] == P){
                        ans = k;
                        goto END;
                    }
                }
            } else {
                cup += have[k];
                have[k] = 0;
            }
            k++;
            k %= n;
        }
    END:;
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
const int INF = 1e9;
const int MODk = 1e9+7;
using LL = long long;
const LL LINF = 1e18;
const double EPS = 1e-10;
using namespace std;
class Edge{
public:
    int from,to,value;
    Edge(LL a,LL b,LL c){
        from = a;
        to = b;
        value = c;
    }
    Edge(LL a,LL b){
        from = a;
        to = b;
    }
};

int main(){
    int n,p;
    while (cin >> n >> p) {
        if(n == 0 && p == 0)return 0;
        vector<int> peo(n);
        int i = 0;
        int ans = p;
        while(1){
            if(p == 0){
                p = peo.at(i%n);
                peo.at(i%n) = 0;
            }else{
                peo.at(i%n)++;
                p--;
            }
            if(peo.at(i%n) == ans){
                cout<<i%n<<endl;
                break;
            }
            i++;
        }
    }
}