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 <vector>

using namespace std;

int game (int n, int p)
{
    int a[n]={0};
    bool ok;
    for (int i=0; i<n; i++)
    {
        if (p!=0)
        {
            a[i]++; p--;
            if (p==0)
            {
                ok=true;
                for (int j=0; j<n; j++)
                {
                    if (i==j) continue;
                    if (a[j]!=0) ok=false;
                }
                if (ok) return i;
            }
        }
        else { p+=a[i]; a[i]=0; }
        if (i==n-1) i=-1;
    }
}

int main()
{
    int n, p;
    vector<int> ans;
    while (1)
    {
        cin>>n>>p;
        if (n==0 && p==0) break;
        ans.push_back(game(n,p));
    }
    for (int i=0; i<ans.size(); i++) cout<<ans[i]<<'\n';
    return 0;
}

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

using namespace std;

int main() {
    int n, p;
    while (cin >> n >> p, n | p) {
        vector<int> stones(n, 0);
        int cur = 0, bowl = p;
        while (1) {
            if (!bowl) {
                swap(bowl, stones[cur]);
                cur = (cur < n - 1) ? cur + 1 : 0;
                continue;
            }
            bowl--;
            stones[cur]++;
            if (!bowl && stones[cur] == p) {
                cout << cur << endl;
                break;
            }
            cur = (cur < n - 1) ? cur + 1 : 0;
        }
    }
    return 0;
}

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

using ll = long long;
#define int long long

// [a -> b-1]
#define reps(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
// [0 -> a-1]
#define rep(i, a) reps(i, 0, (a))
// [a-1 -> b]
#define rreps(i, a, b) for (int i = (int)((a)-1); i >= (int)(b); i--)
// [a-1 -> 0]
#define rrep(i, a) rreps(i, a, 0)
#define all(v) (v).begin(), (v).end()
// next_permutation(all(v))
#define PERM(v) next_permutation(all(v))
/*sort(all(v));
 * (v).erase(unique(all(v)), v.end())*/
#define UNIQUE(v)\
	sort(all(v));\
	(v).erase(unique(all(v)), v.end())
#define MINV(v) min_element(all(v))
#define MAXV(v) max_element(all(v))
#define MIN3(a, b, c) min(min(a, b), c)
#define MIN4(a, b, c, d) min(MIN3(a, b, c), d)
#define MIN5(a, b, c, d, e) min(MIN4(a, b, c, d), e)
#define MIN6(a, b, c, d, e, f) min(MIN5(a, b, c, d, e), f)
#define MAX3(a, b, c) max(max(a, b), c)
#define MAX4(a, b, c, d) max(MAX3(a, b, c), d)
#define MAX5(a, b, c, d, e) max(MAX4(a, b, c, d), e)
#define MAX6(a, b, c, d, e, f) max(MAX5(a, b, c, d, e), f)
// b is [a, c)
#define RANGE(a, b, c) ((a) <= (b) && (b) < (c))
// c is [a, e) && d is [b, f)
#define RANGE2D(a, b, c, d, e, f) (RANGE((a), (c), (e)) && RANGE((b), (d), (f)))
#define chmin(a, b) a = min(a, (b))
#define chmin3(a, b, c) a = MIN3(a, (b), (c))
#define chmin4(a, b, c, d) a = MIN4(a, (b), (c), (d))
#define chmin5(a, b, c, d, e) a = MIN5(a, (b), (c), (d), (e))
#define chmin6(a, b, c, d, e, f) a = MIN6(a, (b), (c), (d), (e), (f))
#define chmax(a, b) a = max(a, (b))
#define chmax3(a, b, c) a = MAX3(a, (b), (c))
#define chmax4(a, b, c, d) a = MAX4(a, (b), (c), (d))
#define chmax5(a, b, c, d, e) a = MAX5(a, (b), (c), (d), (e))
#define chmax6(a, b, c, d, e, f) a = MAX6(a, (b), (c), (d), (e), (f))
#define fcout cout << fixed << setprecision(15)
#define YES(f) cout << ((f) ? YES_STR : NO_STR) << endl;

using namespace std;

