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;
	int pp[50];
	while(cin >> N >> P, N && P){
		int c = P;
		for(int i = 0; i < N; i++){
			pp[i] = 0;
		}
		
		int idx = 0;
		while(1){
			if(c > 0){
				pp[idx] += 1;
				c -= 1;
				if(pp[idx] == P){
					cout << idx << endl;
					break;
				}
			}else{
				c = pp[idx];
				pp[idx] = 0;
			}
			idx = (idx + 1) % N;
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <string>
#define FI first
#define SE second
#define PF push_front
#define PB push_back
#define PPF pop_front
#define PPB pop_back
#define MA make_pair
#define ll long long
#define PA pair<int,int>
#define VE vector<int>
#define VP vector<PA>
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ROF(i,a,b) for(int i=b-1;i>=a;i--)
#define YES(i) cout<<(i?"YES":"NO")<<endl
#define Yes(i) cout<<(i?"Yes":"No")<<endl
using namespace std;
//
const int INF=1e9+7;
const int mod=1e9+7;
//
struct poi{
	int X;int Y;int Z;
	bool operator<(const poi&R)const{
		return X==R.X ? Y==R.Y ? Z<R.Z : Y<R.Y : X<R.X;
	}
};
/*
ll GCD(ll A,ll B){
  return (B==0)?(A):(GCD(B,A%B));
}
ll LCM(ll A,ll B){
  return A/GCD(A,B)*B;
}
*/
int main(){
	int N,P;
	while(1){
		cin>>N>>P;
		if(N==0&&P==0){
			break;
		}
		int M=P;
		//
		int A[50];
		fill(A,A+N,0);
		int now=0;
		while(1){
			if(P>0){
				P--;
				A[now]++;
				if(A[now]==M){
					cout<<now<<endl;
					break;
				}
				now++;
			}else{
				P=A[now];
				A[now]=0;
				now++;
			}
			if(now==N){
				now=0;
			}
		}
	}
	return 0;
}

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

using namespace std;

const int ddx[8] = {1,1,1,0,0,-1,-1,-1};
const int ddy[8] = {1,0,-1,1,-1,1,0,-1};

bool visited[60][60] ={false};
int v[60][60] = {0};
int h,w;

int dfs(int i ,int j){
    if(visited[i][j] || v[i][j] == 0) return 0;
    
    visited[i][j] = true;

    for(int x = 0; x < 8; x++){
        for(int y = 0; y < 8;y++){
            int posx = j + ddx[x];
            int posy = i + ddy[y];
            if(posx >= 0 && posx < w && posy >= 0 && posy < h){
                if(!visited[posy][posx] && v[posy][posx]){
                    dfs(posy, posx);
                }
            }
        }
    }
    return 1;
}

int main()
{
    while(cin >> w >> h, w and h){
        int ans = 0;

        for(int i = 0; i < 60 ;i++){
            for(int j = 0;j < 60; j++){
                visited[i][j] = false;
            }
        }

        for(int i = 0; i < h ;i++){
            for(int j = 0;j < w; j++){
                cin >> v[i][j];
            }
        }

        for(int i = 0; i < h ;i++){
            for(int j = 0;j < w; j++){
                ans += dfs(i,j);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)

int main(){
	while(1){
		int a,b;
		cin>>a>>b;
		if(a==0 && b==0)break;
		
		int hav[111]={0};
		int pos=0;
		int val=b;
		while(1){
			if(val==0){
				val = hav[pos];
				hav[pos]=0;
			}else{
				hav[pos]++;
				val--;
			}
			if(val==0){
				
				int sum = 0;
				rep(i,a){
					if(i!=pos)sum+=hav[i];
				}
				
				if(sum==0){
					printf("%d\n",pos);
					break;
				}
			}
			pos = (pos+1)%a;
		}
	}
}

C++: #include<iostream>

using namespace std;

int main(){
  int n,p;
  while(1){
    cin >> n >> p;
    if(n == 0 && p == 0)break;
    int pmax = p;
    int s[50]={0};
    int i = 0;
    while(1){
      if(p>0){
	s[i]++;
	p--;
      }else if(p==0){
	p = s[i];
	s[i]=0;
      }

      if(s[i]==pmax)break;
      
      
      if(i == n-1){
	i = 0;
      }else{
	i++;
      }
      
    }
    cout << i << endl;
  }
  
  return 0;
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) (v).begin(),(v),end()
#define pb push_back
typedef long long ll;
typedef pair<int,int> P;

template<typename A,typename B> inline void chmin(A &a,B b){if(a>b) a=b;}
template<typename A,typename B> inline void chmax(A &a,B b){if(a<b) a=b;}
//-----------------------------------------------------------------------

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


    while(true)
    {
        int n,p,cnt[50]={0};
        cin>>n>>p;
        if(n==0 && p==0) break;
     
        int t=p;
        while(true)
        {
            REP(i,n){
                if(t>0) t--,cnt[i]++;
                else t+=cnt[i],cnt[i]=0;
                if(t==0 && cnt[i]==p){
                    cout<<i<<endl;
                    goto g;
                }
            }
        }
        g:;
    }

}

C++: #include<iostream>

using namespace std;

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

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define pb            push_back
template<class T, class S> inline pair<T, S> mp(T x, S y){ return make_pair(x, y); }
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

static const int    INF = 1000000000;
static const ll     MOD = 1000000007LL;
static const double EPS = 1E-12;

int n, p;

int main()
{
  while(cin >> n >> p, n){
    int cnt = p;
    vi d(n, 0);
    bool ok = true;
    while(ok){
      rep(i, n){
        if(cnt){
          cnt -= 1;
          d[i] += 1;
          if(d[i] == p){
            cout << i << endl;
            ok = false;
            break;
          }
        }
        else{
          swap(cnt, d[i]);
        }
      }
    }
  }

  return 0;
}

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

int main(){

  while(1){
    int n,p;
    cin >> n >> p;
    if(n==0)break;
    
    int st[55]={};
    int i=0,hoge=p;

    while(1){
      if( st[i] == hoge-1 && p>0){
	cout << i << endl;
	break;
      }
      if(p>0){
	st[i]++;
	p--;
      }
      else{
	p = st[i]; 
	st[i]=0;
      }

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