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<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;

using namespace std;


int main() {
	int n, p; // テ、ツクツ甘ゥツ卍?50 50
	int kouho[50];

	//10^6
	while(cin >> n >> p && n && p)
	{
		//cout << n << " " << p << endl;
		rep(i,50){kouho[i]=0;}
		int box = p;
		int index=0;
		while(true)
		{
			if(index >= n){ index=0; }	
			if(box>0){ 
				box--; kouho[index]++;
			}else{
				box=kouho[index]; kouho[index]=0;
				bool flag=false;
				/*
				rep(i,n){if(kouho[i]!=0){flag=false;}}
				if(flag){cout << index << endl; break;}
				*/
				rep(i,n){
					if(kouho[i]==p){flag=true;}	
				}
				if(flag){
					if(index==0){index=n;}
					cout << (index-1)%n << endl; break;
				}
			}
			index++;
		}
	}
	return 0;
}

C++: #include<stdio.h>
int have[50];
int main(){
	int a,b;
	while(scanf("%d%d",&a,&b),a){
		int where=0;
		int cup=b;
		for(int i=0;i<a;i++)have[i]=0;
		while(1){
			if(cup>0){
				cup--;
				have[where%a]++;
				if(have[where%a]==b){
					printf("%d\n",where%a);
					break;
				}
			}else{
				cup=have[where%a];
				have[where%a]=0;
			}
			where++;
		}
	}
}

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

using namespace std;

int main(){
  int n, p;

  while(1){
    cin >> n >> p;
    if(n == 0 && p == 0){
      break;
    }else{
      int kouho[n],wan;
      for(int i = 0 ; i < n ; i++){
	kouho[i] = 0;
      }
      wan = p;
      // shokika owari

      
      while(1){
	int id = 0;
	for(int i = 0; i < n;i++){
	  if(wan != 0){
	    wan--;
	    kouho[i]++;
	  }else {
	    wan = kouho[i];
	    kouho[i] = 0;
	  }
	  
	  if(kouho[i] == p){
	    cout << i << endl;
	    id = i;
	    break;
	  }
	  //cout << kouho[i] << endl;
	}
	//1 syu owari
	if(kouho[id] == p){
	  break;
	}
      }
      
      
    }
    
  }

}

C++: #include <iostream>

using namespace std;

int main(){
	while(true){
		int n, p;
		cin >> n >> p;
		if(n == 0 && p == 0){ break; }
		int answer = 0, q = p, stones[50] = { 0 };
		while(true){
			if(q > 0){
				if(++stones[answer] == p){ break; }
				--q;
			}else{
				q = stones[answer];
				stones[answer] = 0;
			}
			answer = (answer + 1) % n;
		}
		cout << answer << endl;
	}
	return 0;
}

C++: #include <algorithm>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <locale>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int solve(int n, int p) {
    int pepole[n];
    int ans = -1;
    for (int i = 0; i < n; i++) {
        pepole[i] = 0;
    }
    int check = 0;
    while (1) {
        for (int i = 0; i < n; i++) {
            if (p != 0) {
                p--;
                pepole[i]++;
            } else if (pepole[i] != 0) {
                p += pepole[i];
                pepole[i] = 0;
            } else {
                check = 0;
                for (int j = 0; j < n; j++) {
                    if (pepole[j] != 0) {
                        ans = j;
                        check++;
                    }
                }
                if (check == 1) break;
            }
        }
        if (check == 1) break;
    }
    return ans;
}

int main() {
    cin.tie(0);
    vector<int> ans;
    int n, p;
    while (1) {
        cin >> n >> p;
        if (n == 0 && p == 0) break;
        ans.push_back(solve(n, p));
    }
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << endl;
    }
}

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

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

C++: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;

#define rep(i,N) for(int i=0;i<(int)(N);++i)
#define rrep(i,N) for(int i=(int)(N)-1;i>=0;--i)
#define debug(x) cout<<#x<<"="<<x<<endl;
constexpr ll MOD=1000000007;
constexpr ll INF=1LL<<60;
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<typename T> void fail(T v){cout << v << endl;exit(0);}
//template end

void solve(){
    int N,P;
    while(1){
        cin>>N>>P;
        if(!(N|P))break;
        int idx=0,now=P;
        vector<int> A(N);
        while(1)
        {
            if(now){
                A[idx]++;now--;
            }else{
                now+=A[idx];
                A[idx]=0;
            }
            if(A[idx]==P)break;
            idx++;
            if(idx==N)idx=0;
        }
        cout<<idx<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    solve();
    return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <limits.h>
#include <vector>

#define el endl
#define fd fixed

using namespace std;

int main() {
  while (1) {
    int n, p, i = 0, tmp_p;
    cin >> n >> p;
    if (n == 0 && p == 0) break;
    vector<int> stone(n, 0);
    tmp_p = p;
    while (1) {
      if (tmp_p > 0) {
        stone[i]++;
        tmp_p--;
      } else {
        tmp_p = stone[i];
        stone[i] = 0;
      }
      if (stone[i] == p) break;
      i = (i+1) % n;
    }
    cout << i << el;
  }
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cmath>
#include <string>

using namespace std;

typedef vector <int> VI;
typedef vector <VI> VVI;
typedef vector <string> VS;
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int, LL> PIL;
typedef vector <PIL> VPIL;


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

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

#define MAX_INT 2147483647

int main(){
	int n, p, cup, tmp;
	VI have;
	while(1){
		cin >> n >> p;
		if (! (n and p)) break;
		tmp = 0;
		cup = p;
		have = VI(n,0);
		while(1){
			if (cup == 0){cup += have[tmp]; have[tmp] = 0;}
			else if (cup == 1 and have[tmp] == p-1) break;
			else { have[tmp] += 1; cup--;}

			if (tmp == n-1) tmp = 0;
			else tmp++;
		}
		cout << tmp << endl;
	}
	return 0;
}