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 n, p, stone_left, P[50], winner;

int main(){
    while(scanf("%d%d", &n, &p) && n*p!=0){
        stone_left = p;
        winner = n;
        for (int i = 0; i < n; i++) {
            P[i] = 0;
        }
        while(1){
            for(int i = 0; i < n; i++){
                if(stone_left != 0){
                    P[i] += 1;
                    stone_left -= 1;
                    if(P[i] == p) {
                        winner = i;
                        cout << winner << endl;
                        break;
                    }
                    if(winner != n) break;
                } else {
                    stone_left = P[i];
                    P[i] = 0;
                }
            }
            if(winner != n) break;
        }
    }
}

C++: #include <iostream>

using namespace std;

int main()
{
    int n, p;

    while (cin >> n >> p, n || p) {
        int pl[50];

        for (int i = 0; i < n; i++)
            pl[i] = 0;

        int idx, res;
        
        idx = 0;
        res = p;
        while (true) {
            if (pl[idx] == p) {
                cout << idx << endl;
                break;
            }

            if (res == 0) {
                res += pl[idx];
                pl[idx] = 0;
            }
            else {
                pl[idx]++;
                res--;
            }
            idx = (idx + 1) % n;
        }
    }

    return 0;
}

C++: #include<stdio.h>
int main(void){
    int n,p;
    int i;
    while(1){
        scanf("%d %d",&n,&p);
        if(n==0&&p==0){
            break;
        }
        int mem[50]={0};
        int num=p;
        while(1){
            for(i=0;i<n;i++){
                if(num==0&&mem[i]==p){
                    printf("%d\n",i);
                    break;
                }
                else if(num==0&&mem[i]!=p){
                    num=mem[i];
                    mem[i]=0;
                }
                else{
                    num--;
                    mem[i]++;
                }
            }
            if(num==0&&mem[i]==p){
                break;
            }
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main(){

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

    vector<int> V(n);
    int count = 0;

    for(i = 0; i < n; i++){
      if(p == 0){
        p += V[i]; V[i] = 0;
      }
      else if(p != 0){
        p--; V[i]++;
      }

      if(p == 0){
        for(int j = 0; j < n; j++){
          if(V[j] != 0) count++;
        }
      }
      if(count == 1) break;
      //cout << count << " ";
      count = 0;
      if(i == n-1) i = -1;
    }
    cout << i << endl;
  }

	return 0 ;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,p;
  while(cin >> n >> p && n && p){
    vary(ll,v,n,0);
    ll pl = 0,P = p;
    while(1){
      if(p){
        --p;
        v[pl]++;
      }
      else{
        p += v[pl];
        v[pl] = 0;
      }
      if(v[pl] == P){
        cout << pl << endl;
        break;
      }
      ++pl;
      pl %= n;
    }
  }
  return 0;
}

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

using namespace std;

int solve(int N, int P) {
	int p = P;
	vector<int> have(N, 0);
	int turn = 0;
	while (1) {
		if (p > 0) {
			--p;
			if (++have[turn] == P) return turn;
		}
		else {
			swap(p, have[turn]);
		}
		turn = (turn + 1) % N;
	}
}

int main() {
	int N, P;
	while (cin >> N >> P, N || P) {
		cout << solve(N, P) << endl;
	}
}

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

int allnostone(int,int,int []);

int main(void){
	
	while(1){
		//入力
		int kouho_num,wan;
		cin >> kouho_num>> wan;
		if(kouho_num==0&&wan==0)
			break;

		//選出
		int current=0;
		int *kouho = new int[kouho_num];
		for(int i=0; i<kouho_num; i++)
			kouho[i]=0;
		while(1){
			if(wan!=0){
				kouho[current]++;
				wan--;
				if(wan==0){
					if( allnostone(kouho_num,current,kouho) )
						break;
				}
			}else if(kouho[current]!=0){
				wan+=kouho[current];
				kouho[current]=0;
			}
			current++;
			if(current>=kouho_num)
				current=0;
		}

		cout << current << endl;
	}
}

int allnostone(int kouho_num,int current,int kouho[]){
	for(int i=0; i<kouho_num; i++){
		if( current!=i&&kouho[i]!=0 )
		return 0;
	}
	return 1;
}

C++: #include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
int main(void){
    int n,r,p;
    while(cin>>n>>r,n){
        vector<int> mayor(n,0);
        p=r;
        rep(i,1000000){
            if(p==0){
                p+=mayor[i%n]; mayor[i%n]=0;
            }else{
                p--;mayor[i%n]++;
                if(mayor[i%n]==r){
                    cout<<i%n<<endl;
                    break;
                }
            }
        }
    }
}