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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>


using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define clear(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };


int main()
{
	int n, p;
	while (cin >> n >> p, n)
	{
		int s[64];
		clear(s, 0);
		for (int i = 0, dog = p; ; ++i %= n)
		{
			if (dog == 0)
			{
				dog = s[i];
				s[i] = 0;
			}
			else
			{
				--dog;
				if (++s[i] == p)
				{
					cout << i << endl;
					break;
				}
			}
		}
	}
}

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

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
template<typename V> constexpr typename V::value_type sum(const V &v) { return accumulate(all(v), 0); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n, p;
  while (cin >> n >> p, n) {
    int c = p, t = 0;
    vi v(n);
    for (;;) {
      if (c) {
        v[t]++;
        c--;
        if (!c) {
          bool f = true;
          rep(i, n) f &= !v[i] || i == t;
          if (f) {
            cout << t << endl;
            break;
          }
        }
      } else {
        c    = v[t];
        v[t] = 0;
      }
      t = (t + 1) % n;
    }
  }
}

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

using namespace std;

int main()
{
    while (true) {
        int n, p;
        cin >> n >> p;
        const int P = p;
        if (n == 0 and p == 0) {
            break;
        }
        int s = 0;
        vector<int> stone(n, 0);
        while (true) {
            if (p == 0) {
                p = stone[s];
                stone[s] = 0;
            } else {
                stone[s]++;
                p--;
                if (p == 0 and stone[s] == P) {
                    break;
                }
            }
            s = (s == n - 1) ? 0 : s + 1;
        }
        cout << s << endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30)
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する

void solve(int n,int p){
    vector<int> v(n);
    int one = p;
    int cur = 0;
    while(true){
        if(one == 0){
            one = v[cur];
            v[cur] = 0;
        }else{
            one--;
            v[cur]++;
        }
        if(one == 0 && v[cur] == p){
            cout << cur << endl;
            return;
        }
        cur++;
        cur %= n;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while(true){
        int n,p;
        cin >> n >> p;
        if(n == 0) break;
        solve(n,p);
    }


    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <stack>

using namespace std;


int main(){

	int n, p;

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

		int sum[n];
		int win=p;
		int winner;
		int flag = 0;

		for(int i=0 ; i<n ; i++){
			sum[i] = 0;
		}

		while(1){
			for(int i=0 ; i<n ; i++){
				if(p>0){
					sum[i]++;
					p--;
					if(sum[i]==win){
						flag = 1;
						winner = i;
						break;
					}
				}else{
					p = sum[i];
					sum[i] = 0;
				}
			}

			if(flag == 1) break;
		}

		cout << winner << endl;
	}
	return 0;
}