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>
using namespace std;

int main(void){
  while(1){
    int stone[50];
    for(int k = 0 ; k < 50 ; k++){stone[k] = 0;}

    int bowl;
    int n, p;  // n：候補者人数，p：小石の総数
    cin >> n;
    cin >> p;

    if(n == 0 && p == 0){return 0;}

    bowl = p;

    bool flg = true;
    int ans;

    for(int i = 0 ; flg ; i = (i+1) % n){

      if(bowl == 0){
        bowl = bowl + stone[i];
        stone[i] = 0;
      }else{
        stone[i]++;
        bowl--;
        if(bowl == 0 && stone[i] == p){ans = i; flg = false;}
      }
    }
    cout << ans << " " << endl;
  }
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
static const int INF=1<<24;

int main(){
	int n,p;
	while(cin>>n>>p,n&&p){
		queue<pii> q;
		rep(i,n){
			q.push(mkp(0,i));
		}
		/*
		rep(i,n){
			cout<<q.front().F<<" "<<q.front().S<<endl;
			q.push(q.front());
			q.pop();
		}
		*/
		int tp=p;
		while(q.front().F!=p){
			pii t=q.front();
			q.pop();
			//cout<<t.F<<" "<<t.S<<endl;
			if(tp==0){
				tp+=t.F;
				t.F=0;
			}
			else{
				tp--;
				t.F++;
			}
			q.push(t);
		}
		cout<<q.front().S<<endl;
	}
	return 0;
}

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

using namespace std;


int main(void){
	//FILE* fp_in = freopen("data.txt", "r", stdin);

	while(1) {
		int n, p, bowl;
		vector<int> cand;

		cin >> n >> p;
		bowl = p;

		//終了処理
		if( n == 0 && p == 0) break;

		for(int i = 0; i < n; i++)
			cand.push_back(0);	//各候補者が持っている石の数
		
		int j = 0;

		while(1) {

			if(bowl > 0) {
				//椀の中に石が残っているとき
				cand.at(j) ++;
				bowl --;

				if( bowl == 0 && cand.at(j) == p ) {
					cout << j << endl;
					break;
				}

			} else if(bowl == 0) {
				//椀の中に石が残っていないとき
				bowl += cand.at(j);
				cand.at(j) = 0;
			}

			j ++;	//順番を回す。円形であることに注意
			j = j % n;
		}
		

	}

	//while(1){}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
int main(){
  int n, p;
  while(cin >> n >> p){
    if(n == 0 && p == 0) break;

    int stone[n];
    int left = p;
    
    for(int i = 0 ; i < n ; i++){
      stone[i] = 0;
    }

    int i = 0;

    while(1){
      if(left > 0){
	stone[i]++;
	left--;
      }
      else{
	left += stone[i];
	stone[i] = 0;
      }

      if(left == 0 && stone[i] == p){
	break;
      }
      i++;
      if(i == n) i = 0;
    }
    cout << i << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <numeric>
#include <iomanip>
#include <cmath>
#include <cstring>
using namespace std;
int stones[50];

void solve(int n, int p){
  for(int i = 0; i < n; i++){
      stones[i] = 0;
    }

    int cur_ind = 0;
    int pot = p;
    while(true){
      if(pot == 1 && pot + stones[cur_ind] == p){
        cout << cur_ind << endl;
        return;
      }

      if(pot != 0){
        stones[cur_ind] ++;
        pot --;
      } else {
        pot = stones[cur_ind];
        stones[cur_ind] = 0;
      }
      cur_ind = (cur_ind + 1) % n;
    }
}
int main(){
  
  while(true){
    int n;
    int p;
    cin >> n >> p;
    if(n == 0 && p == 0){
      break;
    }

    solve(n, p);
  }

  return 0;
}

C++: #include<iostream>

using namespace std;


int next(int now,int n){
	if(now+1<n)return now+1;
	return 0;
}
int main(){
	int n,q;
	while(cin>>n>>q&&n&&q){
		int data[50];
		int wan=q;
		fill(data,data+50,0);
		int now=0;
		bool end=false;
		while(!end){
			if(wan>0){
				data[now]++;
				wan--;
			}
			else {
				wan=data[now];
				data[now]=0;
			}
			if(data[now]==q){
				end=true;
				cout<<now<<endl;
			}
			now=next(now,n);
		}
	}
	
	
}

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

using namespace std;

int main(){
    while(1){
        int cand, boul;
        cin >> cand >> boul;
        if(cand==0 && boul==0) break;
        
        int turn = 0;
        int allstone = boul;
        vector<int> stone(50,0);
        
        while(1){
            if(boul == 0){
                if(stone[turn] == allstone){
                    cout << turn << endl;
                    break;
                }else{
                    boul = stone[turn];
                    stone[turn] = 0;
                }
            }else{
                stone[turn]++;
                boul--;
            }
            turn = (turn+1)%cand;
        }
    }
            
    return 0;
}

C++: #! /usr/bin/env python
# -*- coding: utf-8 -*-
import copy

(n, p) = map(int, raw_input().split())
while n!=0 and p!=0:
    bawl = copy.copy(p)
    peb = [0]*n
    for i in range(1000000):
        if bawl > 0:
            bawl -= 1
            peb[i%n] += 1
        else:
            bawl += peb[i%n]
            peb[i%n] = 0

        if bawl == p:
            print i%n
            break
    (n, p) = map(int, raw_input().split())

C++: #include <iostream> // cin, cout, cerr, clog
#include <algorithm> // minmax, sort, swap
#include <numeric> // iota, accumulate, inner_product
#include <cstdio> // printf, scanf
#include <climits> // INT_MIN, LLONG_MIN
#include <cmath> // long, trig, pow
#include <string> // string, stoi, to_string
#include <vector> // vector
#include <queue> // queue, priority_queue
#include <stack> // stack
#include <map> // key-value pairs sorted by keys
#include <set> // set
#include <iomanip> // cout<<setprecision(n)

#define rep(i,n) for(int i = 0; i < n; i++)
#define ENDL "\n"
#define print(i) std::cout << (i) << "\n"

#define int long long // at least int64 > 9*10^18
#define all(v) v.begin(), v.end()

signed main() {
	while (1) {
		int n,p;
		std::cin >> n >> p;
		if(n==0) break;
		std::vector<int> candidates(n,0);
		int bowl = p;
		int i = 0;
		while(true) {
			if (bowl>0) {
				candidates[i] ++;
				bowl--;
			}
			else {
				bowl = candidates[i];
				candidates[i] = 0;
			}
			if(candidates[i] == p) break;
			i = (i+1)%n;
		}
		std::cout << i << ENDL;
	}
	return 0;
}