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

struct Candidate{
	int num;
};

int main(void) {
	for (;;) {
		int n, p;
		cin >> n >> p;
		if (n == 0 && p == 0)
			break;
		Candidate candidate[50];
		for (int i = 0; i < 50; i++) {
			candidate[i].num = 0;
		}
		
		int winner;
		int t = p;
		for (int i = 0; i <= 1000000; i=(i+1)%n) {
			if (p != 0) {
				candidate[i].num++;
				p--;
			}
			else if (p == 0) {
				p+=candidate[i].num;
				candidate[i].num = 0;
			}
			if (candidate[i].num == t) {
				winner = i;
				break;
			}
		}
		printf("%d\n", winner);
	}
	return 0;
}

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

int main() {
	int a, b;
	while (cin >> a >> b, a | b) {
		int temoti[50]{}, ba = b;
		for (int i = 0;; i++) {
			if (ba) {
				ba--;
				temoti[i%a]++;
				if (temoti[i%a] == b) { cout << i % a << endl; break; }
			}
			else {
				ba = temoti[i%a];
				temoti[i%a] = 0;
			}
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int64_t i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)

void solve()
{
    int n,p;
    while(cin>>n>>p,n|p){
        int now=0,tmp=p;
        vector<int>s(n,0);
        while(1){
            if(tmp==0&&s[now]==p){
               cout<<now<<endl;break;
            }
            else if(tmp>0)tmp--,s[now]++;
            else if(tmp==0)tmp=s[now],s[now]=0;
            now++;
            now%=n;
        }
    }
}

int main() 
{
    cin.tie(0);
	ios::sync_with_stdio(false);
    solve();
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <iomanip>
#include <vector>
#include <queue>



using namespace std;



int main()
{
	int n, p, wan, temoto[50];
	
	while (cin >> n >> p)
	{
		if (n == 0 && p == 0) break;
		
		//wan, temotoの初期化
		wan = p;
		for (int i=0; i<n; i++) temoto[i] = 0;
		
		for (int i=0; i<1000000; i++)
		{
			if (wan >= 2)
			{
				wan--;
				temoto[i % n]++;
			} else if (wan == 0) {
				wan = temoto[i % n];
				temoto[i % n] = 0;
			} else if (wan == 1) {
				bool flag = true;		//forの後trueならその人の勝ち
				for (int j=0; j<n; j++)
				{
					if (j == i % n) continue;
					if (temoto[j] != 0) flag = false;
				}
				if (flag)
				{
					cout << i % n << endl;
					break;
				} else {
					wan--;
					temoto[i % n]++;
				}
			}
		}
	}
	
	return 0;
}

C++: #include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<deque>
#include<regex>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<iomanip>

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

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);

int main() {
    int n,k;
    while(true){


        cin>>n>>k;
        if(n==0&&k==0)break;
        deque<pair<int,int>> q;
        for(int i=0;i<n;i++){
            q.push_back({i,0});
        }


        int cha=k;
        while(true){
            pair<int,int> pl;
            pl=q.front();
            q.pop_front();
            if(pl.second==k){
                if(q.front().first!=0){
                    cout<<q.front().first-1<<endl;
                }else{
                    cout<<n-1<<endl;
                }
                break;
            }
            if(cha==0){
                if(pl.second>0){
                    cha+=pl.second;
                    pl.second=0;
                    q.push_back(pl);
                }else{
                    q.push_back(pl);

                }
            }else{
                cha--;
                pl.second++;
                q.push_back(pl);
            }


        }



    }



    return 0;
}