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;

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define debug3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define debugB(value, size) cerr<<#value<<": "<<bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

template<typename T>
void printA(vector<T> &printArray, char between = ' ') {
   int paSize = printArray.size();
   for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between;}
   if (between != '\n') {cerr << endl;}
}

// ------------------------------------------------------------------------------------------

int v[55];

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

      int wan = p;
      fill(v, v+55, 0);
      [&]() {
         while (1) {
            rep (i, n) {
               if (wan) {
                  v[i]++, wan--;
               } else {
                  wan += v[i], v[i] = 0;
               }
               if ( [&]() { rep (i, n) { if (v[i]==p) return true; } return false; }() ) {
                  cout << i << endl;
                  return;
               }
               // debug(wan)
            }
         }
      }();

   }

   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;}
      }
    }

    cout << ans << endl;
  }
}

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

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) { ll d = euclid(b, a % b, y, x);
        return y -= a/b * x, d; }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1) ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1); return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e) return Mod(1);
        Mod r = *this ^ (e / 2); r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion


int main() {
    cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit);

    int n, p;

    while (true) {
        cin >> n >> p;
        if (!n && !p) break;
        
        vector<int> hand(n);
        int bowl = p;

        for (int i = 0; ; i = (i + 1) % n) {
            if (bowl) hand[i]++, bowl--;
            else bowl += hand[i], hand[i] = 0;
            if (hand[i] == p) {
                cout << i << endl;
                break;
            }
        }
    }
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int knum[50];

int main(){
	while(1){
		int n,p;
		cin >> n >> p;
		if(n==0&&p==0) break;
		REP(i,50) knum[i] = 0;
		int nowwan = p;
		int nowp = 0;
		int winner;
		while(1){
			if(nowwan==0){
				bool rst = true;
				REP(i,n) if(i!=nowp&&knum[i]!=0) rst = false;
				if(rst) {
					winner = nowp;
					break;
				}else{
					nowwan = knum[nowp];
					knum[nowp] = 0;
				}
			}else{
				nowwan--;
				knum[nowp]++;
			}
			if(nowp == n-1) nowp = 0;
			else nowp++;
		}
		cout << winner << endl;
	}
}

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

using namespace std;

int main(){
	int n, p;
	while(cin >> n >> p, n, p){
		int a[n];
		int owan = p;
		int cur = 0;
		for(int i = 0; i < n; i++) a[i] = 0;
		owan -= 1;
		a[0] += 1;
		cur++;
		while(owan != p){
			if(owan){
				owan -= 1;
				a[cur]++;
				if(cur == n - 1) cur = 0;
				else cur++;
			}else{
				owan = a[cur];
				a[cur] = 0;
				if(cur == n - 1) cur = 0;
				else cur++;
			}
		}
		if(!cur) cout << n - 1 << endl;
		else cout << cur - 1 << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;

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


int temoto[55];

int main(){
	while(1){
		int n,p;
		scanf("%d %d",&n,&p);
		for(int i = 0;i < n;i++) temoto[i] = 0;
		int wan = p;
		int ans = 0;
		if(n + p == 0) break;
		for(int i = 0; ;i++){
			if(i == n) i = 0;
			if(wan == 1 && temoto[i] == p-1){
				ans = i;
				break;
			}else if(wan > 0){
				wan--;
				temoto[i]++;
			}else{
				wan = temoto[i];
				temoto[i] = 0;
			}
		}
		printf("%d\n",ans);
	}
}

C++: #include<iostream>
#include<vector>
using namespace std;
int main() {
	int n, p;
	while (cin >> n >> p, n) {
		int now = 0,wan=p;
		vector<int>person(n,0);
		while (true) {
			if (wan) {
				person[now]++; wan--;
				if (person[now] == p) {
					cout << now << endl;
					break;
				}
			}
			else if (person[now]) {
				wan = person[now]; person[now] = 0;
			}
			now++;
			if (now >= n)now = 0;
		}
	}
}