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<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

void solve(int n,int m){
    vector<int> cnt(n);
    int cur=m;
    int idx=0;
    while(1){
        if(cur){
            ++cnt[idx];
            --cur;
        }
        else if(cnt[idx]){
            cur+=cnt[idx];
            cnt[idx]=0;
        }
        if(cnt[idx]==m){
            cout<<idx<<endl;
            return;
        }
        idx=(idx+1)%n;
    }
}

int main(){
    int n,m;
    while(cin>>n>>m,n!=0){
        solve(n,m);
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, p;
int w[100];

int main(){
	while (cin >> n >> p&&n + p){
		fill(w, w + 100, 0);
		int co = 0;
		int wan = 0;
		while (1){
			if (wan == p){
				wan -= w[co];
				w[co] = 0;
				co++;
				if (co == n)co = 0;
				continue;
			}
			w[co]++;
			if (w[co] == p)break;
			co++;
			wan++;
			if (co == n)co = 0;
		}
		cout << co << endl;
	}
}

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

int main(){
  
  while(1){
    int n,p;
    int May_wan[50];
    int i,ans;

  cin >> n >> p;
  int p_max = p;
  if(n == 0&&p == 0)break;
  for(int i = 0; i < 50; i++)May_wan[i] = 0;
   i =0,ans = 0;
  while(1){
    if(p_max == May_wan[i]){
      ans = i;
      break;
}    
    if(p > 0){
      May_wan[i]++;
      p--;
    }

    else if(p == 0){
      p+=May_wan[i];
      May_wan[i] = 0;
    }
    

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

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

int main(){
	while (1){
		int n, p;
		int tp;
		cin >> n >> tp;
		if (n == 0 && tp == 0) break;
		p = tp;
		int N[50] = { 0 };
		//?????????????§?
		for (int win = 0;; win++){
			if (win == n) win = 0;
			p -= 1; //????????????
			N[win] += 1; //???????????????

			//?¢????????????¨???
			if (p == 0){
				//???????????¨?????????????????£??????????????????
				if (N[win] == tp){
					cout << win << endl;
					break;
				}
				else{
					p += N[win];
					N[win] = 0;
				}
			}
		}
	}
	return 0;
}

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

using namespace std;

void solve(int n, int p) {
  int c = p;
  vector<int> stones(n);
  
  int t = 0;
  while (true) {
    if (c > 0) {
      c--;
      stones[t]++;
      if (c == 0 && stones[t] == p) {
        cout << t << endl;
        return;
      }
    } else {
      c = stones[t];
      stones[t] = 0;
    }
    t = (t + 1) % n;
  }
}

int main() {
  int n, p;
  while (true) {
    cin >> n >> p;
    if (n + p == 0) break;
    solve(n, p);
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
using namespace std;

int main()
{
  int n, p;
  while (scanf("%d %d", &n, &p), n|p) {
    int hand[n];
    memset(hand, 0, sizeof(hand));
    int owan = p;
    for (int turn = 0;; turn = (turn + 1) % n) {
      if (owan == 1) {
        int i;
        for (i = 0; i < n; ++i) {
          if (i == turn)
            continue;
          if (hand[i] > 0)
            break;
        }
        if (i == n) {
          printf("%d\n", turn);
          break;
        }
      }
      if (owan > 0) {
        ++hand[turn];
        --owan;
      } else {
        owan += hand[turn];
        hand[turn] = 0;
      }
    }
  }
}

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

int main(){
  int n,p;
  int player[50] = {0};
  int step, stone;

  cin >> n >> p;
  while(n!=0 && p != 0){
    for(int i=0; i<n; i++)
      player[i] = 0;
    step = 0;
    stone = p;

    while(1){
      if(stone == 0){
	stone = player[step];
	player[step] = 0;
      }else{
	if(stone == 1 && player[step] == p-1){
	  break;
	}
	stone--;
	player[step]++;
      }

      if(step < n-1)
	step++;
      else
	step=0;
    }

    cout << step << "\n";
    cin >> n >> p;
  } 

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<vector>
#include<climits>
#include<map>
#include<string>
#include<functional>
#include<iomanip>
#include <deque>

using namespace std;
typedef long long ll;

vector<ll> ans;

int main() {
	while (1) {
		ll n, p, stone[50] = {};
		cin >> n >> p;
		if (n == 0 && p == 0)break;
		for (int i = 0;; i++) {
			if (p > 0) {
				stone[i%n]++;
				p--;
				if (p == 0) {
					bool flag = false;
					for (int j = 0; j < n; j++) {
						if (j != i%n && stone[j] > 0) {
							flag = true;
							break;
						}
					}
					if (!flag) {
						ans.push_back(i%n);
						break;
					}
				}
			}
			else {
				p += stone[i%n];
				stone[i%n] = 0;
			}
		}
	}
	for (int i = 0; i < ans.size(); i++)cout << ans[i] << endl;
	return 0;
}