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>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) cerr << #x << "=" << static_cast<bitset<16> >(x) << endl;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
int dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};
using namespace std;
typedef long long  ll;
typedef pair<int, int> P;

/** Problem1159 : Next Mayor **/
int main()
{
	int n, get[50], stone;

	
	while (cin >> n >> stone, n||stone) {
		fill(get, get+50, 0);
		int i=0;
		while (1) {
			if (stone>0) {
				stone--;
				get[i%n]++;
			} else {
				bool flg=true;
				rep(j, n) {
					if (j!=i%n && get[j]>0) {
						flg=false;
						break;
					}
				}
				if (flg) {
					cout << i%n << endl;
					goto end;
				} else {
					stone = get[i%n];
					get[i%n] = 0;
				}
			}
			i++;
		}
	end:;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
const int MAX = 50;
int main()
{
	while (true)
	{
		int n, p;
		cin >> n >> p;
		if (!n && !p)
			break;
		int pOfCan[MAX] = { 0 };
		
		int pOfBowl = p;
		int winIdx = -1;
		int idx = 0;
		while (true)
		{
			if (idx > n - 1)
				idx = 0;
			if (pOfBowl > 0)
			{
				pOfCan[idx]++;
				pOfBowl--;
			}
			else if (pOfBowl == 0)
			{
				if (pOfCan[idx] == p)
				{
					winIdx = idx;
					break;
				}
				pOfBowl = pOfCan[idx];
				pOfCan[idx] = 0;
			}
			idx++;
		}
		cout << winIdx << endl;
	}
}

C++: #include<iostream>
using namespace std;
int main(){
  while(1){
    int n,p;
    cin>>n>>p;
    if(n==0&&p==0)break;
    int k=p;
    int ch[50]={},f=0;
    while(1){
      if(k==0){
	k=ch[f%n];
	ch[f%n]=0;
      }     
      else{
	ch[f%n]++;
	k--;
      }	 
      if(ch[f%n]==p)break;
      f++;
    }
    cout<<f%n<<endl;
  } 
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;

int main(){
  while(1){
    int n,p;
    cin >> n >> p;
    if(n==0&&p==0)break;
    vector<int> stone(n);
    int pot=p;
    rep(i,n) stone[i]=0;
    rep(i,1000000){
      if(pot){
        pot--;
        stone[i%n]++;
      }
      else if(stone[i%n]){
        pot+=stone[i%n];
        stone[i%n]=0;
      }
      if(pot==0){
        rep(j,n){
          if(stone[j]==p){cout << j << endl;i=1010000;break;}
        }
      }
    }
  }
  return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
	while (1){
		int n, p;
		int tp;
		cin >> n >> tp;
		if (n == 0 && tp == 0) break;
		p = tp;
		int win = 0;
		int N[50] = { 0 };
		while (1){
			p -= 1;
			N[win] += 1;
			if (p == 0){
				if (N[win] == tp){
					cout << win << endl;
					break;
				}
				else{
					p += N[win];
					N[win] = 0;
				}
			}

			win++;
			if (win >= n) win = 0;
		}
	}
	return 0;
}

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

using namespace std;

int main(){
	
	int n, p;
	
	while(true){
		cin >> n >> p;
		if(n == 0){
			break;
		}
		int cup = p;
		
		int num[100];
		
		int ans = -1;
		
		for(int i = 0; i < n; i++){
			num[i] = 0;
		}
		
		while(true){
			for(int i = 0; i < n; i++){
				if(cup != 0){
					cup--;
					num[i]++;
					if(num[i] == p){
						ans = i;
						break;
					}
				}else{
					cup = num[i];
					num[i] = 0;
				}
				//cout << cup << " ";
			}
			if(ans != -1){
				break;
			}
		}
		
		cout << ans << endl;
	}
	
	return 0;
}

C++: import java.util.*;
public class Main {
	int[] N;
	int n,p;
	Main(){
		n=-1;p=-1;
	}
		void run(){
		Scanner sc=new Scanner(System.in);
		while(true){
			n=sc.nextInt();
			p=sc.nextInt();
			if(n==0&&p==0)break;
			N=new int[n];
			System.out.println(solve(0,p));
		}
	}
		int solve(int current,int remains){
			while(true){
				if(remains==1&&N[current]+1==p)return current;
				if(remains==0){
					remains=N[current]+1;
					N[current]=0;
				}
				else{
					N[current]+=1;
				}
				current=(current+1)%n;
				remains=remains-1;
				for(int i=0;i<n;i++){
				}
			}
		}
	public static void main(String[] args) {
		new Main().run();
	}
}

C++: #include<iostream>
using namespace std;
int main(){
	int n,p,temp,a[60],i;
	while(cin>>n>>p,n||p){
		for(int j=0;j<n;j++)
			a[j]=0;
		temp=p;
		i=0;
		for(;;){
			if(temp>0){
				temp--;
				a[i++]++;
			}
			else if(temp==0){
				if(a[n-1]==p){
					cout<<n-1<<endl;
					break;
				}
				else
				if(a[i-1]==p){
					cout<<i-1<<endl;
					break;
				}
				else{
					temp=a[i];
					a[i++]=0;
				}
			}
			if(i>n-1)
				i=0;
		}
	}
	return 0;
}

C++: #include <stdio.h>
int main(){
  
  int player[51];
  int n,p,pool=0,i;

  scanf("%d %d",&n,&p);

  pool=p;
  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;
    }

  }
  printf("%d\n",i);


  return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
// 18:45 -
using namespace std;

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

    return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
//#define int long long
const int MAX_N = 55;

int n, p;

int check(){
  int v[MAX_N], pp = p;
  memset(v, 0, sizeof(v));
  for(int i = 0; ; ++i){
    int now = i % n;
    if(p != 0){
      v[now] += 1;
      --p;
      if(v[now] == pp) return now;
    } else{
      p = v[now];
      v[now] = 0;
    }
  }
}

int main() {
  while(cin >>n >>p && n) cout <<check() <<endl;
  return 0;
}