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>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <utility>
using namespace std;
#define INF 100000005
#define MAX 200005

int a[55];
int main(){
	int n, p, s, num, ans, flag;
	while(cin >> n >> p, n){
		fill(a, a+55, 0);
		s = p;
		flag = 1;
		while(flag){
			num = 0;
			for(int i = 0;flag && i < n;i++){
				if(!s && a[i]){
					s = a[i];a[i] = 0;
				}else if(s){
					a[i]++;s--;
					if(a[i] == p){
						ans = i;
						flag = 0;
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #pragma GCC optimize("Ofast")
#include <cstdio>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <bitset>
#include <cmath>
#include <limits>
#include <iostream>
#include <map>
#include <set>
#include <tuple>
using namespace std;
#define INF 1LL << 61
#define MAX 1LL << 30
#define MOD 1000000007
typedef long long ll;
//typedef pair<ll,ll> P;
//typedef pair<pair<int,int>,int> p;
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)
struct edge{ll to,cost,val;};
template<class T,class U>bool chmin(T&a,const U&b){if(a<=b)return false;a=b;return true;}
template<class T,class U>bool chmax(T&a,const U&b){if(a>=b)return false;a=b;return true;}
//__builtin_popcount(S);
//C.erase(unique(C.begin(),C.end()),C.end());
//#define int ll
int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

signed main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);


    while(true){
        int N,P;
        cin>>N>>P;
        if(N==P && N==0){
            return 0;
        }
        int C[51];
        for(int i=0;i<51;i++) C[i]=0;
        bool flag=true;
        int p=P;
        while(flag){
            for(int i=0;i<N;i++){
                if(P>0){
                    C[i]++;
                    P--;
                }else{
                    P=C[i];
                    C[i]=0;
                }
                if(p==C[i]){
                    flag=false;
                    cout<<i<<endl;
                    break;
                }
            }
        }
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
int main(void){
    int n,r,p;
    while(cin>>n>>r,n){
        valarray<int> mayor(0,n);
        p=r;
        rep(i,1000000){
            if(p==0){
                p+=mayor[0]; mayor[0]=0;
            }else{
                p--;mayor[0]++;
                if(mayor[0]==r){
                    cout<<i%n<<endl;
                    break;
                }
            }
            mayor=mayor.cshift(1);
        }
    }
}

C++: #include <iostream>

using namespace std;

bool isallzero(int n[], int m, int num) {
	for (int i = 0; i < num; i++) {
		if (i == m) {
			continue;
		} else if (n[i] != 0) {
			return false;
		}
	}

	return true;
}

int main() {
	while (true) {
		int n, p;
		cin >> n >> p;
		if (n == 0) {
			break;
		}
		
		int bowl = p;
		int candidates[n];
		int current = 0;
		for (int i = 0; i < n; i++) {
			candidates[i] = 0;
		}

		while (true) {
			if (bowl > 0) {
				bowl--;
				candidates[current]++;
				if (bowl == 0 && isallzero(candidates, current, n)) {
					cout << current << endl;
					break;
				}
			} else {
				bowl = candidates[current];
				candidates[current] = 0;
			}
			current = (current + 1) % n;
		}
	}
	
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstdio>
#include<string>
#include<cmath>
typedef long long int ll;
#define VI vector<int>

#define rep(i,a) for(int i=0;i<(a);i++)
#define all(i) i.begin(),i.end()
#define INF 1000000000


using namespace std;
int n,p;

int check(int mem[100]){
	int count=0;
	int ret;
	rep(i,n){
		if(mem[i]!=0){
			count++;
			ret=i;
		}
	}
	if(count==1)return ret;
	return -1;
}

int main(){
	while(1){
		int mem[100]={};
		cin>>n>>p;
		if(n==0 && p==0)break;
		int box=p;
		int count=0;
		while(1){
			if(box==0){
//				cout<<"1"<<endl;
				box+=mem[count%(n)];
				mem[count%(n)]=0;
			}
			else{
//				cout<<"2"<<endl;
				mem[count%(n)]++;
				box--;
				int ret;
				if( (ret=check(mem))!=-1 && box==0){
					cout<<ret<<endl;
					break;
				}
			}
			count++;
		}
	}
	return 0;
}

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

int main()
{
	int n,p;
	while(cin >> n >> p, n||p)
	{
		int s=p;
		int snum[64] = {0};

		for(int i=0; true; i=(i+1)%n)
		{
			if(s)
			{
				s--;
				snum[i]++;
				if(snum[i]==p)
				{
					cout << i << endl;
					break;
				}
			}
			else
			{
				s=snum[i];
				snum[i]=0;
			}
		}
		
	}
}

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

using namespace std;

#define INF 999999999

int main(){
	int n, p, now, m[50], k;
	while(1){
		cin >> n >> p;
		if(!n)break;
		for(k=0;k<n;k++){
			m[k] = 0;
		}
		k=0;
		now=p;
		while(1){
			if(now){
				m[k]++;
				now--;
			}
			else{
				now+=m[k];
				m[k]=0;
			}
			if(m[k]==p){
				cout << k << endl;
				break;
			}
			else{
				if(k!=n-1) k++;
				else k = 0;
			}
		}
	}
	return 0;
}

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

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define SORT(v) sort(ALL(v))
//#define scanf scanf_s

int main()
{
	int n, p;
	while (cin >> n >> p, n)
	{
		int m[55] = {};
		int wan = p;
		int i, j;
		j = 0;
		for (i = 1; i < 1000005; i++)
		{
			if (wan>0)
			{
				wan--;
				m[j]++;
				if (m[j] == p)
				{
					cout << j << endl;
					break;
				}
			}
			else
			{
				wan += m[j];
				m[j] = 0;
			}

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

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

int main()
{
  while(1){
    int n, p;
    cin >> n >> p;
    if(n == 0 && p == 0){
      break;
    }else{
      int c[50] = {};
      int i = 0, b = p;
      while(1){
	if(p){
	  c[i]++;
	  p--;
	}else{
	  p += c[i];
	  c[i] = 0;
	}
	if(c[i] == b){
	  cout << i << endl;
	  break;
	}
	i++;
	if(i > n - 1)
	  i = 0;
      }
    }
  }
}