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<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int a[100];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m),(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;
                    }
                }
                if(a[i]==m)
                {
                    k=i;
                    flag=1;
                    break;
                }
            }
            if(flag==1)
                break;
        }
        printf("%d\n",k);
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,p;
  while(cin>>n>>p, n){
    int vi[51]={};
    int i=0;
    while(1){
      int flag = 0;
      if(p){
        vi[i]++;
        p--;
      }
      else{
        p=vi[i];
        vi[i]=0;
      }
      for(int j=0;j<n;++j)
        if(j!=i && vi[j]) flag=1;
      if(flag==0 && p==0){
        cout<<i<<endl;
        break;
      }
      i++;
      if(i==n) i=0;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(void){
    int n,p;
    while (cin>>n>>p) {
        if (n==0&p==0) {
            break;
        } else {
            std::deque<int> deq(n,0);
            int i=0;
            while (1) {
                if (p!=0) {
                    p-=1;
                    deq[i]++;
                    i++;
                    if (i>n-1) {
                        i=0;
                    }
                } else if (p==0) {
                    int flag=0;
                    for (int j=0;j<n;j++) {
                        if (i!=j&&deq[j]!=0) {
                            flag=1;
                            break;
                        }
                    }
                    if (flag==0) {
                        break;
                    } else {
                        p=deq[i];
                        deq[i]=0;
                        i++;
                        if (i>n-1) {
                            i=0;
                        }
                    }
                }
            }
            cout<<i<<endl;
        }
    }
}

C++: #include <iostream>
#include <string.h>

using namespace std;

void solve()
{
	int n, p;
	while(cin >> n >> p, n || p)
	{
		int have[51];
		memset(have, 0, sizeof(have));
		int pos = 0;
		int stone = p;
		while(true)
		{
			if(stone > 0)
			{
				++have[pos];
				--stone;
			}
			else
			{
				stone += have[pos];
				have[pos] = 0;
			}
			if(have[pos] == p)
			{
				cout << pos << endl;
				break;
			}
			++pos;
			if(pos == n)
			{
				pos = 0;
			}
		}
	}
}

int main()
{
	solve();
	return(0);
}

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

int p[50];
int main()
{
loop:
  memset(p, 0, sizeof p);
  int m, n, t = 0, sm;
  cin >> n >> m;
  sm = m;
  if(!(m || n))
    return 0;
  while(1)
  {
    if(!m)
    {
     m = p[t];
     p[t] = 0;
    }
    else
    {
      p[t]++; 
      m--;
      if(m == 0 && p[t] == sm)
      {
        cout << t << endl;
        break;
      }
    }
    t = (t + 1) % n;
  }
  goto loop;
}

C++: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
const double PI = acos(-1);
const ll MOD = 1000000007;
// const ll MOD = 998244353;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
 
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
///////////////////////////////////////////////////////////////
 
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    while (1) {
        int N,P; cin >> N >> P;
        if (N == 0 && P == 0) break;
        int k = P;
        int cnt = 0;
        vector<int> A(N);
        while (1) {
            if (k != 0) {
                k--;
                A[cnt]++;
                if (A[cnt] == P) break;
            } else {
                k = A[cnt];
                A[cnt] = 0;
            }
            cnt++;
            if (cnt == N) cnt = 0;
        }
        cout << cnt << endl;
    }
}

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

int main(void) {
    int n, p;
    while (cin >> n >> p, n+p) {
        vector<int> m(n, 0);
        int pos = 0;
        int wan = p;

        while (1) {
            // for (int i = 0; i < n; i++) {
            //     cout << "m[" << i << "]: " << m[i] << ", ";
            // }
            // cout << endl;
            if (m[pos] == p) break;

            if (wan == 0) {
                wan += m[pos];
                m[pos] = 0;
            } else {
                m[pos]++;
                wan--;
            }

            pos = (pos+1) % n;
        }

        cout << pos << endl;
    }

    return 0;
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#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 ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };
int n, p;
int vl[50];
int main() {
	while (cin>>n>>p, n|p){
		int max = p;
		int ans = 0;
		for (int i = 0; i < n; i++){
			vl[i] = 0;
		}
		
		for (int i = 0; i < 10000000;i++){
			if (p > 0){
				vl[i%n]++;
				p--;
				if (p == 0 && vl[i%n] == max){
					ans = i%n;
					break;
				}
			}
			else{
				p=vl[i%n]++;
				vl[i%n] = 0;
			}
		}
		cout << ans << endl;
	}
}