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 <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 << "\n";
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cctype>
#include<climits>
#include<bitset>
#define ld long double
#define ll long long int
#define ull unsigned long long int
#define rep(i,n) for(i=0;i<n;i++)
#define fin(ans) cout<<(ans)<<endl
#define INF INT_MAX
#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define vvc vector<vector<char> >
#define vvs vector<vector<string> >
#define P 1000000007
using namespace std;
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
const long long LLINF=1LL<<60;
//set<int>::iterator it;
//|_
//g++ -std=c++1z temp.cpp
//./a.out

int main(void){ios::sync_with_stdio(false);cin.tie(0);
//////////////////////////////////////////////////////
	
    int n,p,i,j,ans;
    bool ch;
    while(1){
        cin>>n>>p;
        if(n==0&&p==0)break;
        
        vector<int> v(n,0);
        j=0;
        while(1){
            j%=v.size();
            if(p>0){
                v[j]++;
                p--;
            }
            else{
                p=v[j];
                v[j]=0;
            }
            ch=false;
            ans=-1;
            if(p==0){
                rep(i,v.size()){
                    if(ans==-1&&v[i]>0){
                        ans=i;
                        ch=true;
                    }
                    else if(v[i]>0){
                        ch=false;
                        break;
                    }
                }
                if(ch){
                    
                    fin(ans);
                    break;
                }
            }
            j++;
        }
    }
    
//////////////////////////////////////////////////////
return 0;}

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

using namespace std;

int main()
{
    int n, p;
    while (cin >> n >> p, n) {
        int m = 0, q = p, st[50] = {};
        while (1) {
            if (p == 0) {
                p += st[m];
                st[m] = 0;
            } else {
                p--;
                st[m]++;
                if (p == 0 && st[m] == q) {
                    break;
                }
            }
            m = (m + 1) % n;
        }
        cout << m << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>

#define REP(i,n) for(int i=0;i<n;++i)
#define RREP(i,n) for(int i=n-1;i>=0;--i)
#define FOR(i,m,n) for(int i=m;i<n;++i)
#define RFOR(i,m,n) for(int i=m-1;i>=n;--i)
#define ALL(v) v.begin(),v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007

using namespace std;


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, p;
	while (1) {
		cin >> n >> p;
		int* m = new int[n];
		for (int i = 0;;++i) {
			int j = i%n;
			if (p == 0) {
				p += m[j];
				m[j] = 0;
			}
			else {
				p--;
				m[j]++;
				if (p == 0) {
					int flag = 1;
					for (int k = 0;k < n;++k) {
						if (m[k] !=0&&k!=j)flag = 0;
					}
					if (flag) {
						cout << j << endl;
						break;
					}
				}
			}
			
		}
		
	}
	return 0;
}

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

int s[50];

int main(){
  int n, p;
  
  while(cin >> n >> p, n * p){
    for(int i = 0; i < n; i++) s[i] = 0;
    int ans = 0, sum = p;
    int c = 0;
    while(1){
      if(sum == 0){
	sum += s[c];
	s[c] = 0;
      }
      else{
	sum--;
	s[c]++;
	if(s[c] == p) break;
      }
      c = (c + 1) % n;
      ans++;
    }
    cout << c << endl;
  }
}

C++: null

C++: #include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<stack>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define REEP(i, N) for(ll i = N - 1; i >= 0; --i)
#define FOR(i,m,n) for(ll i = m; i < n; ++i)
#define RFOR(i,m,n) for(ll i = n - 1; i >= m; --i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define PB push_back
#define KIRIAGE(a, b) a += (b - a % b) % b // a ??? b ????????°????????§?¢???????
#define CEIL(c, a, b) if(a % b == 0) { c = a / b;} else { c = a / b + 1;} // a / b ??\??????????°???´??°
#define sp(n) setprecision(n)
#define INF 1000000000
#define MOD 1000000007
 
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, 1, 0, -1};
int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
int main(void) {
    while(true) {
        int n, r;
        cin>>n>>r;
        if(n==0&&r==0) return 0;
        vector<int> c(n, 0);
        int hav = 0;
        int pos = 0;
        int winner = -1;
        while(true) {
            if(r > 0) {
                ++c[pos];
                if (c[pos] == 1) ++hav;
                --r;
                if(r == 0 && hav == 1) {
                    winner = pos;
                    break;
                }
            } else {
                if(c[pos] != 0) --hav;
                r = c[pos];
                c[pos] = 0;
            }
            if(pos < n - 1) ++pos;
            else pos = 0;
        }
        cout<<winner<<endl;
    }
}

C++: #include<stdio.h>

int main(){
	int n,t=0,a[64],k,s,i,j;
	while(1){
		scanf("%d %d",&n,&s);
		if(n==0&&s==0)break;
		k=s;
		for(i=0;i<64;i++)a[i]=0;
		for(i=0;i<n;i=(i+1)%n){
			//for(j=0;j<n;j++)printf("%d ",a[j]);
			//printf("\n");
			if(k){
				a[i]++;
				k--;
			}else if(a[i]){
				k=a[i];
				a[i]=0;
			}
			for(t=0,j=0;j<n;j++)if(!a[j])t++;
			if(t==n-1&&a[i]==s)break;
		}
		printf("%d\n",i);
	}	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main() {
	int n,m;
	while(cin>>n>>m,n,m){
		int num[50]={},now=0,storn=m;
		while(1){
			if(now==n)now=0;
			if(storn>0){
				num[now]++;
				storn--;
			}
			else if(num[now]>0){
				storn=num[now];
				num[now]=0;
			}
			if(num[now]==m){
				cout<<now<<endl;
				break;
			}
			now++;
		}
	}
}