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 <cstdio>
#include <iostream>
#include <list>

int main()
{
	int n, p;
	while(1){
		std::cin >> n >> p;
		if(n == 0 && p == 0) break;
		int a[50], b, c;
		b = p / n;
		c = p % n;
		for(int i = 0; i < n; i++){
			if(i < c) a[i] = b+1;
			else a[i] = b;
		}
		int start = c, bowl;
		while(1){
			if(a[start] != 0){
				bowl = a[start];
				a[start] = 0;
				b= bowl / n;
				c = bowl % n;
				for(int j = 1; j <=n; j++){
					if(j <= c) a[(start + j)%n] += b+1;
					else a[(start+j)%n] += b;
				}
				start = (start + c) % n;
				if(a[start] == p){
					std::cout << start << std::endl;
					break; 
				}
			}
			start = (start + 1) % n;	
		}
	}
	return 0;
}

C++: #include<cstdio>

using namespace std;

int main(){
	for(int n,p;scanf("%d%d",&n,&p),n;){
		int bowl=p,got[50]={};
		for(int i=0;;i=(i+1)%n){
			if(bowl>0)	got[i]++,bowl--;
			else		bowl+=got[i],got[i]=0;
			if(got[i]==p){
				printf("%d\n",i);
				break;
			}
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
    int n, p;
    while (cin >> n >> p && n&&p) {
        vector<int> d(n,0);
        int cnt = 0,turn=0;
        while (cnt != 1||!d[turn]||p!=1) {
            for (int i = 0; i < p; ++i) {
                if (!d[(turn+i)%n]) ++cnt;
                ++d[(turn+i)%n];
            }
            turn += p;
            p = 0;
            while (!d[turn%n]) {
                ++turn;
            }
            --cnt;
            turn %= n;
            p = d[turn];
            d[turn] = 0;
            turn = (turn + 1) % n;
        }
        cout << turn << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int main(){
  int n,p;
  while(cin>>n>>p, n!=0 || p!=0){
    int A[n];
    memset(A, 0, sizeof(A));
    int t=0;
    while(1){
      if(p==0){
        p=A[t];
        A[t]=0;
      } else {
        p-=1;
        A[t]+=1;
      }
      int f=1;
      rep(i,n) {
        if(t!=i && A[i]!=0) f=0;
      }
      if(p==0 && f) break;
      t=(t+1)%n;
    }
    cout<<t<<endl;
  }
  return 0;
}

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

#define MAX_N 50

using namespace std;

int n, p;
int d[MAX_N];

void solve() {
	for (int i = 0; i < n; i++)
	{
		d[i] = 0;
	}
	int w = p;
	while(1){
		for (int i = 0; i < n; i++)
		{
			if(w > 0){
				d[i]++;
				w--;
				if(w == 0 && d[i] == p){
					cout << i << endl;
					return;
				}
			}else{
				w = d[i];
				d[i] = 0;
			}
		}
	}
}

int main(){
	while(1){
		cin >> n >> p;
		if(n == 0 && p == 0) break;
		solve();
	}
	return 0;
}

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

#define var auto
#define long long long
#define ll long
#define Max std::max
#define Min std::min
#define Abs std::abs

void Solve();

using namespace std;

int main() {
    while (true) {
        Solve();
    }
}

void Solve() {
    int n, r;
    cin >> n >> r;
    if (n == 0 && r == 0) exit(0);
    int ball = r;
    vector<int> hands(n);
    for (int i = 0; true; i = (i + 1) % n) {
        if (ball == 0){
            ball = hands[i];
            hands[i] = 0;
        }
        else{
            hands[i] += 1;
            ball -= 1;
            if (hands[i] == r){
                cout << i << endl;
                return;
            }
        }
    }
}

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

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)

using namespace std;

int main(){

	int n,p;
	while(1){
		cin>>n>>p;
		if(n==0&&p==0) break;
		int wan=p,a[50]={},i=0;
		while(1){
			if(wan>0){
				wan--;
				a[i]++;
			}else{
				if(a[i]==0){

				}else{
					wan += a[i];
					a[i] = 0;
				}
			}
			i++;
			if(i==n) i=0;
			bool flag = false;
			for(int j=0; j<n; j++){
				if(j!=i && a[j]!=0) flag = true;
			}
			if(!flag){
				cout<<i<<endl;
				break;
			}
		}
	}

	return 0;
}

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

using namespace std;

int main()
{
	int n, p;

	while (true)
	{
		cin >> n >> p;

		if (n == 0 && p == 0) { break; }

		int c = 0, r = p;

		vector<int> d(n, 0);

		while (true)
		{
			if (p != 0)
			{
				d[c]++; p--;
			}
			else
			{
				p = d[c]; d[c] = 0;
			}

			bool ok = false;

			if (p == 0)
			{
				ok = true;

				for (int i = 0; i < n; i++)
				{
					if (i != c && d[i] != 0)
					{
						ok = false;
					}
				}
			}

			if (ok)
			{
				break;
			}

			c = (c + 1) % n;
		}

		cout << c << endl;
	}
}

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

int main(void){
  while(1){
    int stone[50];
    for(int k = 0 ; k < 50 ; k++){stone[k] = 0;}

    int bowl;
    int n, p;  // n：候補者人数，p：小石の総数
    cin >> n;
    cin >> p;

    if(n == 0 && p == 0){return 0;}

    bowl = p;

    bool flg = true;
    int ans;

    for(int i = 0 ; flg ; i = (i+1) % n){

      if(bowl == 0){
        bowl = bowl + stone[i];
        stone[i] = 0;
      }else{
        stone[i]++;
        bowl--;
        if(bowl == 0 && stone[i] == p){ans = i; flg = false;}
      }
    }

    for(int i = 0 ; i < 1000000 ; i++){
        for(int j = 0 ; j < 1000000 ; j++){
            int k = i + j;
        }    
    }


    cout << ans << endl;
  }
}

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

C++: #include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
const int INF=(ll)(1LL<<31)-1;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
//int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int n,m;

int main(){
	while(1){
		cin>>n>>m;
		if(n==0&&m==0)break;
		int a[n]={};
		int plate=m;
		while(1){
			for(int i=0;i<n;i++){
				if(plate){
					plate--;
					a[i]++;
				}else{
					plate+=a[i];
					a[i]=0;
				}
				if(a[i]==m){
					cout<<i<<endl;
					goto A;
				}
			}
		}
		A:;
	}
}