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>
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 % people] == 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;
	}

}

C++: #include<iostream>
#include<vector>
using namespace std;
 
int main()
{
int n,p;
int i,cup;
vector<int> v;
while(cin>>n>>p,n||p){
 v.clear();
 v.resize(n);
 cup=p;
 for(i=0;;i++){
  if(i==n)i=0;
  if(cup){
   v[i]++;
   cup--;
  }else{
   cup+=v[i];
   v[i]=0;
  }
  if(v[i]==p)break;
 }
 cout<<i<<endl;
}
 
}

C++: // 2013/07/05 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int n, p;
		cin >> n >> p;

		if(n==0 && p==0)
			break;

		int B = p;
		int C[50] = {0};

		for(int i=0; ; i=(i+1)%n){
			if(B>0){
				C[i]++;
				B--;
			}
			else{
				B = C[i];
				C[i] = 0;
			}

			if(C[i]==p){
				cout << i << endl;
				break;
			}
		}
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
using namespace std;

int main(void) {
	int N, P;
	while (cin >> N >> P, N) {
		vector<int> player(N, 0);
		int nowi = 0;
		int wan = P;
		
		while (true) {
			if (wan != 0) {
				wan--;
				player[nowi]++;
			}else {
				wan += player[nowi];
				player[nowi] = 0;
			}

			if (wan == 0) {
				int pi;
				for (pi = 0; pi < N; pi++) {
					if (player[pi] != 0 && nowi != pi){
						break;
					}
				}
				if (pi == N) {
					break;
				}
			}


			nowi = (nowi + 1) % N;
		}

		cout << nowi << endl;
	}

	return 0;
}

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

int main(){
	cin.tie(0);
    ios::sync_with_stdio(false);
	vector<int> ansvec;

	for(;;){
		int n, p;
		cin >> n >> p;
		if (n == 0) break;
		
		int bowlp = p;
		int turn = 0;
		int mayors[50]{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++;
		}
		ansvec.push_back(turn);
	}

	for (int ans : ansvec){
		cout << ans << endl;
	}
	return 0;
}

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

int main(){
	while(1){
		int n, p;
		cin >> n >> p;
		if(n + p == 0) break;
		int d[51] = {};
		int now = p;
		int t = 0;
		int ans = -1;
		for(int i = 0;; i++){
			t = i % n;
			if(now >= 1){
				now--;
				d[t]++;
			}else if(now == 0){
				now = d[t];
				d[t] = 0;
			}
			if(d[t] == p){
				ans = t;
				break;
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
#define N 50

int main(){
	while(1){
		int n,m;
		int ca[N];
		cin >> n >> m;
		if(!n&&!m) break;
		fill(ca,ca+n,0);
		
		int wan = m;
		int k = 0;
		while(1){
			while(wan){
				ca[k]++;
				k++;
				k = k%n;
				wan--;
			}
			if((k==0&&ca[n-1]==m)||(ca[k-1]==m)){
				break;
			}else{
				wan = ca[k];
				ca[k] = 0;
				k++;
				k = k%n;
			}
		}
		if(!k){
			cout << n-1 << endl;
		}else{
			cout << k-1 << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()
#define let auto const

int main() {
  i64 n, p;
  while(cin >> n >> p, n && p) {
    i64 P = p;
    vector<i64> r(n);
    for(int i = 0;;i = (i + 1) % n){
      if(p == 0) {
        p = r[i];
        r[i] = 0;
      }
      else {
        p--;
        r[i]++;
      }
      if(p == 0 && r[i] == P) {
        cout << i << endl;
        break;
      }
    }
  }
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
#include <stack>
#include <assert.h>


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

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

#define REP(i, n) for(ll i = 0; i < n; i++)







int main(){
    int n, p;
    cin >> n >> p;
    while(n || p){
        ll m = p, idx = 0;
        vector<ll> stone(n);
        for(;;){
            if(m > 0){
                m--;
                stone[idx]++;
            }
            else if(stone[idx] < p){
                m = stone[idx];
                stone[idx] = 0;
            }
            else{
                cout << idx << endl;
                break;
            }
            idx++;
            idx %= n;
        }
        
        cin >> n >> p;
    }
}

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

int main(){
  int n,p;
  int w, ss[50], i;

  while( true ){
    cin >> n >> p;
    if( !n && !p ) break;

    w = p;
    for( i=0;i<n;i++ ) ss[i]=0;

    i=0;
    while( true ){
      if( w>0 ){
	w--; ss[i]++;
	if( ss[i]==p ){
	  cout << i << endl;
	  break;
	}
      }else{
	w+=ss[i]; ss[i]=0;
      }
      i = (i+1)%n;
    }
  }
  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define max_n 53
#define max_p 53
int n,p,stone;
int score[max_n];
bool input(){
  cin>>n>>p;
  return (n!=0);
}

void solve(){ 
  REP(i,max_n)score[i]=0;
  stone=p;
  int person =0;
  while(true){
    if(stone>0){
      stone--;
      score[person]+=1;
      if(score[person]==p)break;
  }else{
    stone=score[person];
    score[person]=0;
  }
  person++;
  person%=n;
  }
  cout<<person<<endl;
}

int main(){
  while(input()){
    solve();
  }
  return 0;
}