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++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

void hoge(int n, int p) {
	vector<int> a(n);
	int _p = p;
	for (;;)
		for (int i = 0; i < n; i++) {
			if (p > 0) { p--; a[i]++; }
			else { p += a[i]; a[i] = 0; }
			if (a[i] == _p) {
				cout << i << endl;
				return;
			}
		}
}

int main() {
	for (;;) {
		int n, p; cin >> n >> p;
		if (n == 0 && p == 0) break;
		hoge(n, p);
	}
}

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


int main(void){

	int n, p;

	while (cin >> n >> p, n, p){

		int num[100] = { 0 };
		int stack = p;
		int result;

		for (int i = 0; i < 1100000; i++){
			if (stack == 0){
				stack += num[i%n];
				num[i%n] = 0;
			}
			else if (stack == 1 && num[i%n] == p - 1){
				result = i % n;
				break;
			}
			else{
				stack--;
				num[i%n]++;
			}
		}

		cout << result << endl;

	}

	return 0;
}

/*
for(i=0;i<N;i++){
cin >> X[i];
}


for(i=0;i<N;i++){
for(j=0;j<N;j++){
cin >> X[i][j];
}
}
*/

C++: //
//  main.cpp
//  AOJ1159
//
//  Created by aki33524 on 2014/07/15.
//  Copyright (c) 2014年 aki33524. All rights reserved.
//

#include <iostream>
#include <vector>
using namespace std;

void solve(int n, int p){
    vector<int> cans(n, 0);
    int peb = p;
    int pos = 0;
    
    while(1){
        if(0 < peb){
            cans[pos]++;
            peb--;
            if(cans[pos] == p)
                break;
        }else{
            peb += cans[pos];
            cans[pos] = 0;
        }
        pos = (pos+1)%n;
    }
    cout << pos << endl;
}

int main(int argc, const char * argv[])
{
    int n, p;
    while(cin>>n>>p, n|p){
        solve(n, p);
    }

    return 0;
}

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

using namespace std;

#define repl(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define rep(i,n) repl(i,0,n)
#define maxch(a,b) a=max(a,b)

int n, p;
int c[50];

int main(int argc, char const *argv[])
{
  while(cin >> n >> p, n | p){
    memset(c, 0, sizeof(c));
    int cnt = 0, turn = 0;
    int tmp = p;
    while(true) {
      if (p > 0) {
        c[turn]++;
        p--;
        if(tmp == c[turn]) break; 
      } else {
        p = c[turn];
        c[turn] = 0;
      }
      cnt++;
      turn = (turn + 1) % n;
    }

    cout << turn << endl;
  }
  return 0;
}

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

int main(){
  int n,p;
  while(cin>>n>>p,n){
    int man[n];
    for(int i=0;i<n;i++){
      man[i]=0;
    }
    int cnt=0;
    int stone=p;
    while(man[cnt]!=p){
      if(stone!=0){
	man[cnt]++;
	stone--;
      }else{
	stone=man[cnt];
	man[cnt]=0;
      }
      if(cnt==n-1)cnt=0;
      else cnt++;
    }
    cout<<cnt<<endl;
  }
  return 0;
}

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

int main() {
	int n, p;
	while(cin >> n >> p, n || p) {
		int pp = p;
		int cnt[60] = {};
		int i = 0;
		while(1) {
			if(pp == 0) {
				pp = cnt[i];
				cnt[i] = 0;
			}
			else {
				pp -= 1;
				cnt[i]++;
				if(cnt[i] == p) {
					cout << i << endl;
					break;
				}
			}
//			cout << "@" << cnt[i] << endl;
			i++;
			i %= n;
		}
	}
}

C++: #include <iostream>
#include <vector>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

template <typename X, typename T>
auto vectors(X x, T a) {
	return vector<T>(x, a);
}

template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
	auto cont = vectors(y, z, zs...);
	return vector<decltype(cont)>(x, cont);
}

template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
	rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
	for(T& x: v){ is >> x; } return is;
}

int main(){
	int n, p;
	while(cin >> n >> p, n||p){
		int sum = p;
		int idx = 0;
		vector<int> a(n, 0);
		while(true){
			if(sum == 0){
				sum = a[idx];
				a[idx] = 0;
			}else{
				sum--;
				a[idx]++;
			}
			if([&]() -> bool {
					int cnt = 0;
					rep(i,n){
						if(a[i] == 0) cnt++;
					}
					return n - 1 == cnt and sum == 0;
					}()
			  ){
				break;
			}
			idx++;
			idx %= n;
		}
		cout << idx << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n, p;
    while(cin >> n >> p, n|p){
        vector<int> v(n); int cur = 0, cnt = 0;
        int pp = p;
        while(true){
            if(p) {
                v[cur]++, p--;
                if(p == 0 && v[cur] == pp){
                    cout << cur << "\n";
                    break;
                }
            }
            else {
                p += v[cur];
                v[cur] = 0;
            }
            cur++;
            if(cur >= n) cur -= n;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <climits>
#include <stack>
#include <string>
#include <random>
#include<set>
#include <iomanip>
#include <climits>
#include <memory>

int main() {
	int n, p;
	std::cin >> n >> p;
	while (n != 0) {
		std::vector<int> players(n, 0);
		int pos = 0;
		int in_cup = p;
		while (in_cup != 1 || players[pos] + 1 != p) {
			if (in_cup > 0) {
				players[pos]++;
				in_cup--;
			}
			else {
				in_cup += players[pos];
				players[pos] = 0;
			}
			pos = (pos + 1) % n;
		}
		std::cout << pos << std::endl;
		std::cin >> n >> p;
	}
}

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

int main(){
    while(1){
        int n,p;
        int flag=0;
        int have[50];
        //読み込み
        cin>>n>>p;
        int cup = p;
        if(n == 0 && cup==0){
            return 0;
        }

        for(int i=0; i<n; i++){
            have[i]=0;
        }

        for(int i=0;;i=(i+1)%n){
            if(cup==0){
                cup = have[i];
                have[i]=0;
            }else if(cup>0){
                have[i]++;
                cup--;
                if(have[i]==p){
                    cout<<i<<endl;
                    break;
                }
            }
        }
    }
}