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>

using namespace std;

int n[55]={};
int p[55] = {};
int have[55] = {};
int winner[100]={};
int judge=0;

int main()
{
    int i=0;
    while(1){
        scanf("%d %d",&n[i],&p[i]);
        if(n[i]==0 && p[i] == 0){
            break;
        }
        i++;
    }
    int times = i;

    for(i=0;i<times;i++){
        int pebble = p[i];
        for(int j=0;j<n[i];j++){
            have[j]=0;
        }
        while(1){
            for(int j=0;j<n[i];j++){
                if(pebble != 0){
                    have[j]++;
                    pebble--;
                    if(have[j] == p[i]){
                        winner[i] = j;
                        judge = 1;
                        break;
                    }
                }else{
                    pebble += have[j];
                    have[j] = 0;
                }
            }
            if(judge == 1){
                cout << winner[i] << endl;
                judge = 0;
                break;
            }
        } 
    }

    return 0;
}

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

using namespace std;

int main(){
    int n,p;
    while(cin>>n>>p,n){
        vector<int> a(n,0);
        int sum=p;
        for(int i=0;i<1e7;i++){

            if(sum!=0)
                a[i%n]++,sum--;
            else if(a[i%n]==p){
                cout<<i%n<<endl;
                break;
            }
            else{
                sum+=a[i%n];
                a[i%n]=0;
            }
        }

    }
    return 0;
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
using namespace std;
using LL = long long;
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
const double PI = acos(-1);

int main() {
	int n, p;
	while (cin >> n >> p&&n&&p) {
		int wan = p;
		int maxl = 0;
		int count[50] = {};
		while (maxl != p) {
			for (int i = 0; i < n; i++) {
				maxl = 0;
				if (wan != 0) {
					wan--;
					count[i]++;
				}
				else{
					wan = count[i];
					count[i] = 0;
				}
				for (int j = 0; j < n; j++) {
					maxl = max(maxl,count[j]);
				}
				if (maxl == p)break;
			}
		}
		for (int i = 0; i < n; i++) {
			if (count[i] == p)cout << i << endl;
		}
	}
	
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
using namespace std;
const int INF = 1e9;

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

        vector<int> cnt(n);
        int man = 0, now = p;
        while (1) {
            if (now == 0) {
                now = cnt[man];
                cnt[man] = 0;
            } else {
                ++cnt[man];
                --now;
            }
            if (cnt[man] == p) {
                cout << man << "\n";
                break;
            }

            man = (man + 1) % n;
        }
    }

    return 0;
}

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

#define ll long long int
#define ld long long double
#define reps(i,f,n) for(int i=f;i<n;i++)
#define rep(i,n) reps(i,0,n)
#define m1 cout<<"move1"<<endl
#define m2 cout<<"move2"<<endl
#define m3 cout<<"move3"<<endl
#define m4 cout<<"move4"<<endl


int main(){
  vector<int> v(100);
  ll P;
  ll N;
  

  while(cin>>N>>P,N,P){
    ll ans = P;
    ll ans_ind = -1;
    rep(i,N) v[i] = 0;
    while(1){
      rep(i,N){
	if( P == 0){
	  P = v[i];
	  v[i] = 0;
	}
	else{
	  v[i]++;
	  P--;
	}
	if(v[i] == ans){
	  ans_ind = i;
	  break;
	}
      }
      if(ans_ind != -1){
	cout<<ans_ind<<endl;
	break;
	  }
    }
  }
  return 0;
}

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

int main(void){
    int n, p;
    int cand[51];
    while(cin >> n >> p && n && p) {
        int now = 0;
        rep(i, 51) cand[i] = 0;
        while(true) {
            if(p == 1) {
                bool end = true;
                rep(i, n) {
                    if(i != now && cand[i] != 0) {
                        end = false;
                        break;
                    }
                }
                if(end) break;
            }
            if(p > 0) {
                cand[now]++;
                p--;
            } else {
                p = cand[now];
                cand[now] = 0;
            }
            now = (now == n-1 ? 0 : now+1);
        }
        cout << now << endl;
    }
}

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

int cnt[50];
int main() {
	int n, p;
	while (scanf("%d%d", &n, &p), n) {
		int k = p;
		memset(cnt, 0, sizeof(cnt));
		for (int i = 0;; i = (i + 1) % n) {
			if (k == 1 && cnt[i] == p - 1) {
				printf("%d\n", i);
				break;
			}
			if (k) { cnt[i]++; k--; }
			else { k = cnt[i]; cnt[i] = 0; }
		}
	}
}

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

using namespace std;

bool ok(vector<int> &K, int now) {
    if (now != 0) return false;
    int cnt = 0;
    for(int i = 0; i < K.size(); i++) {
        if (K[i] != 0) cnt++;
    }
    return cnt == 1;
}

void solve(int N, int P) {
    vector<int> K(N);
    int now = P;
    int now_i = 0;
    while(!ok(K, now)) {
        if (now == 0) {
            now = K[now_i];
            K[now_i] = 0;
        } else {
            K[now_i]++;
            now--;
        }
        now_i = (now_i + 1) % N;
    }
    cout << (now_i + N - 1) % N << endl;
}

int main() {
    int N, P;
    while(cin >> N >> P, N) {
        solve(N, P);
    }
    return 0;
}

C++: 
#include <cstdio>

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

        }
    }
}

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

int n, p;
int d[50], wan, now;

bool isEnd() {
  int counter = 0;
  for (int i = 0; i < n; i++) if(d[i] > 0) counter++;
  return counter == 1;
}

int main() {
  while (1) {
    scanf("%d%d", &n, &p);
    if (n == 0) break;
    for (int i = 0; i < n; i++) d[i] = 0;
    wan = p, now = n - 1;
    while (!isEnd()) {
      wan += d[now];
      d[now++] = 0;
      if (now == n) now = 0;
      for (int i = 0; i < n; i++) d[i] += wan / n;
      wan %= n;
      for (; wan > 0; wan--) {
        d[now++]++;
        if (now == n) now = 0;
      }
    }
    for (int i = 0; i < n; i++) if (d[i] > 0) {
      printf("%d\n", i);
      break;
    }
  }
}