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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>


using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x*x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXISTch(s,c) ((((s).find_first_of(c)) != std::string::npos)? 1 : 0)//cがあれば1 if(1)
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  FOR(i,0,n)
#define loop(n) FOR(i,0,n)
#define rrep(i,a,b) for(int i=(a);i>=(b);--i)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;//10^9+7
const LL INF2 = (LL)100000000000000000;//10^18



int main(){ 

	int n, p;

	while (1) {
			cin >> n >> p;

			if (n == 0 && p == 0)break;

			vector<int>hand(n, 0);

			int turn = 0;
			int wan = p;

			while (true) {
				if (wan > 0) {
					wan--;
					hand[turn]++;

					if (hand[turn] == p) {
						break;
					}
				}
				else {
					wan += hand[turn];
					hand[turn] = 0;
				}

				turn = (turn + 1) % n;
			}

			cout << turn << endl;
		}

	return 0;
}

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

int main(void){
	int a[50] = {0};
	int n,p,dp,i = 0;

	while(cin >> n >> p, n){
		i = 0; dp = p;
		memset(a, 0, sizeof(a));
		while(1){
			if(dp){ a[i]++; dp--;}
			else{ dp = a[i]; a[i] = 0;}
			if(a[i] == p) break;
			if(++i == n) i = 0;
		}
	cout << i << endl;
	}
	return 0;
}

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

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

#define lf double
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll n, p;
	while(cin>>n>>p, n!=0){
		vll pos(n, 0);
		ll m = p;
		for(ll i=0; ;i++){
			int j = i % n;
			if (p>0){
				p--;
				pos[j]++;
				if (pos[j] == m){
					cout << j << "\n";
					break;
				}
			}else{
				p += pos[j];
				pos[j] = 0;
			}
		}
	}
	return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

int main(void){
    for(int n, p, res; cin >> n >> p, n; cout << res << endl){
        res = 0;
        int rest = p;
        vi s(n);

        while(1){
            if(rest != 0){
                s[res]++;
                rest--;

                if(rest == 0 && s[res] == p){
                    break;
                }
            }
            else{
                rest += s[res];
                s[res] = 0;
            }
            res = (res + 1) % n;
        }
    }

	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>
#include <vector>
using namespace std;
int main(){
	int n, p, q;
	while( 1 ){
		cin >> n >> p;
		vector<int> stone(n);
		if( n == 0 && p == 0 ) break;
		for( int j = 0; j < n; j++ ) stone[j] = 0;
		q = p;
		int i = 0;
		while( 1 ){
			if( q != 0 ){
				stone[i]++;
				q--;
				if( q == 0 && p == stone[i] ){
					cout << i << endl;
					break;
				}
			}else{
				q += stone[i];
				stone[i] = 0;
			}
			i++;
			if( i == n ) i = 0;
		}
	}
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
#define INF 1e+18
#define int long long
using namespace std;

signed main(){
	int n,p;
	while(cin >> n >> p,n){
		int wan = p,cnt[50] = {},pos = 0;
		while(1){
			if(wan == 0){
				int win = 0,m;
				for(int i = 0;i < n;i++){
					if(cnt[i]){
						win++;
						m = i;
					}
				}
				if(win == 1){
					cout << m << endl;
					break;
				}
				wan += cnt[pos];
				cnt[pos] = 0;
			}else{
				wan--;
				cnt[pos]++;
			}
			pos = (pos + 1) % n;
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  while(1){
    int n,p;
    if(n==0&&p==0)break;
    cin>>n>>p;
    int k=p;
    int ch[51]={},f=0;
    while(1){
      if(k==0){
	k=ch[f%n];
	ch[f%n]=0;
      }     
      else{
	ch[f%n]++;
	k--;
      }	 
      if(ch[f%n]==p)break;
      f++;
    }
    cout<<f%n<<endl;
  } 
  return 0;
}