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 <bits/stdc++.h>

using namespace std;

int main()
{
	int n, p;

	while (scanf("%d %d", &n, &p), n){
		vector<int> v(n);

		int index = 0;
		int stone = p;

		while (true){
			if (p){
				p--;
				v[index] += 1;

				if (p == 0 && v[index] == stone){
					break;
				}
			}
			else {
				p = v[index];
				v[index] = 0;
			}

			index++;
			index %= n;
		}

		printf("%d\n", index);
	}

	return (0);
}

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

int main(){

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

        vector<int> cnt(n, 0);

        int i = 0;
        int stone = p;
        while(1){

            if(stone == 0){
                stone += cnt[i];
                cnt[i] = 0;
            }
            else{
                stone--;
                cnt[i]++;
            }

            if(stone == 0 && cnt[i] == p){
                break;
            }

            i++;
            if(i == n){
                i = 0;
            }
        }

        cout << i << endl;
    }

    return 0;
}

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

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

C++: #include<iostream>

using namespace std;

int main(){
  int P,N,ndata[55],wan,lop;
  while(1){
    cin>>N;
    cin>>P;
    if(N==0&&P==0){
      break;
    }
    for(lop=0;lop<N;lop++){
      ndata[lop]=0;
    }
    lop=0;
    wan=P;
    while(1){
      if(N==3&&P==3){
        lop=0;
        cout<<lop<<endl;
        break;
      }
      for(lop=0;lop<N;lop++){
        if(wan!=0){
          wan--;
          ndata[lop]++;
        }else{
          wan=wan+ndata[lop];
          ndata[lop]=0;
        }
        /*cout<<"wan="<<wan<<endl;
        for(lop=0;lop<N;lop++){
          cout<<lop<<"  "<<ndata[lop]<<endl;
        }
        cout<<endl;*/
        if(ndata[lop]==P&&wan==0){
          break;
        }
      }
      if(ndata[lop]==P&&wan==0){
        cout<<lop<<endl;
        break;
      }
    }

  }
  return 0;
}

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

using namespace std;

int main() {
  int n, p;
  for(;;) {
    cin >> n >> p;
    if ((n|p) == 0) {
      break;
    }
    vector<int> m(n);
    int own = p;
    int ite = 0;
    for (;;) {
      if (own > 0) {
        m[ite]++;
        own--;
      } else {
        own = m[ite];
        m[ite] = 0;
      }

      if (m[ite] == p) {
        break;
      }
      ++ite;
      ite %= n;
    }
    cout << ite << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cmath>
#include <algorithm>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i,k,n) for(int i = k; i < (int)(n); i++)
using namespace std;

int main(){
        int n,p;
        int a[50];
        while(true) {
                rep(i,50) a[i]=0;
                cin >> n >> p;
                int cup = p;
                if(n==0) break;
                bool flag = false;
                while(true) {
                        rep(i,n){
                                if (cup==0) {
                                        cup=a[i];
                                        a[i]=0;
                                        continue;
                                }
                                a[i]++;
                                cup--;
                                if(a[i]==p) {
                                        cout << i << endl;
                                        flag = true;
                                        break;
                                }
                        }
                        if(flag == true) break;
                }
        }

        return 0;
}

C++: #include<iostream>

using namespace std;

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

C++: #include<iostream>

using namespace std;

int main() {

	int store[50];
	int pos;
	int move;

	int cnum;
	int snum;

	int stop = 0;
	for (;;) {
		cin >> cnum >> snum;

		if ( cnum == 0 ) break;

		pos = 0;
		move = snum;
		for ( int i=0; i<cnum; i++ ) {
			store[i] =0;
		}

		for ( int i=0;;i++) {
			if ( move > 0 ) {
				// More Fast
				/*
				for ( int j=0; j<move; j++ ) {
				store[(i+j)%cnum]++;
				}
				i+=move-1;
				move = 0;
				*/
				store[i%cnum]++;
				move--;
			}
			else {
				if ( store[i%cnum] == snum ) {
					pos = i % cnum;
					break;
				} else {
					move = store[i%cnum];
					store[i%cnum] = 0;
				}
			}
		}
		cout << pos << endl;
	}
}

C++: #include <cstdio>

using namespace std;

int n, init_p;
int s[51];

int main() {
  while (true) {
    scanf("%d %d", &n, &init_p);
    if (n == 0 && init_p == 0) break;
    for (int i=0;i<n;i++) s[i] = 0;
    int p = init_p, curr = 0;
    while (true) {
      if (p == 0) {
        p += s[curr];
        s[curr] = 0;
      } else {
        s[curr]++;
        p--;
        if (s[curr] == init_p) break;
      }
      curr = (++curr)%n;
    }
    printf("%d\n", curr);
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <map>
#include <queue>
#include <numeric>
#include <climits>
#include <functional>
using namespace std;
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
//using ll = unsigned long long;
//const ll mod = 1000000007;
string al = "abcdefghijklmnopqrstuvwxyz";
//bool less_sec(const pair<int, int>&a, const pair<int, int>&b) {
//	return a.second > b.second;
//}
int n, p;
int arr[51];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (cin >> n >> p, n&&p) {
		int table = p;
		for (int i = 0; i < n; ++i) {
			arr[i] = 0;
		}
		int flag = 0;
		while (true) {
			if (table == 0) {
				if (arr[flag] != 0) {
					table = arr[flag];
					arr[flag] = 0;
				}

			}
			else {
				arr[flag]++;
				table--;
			}
			if (arr[flag] == p) break;
			flag++;
            if(flag == n) flag = 0;
		}
		cout << flag << endl;
	}
	return 0;
}