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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

int main() {
	while (true) {
		int n, pmax;
		int stone[50] = {};

		cin >> n >> pmax;

		int i;
		int p = pmax;
		for (i = 0; 1; i++) {
			if (p) {
				p--;
				stone[i % n]++;
				if (!p && stone[i % n] == pmax) {
					break;
				}
			}
			else {
				p += stone[i % n];
				stone[i % n] = 0;
			}
		}

		cout << i % n << endl;
	}

	return 0;
}

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

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

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

C++: #include <iostream>
#include <map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  int n, p, all;
  vector<int> r;
  while (cin >> n >> p && n && p) {
    bool flag = false;
    all = p;
    map<int, int> m;
    rep(i, n) m[i] = 0;
    while (!flag) {
      // cout << n << ' ' << p << endl;
      rep(i, m.size()) {
        if (p > 0) {
          p--;
          m[i]++;
        } else {
          p += m[i];
          m[i] = 0;
        }
      }
      rep(i, m.size()) {
        if (m[i] == all) {
          r.push_back(i);
          flag = true;
        }
      }
    }
  }
  rep(i, r.size()) { cout << r[i] << endl; }
  return 0;
}

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

using namespace std;

// 多次元 mul_array<int, 1, 2, 3> a
//template <typename T, std::size_t... Ns>
//struct index_reverse_for_array;
//template <typename T, std::size_t N, std::size_t... Ns>
//struct index_reverse_for_array<T, N, Ns...> { using type = std::array< typename index_reverse_for_array<T, Ns...>::type, N>; };
//template <typename T, std::size_t N>
//struct index_reverse_for_array<T, N> { using type = std::array<T, N>; };
//template <class T, std::size_t... Ns> using mul_array = typename index_reverse_for_array<T, Ns...>::type;
//
//using u32 = uint32_t;
//using s32 = uint32_t;
//using u64 = uint64_t;
//using s64 = uint64_t;

int main() {
    //cout << fixed << setprecision(10);

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

        int hand = p;
        int now = 0;
        vector<int> otete(n);
        for (int i = 0; i < n; i++)
            otete[i] = 0;

        int emememememememememe = n;
        int nnnnnnnnnnnnnnnnnnn = 0;
        do {
            //cout << hand << ", " << now << ", " <<  emememememememememe << " : ";
            //for (int i = 0; i < n; i++) { cout << otete[i] << ", "; }
            //cout << endl;
            if (hand == 0) {
                if (otete[now] != 0) {
                    hand = otete[now];
                    otete[now] = 0;
                    emememememememememe++;
                }
            }
            else {
                otete[now]++;
                hand--;
                if (otete[now] == 1)
                    emememememememememe--;
                if (emememememememememe == n -1 && hand == 0) {
                    cout << now << endl;
                    break;
                }
            }
            now = (now + 1) % n;
            //getchar();

        } while (true);

    }

    return 0;
}

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

using namespace std;

int main() {

	int n, p;

	while (cin >> n, cin >> p) {
		if (n == 0 && p == 0) {
			break;
		}
		else {
			int i = -1;
			vector<int> a(n, 0);//n個の配列を全て0で初期化
			while (1) {
				i++;
				if (i == n) {
					i = 0;
				}
				if (p == 0) {
					p += a[i];
					a[i] = 0;
				}
				else {
					p--;
					a[i]++;
					if (p == 0) {
						int z = 0;
						for (int j = 0; j < n; j++) {
							if (a[j] == 0) {
								z++;
							}
						}
						if (z == n - 1) {
							cout << i << endl;
							break;
						}
					}
				}
			}
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define Rep(i, N) for(int i = 0; i < N; i++)
#define Reps(i, x, N) for(int i = x; i < N; i++)

const int LLINF = 1LL << 60;

signed main()
{
  int N, P;

  while(cin >> N >> P, N || P) {
    
    int w = P;
    int have[55] = {0};
    int i = 0;
    while(true) {
      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++: // 2009à\IA ú¬·
#include <iostream>

using namespace std;

int main(){
	int n, p;
	while(cin >> n >> p, n){
		int num = p, pos = 0;
		int s[50] = {};
		while(true){
			pos %= n;
			if(num==0) swap(num, s[pos]);
			else       num--, s[pos]++;
			if(s[pos]==p){
				cout << pos << endl;
				break;
			}
			pos++;
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  while (true) {
    int n, p; cin >> n >> p;
    if (!n) break;
    V<> a(n);
    int c = p;
    for (int i = 0; ; i = (i + 1) % n) {
      if (p) --p, ++a[i];
      else p += exchange(a[i], 0);
      if (a[i] == c) {
        cout << i << '\n';
        break;
      }
    }
  }
}

C++: #define _GLIBCXX_DEBUG //vectorの範囲外アクセスなどのエラーメッセージが分かりやすくなるらしい


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
//#include <algorithm>

// using namespace std;
#include<numeric>

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<cstring>
#include<cmath>
#include<complex>


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

#define FOR(i,c) for(typeof((c).begin())i=(c).begin();i!=(c).end();++i)


#define EXIST(e,s)((s).find(e)!=(s).end())
#define RESET(a) memset((a),0,sizeof(a))
#define SET(a) memset((a),-1,sizeof(a))

 



#define ll long long

using namespace std;

int main(void){
  int n;
  int p;

  int temoto[50];
  int wan;
  bool flag;

  while(cin >> n >> p, n || p){
    flag = false;
    while(1){
      for(int i=0; i<n; i++){
	if(wan > 0){
	  wan--;
	  temoto[i]++;
	  if(temoto[i] == p){
	    cout << i;
	    flag = true;
	    break;
	  }
	}else{
	  wan += temoto[i];
	}
      }
      if(flag){
	break;
      }
    }
  }
    
    
    
    return 0;
}