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 <unistd.h>

using namespace std;

#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
#define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}


typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1000000007;
//const ll MOD = 998244353;
const double pi = 3.14159265358979323846;
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { 1,0, -1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
//#define mp make_pair


int main() {
  while (true) {
    int n, r;
    cin >> n >> r;
    if (n == 0) return 0;
    vi a(n);
    int i = 0;
    int now = r;
    while (true) {
      if (now > 0) {
        now--;
        a[i]++;
        if (a[i] == r) {
          cout << i << endl;
          break;
        }
      }
      else {
        now += a[i];
        a[i] = 0;
      }
      i++;
      i %= n;
    }
  }
}

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

int main(){
    int n,p;
    while(cin >> n >> p, n|p){
        int index = 0;
        int perNum[n] = {};
        int inNum = p;
        bool isEmpty = false;
        int ans;
        while(!isEmpty){
            if(inNum){
                inNum--;
                perNum[index]++;
            }else{
                inNum = perNum[index];
                perNum[index] = 0;
            }

            bool turnIsEmpty = true;
            for(int i =0; i < n; i++){
                if(perNum[i]) 
                    turnIsEmpty = false;
            }
            if(turnIsEmpty){
                isEmpty = true;
                ans = index;
            }

            if(index == n-1){
                index = 0;
            }else{
                index++;
            }
        }

        cout << ans << "\n";
    }
}

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

#define rep(i,j,k) for(int i=j;i<k;i++)
int main(void){
    while(1){
    int n,p,index=0,wan;
    cin>>n>>p;
    if(n==0 && p==0)break;
    vector<int> v(n,0);
    wan=p;
    
    while(1){
       index++;
       if(index>n-1)index=0;
       
       if(wan!=0){
           v[index]++;
           wan--;
           if(wan==0){
               bool judge=true;
               rep(i,0,n){
                   if(v[i]!=0 && i!=index){
                       judge=false;
                       break;
                   }
               }
               if(judge){
                   cout<<(((index-1)>=0)?(index-1):(n-1))<<endl;
                   break;
               }
           }
       }else {
           wan=v[index];
           v[index]=0;
       }
    }
    }
    
    return 0;
}

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

using namespace std;

int main()
{
  int n, p;
  while(cin >> n >> p, n || p){
    int wan = p, te[50] = {0};
    for(int s = 0; s < 10000000; s++){
      if(wan > 0){
	te[s%n]++, wan--;
	if(wan == 0 && te[s%n] == p){
	  cout << s%n << endl;
	  break;
	}
      } else {
	wan = te[s%n], te[s%n] = 0;
      }
    }
  }
  return 0;
}

C++: #include<iostream>

int main(){

	while (true){
		int n, base, p;
		int cnt[51] = { 0 };

		std::cin >> n >> base;

		if (n == 0 && base == 0)break;

		p = base;

		int i = 0;
		while (true){
			if (i == n)i = 0;

			if (p == 0){
				p += cnt[i];
				cnt[i] = 0;
			}
			else {
				p--;
				cnt[i]++;
				if (cnt[i] == base)break;
			}

			i++;
		}

		std::cout << i << std::endl;
	}

	return 0;
}

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

using namespace std;

int n, p;
int peb[55];

void solve() {
	int c = 0;
	int p_ = p;
	
	while (1) {
		if (p_ == 0) {
			p_ += peb[c];
			peb[c] = 0;
			c++;
		} else {
			peb[c]++;
			if (peb[c] == p) break;
			p_--;
			c++;
		}
		
		c %= n;
	}
	
	cout << c << endl;
}

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

C++: #include <bits/stdc++.h>
#define cinf(n,x) for(int i=0;i<(n);i++) cin >> x[i];
typedef long long int ll;
using namespace std;

int main(){
	int n,p;
	int have[50];
	while(true){
		cin >> n >> p;
		if(n==0 && p==0) break;
		for(int i=0;i<n;i++) have[i]=0;
		int turn=0;
	    int cnt=p;
		while(true){
			if(cnt>0){
				cnt--;
				have[turn%n]++;
				if(have[turn%n]==p){
					cout << turn%n << endl;
					break;
				}

			}
			else{
					cnt =have[turn%n];
					have[turn%n]=0;
				}
			turn++;

		}

	}
}

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

int main(){
	int mayors[50]{0};
	vector<int> ansvec;

	for(;;){
		int n, p;
		cin >> n >> p;
		if (n == 0) break;

		for (int i = 0; i < n; i++) mayors[i] = 0;
		int bowlp = p;
		int turn = 0;
		for(;;){
			if (bowlp == 1 && mayors[turn] == p - 1) break;
			else if (bowlp == 0){
				bowlp += mayors[turn];
				mayors[turn] = 0;
			}
			else{
				mayors[turn]++;
				bowlp--;
			}

			if (turn == n - 1) turn = 0;
			else turn++;
		}
		ansvec.push_back(turn);
	}

	for (int ans : ansvec){
		cout << ans << endl;
	}
	return 0;
}