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 <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define digit(N) cout << fixed << setprecision((N))

int N, Q;

int main()
{
    while (cin >> N >> Q)
    {
        if (N == 0 && Q == 0)
            return 0;
        set<int> wan;
        int player = 0;
        vector<set<int>> temoto(N);

        for (int i = 0; i < Q; i++)
        {
            wan.insert(i);
        }

        while (1)
        {
            if (wan.size() == 1 && temoto[player].size() == Q - 1)
            {
                cout << player << "\n";
                break;
            }
            if (wan.empty())
            {
                while (temoto[player].size())
                {
                    wan.insert(*temoto[player].begin());
                    temoto[player].erase(*temoto[player].begin());
                }
            }
            else
            {
                temoto[player].insert(*wan.begin());
                wan.erase(*wan.begin());
            }
            player++;
            player %= N;
        }
    }
}

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

C++: #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
typedef long long int ll;

#define EPS (1e-7)
#define INF (1e9)
#define LLINF (1e18)
#define PI (acos(-1))
#define MOD (1000000007)
#define ALL(v) (v).begin(), (v).end()
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

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

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, p;
    while (true)
    {
        cin >> n >> p;
        int now = 0;
        if (n == 0 && p == 0)
        {
            return 0;
        }
        int owan = p;
        vector<int> stone(n, 0); // 各候補者が持つ石の個数
        int ans;
        while (true)
        {
            if (owan > 0)
            {
                owan--;
                stone[now % n]++;
                if (stone[now % n] == p)
                {
                    cout << now % n << endl;
                    break;
                }
            }
            else
            {
                owan = stone[now % n];
                stone[now % n] = 0;
            }
            now++;
        }
    }

    return 0;
}

C++: #pragma region _head

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<iomanip>
#include<queue>
#include<stack>
#include<numeric>
#include<utility>
#include<regex>

void Init() {
	std::cin.tie(0); std::ios::sync_with_stdio(false);
	struct Init_caller { Init_caller() { Init(); } }caller;
}

#pragma region _define

#define int LL
#define f(i,a,b) for(LL i=a;i<b;i++)
#define f_vI(v,n) f(i,0,n)cin>>v[i]
#define f_v2I(v1,v2,n) f(i,0,n)cin>>v1[i]>>v2[i]
#define f_v3I(v1,v2,v3,n) f(i,0,n)cin>>v1[i]>>v2[i]>>v3[i]
#define f_vO(v,n) f(i,0,n)cout<<v[i]<<endl
#define ei else if
#define all(a) a.begin(),a.end()
#define size(s) ((LL)s.size())
#define F first
#define S second
#define check() cout<<"! ! !"
#define endl "\n"
#define _y() cout<<"Yes"<<endl
#define _Y() cout<<"YES"<<endl
#define _n() cout<<"No"<<endl
#define _N() cout<<"NO"<<endl
#define INT_INF 1<<29
#define LL_INF 1LL<<60
#define MOD 1000000007

#pragma endregion

#pragma region _using

using namespace std;

using LL = long long;
using st = string;
using vi = vector<LL>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<st>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using qi = queue<LL>;
using qc = queue<char>;
using qs = queue<st>;
using si = stack<LL>;
using sc = stack<char>;
using ss = stack<st>;
using pi = pair<LL, LL>;
using ppi = pair<pi, LL>;
using mll = map<LL, LL>;
using mcl = map<char, LL>;
using vpi = vector<pi>;
using vppi = vector<ppi>;

#pragma endregion

struct edge {
	int from, to, cost;
};

void y_n(bool p) {
	p ? _y() : _n();
}

void Y_N(bool p) {
	p ? _Y() : _N();
}

LL vmax(vi v, LL n) {
	int MAX = 0;
	f(i, 0, n) {
		MAX = max(MAX, v[i]);
	}
	return MAX;
}

LL vmin(vi v, LL n) {
	int MIN = LL_INF;
	f(i, 0, n) {
		MIN = min(MIN, v[i]);
	}
	return MIN;
}

LL gcd(LL a, LL b) {
	LL r;
	while ((r = a % b) != 0) {
		a = b;
		b = r;
	}
	return b;
}
LL lcm(LL a, LL b) {
	return (a / gcd(a, b) * b);
}

//素数判定
bool is_prime(int n) {
	for (int i = 2; i*i <= n; i++) {
		if (n % i == 0) {
			return false;
		}
	}
	return true;
}

//エラトステネスの篩
void sieve(LL n) {
	vb s(n + 1, true);
	f(i, 2, n + 1) {
		for (int j = 2; i*j <= n; j++) {
			s[i*j] = false;
		}
	}
	cout << "2から" << n << "までの素数" << endl;
	f(i, 2, n + 1) {
		if (s[i]) {
			cout << i << " ";
		}
	}
}

//階乗計算
LL factorial(LL n) {
	LL a = 1, ret = 1;
	while (a < n) {
		a++;
		ret *= a;
		ret %= MOD;
	}
	return ret;
}


#pragma endregion 


/*****************************************************************************/
signed main(void) {
	while (1) {
		int n, p;
		cin >> n >> p;
		if (n == 0 && p == 0)break;
		int box = p;
		vi u(n, 0);
		int i = 0;
		while (1) {
			if (box == 1 && u[i] == p - 1) {
				cout << i << endl;
				break;

			}
			if (box != 0) {
				u[i]++;
				box--;
			}
			else {
				box += u[i];
				u[i] = 0;
			}
			i++;
			i %= n;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
int a[50];
signed main() {
  int N, P;
  while (cin>>N>>P, N+P!=0) {
    for (int i = 0; i < N; i++) a[i] = 0;
    int q = P;
    int i = -1;
    while (a[i] != P) { 
      i++;
      if (i >= N) i=0;
      if (q == 0) {
        q += a[i];
        a[i] = 0;
      } else {
        q--;
        a[i]++;
      }
    }
    cout << i << endl;
  }
}

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

using namespace std;

int main(){
	while(true){
		int n,p; cin >> n >> p;
		if(n == 0)break;
		vector<int> a(n,0);
		int s = p;
		int cnt = 0;
		while(true){
			if(s == 0){
				s = a[cnt];
				a[cnt] = 0;
			}
			else{
				s--;
				a[cnt]++;
			}
			if(s == 0 && a[cnt] == p)break;
			cnt++;
			cnt = cnt % n;
		}
		cout << cnt << endl;
	}
	
	return 0;
}