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 n, p;
	int stoneOfPerson[50];
	int stoneInBowl;
	int winner;
	
	while (1) {
		cin >> n >> p;
		if ((n == 0) && (p == 0)) {
			break;
		}
		for (int i = 0; i < n; i++) {
			stoneOfPerson[i] = 0;
		}
		stoneInBowl = p;
		
		int i = 0;
		while (1) {
			if (stoneInBowl > 0) {
				stoneOfPerson[i]++;
				stoneInBowl--;
				if ((stoneInBowl == 0) && (stoneOfPerson[i] == p)) {
					winner = i;
					break;
				}
			} else {
				stoneInBowl = stoneOfPerson[i];
				stoneOfPerson[i] = 0;
			}
			i++;
			if (i >= n) {
				i = 0;
			}
		}
		cout << winner << endl;
	}
	return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define rer(i, a, b) for (int i = (int)a; i <= (int)b; ++i)
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define all(v) v.begin(), v.end()
#define mset(a, n) memset(a, n, sizeof(a))

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int> > vvi;
typedef vector<pair<int, int> > vpii;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int inf = 1000000000;
const int mod = 1000000007;
const double EPS = 1e-9;
const int dx[] = { -1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int n, p;

int main() {
	// cin.tie(0); // tie
	// ios::sync_with_stdio(false); // sync
	while (cin >> n >> p, n || p) {
		int tmp = p;
		int a[n];
		rep (i, n) a[i] = 0;
		for (int i = 0; i < n; ++i) {
			if (p != 0) {
				a[i]++;
				p--;
				if (p == 0 && a[i] == tmp) {
					cout << i << endl;
					break;
				}
			} else {
				p = a[i];
				a[i] = 0;
			}
			if (i == n - 1) i = -1;
		}
	}
	return 0;
}

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

int main(){
  int n,p;

  for(;;){
    cin >> n >> p;
    if(n==0 && p==0)break;
    
    int mayor[50] = {0}, id = 0, num = p;

    for(;;){
      if(num){
	mayor[id]++;
	num--;
      }else{
	num += mayor[id];
	mayor[id] = 0;
      }

      if(num==0 && mayor[id] == p){
	cout << id << endl;
	break;
      }
      id = (id+1) % n;
    }
  }
}

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

void main() {
	int n, p;

	while (1) {
		int flag = 0;
		int player[50] = { 0 };
		cin >> n >> p;
		if (n == 0 && p == 0) break;
		int p_max = p;

		while (1)
		{
			for (int j = 0; j < n; j++) {
				if (p != 0) player[j] += 1, p--;
				else p = player[j], player[j] = 0;
				if (player[j] == p_max) {
					flag = -1;
					cout << j << endl;
					break;
				}
			}
			if (flag == -1) break;
		}
	}
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i;
  int a[50],n,p,q;
  memset(a,0,sizeof(a));
  while(cin>>n>>p&&n+p){
    q=p;
    for(i=0;;i=(i+1)%n){
      if(p){
	p--;
	a[i]++;
	if(a[i]==q)
	  break;
      }else{
	p=a[i];
	a[i]=0;
      }
    }
    a[i]=0;
    cout<<i<<endl;
  }
  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;
  }
}