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

#define REP(i,n) for(int i=0;i<n;++i)
#define RREP(i,n) for(int i=n-1;i>=0;--i)
#define FOR(i,m,n) for(int i=m;i<n;++i)
#define RFOR(i,m,n) for(int i=m-1;i>=n;--i)
#define ALL(v) v.begin(),v.end()
#define PB(a) push_back(a)
#define INF 1000000001
#define MOD 1000000007

using namespace std;


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (1) {
		int n, p;
		cin >> n >> p;
		if (n == 0) break;
		int* m = new int[n];
		REP(i, n) m[i] = 0;
		for (int i = 0;;++i) {
			int j = i%n;
			if (p == 0) {
				p += m[j];
				m[j] = 0;
			}
			else {
				p--;
				m[j]++;
				if (p == 0) {
					int flag = 1;
					for (int k = 0;k < n;++k) {
						if (m[k] !=0&&k!=j)flag = 0;
					}
					if (flag) {
						cout << j << endl;
						break;
					}
				}
			}
			
		}
		delete[] m;
		
	}
	return 0;
}

C++: #include <stdio.h>
int main()
{
  int n,i,t,max,min,sum;
  while(1){
    scanf("%d",&n);
    if(n==0)break;
    max=0;
    min=1001;
    sum=0;
    for(i=0;i<n;i++){
      scanf("%d",&t);
      if(t>max)max=t;
      if(t<min)min=t;
      sum+=t;
    }
    sum=sum-max-min;
    printf("%d\n",sum/(n-2));
  }
  return 0;
}

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

int main(){
  int n,p;
  vector<int> person(50);


  while(1){
    int i=0;
    cin>>n>>p;
    if(n==0&&p==0)break;
    for(int i=0;i<n;i++)person[i]=0;
    
    while(1){
      int flag=0;
      if(p!=0){ 
	person[i]+=1;
	p--;
      }else if(p==0&&person[i]>0){
	p=person[i];
	person[i]=0;
      }
      
      if(p==0){
	//	for(int k=0;k<n;k++) cout<<person[k];
	//	cout<<endl;
	//	cout<<flag<<endl;
	for(int j=0;j<n;j++){
	  if(person[j]>0){
	    flag++;
	  }
	  if(flag>1)break;
	}
      }

      if(flag==1)break;
      i++;
      if(i==n)i=0;  
    }
    cout<<i<<endl;
    
  }

  return 0;
}

C++: //YukiOtake-2018/03/04
//time:12:11
import java.util.Scanner;
class Main{
	public static void main(String args[]){
		Scanner scan = new Scanner(System.in);
		while(true){
      int n=scan.nextInt();
      int p=scan.nextInt();
      // int n=3,p=2;
      if(n==0 && p==0){
        break;
      }
      int[] data=new int[n];
      int i=0,temp=p;
      while(true){
        if(i==n){
          i=0;
        }
        if(temp>0){
          temp--;
          data[i]++;
        }
        else{
          temp=data[i];
          data[i]=0;
        }
        if(data[i]==p){
          break;
        }
        i++;        
      }
      System.out.println(i);
		}
	}
}

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

int main() {
    int n, p;
    while(cin >> n >> p, n) {
        const int total_p = p;
        int current_player = 0;
        vector<int> v(n);
        int winner = 0;
        while(true) {
            if(p > 0) {
                ++v[current_player];
                --p;
                current_player = (current_player+1)%n;
            } else if(p == 0) {
                if(v[current_player] == total_p) {
                    winner = current_player;
                    break;
                } else {
                    p += v[current_player];
                    v[current_player] = 0;
                    current_player = (current_player+1)%n;
                }
            }
        }
        cout << winner << endl;
    }
}

C++: #include <iostream>

using namespace std;

int cmax(int n, int count[50]){
	int max;
	for(int i = 0;i < n;i++){
		if(i == 0 || max < count[i]){
			max = count[i];
		}
	}

	return max;
}

int main(){
	int n, p;

	while(1){
		cin >> n >> p;
		if(n == 0 && p == 0) break;

		int i = 0;
		int q = p;
		int count[50] = {0};
		while(1){
			if(p == 1){
				count[i]++;
				p--;
				if(count[i] == q) break;
			} else if (p == 0){
				p += count[i];
				count[i] = 0;
			} else {
				count[i]++;
				p--;
			}

			if(i == n - 1){
				i = 0;
			} else {
				i++;
			}
		}

		cout << i << endl;
	}

	return 0;
}

C++: #include "bits/stdc++.h"
#define REP(i,a,b) for(i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define SQR(X)  ( (X)*(X) )
#define CUBE(X) ( (X)*(X)*(X) )
#define ALL(a) (a).begin(),(a).end()
#define ifnot(a) if(not a)
#define dump(x)  cerr << #x << " = " << (x) << endl
using namespace std;
 
 
void reader(int &a){scanf("%d",&a);}
void reader(double &a){scanf("%lf",&a);}
void reader(char a[]){scanf("%s",a);}
void reader(ll &a){scanf("%lld",&a);}
void reader(ull &a){scanf("%llu",&a);}
// void reader(string& a){cin >> a;};
template<class T,class U> void reader(T& t,U& u){reader(t); reader(u);}
template<class T,class U,class V> void reader(T& t,U& u,V& v){reader(t); reader(u); reader(v);}
 
void writer(int a,char c) {printf("%d",a); putchar(c);}
void writer(ll a,char c) {printf("%lld",a); putchar(c);}
void writer(double a,char c) {printf("%.20lf",a); putchar(c);}
void writer(char a[]) {printf("%s",a);};
void writer(char a[],char c) {printf("%s",a);putchar(c);};
void writer(char a,char c) {putchar(a); putchar(c);};
template<class T>void writerLn(T t){writer(t,'\n');}
template<class T,class U>void writerLn(T t,U u){writer(t,' ');writer(u,'\n');}
template<class T,class U,class V> void writerLn(T t,U u,V v){writer(t,' ');writer(u,' ');writer(v,'\n');}
template<class T> void writerArr(T x[], int n){int i;if(!n){putchar('\n');return;}rep(i,n-1) writer(x[i],' ');writer(x[n-1],'\n');}
template<class T> void writerVec(vector<T> x){int n=x.size();int i;if(!n){putchar('\n');return;}rep(i,n-1) writer(x[i],' ');writer(x[n-1],'\n');}
 

 
// #define int ll
 
bool test = 1;

//............................
#define MAX 300000
signed main(){
  int i,j,k,l;
  while(1) {
	int n,p;
	cin >> n >> p;
	if(n == 0) break;
	vector<int> a1(n);
	int step = 0;
	int pabble_of_bowl = p;
	for(;;step++) {
	  if(pabble_of_bowl) {
		a1[step%n]++;
		pabble_of_bowl--;
		if(a1[step%n] == p) {
		  goto end;
		}
	  }
	  else {
		pabble_of_bowl += a1[step%n];
		a1[step%n] = 0;
	  }
	}
  end: 
	cout << step%n << endl;
  }
  return 0;
}