int solve(int n, int p) {
	vector<int> v(n, 0);
	int w = p;
	for (int i = 0; ; i = (i + 1) % n) {
		if (w) v[i]++, w--;
		else w += v[i], v[i] = 0;
		if (v[i] == p) return i;
	}
}

signed main() {
	int n, p;
	while (cin >> n >> p, n) cout << solve(n, p) << endl;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
int main(){
	int n,p;
	while(true){
		scanf("%d%d",&n,&p);
		if(n==0) break;
		int st[50]={},in=p;
		for(int cnt=0;;cnt++){
			if(cnt==n) cnt=0;
			if(in!=0) st[cnt]++,in--;
			else in+=st[cnt],st[cnt]=0;
			if(st[cnt]==p){
				printf("%d\n",cnt);
				break;
			}
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
	int n,p;
	while(cin>>n>>p,n) 
	{
		int nums[n];
		for(int i = 0; i < n; ++i)
		{
			nums[i]=0;
		}
		int zero=n;
		int ans=0,pos=0;
		while(1)
		{
			if(p==1&&zero==n-1)
			{
				ans=pos;
				break;
			}
			else if(p>=1)
			{
				p--;
				if(nums[pos]==0) zero--;
				++nums[pos];
			}
			else
			{
				p+=nums[pos];
				if(nums[pos]!=0) zero++;
				nums[pos]=0;
			}	
			pos=(pos+1)%n;
		}
		cout<<ans<<endl;
	}
  return 0;
}

C++: /*** ?¬?????????? ***/

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

int main(){
	int n, p;
	int i;
	int count, size;
	int max, num;
	
	cin >> n >> p;
	while(n!=0 || p!=0){
		vector<int> v;
		for(i=0; i<n; ++i) v.push_back(0);
		count=0;
		size=p;
		
		while(1){
			for(i=size; 0<i; --i){
				++v[count];
				count=(count+1)%n;
			}
			
			max=0;
			num=-1;
			for(i=0; i<n; ++i){
				if(max<v[i]){
					max=v[i];
					num=i;
				}
			}
			if(max==p) break;
			
			while(v[count]==0) count=(count+1)%n;
			size=v[count];
			v[count]=0;
			count=(count+1)%n;
		}
		cout << num << endl;
		
		cin >> n >> p;
	}
	return 0;
}

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

signed main()
{
  int N, P;

  while(cin >> N >> P, N || P) {
    
    int w = P, i = 0, have[55] = {0};
    while(1) {
      if(w) have[i]++, w--;
      else w += have[i], have[i] = 0;
      if(have[i] == P) { cout << i << endl; break; }
      i++; if(i == N) i = 0;
    }

  }

  return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 100000000

#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>

using namespace std;

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

static const double EPS = 1e-8;

const int tx[] = {-1,0,1,0};
const int ty[] = {0,1,0,-1};


bool check(map<int,int>& hand,int step,int n){
	for(map<int,int>::iterator it = hand.begin(); it != hand.end(); it++){
		if(it->first == step % n) continue;
		if(it->second != 0) return false; 
	}
	return true;
}
int main(){
	int n,p;	
	while(~scanf("%d %d",&n,&p)){
		if(n==p && p==0) break;
		map<int,int> hand;
		int bowl = p;
		int step = 0;
		int res = 0;
		while(1){
			if(bowl > 0){			
				hand[step]++;
				bowl--;
				if(bowl == 0 && check(hand,step,n)){
					res = step;
					break;
				}
			}
			else if(bowl == 0){
				bowl += hand[step];
				hand[step] = 0;
			}
			step = (step+1) % n;
		}

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

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

int main() {
	int n, p;
	while (cin >> n >> p, n || p) {
		int mayor[n];
		fill(mayor, mayor + n, 0);
		int stone = p;
		for (int i = 0;; i = (i + 1) % n) {
			if (stone) {
				stone--;
				mayor[i]++;
				if (mayor[i] == p) {
					cout << i << endl;
					break;
				}
			}
			else {
				stone = mayor[i];
				mayor[i] = 0;
			}
		}
	}
	return 0;
}