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 <string>
#include <vector>
#include <algorithm>

using namespace std;

#define ll long long
#define pb push_back

ll MIN(ll num1,ll num2){
    return num1<num2?num1:num2;
}

int main(){

    while(1){
        int n,p;
        cin>>n>>p;
        if(n==0&&p==0)break;
        //cout<<n<<" "<<p<<endl;
        vector<int> stone;
        for(int i=0;i<n;i++)stone.pb(0);
        int now=0;
        while(1){
            if(p==0){
                p=stone[now];
                stone[now]=0;
            }else{
                p--;
                stone[now]++;
            }

            bool flag=true;
            for(int i=0;i<n;i++){
                if(i==now)continue;
                if(stone[i]!=0)flag=false;
            }
            if(p==0&&flag){
                cout<<now<<endl;
                break;
            }

            now++;
            if(now>=n)now=0;
        }
    }

    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)

typedef long long ll;

int main(void) {
    int n,p;
    while (cin >> n >> p && n){
        vector<int> v(n);
        int a=p , i=0;
        while (1){
            if (p){
                v[i]++;
                p--;
            }else{
                p+=v[i];
                v[i]=0;
            }
            if (v[i] == a)
                break;
            i = (i+1)%n;
        }

        cout << i << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main() {
	int cnt,n, p, a[50] = {};
	while (cin >> n >> p&&n&&p) {
		fill(a, a + 50, 0);
		cnt = p;
		for (int i = 0;; i++) {
			if (a[i%n] == p) {
				cout << i%n << endl;
				break;
			}
			if (cnt == 0) {
				swap(cnt, a[i%n]);
				continue;
			}
			a[i%n]++;
			cnt--;
		}
	}
	return 0;
}

C++: #include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int a[100];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(a,0,sizeof(a));
        int ans=m,k=0,flag=0;
        while(1)
        {
            for(int i=0;i<n;i++)
            {
                if(ans>0)
                {
                    ans--;
                    a[i]++;
                }
                else
                {
                    if(a[i]>0)
                    {
                        ans=a[i];
                        a[i]=0;
                    }
                }
            }
            for(int i=0;i<n;i++)
            {
                if(a[i]==m)
                {
                    k=i;
                    flag=1;
                    break;
                }
            }
            if(flag==1)
                break;
        }
         printf("%d\n",k);
    }
    return 0;
}

C++: #include<stdio.h>
int main()
{
    int n,p;
    while(~scanf("%d%d",&n,&p)&&(n+p))
    {
        int a[150]={0},ans,flag=0,ball=p;
        while(1)
        {
            for(int i=0;i<n;i++)
            {
                if(ball)
                    a[i]++,ball--;
                else
                    ball+=a[i],a[i]=0;
                if(a[i]==p)
                {
                    ans=i;
                    flag=1;
                    break;
                }
            }
            if(flag)
                break;
        }
        printf("%d\n",ans);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
int ppl[55]={0};
int n,p,now,num;
 
int main(){
  int i,j,k;
  while(1){
    cin >> n >> p;
    if(n+p==0)break;
    now = p;
    num = 0;
    while(1){
      if(now>0){
    ppl[num]++;
    now--;
      }
      else if(ppl[num]==p)break;
      else{
    now = ppl[num];
    ppl[num]=0;
      }
      num++;
      num%=n;
    }
    printf("%d\n",num);
    for(i=0;i<n;i++)ppl[i]=0;
  }
  return 0;
}

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


int main(){
  while(1){
    int n,p;
    cin>>n>>p;
    if(n==0&&p==0)return 0;
    vector<int> a(n,0);
    int pool=p;
    int i=0;
    while(1){
      for(i = 0;i<n;i++){
        //assert(0<=i && i<n);
        if(pool==0){
          pool+=a[i];
          a[i]=0;
        }else{
          pool--;
          a[i]++;
        }
        //assert(0<=i && i<n);
        if(a[i]==p)break;
      }
      //assert(0<=i && i<n);
      if(a[i]==p&&i<n)break;
    }
    cout<<i<<endl;
  }
  return 0;
}

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

int main()
{
	int n, m;
	while (cin >> n >> m, n + m)
	{
		int ary[100] = {};
		int p = m;
		int i = 0;
		while (true)
		{
			if (p == 0)
			{
				p = ary[i];
				ary[i] = 0;
			}
			else
			{
				p--;
				ary[i]++;
			}
			if (ary[i] == m)
			{
				cout << i << endl;
				break;
			}
			i++;
			if (i == n) i = 0;
		}
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
using namespace std;

int main(){
  int n, p;
  while(scanf("%d%d", &n, &p) && n && p){
    vector<int>mayor(n);
    int i = 0, now = p;
    while(1){
      if(now){ mayor[i]++;now--;}
      else{ now = mayor[i];mayor[i] = 0;}
      if(mayor[i] >= p) break;
      i++;
      if(i == n) i = 0;
      //      rep(j, n) printf("%d ", mayor[j]);puts(""); 
    }

    printf("%d\n", i);
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
using namespace std;
typedef long long ll;
int INF = (1LL << 30) - 1;
int MOD = 1e9+7;
main(){
    int N,P;
    cin >> N >> P;
    while(N){
        int num = P,turn = 0;
        vector<int> V(N);
        while(1){
            if(num){
                V[turn]++;
                num--;
            }else{
                num = V[turn];
                V[turn] = 0;
            }
            if(!num){
                int cnt = 0,ans;
                rep(i,0,N){
                    cnt += V[i] != 0;
                    if(V[i] != 0)ans = i;
                }
                if(cnt == 1){
                    cout << ans << endl;
                    break;
                }
            }
            turn = (turn + 1) % N;
        }
        cin >> N >> P;
    }
}