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 <algorithm>
//#include <vector>
//#include <stack>
//#include <queue>
//#include <map>
//#include <cmath>
//#include <string>
//#include <sstream>
//#include <iomanip>
//#include <complex>
using namespace std;
 
//#define ll long long
//#define vvi vector< vector<int> >
//#define All(X) X.begin(),X.end()
//#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 pb push_back 
//ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
 
int main(){
  while(1){
    int n, p;
    cin >> n >> p;
    if(n==0&&p==0) break;
    int kago=p;
    int pc[100]={0};
    int i=0;
    while(1){
      int k=i%(n-1);
      if(pc[k]==p){
	cout << k << endl;
	break;}
      if(kago>0){
	kago--;
	pc[k]++;}
      else{
	kago=pc[k];
	pc[k]=0;}
      i++;}
  }

  return 0;
}

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

int main(){
    int owan;
    int n;
    int p;
    int turn;

    while(cin >> n >> p, n != 0 && p != 0){
        turn = 0;
        vector<int> hand(n);

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

        owan = p;

        for(int i=0; i<1000000; i++){
            if(owan != 0){
                owan--;
                hand[turn]++;
            }else if(owan == 0){
                owan = hand[turn];
                hand[turn] = 0;
            }

            if(hand[turn] == p){
                break;
            }

            turn++;

            if(turn == n){
                turn = 0;
            }
        }

        cout << turn << endl;
    }
    return 0;
}

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

using namespace std;

int main() {
    while(true) {
        int N, P;
        cin >> N >> P;
        if(!N && !P) break;

        vector<int> v(N, 0);
        int cup = P;
        int pos = 0;
        int winner = 0;
        while(true) {
            if(cup > 0) {
                v[pos] += 1;
                cup -= 1;
                if(cup == 0) {
                    bool win = true;
                    for(int i = 0; i < N; ++i)
                        if(i != pos && v[i]) win = false;
                    if(win) {
                        winner = pos;
                        break;
                    }
                }
            }
            else {
                cup = v[pos];
                v[pos] = 0;
            }
            pos = (pos+1) % N;
        }

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

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

int n, p;
int cnt;
vector<int> v;

int solve() {
  while (true) {
    for (int i = 0; i < n; ++i) {
      if (cnt) {
        --cnt;
        ++v[i];
      } else {
        if (!v[i]) continue;
        cnt += v[i];
        v[i] = 0;
      }
      if (v[i] == p) return i;
    }
  }
}

int main() {
  while (cin >> n >> p, n) {
    cnt = p;
    v = vector<int>(n, 0);
    cout << solve() << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
//ranker
using namespace std;

#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)

using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using pis = pair<int, string>;
using psi = pair<string, int>;
using D = double;


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

  int n, p;

  while(1){
    cin >> n >> p;
    int p0=p;
    int A[n]={0};
    int turn = 0;
    if(n==0 && p==0) break;

    while(1){
      for(int j=0; j<n; j++){
        if(A[j] == p0){
          cout << j << endl;
          return 0;
        }
      }
      if(turn==n) turn=0;
      if(p != 0){
        p--;
        A[turn]++;
        turn++;
      }
      if(turn==n) turn=0;
      if(p == 0){
        p = A[turn];
        A[turn] = 0;
        turn++;
      }
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,n) for(int i=(a); i<(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;

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

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

        vector<int> vec(n, 0);
        int cur = 0;
        int total = p;
        while (true) {
            if (p) {
                p--;
                vec[cur]++;
            } else {
                p += vec[cur];
                vec[cur] = 0;
            }
            if ((*max_element(all(vec))==total) && (!p)) {
                cout << cur << endl;
                break;
            }
            cur++;
            cur %= n;
        }
    }
}

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

using namespace std;

int main()
{
    int n, p;
    while (cin >> n >> p, n | p) {
        vector<int> candidates(n, 0);
        int wan = p;
        while (true) {
            for (size_t i = 0; i < candidates.size(); ++i) {
                auto & candidate = candidates[i];
                if (wan != 0) {
                    --wan;
                    ++candidate;
                } else {
                    if (candidate == p) {
                        cout << i;
                        goto game_end;
                    }
                    wan += candidate;
                    candidate = 0;
                }
            }
        }
game_end:
        cout << endl;
    }
    return 0;
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value))
template <typename T>
void FILL_(void * ptr, size_t size, T value){
  std::fill((T*)ptr, (T*)ptr+size, value);
}
//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};
int n;//候補者の数
int p;//石の数
int A[100];
int turn;
int ans;
int main()
{
  while(cin>>n>>p,n||p){
    turn = 0;
    ans = 0;
    int sum =p;
    fill(A,A+100,0);
    while(1){
      turn =turn%n;
      if(A[turn]==sum){
	ans = turn;
	break;
      }
      if(p>0){
	A[turn] +=1;
	p--;
	turn++;
	continue;
      }else if(p==0){
	p +=A[turn];
	A[turn] = 0;
	turn++;
	continue;
      }
      
    }
    cout <<ans<<endl;
  }
  return 0;
}