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<algorithm>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<string>
#include<random>
#include<map>
#include<queue>
using namespace std;

int main(){

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

    long long int step = 0;
    vector<int> a(n, 0);
    int tmp = p;
    while(true){
      step %= n;
      if( p != 0 ){
        p--;
        a[step]++;
      }else{
        if( a[step] != 0 && a[step] != tmp ){
          p += a[step];
          a[step] = 0;
        }else if(a[step] == tmp){
          cout << step << endl;
          break;
        }
      }
      step++;
    }
  }

  return 0;
}

C++: #include <iostream>

using namespace std;

int main(){
  while(1){
    int n ,p;
    cin >> n >> p ;
    if(n==0&&p==0)break;
    int N[50]={0};
    int T;
    T=p;
    int mayor=-1;
    while(1){
      for(int i=0;i<n;i++){
        if(T==0){
          T=N[i];
          N[i]=0;
        }else{
          N[i]++;
          T--;
          if(N[i]==p){
            mayor=i;
            break;
          }
        }
      }
      if(mayor!=-1)break;
    }
    cout << mayor << endl;
  }
}

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

int main() {
        int i, j;
        while (true) {
                int n, p;
                cin >> n >> p;
                if (n == 0 && p == 0) break;
                int wan = p;
                int turn = 0;
                vector<int> have(n, 0);
                while (true) {
                        if (wan != 0) {
                                wan --;
                                have[turn] ++;
                                if (have[turn] == p) {
                                        cout << turn << endl;
                                        break;
                                }
                        } else {
                                wan = have[turn];
                                have[turn] = 0;
                        }
                        turn ++;
                        if (turn == n) turn = 0;
                }
        }
        return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define rp(a) while(a--)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
int main(){
	int n,wan;
	while(cin>>n>>wan,n||wan){
		vector<int>have(n);
		int i=0;
		int co=wan;
		while(1){
			if(wan){
				have[i]++;
				wan--;
				if(have[i]==co)break;
			}else{
				wan=have[i];
				have[i]=0;
			}
			i=(i+1)%n;
		}
		cout<<i<<endl;
	}
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
#define ALL(a) (a).begin(),(a).end()
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;
int n,p;
int c;
vector<int> ro;
bool decide = false;
int solve(){
  REP(a,100000000){
  REP(i,n){
    if(c){
      ro[i]++;
      c--;
      if(ro[i] == p){
	cout << i <<endl;
	decide = true;
	break;
      }

    }
    else{
      c +=ro[i];
      ro[i] = 0;
    }
  }
  if(decide) break;
  }
  return 0;
}

  
int main(){

  while(true){
    cin >> n >> p;
    if(n==0&&p==0)break;
    c = p;
    ro.resize(n,0);
    solve();
    ro.clear();
    decide = false;
    
  }
  return 0;
}

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

using namespace std;

int win(int ,int);

int main(){
	int n;//候補数
	int p;//はじめの石の数

	while(1){
		cin>>n>>p;

		if(n==0&&p==0)
			break;
		cout<<win(n,p)<<endl;
	}
	return 0;
}

int win(int n,int p){
	int hito=0;
	int owan=p;
	int moti_isi[50]={0,};
	while(1){
		if(owan!=0){
			moti_isi[hito]++;
			owan--;
		}
		else{
			owan=moti_isi[hito];
			moti_isi[hito]=0;
		}
		
		if(owan==0){
			int hantei=0;
			for(int i=0;i<n;i++){
				if(moti_isi[i]>=p)
					hantei++;
			}
			if(hantei==1)
				break;
		}

		hito++;
		if(hito>n-1)
			hito=0;
	}
	return hito;
}

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>
#include <cmath>

using std::cin;
using std::cout;
using std::endl;

int main(){
  int n;
  int p;
  int a[100];
  int b[100];
  int flag = 0;
  int i = 0;
  int pp;
  while(cin >> n >> p){
    pp = p;
    for(int j = 0;j < n;j++){
      a[j] = 0;
    }
    i = 0;
    flag = 0;
    if(n == 0 && p == 0){
      break;
    }
    while(flag == 0){
      if(p > 0){
        a[i] += 1;
        p -= 1;
      }else{
        p = a[i];
        a[i] = 0;
      }
      if(a[i] == pp){
        cout << i << endl;
        flag = 1;
      }
      i++;
      if(i == n){
        i = 0;
      }
    }
  }
}

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

int main(){
  
  int n,p,m[50];

  while(cin>>n>>p && n&&p){
    int w=p,i=0;
    for(int j=0;j<n;j++) m[j]=0;

    while(m[(i+n-1)%n]!=p){
      if(w) m[i]++,w--;
      else w+=m[i],m[i]=0;
      i=(i+1)%n;
    }
    cout<<(i+n-1)%n<<endl;

  }

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <map>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)

typedef long long ll;

int main(void)
{
	int n, p;
	while (cin >> n >> p, n|p) {
		int bowl = p;
		int idx = 0;
		vector<int> candidates = vector<int>(n, 0);
		for (;;) {
			if (bowl > 0) {
				bowl--;
				candidates[idx]++;
				if (candidates[idx] == p)
					break;
			} else if (bowl == 0) {
				swap(bowl, candidates[idx]);
			}
			idx = (idx+1)%n;
		}
		cout << idx << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
vector <string> v;
string s;
vector<int> ans;
int men[51] = {0};
int main(){
  int n, p;

  cin >> n >> p;
  while((n != 0)&&(p != 0)){
    int men[51] = {0};
    int s =(int)ans.size();
    int z = p;
    while(true){
      for (int i = 0;i<n;i++){
        if(z == 0){
          z += men[i];
          men[i] = 0;
        }else{
        men[i]++;
        z--;
      }
        if(men[i] == p){
          ans.push_back(i);
          break;
        }
      }
      if(s != (int)ans.size())break;
    }
      cin >> n >> p;
  }
  for (int i = 0;i<(int)ans.size();i++){
    cout << ans[i]<<endl;
  }
  return 0;
}