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<bits/stdc++.h>
using namespace std;

int main(){
    int n, p;
    while(cin >> n >> p, n != 0){
        vector<int> candidates(n);
        int i = 0, w = p;
        while(true){
            if(w > 0){
                candidates[i]++;
                w--;
            }else{
                w = candidates[i];
                candidates[i] = 0;
            }
            if(candidates[i] == p){
                cout << i << endl;
                break;
            }
            i = (i + 1) % n;
        }
    }

    return 0;
}

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

int ppl[55]={0};
int n,p,now,num;

int main(){
  int i,j,k;
  while(1){
    cin >> n >> p;
    if(n+p==0)break;
    now = p;
    num = 0;
    while(1){
      if(now>0){
	ppl[num]++;
	now--;
      }
      else if(ppl[num]==p)break;
      else{
	now = ppl[num];
	ppl[num]=0;
      }
      num++;
      num%=n;
    }
    printf("%d\n",num);
    for(i=0;i<n;i++)ppl[i]=0;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>

int cand[100];

int main(void) {
    while(true) {
        int i,n,p,wan;
        scanf("%d%d",&n,&p);
        if(!n && !p) break;

        wan = p;
        memset(cand, 0, sizeof(cand));
        for(i=0; cand[i]!=p; i=(i+1)%n) {
            if(wan) cand[i]++, wan--;
            else wan+=cand[i], cand[i]=0;
        }
        printf("%d\n", i); 
    }
    return 0;
}

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

#define repi(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) for(int i=0;i<(n);i++)

int a[50];
int n, p;

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

C++: #include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<stack>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define REEP(i, N) for(ll i = N - 1; i >= 0; --i)
#define FOR(i,m,n) for(ll i = m; i < n; ++i)
#define RFOR(i,m,n) for(ll i = n - 1; i >= m; --i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define PB push_back
#define KIRIAGE(a, b) a += (b - a % b) % b // a ??? b ????????°????????§?¢???????
#define CEIL(c, a, b) if(a % b == 0) { c = a / b;} else { c = a / b + 1;} // a / b ??\??????????°???´??°
#define sp(n) setprecision(n)
#define INF 1000000000
#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, 1, 0, -1};
int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int main(void) {
	while(true) {
		int n, r;
		cin>>n>>r;
		if(n==0&&r==0) return 0;
		vector<int> c(n, 0);
		int hav = 0;
		int pos = 0;
		int winner = -1;
		while(true) {
			if(r > 0) {
				++c[pos];
				if (c[pos] == 1) ++hav;
				--r;
				if(r == 0 && hav == 1) {
					winner = pos;
					break;
				}
			} else {
				if(c[pos] != 0) --hav;
				r = c[pos];
				c[pos] = 0;
			}
			if(pos < n - 1) ++pos;
			else pos = 0;
		}
		cout<<winner<<endl;
	}
}

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

int num[50]={0};


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

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

using namespace std;

int main(void){
    int n,q,w;
    int i;
    while(true){
        cin >> n >> q;
        if(n == 0) break;
        int p[n] = {0};
        i = 0;w = q;
        while(true){
            if(w){
                p[i]++;
                w--;
            }
            else{
                if(p[i] == q){
                    cout << i << endl;
                    break;
                }
                else{
                    w = p[i];
                    p[i] = 0;
                }
            }
            i = (i+1)%n;
        }
    }
}

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

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
constexpr int PI = (2*acos(0.0));
constexpr int EPS = (1e-9);
constexpr int MOD = (int)(1e9+7);
constexpr int INF = 100000000;
#pragma endregion

int person[50];
int pos;
int wan;
int n;
int p;

void solve() {
    while(true) {
        if(wan) {
            person[pos]++;
            wan--;
            if(person[pos]==p) {
                cout << pos << endl;
                return;
            }
        }
        else {
            wan = person[pos];
            person[pos] = 0;
        }
        pos++;
        if(pos==n) pos = 0;
    }
}

int main()
{
    while(cin>>n>>p, n) {
        rep(i,n) person[i] = 0;
        pos = 0;
        wan = p;
        solve();
    }
    return 0;
}

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