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>
#include <algorithm>
using namespace std;

int main() {
  int n, p;
  while (cin >> n >> p && n) {
    int have[50], cup = p, turn = 0;

    fill(have, have + n, 0);
    while (true) {
      
      if (cup == 1 && have[turn] == p - 1) {
	cout << turn << endl;
	break;
      }

      if (cup == 0)
	swap(cup, have[turn]);

      else
	have[turn]++, cup--;

      turn = (turn + 1) % n;
    }
  }
}

C++: #include<stdio.h>
int main()
{
	int n,p;
	while(scanf("%d%d",&n,&p),n)
	{
		int i,a[50]={0},m=p;
		for(i=0;a[i]-p;i=(i+1)%n)
		{
			if(m)a[i]++,m--;
			else m=a[i],a[i]=0;
		}
		printf("%d\n",i);
	}
	return 0;
}

C++: #include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main (void) {

    vector<unsigned int> ns;
    vector<unsigned int> ps;

    unsigned int tn, tp;
    unsigned int dataset_size = 0;
    unsigned int count = 0;
    do {
        cin >> tn >> tp;
        ns.push_back(tn);
        ps.push_back(tp);
        dataset_size++;
    } while (tn != 0 || tp !=0);

    for (int x=0; x<dataset_size-1; x++) {
        count = 0;
        tn = ns[x];
        tp = ps[x];

        int has_p[tn];
        for (unsigned int i=0; i<tn; i++)
            has_p[i] = 0;

        unsigned int i = 0;
        do {
            if (tp == 0) {
                tp = has_p[i];
                has_p[i] = 0;
            } else {
                tp--;
                if ((++has_p[i]) == ps[x]) { break; }
            }
            i = (++i % tn);
            count++;
        } while (true);
        cout << i << endl;
    }

    return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
  
int main()
{
	int n,m;
	int p[50];
	int i,j;
	int ans;

	while(1){
	scanf("%d %d",&n,&m);
	if((n==0)&&(m==0))
		break;
	//ここまでテンプレ

	for(i=0;i<n;i++)
		p[i]=0;
	j=m;
	ans=-1;
	while(1){
		for(i=0;i<n;i++){
			if(j>0){
				p[i]++;
				j--;
				if(p[i]==m){
					ans=i;
					break;
				}
			}else if(j==0){
				j+=p[i];
				p[i]=0;
			}

		}
		if(ans>-1)
			break;
	}

	printf("%d\n",ans);


	//ここからテンプレ
	}
	return 0;
}

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

using namespace std;

int N, P;

int main()
{
  while (scanf("%d %d ", &N, &P) == 2) {
    if (N == 0 && P == 0) {
      return 0;
    }
    vector<int> v(N);
    int p = P;
    for (int i = 0;; i = (i + 1) % N) {
      if (p == 0) {
	p += v[i];
	v[i] = 0;
      } else {
	v[i]++;
	p--;
      }
      if (v[i] == P) {
	printf("%d\n", i);
	break;
      }
    }
  }
}

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

C++: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <string>
#include <algorithm>
#include <string>
#include <climits>

#define REP(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)
#define REG(i, a, b) for (int (i) = ((int)(a)); (i) < ((int)(b)); i++)
#define ALL(V) ((V).begin(), (V).end())
#define PRINT(STR) (cout << (STR) << endl)

typedef long long ll;
using namespace std;

bool isOK(int n, int h[], int a) {
	REP(i, n) {
		if (i == a) continue;
		if (h[i] != 0) return false;
	}
	return true;
}

void step(int a, int n, int* s, int h[]) {
	if (*s == 0) {
		*s = h[a];
		h[a] = 0;
	} else {
		h[a]++;
		*s -= 1;
	}
}

int main() {
	cin.tie(0); //?????±?????????????????????
	ios::sync_with_stdio(false); //printf??¨??±??????????????????
	int n, p;
	int h[51];
	while (cin >> n >> p, n + p != 0) {
		int a = 0;
		fill(h, h + 51, 0);
		while(true) {
			step(a, n, &p, h); 
			if (p == 0 && isOK(n, h, a) == true) {
				break;
			}
			a++;
			if (a == n) {
				a = 0;
			}
		}
		PRINT(a);
	}

	return 0;
}

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

int main() {
	int n,p;
	while (cin >> n>>p) {
		if (!n)break;
		int ans = -1, i = 0;
		vector<int> v(n,0);
		int c = p;
		
		while (ans<0) {
			for (i=0; i < n; ++i) {
				if (!c) {
					c = v[i];
					v[i] = 0;
				}else{
					v[i]++;
					c--;
				}
				if (p == v[i]) {
					ans = i;
					break;
				}
				
			}
		}
		cout << ans << endl;
		
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

bool search(int p[], int N){
  int count = 0;
  for (int i=0; i<N; ++i){
    if (p[i] != 0) {
      count++;
    }
  }
  if (count == 1){
    return true;
  }
  else{
    return false;
  }
}


int main(void){
  int n, p;
  while (cin >> n >> p){
    if ((n == 0) && (p == 0)){
      return 0;
    }
    int *people = new int[n];
    for (int i=0; i<n; ++i){
      people[i] = 0;
    }
    //finish initialization
    int turn = 0;
    while (1){
      if (p >= 1){
	++people[turn];
	--p;
	if (p==0){
	  if (search(people, n) == true){
	    break;
	  }
	}
      }
      else{ // p<=0]
	p = people[turn];
	people[turn] = 0;
      }
      if (turn == (n-1)){
	turn = 0;
      }
      else{
	++turn;
      }
    }
    cout << turn << endl;
    delete[] people;
  }
  return 0;
}

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

int main(){
	int n, p;
	while(true){
		scanf("%d%d", &n, &p);
		if(n == 0 && p == 0){
			break;
		}
		int mayor[50];
		for(int i = 0; i < 50; i++){
			mayor[i] = 0;
		}	
		bool finish = false;
		int m = -1;
		int pp = p;
		int t = 0;
		while(true){
			// 終了判定
			for(int i = 0; i < n; i++){
				if(mayor[i] == p){
					finish = true;
					m = i;
					break;
				}
			}
			if(finish){
				break;
			}
			
			if(t == n){
				t = 0;
			}
			
			// シュミレート
			
			if( pp > 0){
				mayor[t]++;
				pp--;
				t++;
			}
			else{
				pp = mayor[t];
				mayor[t] = 0;
				t++;
			}
		}
		printf("%d\n", m);
	}
	return 0;
}

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

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

using namespace std;

int game(int num, int pebbles){
    int cand[num];
    REP(i,num) cand[i] = 0;

    int cnt = 0, p = pebbles, ans;
    while(true){
        int turn = cnt % num;
        cnt++;
        if(p == 0){
            if(cand[turn] == 0) continue;
            p += cand[turn];
            cand[turn] = 0;
        }else{
            p--;
            cand[turn]++;
            if(cand[turn] == pebbles){
                ans = turn;
                break;
            }
        }
    }
    return ans;
}

int main(){
    int n, p;
    while(cin >> n >> p){
        if(n == 0 && p == 0) break;
        int ans = game(n, p);
        cout << ans << endl;
    }
    return 0;
}