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 a[50], n, p, q;

    while( cin >> n >> p )
    {
        if( n == 0 && p == 0 )
            break;

        for( int i = 0; i < 50; i++ )
            a[i] = 0;

        q = p;
        
        for( int i = 0; i < n; i++, i %= n )
        {
            if( p > 0 )
            {
                a[i]++;
                p--;
            }
            else if( a[i] != q )
            {
                p += a[i];
                a[i] = 0;
            }
            else
            {
                cout << i << endl;
                break;
            }
        }
    }
    return 0;
}

C++: #include <stdio.h>
int main(){
  
  int player[51] = {0};
  int n,p,pool=0,i;
  for(;;){
  scanf("%d %d",&n,&p);

  if(n==0&&p==0){
    break;
  }
  pool=p;
  i=0; 
  for(;;){
    for(i=0;i<=n-1;i++){
      if(pool==0){
	pool=player[i];
	player[i]=0;
	continue;
      }
      if(pool==1&&player[i]==p-1){
	break;
      }

      if (pool!=0){
	pool=pool-1;
	player[i]+=1;
      }
    }
    if(pool==1&&player[i]==p-1){
      break;
    }
    i++;
  }
  printf("%d\n",i);
  pool=0;
  player[i]=0;
  }
  return 0;
}

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

using namespace std;

int main() {

    int n, p;

    while(cin >> n >> p, n | p) {

        int bowl = p;
        int stone[n];

        fill(stone, stone + n, 0);

        int i = -1;
        do {
            i = (i + 1) % n;
            if(bowl > 0) {
                --bowl;
                ++stone[i];
            } else {
                bowl += stone[i];
                stone[i] = 0;
            }
        } while(stone[i] != p);

        cout << i << endl;

    }

}

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

#define pb         push_back

const int INF = 100000000;

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

		vector<int> stone(n, 0);
		int bawl = p;
		int win = -1;
		while(1){
			for(int i=0; i<n; i++){
				if(bawl > 0){
					bawl--;
					stone[i]++;
					if(bawl == 0 && stone[i] == p) win = i;
				}else{
					bawl = stone[i];
					stone[i] = 0;
				}
			}
			if(win != -1) break;
		}
		cout << win << endl;
	}
	
	return 0;
}

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000
#define MAXN 64

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int pebble[MAXN];

int main(void) {
    int n, p;
    while (cin >> n >> p) {
        if (n == 0 && p == 0) break;
        int center = p, cur = 0;
        for (int i = 0; i < n; i++) pebble[i] = 0;
        while (1) {
            if (center == 0) {
                center += pebble[cur];
                pebble[cur] = 0;
            } else {
                pebble[cur]++;
                center--;
                if (pebble[cur] == p) {
                    cout << cur << endl;
                    break;
                }
            }
            cur = (cur+1) % n;
        }
    }
    return 0;
}

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

int main()
{
	int n, p;
	int member[50];
	while (cin >> n >> p)
	{
		if (n == 0 && p == 0)break;
		int initP = p;
		int index = 0;
		for (int i = 0; i < n; i++)
		{
			member[i] = 0;
		}

		while (true)
		{
			if (p > 0)
			{
				p--;
				member[index % n]++;
				if (member[index % n] == initP)
				{
					break;
				}
			}
			else
			{
				p += member[index % n];
				member[index % n] = 0;
			}
			index++;
		}
		cout << index % n << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;
#define mod (1000000000+7)
#define N (10007)
#define INF 1e16
typedef long long ll;
typedef pair<ll,ll> P;


int main(void){
	int n,p;
	while(true){
		cin>>n>>p;
		if(n==0 && p==0)break;
		set<int>per;
		vector<int>a(n);
		int index=0;
		int num=p;
		while(true){
			if(p!=0){
				a[index]++;
				p--;
				if(p==0){
					if(per.size()==1&&a[index]==num){
						cout<<index<<endl;
						break;
					}
					else per.insert(index);
				}
				else per.insert(index);
			}
			else{
				if(a[index]!=0){
					p+=a[index];
					a[index]=0;
					per.erase(index);
				}
			}
			//for(int i=0;i<n;i++)cout<<a[i]<<" ";
			//cout<<per.size()<<" "<<endl;
			index=(index+1)%n;
		}
	}
    return 0;
}

C++: #include<iostream>

using namespace std;

int main() {
    while (true) {
        int n, p, bowl, player;
        int stone[50] = {};
        cin >> n >> p;
        if (n == 0 && p == 0) {
            break;
        }
        bowl = p;
        for (player = 0; stone[player] < p; player = (player + 1) % n) {
            if (bowl > 0) {
                ++stone[player];
                --bowl;
            }
            else {
                bowl = stone[player];
                stone[player] = 0;
            }
        }
        cout << player << endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>

using namespace std;

int main(){

	int num_man,num_stone,table[50],common,next_mayor,index;

	while(true){
		scanf("%d %d",&num_man,&num_stone);
		if(num_man == 0 && num_stone == 0)break;

		for(int i = 0; i < num_man; i++)table[i] = 0;

		common = num_stone;
		index = 0;

		while(true){
			if(common > 1){
				table[index]++;
				common--;
			}else if(common == 1){
				table[index]++;
				common--;

				if(table[index] == num_stone){
					next_mayor = index;
					break;
				}
			}else{ //common == 0
				common += table[index];
				table[index] = 0;
			}
			index = (index+1)%num_man;
		}
		printf("%d\n",next_mayor);
	}

	return 0;
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;

long long int power(long long int x, long long int n, long long int M) {
	long long int tmp = 1;

	if (n > 0) {
		tmp = power(x, n / 2, M);
		if (n % 2 == 0) tmp = (tmp*tmp) % M;
		else tmp = (((tmp*tmp) % M)*x) % M;
	}
	return tmp;
}

long long int N, M, K, Q, W, H, L, R;
long long int ans;

using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin >> N >> M;
	while (N) {
		int num[50] = {};
		int box = M;
		for (int i = 0; i < 10000000; i++) {
			if (box) {
				num[i%N]++;
				box--;
				if (num[i%N] == M) {
					cout << i%N << endl;
					break;
				}
			}
			else {
				box += num[i%N];
				num[i%N] = 0;
			}
		}
		cin >> N >> M;
	}
	return 0;
}