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++: // nextmayor.cpp
#include <iostream>
using namespace std;
int next_mayor(int n,int p);
int main()
{
	int n;//????£???? 3 ~ 50
	int p;//?°????????????° 2 ~ 50
	while(true){
	cin >> n >> p;
	if(n==0 && p==0){return 0;}
	cout << next_mayor(n,p) << endl;
	}
	cin.get();
	cin.get();
	return 0;
}
int next_mayor(int n,int p){
	int temoto[50];
	int ishi_all = p;
	for(int syoki=0;syoki<50;syoki++){
		temoto[syoki]=0;//???????????????
	}
	while(true){
		for(int i=0;i<n;i++){
			if(p>0){//?¢???????1??\???
				temoto[i] += 1;//n??????????????????????¢???????
					p -= 1;//?¢?????????????????????????
					if(p==0){//????????????????????´??????????????????
						if(temoto[i] == ishi_all){return i;}
				}
			}else if(p==0){//?¢????????????????
				p = temoto[i];
				temoto[i] = 0;
			}
		}
	}
}

C++: #include <iostream>

using namespace std;

int main(){
	int n,p;
	while(1){
		cin>>n>>p;
		if(n==0&&p==0){
			break;
		}
		int sum=p;
		int cnt=0;
		int *data;
		int state=0;
		data = new int[n];
		for(int i=0;i<n;i++){
			data[i]=0;
		}
		while(1){
			
			if(sum==0){
				sum+=data[cnt];
				data[cnt]=0;
			}
			else{
				data[cnt]++;
				sum--;
				if(sum==0){
					int flag=0;
					for(int i=0;i<n;i++){
						if(i==cnt)continue;
						else{
							if(data[i]>0){
								flag=1;
							}
						}
					}
					if(flag==0){
						cout<<cnt<<endl;
						state=2;
					}
				}
			}
			cnt++;
			cnt%=n;
			if(state==2)break;
		}
	}
	return 0;
}

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

int main(){
	int n, p;
	int stone[55];
	long long int count = 0;
	while(cin >> n >> p, n){
		fill(stone, stone+50, 0);
		for(count = 0; ; count++){
			if(p != 0){
				if(p == 1){
					bool isExist = false;
					for(int i=0; i < n; i++){
						if(i == count%n) continue;
						else if(stone[i] != 0) isExist = true;
					}
					if(!isExist) break;
				}
				stone[count%n]++;
				p--;
			}else{
				p += stone[count%n];
				stone[count%n] = 0;
			}
		}
		cout << count%n << endl;
	}
}

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

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

Int INF = 1e18;
Int mod = 1e9+7;
//Int mod = 998244353;

int main() {
    vector<Int> res;
    while (true) {
        Int n, p;
        cin >> n >> p;
        if (n == 0) break;
        vector<Int> v(n, 0);
        Int bowl = p;
        Int step = 0;
        while (true) {
            if (bowl != 0) {
                v[step]++;
                bowl--;
            }
            else {
                bowl += v[step];
                v[step] = 0;
            }
            if (v[step] == p) break;
            step = (step + 1) % n;
        }
        res.push_back(step);
    }
    for (Int i = 0; i < res.size(); i++) {
        dump(res[i]);
    }
    return 0;
}

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;
			std::cout << a[i];
		}
		std::cout << std::endl;
		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>

int main(){
    int n,p;
    while(true){
        scanf("%d%d",&n,&p);
        if(n == 0 && p == 0){
            break;
        }
        int can[n];
        int q = p;
        for(int i = 0;;i++){
            int k =i % n;
            if(q != 0){
                can[k]++;
                q--;
                if(can[k] == p){
                    printf("%d",k);
                    break;
                }
            }
            else{
                q = can[k];
                can[k] = 0;
            }

        }
    }
}

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

int main() {
	int n, p;

	while (1) {
		int flag = 0;
		int player[50] = { 0 };
		cin >> n >> p;
		if (n == 0 && p == 0) break;
		int p_max = p;

		while (1) {
			for (int j = 0; j < n; j++) {
				if (p != 0) player[j] += 1, p--;
				else p = player[j], player[j] = 0;
				if (player[j] == p_max) {
					flag = -1;
					cout << j << endl;
					break;
				}
			}
			if (flag == -1) break;
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p){
		bool f=true;
		int d[55]={};
		int cn=p;
		if(n==0)break;
		while(f){
			for(int i=0;i<n;i++){
				if(cn>0){
					d[i]++;
					cn--;
				}
				else if(cn==0){
					cn=d[i];
					d[i]=0;
				}
				if(d[i]==p){
						cout<<i<<endl;
						f=false;
						break;
				}
			}
		}
	}
	return 0;
}

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

int main(){
	int n, p;
	while(cin >> n >> p, n || p ){
		int person[1000] = {0};
		int index = 0, owan = p, ans;
		while( true ){
			if( owan > 0 ){
				owan--;
				person[index]++;
			}else{
				owan += person[index];
				person[index] = 0;
			}
			if( owan == 0 && person[index] == p ){
				ans = index;
				break;
			}
			index++;
			if( index >= n ) index = 0;
		}
		cout << ans << endl;
	}
}

C++: #include "bits/stdc++.h"

using namespace std;

#define ll long long
#define vi  vector<int>
#define vvi vector<vi>
#define pi  pair<int,int>
#define mp  make_pair
#define pb  push_back
#define MOD 1e9 + 7
#define PAI  3.1415
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
#define pr(x) cout << x << endl
#define Endl endl
#define rep(i,n) for(int i = 0 ; i < n; i++)

const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
const int ddx[8] = {-1,0,1,-1,1,-1,0,1};
const int ddy[8] = {-1,-1,-1,0,0,1,1,1};
const int inf = 99999999;
const ll linf = 1LL << 62;

ll gcd(ll a,ll b){
  if(a < b)swap(a , b);
  if(a % b == 0) return b;
  else gcd(b, a%b);
}

ll lcm(ll a,ll b){
  if(a < b)swap(a , b);
  return (a/gcd(a , b))*b;
}

struct UnionFind {
  vi par; //par[i] : iの親番号（例）par[3] = 2 : 3の親は2

  UnionFind(int N) : par(N) { //最初は全てが根として初期化
    rep(i,N) par[i] = i;
  }

  int root(int x){ //データxが属する木の根を再帰で得る : root(x) = {xの木の根}
    if(par[x] == x)return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y){ //xとyの木を併合
    int rx = root(x); //xの根をrx
    int ry = root(y); //yの根をry
    if(rx == ry)return; //xとyの根が同じ
    par[rx] = ry; //xとyの根が同じでない時、xの根rxをyの根にryにつける。
  }

  bool same(int x, int y){//2つのデータx，yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main(){

  int n,p;

  while(cin >> n,n){
    cin >> p;
    int a = p;
    vi v(n,0);
    int pos = 0;

    while(1){
      if(p > 0){
        ++v[pos];
        --p;
      }
      else{
        p += v[pos];
        v[pos] = 0;
      }

      if(v[pos] == a){
        cout << pos << endl;
        break;
      }
      pos = (pos+1) % n;
    }
  }
  return 0;

}