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;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { 1, -1, 0, 0,  1, -1,  1, -1 };
static const int dy[] = { 0, 0, 1, -1 , 1,  1, -1, -1 };

int n, m;
VI Mayors;

int gcd( int m, int n ) {
	if ( n == 0 ) return m;
	return gcd( n, m % n );
}

int main() {
	while ( ~scanf( "%d %d", &n, &m ) && n && m ) {
		Mayors.resize( n );
		for ( int i = 0; i < n; i++ ) {
			Mayors[ i ] = 0;
		}
		int now = 0;
		int now_stone = m;
		while ( true ) {
			if ( !now_stone ) {
				now_stone += Mayors[ now ];
				Mayors[ now ] = 0;
				now++;
				now %= n;
				continue;
			}
			else {
				Mayors[ now ]++;
				now_stone--;
				if ( !now_stone ) {
					if ( Mayors[ now ] == m ) {
						break;
					}
				}
				now++;
				now %= n;
			}
		}
		printf( "%d\n", now );
	}
	return 0;
}

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

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TEMP_T T    cums(T& v){ T r(v.size()); partial_sum(v.begin(), v.end(), r.begin()); return r; }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }
TEMP_T T    acum(vector<T>& v, T b=0){ return accumulate(v.begin(), v.end(), b); }
TEMP_T vector<T> inpt(int n){ vector<T> v(n); for (int i=0; i<n; i++) cin >> v[i]; return v; }

static inline int in(){ int x; scanf("%d", &x); return x; }

int main()
{
  int n, p;
  while (n = in(), p = in(), n || p){
    int id = 0;
    int stone[64] = {};
    int cnt = p;
    int have_user = 0;
    while (1){
      if (cnt == 1 &&
          ((stone[id] && have_user == 1) ||
          (!stone[id] && have_user == 0))){
        break;
      }
      if (cnt == 0){
        if (stone[id]) have_user--;
        cnt = stone[id];
        stone[id] = 0;
      }
      else {
        if (stone[id] == 0) have_user++;
        cnt--;
        stone[id]++;
      }
      id = (id + 1) % n;
    }
    printf("%d\n", id);
  }
  return (0);
}

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

int main(){
    int n,p;
    while(cin>>n>>p){
        if(n==0)break;
        int s[n];
        memset(s,0,sizeof(s));
        ll cnt=0;
        ll ss=p;
//        ll i=0;
        bool flag=false;
        while(1){
            for(ll i=0;i<n;i++){
                if(p>0){
                    s[i]++;
                    p--;
                }else{
                    ll a = s[i];
                    s[i]=0;
                    p+=a;
                }
                if(p==0){
                    for(int i=0;i<n;i++){
                        if(s[i]==ss){
                            flag=true;
                            cnt=i;
                            break;
                        }
                    }
                }
                if(flag)break;
            }
            // for(int i=0;i<n;i++){
            //     cout<<"s:"<<s[i]<<endl;
            // }
            if(flag)break;
        }
        cout<<cnt<<endl;
    }
}

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

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

int main()
{
	int n,p,flag=0,mayor;
	while(cin>>n>>p , n!=0 && p!=0)
	{
		int stone[50]={0};
		while(1)
		{
			for(int i=0;i<n;i++)
			{
				if(p>0)//石があれば取る
				{
					stone[i]++;
					p--;
					if(p==0)//お椀の石がなくなったとき
					{
						flag=1;
						for(int j=0;j<i;j++)
							if(stone[j]!=0)//他に石を持っている人がいたら
							{
								flag=0;
								break;
							}
						for(int j=i+1;j<n;j++)
							if(stone[j]!=0)
							{
								flag=0;
								break;
							}
						if(flag==1)
						{
							mayor=i;
							break;
						}
					}
								
				}
				else//お椀の石がないとき
				{
					p=stone[i];//お椀に石を入れる
					stone[i]=0;
				}
			}
			if(flag==1)
			{
				cout<<mayor<<endl;
				break;
			}
		}
	}
}

C++: #define _GLIBCXX_DEBUG //vectorの範囲外アクセスなどのエラーメッセージが分かりやすくなるらしい


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
//#include <algorithm>

// using namespace std;
#include<numeric>

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<cstring>
#include<cmath>
#include<complex>


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

#define FOR(i,c) for(typeof((c).begin())i=(c).begin();i!=(c).end();++i)


#define EXIST(e,s)((s).find(e)!=(s).end())
#define RESET(a) memset((a),0,sizeof(a))
#define SET(a) memset((a),-1,sizeof(a))

 



#define ll long long

using namespace std;

int main(void){
  int n;
  int p;

  int temoto[50];
  int wan;
  bool flag;

  while(cin >> n >> p, n || p){
    flag = false;
    memset(temoto, 0, sizeof(temoto[0])*50);
    wan = p;
    while(1){
      for(int i=0; i<n; i++){
	if(wan > 0){
	  wan--;
	  temoto[i]++;
	  if(temoto[i] == p){
	    cout << i << endl;
	    flag = true;
	    break;
	  }
	}else{
	  wan += temoto[i];
	  temoto[i] = 0;
	}
      }
      if(flag){
	break;
      }
    }
  }
    
    
    
    return 0;
}