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 <stdio.h>
int main()
{
  int i=0, l,  p, q, j=0;
  int d[50];
 
  while(1)
    {
      for ( i = 0; i < 50; i++)
    {
      d[i] = 0;
    }   
      scanf("%d %d", &l, &p);
      if( l == 0 && p == 0 ) break;
      q = p;
      while(1)
    {
          if ( i >= l ) i = 0;
      if ( p > 0 )
        {
          d[i] += 1;
          p--;
          if ( d[i] == q && p == 0)
        {
          break;
        }  
        }
      else if ( p == 0 )
        {
          p = d[i];
          d[i] = 0;
        }
      i++;
    }
      printf("%d\n", i);
    }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;

int main() {
	while(1) {
		int n, p;
		cin >> n >> p;
		if(n==0 && p==0) break;
		int player[n];
		for(int i=0; i<n; i++) player[i] = 0;

		int i=0;
		int num = p;
		while(1) {
			i = i%n;
			if(num!=0) {
				player[i]++;
				num--;
				if(player[i] == p) {
					cout << i << endl;
					break;
				}
			}
			else {
				int temp = player[i];
				player[i] = 0;
				num += temp; 
			}
			i++;
		}
	}
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

#define INF 1000000000
#define EPS 1e-9
#define PI acos(-1)

typedef long long ll;

#define MAX_N 50
#define MAX_P 50

int n, p;
int h[MAX_N];

vector<int> ans_list;

int main(){

  while(true){
    cin >> n >> p;
    if(n == 0 && p == 0)
      break;
    memset(h, 0, sizeof(h));
    int step = 0;
    int turn = 0; //現在のターン
    int num = p; //碗の中に入っている石の数
    while(true){

      if(num == 1 && num+h[turn] == p){
	ans_list.push_back(turn);
	break;
      }
      
      if(num > 0){
	h[turn]++;
	num--;
	turn++;
	if(turn == n)
	  turn = 0;
      }

      else{
	num += h[turn];
	h[turn] = 0;
	turn++;
	if(turn == n)
	  turn = 0;
      }

    }

  }

  for(int i = 0; i < ans_list.size(); i++){
    cout << ans_list[i] << endl;
  }

  return 0;

}

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

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

int main(){
        int n,p;
        while(cin>>n>>p&&n,p){
                vector<int>a(n);
                int num=p,ans;
                bool ok=true;
                while(ok){
                        for(int i=0;i<n;i++){
                                if(num)num--,a[i]++;
                                else if(a[i]&&!num)num+=a[i],a[i]=0;
                                if(a[i]==p){
                                        ok=false;
                                        ans=i;
                                        break;
                                }
                        }
                }
                cout<<ans<<endl;
        }
        return 0;
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cctype>

#define mp make_pair
#define pb push_back
#define REP(i,a,n) for(int i = a;i < (n);i++)
#define rep(i,n) for(int i = 0;i < (n);i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x,min,max) ((min) <= (x) && (x) <= (max))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VII;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef pair<int,int> PII;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int n, p;
int P;
VI cnt;

int check_winner(){
	rep(i, cnt.size()){
		if(cnt[i] == P) return i + 1;
	}
	return 0;
}

int solve(){
	P = p;
	int ans;
	rep(i, 1000000){
		rep(j, n){
			if(ans = check_winner()){
				return ans - 1;
			}else if(p == 0){
				p = cnt[j];
				cnt[j] = 0;
			}else{
				p--;
				cnt[j]++;
			}
		}
	}
	return 0;
}

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	while(1){
		cnt.clear();
		cin >> n >> p;
		cnt.resize(n, 0);
		if(n == 0 && p == 0) break;
		
		cout << solve() << endl;
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

int n[55]={};
int p[55] = {};
int have[55] = {};
int winner[100]={};
int judge=0;

int main()
{
    int i=0;
    while(1){
        scanf("%d %d",&n[i],&p[i]);
        if(n[i]==0 && p[i] == 0){
            break;
        }
        i++;
    }
    int times = i;

    for(i=0;i<times;i++){
        int pebble = p[i];
        for(int j=0;j<n[i];j++){
            have[j]=0;
        }
        while(1){
            for(int j=0;j<n[i];j++){
                if(pebble != 0){
                    have[j]++;
                    pebble--;
                    if(have[j] == p[i]){
                        winner[i] = j;
                        judge = 1;
                        break;
                    }
                }else{
                    pebble += have[j];
                    have[j] = 0;
                }
            }
            if(judge == 1){
                cout << winner[i] << endl;
                judge = 0;
                break;
            }
        } 
    }

    return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

int main() {
	int n, p;

	while(cin >> n >> p, n, p) {
		vi players(n, 0);

		int i = 0;
		int wan = p;
		while(true) {		
			if(wan == 0) {
				wan = players[i];
				players[i] = 0;
			} else {
				players[i] += 1;
				wan -= 1;
			}

			if(players[i] == p) {
				break;
			}

			i = (i+1)%n;
		}

		cout << i << endl;
	}
}