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<vector>
#include<math.h>
#include<algorithm>
#define ll long long
#define INF 1145141919810
#define rep(i,a) for(int i=0;i<a;i++)
#define nrep(i,a,b) for(int i=a;i<b:i++)
#define mrep(i,a) for(int i=a;i>=0;i--)
using namespace std;

bool end_check(vector<ll> a,ll p,ll winner){
	bool flag=true;
	rep(i,a.size()) if(i!=winner && a[i]) return false;
	if(!p) return true;
	return false;
}

int main(){
	while(true){
	ll n,p; cin>>n>>p;
	if(!n&&!p) break;
	vector<ll> a(n);
	ll winner=0;
	while(true){
		//step
		if(p){
			p--;
			a[winner]++;
		}else{
			p+=a[winner];
			a[winner]=0;
		}
		
		if(end_check(a,p,winner)) break;
		
		//next
		if(winner==n-1) winner=0;
		else winner++;
		
	}cout<<winner<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<climits>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<iomanip>
using namespace std;

#define rep(i,j,n) for(int i=(j);i<(n);i++)
#define rep2(i,j,n) for(int i=(j);i<=(n);i++)
#define all(i) i.begin(),i.end()

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef pair<int, int> pi;
typedef long long i64;

int main() {
	int n, p;
	while (cin >> n >> p, n) {
		int x = p;
		vi a(n, 0);
		rep(i, 0, 1000000) {
			if (x == 0) {
				x += a[i%n];
				a[i%n] = 0;
			}
			else {
				a[i%n]++;
				x--;
				if (x == 0 && a[i%n] == p) {
					cout << i % n << endl;
					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 RFOR(i, a, b) for (int i=a;i>=b;--i)
#define ll long long
#define all(a) a.begin(),a.end()
#define ull unsigned long long
#define pb(x) push_back(x)
#define dbg(x) cout<<#x<<" : "<<(x)<<endl;
using namespace std;
const int INF = 1500000000;
const double PI = 3.14159265359;
const int mod = 1000000000 + 7;

const int MAX_N = 50, MAX_P = 50;
int n, p, res, v[MAX_N];

void solve() {
  rep(i, n) v[i] = 0;
  int c = 0;
  while(true) {
    if (p == 0) {
      p = v[c];
      v[c] = 0;
    } else {
      p--;
      v[c]++;
      if (p == 0) {
        bool flg = true;
        rep(i, n) if (i != c && v[i] > 0) flg = false;
        if (flg) break;
      }
    }
    c = (c == n-1) ? 0 : c + 1;
    res = c;
  }
}

int main() {
  while(true) {
    cin >> n >> p;
    if (!n && !p) break;
    solve();
    cout << res << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,1,0,-1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << endl;}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << endl;
}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int main(){
	while(1){
		ll n,P; cin >> n >> P;
		if(n==0 && P==0) break;
		vl v(n,0);
		ll now = 0;
		ll step = 0;
		ll p = P;
		while(1){
			if(p){
				p--;
				v[now]++;
			}else{
				p += v[now];
				v[now] = 0;
			}
			if(v[now]==P){
				cout << now << endl;
				break;
			}
			now = (now + 1) % n;
			step++;
		}
	}
}

C++: #include "algorithm"
#include "iostream"
#include "cmath"
#include "string"
#include "map"
using namespace std;


int main(){
  while(true){
    int n,p;
    cin >> n >> p;
    if(!n&&!p)break;
    int a[n]={0};
    int wan=p,i;
    for(i=1;i<1000001;++i){
      if(wan==0){
        wan+=a[i%n];
        a[i%n]=0;
      }else {
        --wan;
        ++a[i%n];
        if(a[i%n]==p)break;
      }
    }
    cout << (i-1)%n <<endl;
  }
  return 0;
}

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;
		if (!n && !pmax) break;

		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<algorithm>
#include<vector>
using namespace std;
int main() {
	int n, p;
	while (cin >> n >> p && n&&p) {
		vector<int> d(n,0);
		int cnt = 0,turn=0;
		while (cnt != 1||!d[turn]) {
			for (int i = 0; i < p; ++i) {
				if (!d[(turn+i)%n]) ++cnt;
				++d[(turn+i)%n];
			}
			turn += p;
			p = 0;
			while (!d[turn%n]) {
				++turn;
			}
			--cnt;
			turn %= n;
			p = d[turn];
			d[turn] = 0;
			turn = (turn + 1) % n;
		}
		cout << turn << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <set>
#include <map>
#include <algorithm>

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, -1, 0, 1 };

int nextInt() {int a; cin >> a; return a;}
char nextChar() {char a; cin >> a; return a;}
double nextDouble() {double a; cin >> a; return a;}
string nextString() {string a; cin >> a; return a;}

void inputVector(vector<int> &v, int &n) {rep(i,n){v.push_back(nextInt());}}
void inputVector(vector<double> &v, int &n) {rep(i,n){v.push_back(nextDouble());}}
void inputVector(vector<string> &v, int &n) {rep(i,n){v.push_back(nextString());}}

signed main() {
    int n, p;
    while (cin >> n >> p) {
	if (!n) break;

	int now = 0;
	vector<int> c(n, 0);

	int q = p;
	while (true) {
	    if (p > 0) {
		c[now]++;
		p--;
	    } else {
		p = c[now];
		c[now] = 0;
	    }
	    if (c[now] == q) break;
	    now++;
	    if (now == n) now = 0;
	}
	cout << now << endl;
    }

    return 0;
}