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>

using namespace std;

int player[55];

int main(void){
	int n,p;
	while(cin >> n >> p){
		if(n==0&&p==0)
			break;
		for(int i=0;i<n;++i) player[i]=0;

		int cur=0;
		int ans=-1;
		int center=p;
		while(ans==-1){
			if(center){
				player[cur]++;
				center--;
			}else{
				center+=player[cur];
				player[cur]=0;
			}
			cur=(cur+1)%n;
			for(int i=0;i<n;++i)
				if(player[i]==p)
					ans=i;
		}
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

int main()
{
    int n, p;
    
    while (scanf("%d %d", &n, &p) && n){
        int a = p, b[50] = {0}, c = 0;
        while (1){
            if (a){
                b[c]++;
                a--;
            }
            else {
                a += b[c];
                b[c] = 0;
            }
            if (b[c] == p){
                printf("%d\n", c);
                break;
            }
            c = (c + 1) % n;
        }
    }
    return (0);
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <limits>
#include <cmath>
#include <cassert>

using namespace std;
using ll = long long;

const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, p;
    while(cin >> n >> p, n or p)
    {
        int num = p;
        vector<int> v(n, 0);
        int pos = 0;
        while(true)
        {
            if(num > 0)
            {
                v[pos]++;
                num--;
            }
            else
            {
                num = v[pos];
                v[pos] = 0;
            }
            if(v[pos] == p) break;
            pos = (pos + 1) % n;
        }
        cout << pos << endl;
    }
    return 0;
}

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

using namespace std;

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

    int n,p;
    while(cin>>n>>p,n&&p){
        int idx=0;
        vector<int> stones(n,0);
        int cup=p;
        while(true){
            if(cup){
                stones[idx]++;
                cup--;
            }
            else{
                cup+=stones[idx];
                stones[idx]=0;
            }
            if(stones[idx]==p) break;
            idx++;
            idx%=n;
        }
        cout<<idx<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i=0;i<n;++i)
#define FOR(i, a, b) for(int i=a;i<b;++i)
#define RFOR(i, a, b) for (int i=a;i>=b;--i)
#define ll long long
#define all(a) a.begin(),a.end()
#define ull unsigned long long
#define pb(x) push_back(x)
#define dbg(x) cout<<#x<<" : "<<(x)<<endl;
using namespace std;
const int INF = 1500000000;
const double PI = 3.14159265359;
const int mod = 1000000000 + 7;

const int MAX_N = 50, MAX_P = 50;
int n, p, res, v[MAX_N];

void solve() {
  memset(v, 0, n);
  int c = 0;
  while(true) {
    if (p == 0) {
      p = v[c];
      v[c] = 0;
    } else {
      p--;
      v[c]++;
      if (p == 0) {
        bool flg = true;
        rep(i, n) if (i != c && v[i] > 0) flg = false;
        if (flg) break;
      }
    }
    c = (c == n-1) ? 0 : c + 1;
    res = c;
  }
}

int main() {
  while(true) {
    cin >> n >> p;
    if (!n && !p) break;
    solve();
    cout << res << endl;
  }
  return 0;
}

C++: #include<iostream>

using namespace std;

int n, p, stone_left, P[50], winner;

int main(){
    while(cin >> n >> p && n*p!=0){
        stone_left = p;
        winner = n;
        for (int i = 0; i < n; i++) {
            P[i] = 0;
        }
        while(1){
            for(int i = 0; i < n; i++){
                if(stone_left != 0){
                    P[i] += 1;
                    stone_left -= 1;
                    if(P[i] == p) {
                        winner = i;
                        cout << winner << endl;
                        break;
                    }
                    if(winner != n) break;
                } else {
                    stone_left = P[i];
                    P[i] = 0;
                }
            }
            if(winner != n) break;
        }
    }
}

C++: #include <iostream>
//#include <algorithm>
//#include <vector>
//#include <stack>
//#include <queue>
//#include <map>
//#include <cmath>
//#include <string>
//#include <sstream>
//#include <iomanip>
//#include <complex>
using namespace std;
 
//#define ll long long
//#define vvi vector< vector<int> >
//#define All(X) X.begin(),X.end()
//#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
//#define pb push_back 
//ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
 
int main(){
  while(1){
    int n, p;
    cin >> n >> p;
    if(n==0&&p==0) break;
    int kago=p;
    int pc[n-1];
    int i;
    while(1){
      int k=i%(n-1);
      if(pc[k]==p){
	cout << k << endl;
	break;}
      if(kago>0){
	kago--;
	pc[k]++;}
      else{
	kago=pc[k];
	pc[k]=0;}
      i++;}
  }

  return 0;
}

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

int main(void){
  while(1){
    int stone[50];
    for(int k = 0 ; k < 50 ; k++){stone[k] = 0;}

    int bowl;
    int n, p;  // n：候補者人数，p：小石の総数
    cin >> n;
    cin >> p;

    if(n == 0 && p == 0){return 0;}

    bowl = p;

    bool flg = true;
    int ans;

    for(int i = 0 ; flg ; i = (i+1) % n){

      if(bowl == 0){
        bowl = bowl + stone[i];
        stone[i] = 0;
      }else{
        stone[i]++;
        bowl--;
        if(bowl == 0 && stone[i] == p){ans = i; flg = false;}
      }
    }

    usleep(1000000);

    cout << ans << endl;
  }
}

C++: #include <cstdio>

using namespace std;

int main(void)
{
    for (;;) {
        int n, p;
        int m[50] = {0}, k, t;

        scanf("%d %d", &n, &p);
        if (n == 0 && p == 0)
            break;

        k = p;
        for (t = 0;; t = (t + 1) % n) {
            if (k == 0) {
                k = m[t];
                m[t] = 0;
            } else {
                --k;
                ++m[t];
                if (m[t] == p)
                    break;
            }
        }

        printf("%d\n", t);
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  long long int n=0,p=0,count=0,answer=0;



while(1){
cin >> n >> p;
vector<int> v(n,0);
if(n==0&&p==0)
  return 0;
  while(1){
    for(int i=0;i<n;i++){
      if(p==0){

        if(v[i]!=0){
          p+=v[i];
          v[i]=0;
          count=0;
        }else{
          count++;
        }

      }else{
        v[i]++;
        p--;
        count=0;
      }
      if(count==n-1){
      //  cout << count << endl;
        break;
      }
    }if(count==n-1){
    //  cout << count << endl;
      break;
    }


  }


  for(int i=0;i<n;i++){
    if(v[i]!=0){
      answer = i;
    }
  }

  cout<<answer<<endl;
}
}