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 main(){
    while(1){
        int stone[50]={},p,n,flag,winer=-1;
        cin >> n >> p;
        if(n==0&&p==0)  break;
        while(winer<0){
            for(int i=0;i<n;i++){
                flag = 0;
                for(int j=0;j<n;j++){
                    if(stone[j]!=0&&j!=i){
                        flag=1;
                        break;
                    }
                }
                
                if(p==1&&flag==0){
                    winer = i;
                    break;
                }
                else if(p==0) {
                    p+=stone[i];
                    stone[i]=0;
                }
                else{
                    p--;
                    stone[i]++;
                }
            }
        }
        cout << winer << "\n";
    }
}

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

using namespace std;

bool check(const vector<int> &v){
  bool fg = true;
  for(int i=0;i<v.size();i++){
    fg &= v[i]==0;
  }
  return fg;
}

int main(void){

  int n,p;
  while(cin >> n >> p, n|p){
    vector<int>v(n,0);

    int k;
    for(k=0;;k=(k+1)%n){
      if(p>0)v[k]++,p--;
      else p+=v[k],v[k]=0;
      if(check(v))break;
    }
    cout << k << endl;
  }
  
  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 = n % i;
            if(q != 0){
                can[k]++;
                q--;
                if(can[k] == p){
                    printf("%d",k);
                    break;
                }
            }
            else{
                q = can[k];
                can[k] = 0;
            }

        }
    }
}

C++: /*

 *
 *  Created on: 2017/05/27
 *      Author: aihara
 */

#define MYDEBUG
#include <bits/stdc++.h>

#ifdef MYDEBUG
#define dbp(x) cout << "#x" << ": " << x << endl;
#else
#define dbp(x)
#endif
#define ll long long
using namespace std;

const int MAX_N = 60;
int per[MAX_N];
int main() {
	int n, p, winner, box;
	while (cin >> n >> p, n || p) {
		box = p;
		for (int i = 0; i < MAX_N; ++i) {
			per[i] = 0;
		}

		while (true) {
			bool finish = false;
			for (int i = 0; i < n; ++i) {
				if (per[i] != 0 && box == 0) {
					box = per[i];
					per[i] = 0;
				} else if (per[i] == 0 && box != 0) {
					per[i]++;
					box--;
				} else if (per[i] == 0 && box == 0) {
					continue;
				} else {
					per[i]++;
					box--;
				}
				if (per[i] == p) {
					winner = i;
					finish = true;
					break;
				}
			}
			if (finish) {
				cout << winner << endl;
				break;
			}
		}
	}
	return 0;
}

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

using namespace std;

int main(){
	
	while(1){
		int n,p;
		
		cin >> n >> p;
		
		if(n==0&&p==0){
			return 0;
		}
		
		int play[n]={};
		
		int t=p;
		int i=0;
		
		while(1){
			if(t!=0){
				play[i] += 1;
				t--;
				if(t==0 && play[i]==p){
					cout << i << endl;
					break;
				}
				i++;
			}else{
				t += play[i];
				play[i] = 0;
				i++;
			}
			if(i==n)
				i=0;
		}
	}
}

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

using namespace std;
static const int MAXV = 1000;

int main() {
  while(1) {
  int n, m, p, M[MAXV]={}, i=0;
  scanf("%d %d", &n, &m);
  if (n==0 && m==0) break;
  p=m;
  while(1) {
    //printf("%d   %d   %d\n", M[0], M[1], M[2]);
    if (p == 0) {
      p=M[i]; M[i]=0;
    } else{
      M[i]++; p--;
    }
    if(M[i] == m) break;
    i++;
    if(i == n) i=0;
  }
  printf("%d\n", i);
  }
}

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

int main(){
int n,p;
int c[50];
for(;;){
cin >> n >> p;
if(!n && !p)break;
for(int i=0;i<n;i++)c[i] = 0;

int cnt = 0;
int pos = -1;
while(cnt!=1 || p){
pos = (pos+1)%n;
if(p){
if(!c[pos])cnt++;
c[pos]++;
p--;
}else{
if(!c[pos])
p = c[pos];
c[pos] = 0;
}
pos = (pos+1)%n;
}
cout << pos << endl;
}
}

C++: #include <iostream>
using namespace std;
int main(){
  int n,p,data[50],i,d,result;
  while(1){
    cin >> n >> p;
    d=p;
    if(n==0&&p==0) break;
    for(i=0;i<n;i++) data[i]=0;
    i=0;
    while(1){
      if(p>0){
    p--;
        data[i]++;
    if(data[i]==d){
      result=i;
      break;
    }
      }else if(p==0){
        p=data[i];
        data[i]=0;
      } 
      if(i+1==n) i=0;
      else i++;
    }
    cout << result << endl;
  }
  return 0;
}

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

using namespace std;

int main(){
	while(1){
		int n, p; cin >> n >> p;
		if(!n && !p)break;
		
		int i=0, num=p;
		vector <int> count(n);
		while(1){
			if(num==0){
				num = count[i];
				count[i] = 0;
			} else{
				count[i]++;
				num--;
				if(count[i] == p) break;
			}
			i++;
			if(i >= n) i=0;
		}
		cout << i << endl;
	}
	return 0;
}

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

using namespace std;

int game(int num, int stone)
{
        int i = 0, in_the_box = stone;
        vector<int> mayor(num, 0);
        while(1){
                if(in_the_box != 0){
                        mayor[i] += 1;
                        in_the_box -= 1;
                        if(mayor[i] == stone){
                                break;
                        }
                }else if(mayor[i] != 0){
                        in_the_box = mayor[i];
                        mayor[i] = 0;
                }

                i = (i + 1) % num;
        }
        return i;
}

int main(void)
{
        int i, n, p, next_mayor;

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

                next_mayor = game(n, p);
                cout << next_mayor << endl;
        }
        return 0;
}

C++: #include <iostream>
using namespace std;
int main(void) {
  int n, p;
  while(cin >> n >> p, n+p) {
    int st[64] = {};
    int b = p;
    for(int i = 0; ; i = (i + 1) % n) {
      if(b == 1) {
        bool f = true;
        for(int j = 0;j < n; j++) {
          if(st[j] != 0 && j != i) {
            st[i] += b--;
            f = false;
            break;
          }
        }
        if(f) {
          cout << i << endl;
          break;
        }
      }
      else if(b) st[i]++, b--;
      else b += st[i], st[i] = 0;
    }
  }
}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<ll,int> P;
typedef pair<ll,P> PP;

//-----------------------------------------------------------------------------


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

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

	return 0;
}