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>
#define REP(i, n) for (int i = 0; i < n ; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) // decltypeで型取得、引数があればva_argsのところに入れる
using namespace std;
typedef long long ll; // long longをllでかけるようにした
const int INF = 1e9;

int main(void){
    while(true){
        int n, p; cin >> n >> p;
        if(n == 0 and p == 0) return 0;
        else{
            vector<int> person(n, 0);
            int turn = 0, cup = p; // turnは今誰のターンか示す, cupは椀の中に入っている小石の個数
            while(true){
                if(cup != 0){ //cupに小石がある時
                    person[turn]++;
                    cup--;
                }
                else if(person[turn] != 0){ //小石を持っているとき
                    cup += person[turn];
                    
                    person[turn] = 0;
                }

                if(person[turn] == p) break;
                turn++;
                if(turn == n) turn = 0;
            }
            cout << turn << endl;
        }
    }
}

C++: #include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int ary[60];

int main(){ _;
  int n,p;
  while(cin>>n>>p,!(n==0&&p==0)){
    REP(i,n)ary[i]=0;
    int rest=p;

    for(;;){
      REP(i,n){
	if(rest==0){
	  if(ary[i]==p){
	    cout<<i<<endl;
	    goto found;
	  }else swap(ary[i],rest);
	}else{
	  rest--;
	  ary[i]++;
	}
      }
    }
  found:;
  }
}

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

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

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

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

int main() {
	int n,p;//~50
	int stone,player[50]={};
	int winner;
	int i;
	
	scanf("%d%d",&n,&p);
	while(1){
	for(i=0;i<50;i++){
		player[i]=0;
	}
	stone=p;
	for(i=0;;i++){
		if(stone==0){
			stone=player[i%n];
			player[i%n]=0;
		}
		else{
			stone-=1;
			player[i%n]+=1;
		}
		if((stone==0)&&(player[i%n]==p)){
			winner=i%n;
			break;
		}
	}
	printf("%d\n",winner);
	scanf("%d%d",&n,&p);
	if((n==0)&&(p==0)){break;}
	}
	return 0;
}

C++: //#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <cstring>
#include <climits>
#define INF INT_MAX / 2
using namespace std;

typedef pair<double, double> P;
typedef long long ll;

int judge(vector<int> a,const int& n){
	for(int i=0;i<a.size();i++){
		if(a[i] == n)return i;
	}
	return -1;
}

int main(void) {
	int n,p;
	while(cin >> n >> p,n+p!=0){
	int i=0,ba = p;
	vector<int> a(n);
	int ret;
	while((ret=judge(a,p))== -1){
		if(ba > 0){
			a[i%n] += 1;
			ba -= 1;
		}else if(a[i%n] > 0){
			ba += a[i%n];
			a[i%n] = 0;
		}
		i++;
	}
	cout << ret << endl;
}
}

// command ctrl 上下
// command D
// command caps G → '' ""

C++: #include <iostream>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <math.h>
using namespace std;

int main(){
	
	int n,b,ib,p[50],q;
	while(1){
		cin>>n>>b;ib=b;
		if(n==0&&b==0)return 0;
		for(int i=0;i<n;i++)p[i]=0;
		q=0;
		while(1){
			if(b>0){p[q]++;b--;if(p[q]==ib){cout<<q<<endl;break;}}
			else {b+=p[q];p[q]=0;}
			q++;if(q==n)q=0;
		}
	}
}

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

#ifdef LOCAL
#include "../dump.hpp"
#else
#define dump(...)
#endif

#define rep(i,n) for(ll i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(ll i=(a),i##_cond=(b);i<i##_cond;++i)
#define ROF(i,a,b) for(ll i=(a)-1,i##_cond=(b);i>=i##_cond;--i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend() //sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
#define SUM(a) accumulate(all(a),0)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll,ll> pii;

const ll inf = 1ll<<62;
const ll mod = 1e9+7;

#define int ll

main()
{
  while(1){
    int n,p; cin >> n >> p;
    if(n == 0) break;
    vi m(n);
    int pos = 0;
    int tmp = p;
    while(1){
      if(tmp > 1){
	tmp--;
	m[pos]++;
      }else if(tmp == 1){
	tmp--;
	m[pos]++;
	if(m[pos] == p){
	  cout << pos << endl;
	  break;
	}
      }else{
	tmp = m[pos];
	m[pos] = 0;
      }
      pos++;
      pos %= n;
    }	
  }
}

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

int main(){

  while(1){
    int n,p;
    cin  >> n >> p;
    if(n == 0 && p == 0) break;
    int stone[60]={}, i = 0, x = p;
    while(1){
      if(stone[i] == p){
	cout << i <<endl;
	break;
      }
      if(x == 0){
	x = stone[i];
	stone[i] = 0;
      }
      else{
	stone[i]++;
	x--;
      }
      i++;
      if(i == n) i = 0;
    }


  }

  return 0;
}

C++: #include<bits/stdc++.h>
#define endl '\n';
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double INFD = 1e100;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
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; }
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N,p;
  cin >> N >> p;
  while(N != 0){
    vector<int> cnt(N,0);
    int pos = 0;
    while(1){
      if(p == 1){
        int temp = pos;
        temp++;
        bool fin = true;
        while(temp != pos){
          temp %= N;
          if(cnt[temp]) fin = false;
          temp++;
          temp %= N;
        }
        if(fin){
          cout << pos << endl;
          break;
        }
      }
      if(p == 0){
        p += cnt[pos];
        cnt[pos] = 0;
      }else{
        p--;
        cnt[pos]++;
      }
      pos++;
      pos %= N;
    }
    cin >> N >> p;
  }
  return 0;
}