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 <vector>

using namespace std;

void solve()
{
	int n, p;
	while(cin >> n >> p)
	{
		if(!n && !p)
		{
			break;
		}
		vector<int> Vec(n);
		int stone = p;
		int count = 0;
		while(true)
		{
			if(stone)
			{
				Vec[count]++;
				stone--;
				if(Vec[count] == p)
				{
					cout << count << endl;
					break;
				}
			}
			else
			{
				stone = Vec[count];
				Vec[count] = 0;
			}
			count++;
			count %= n;
		}
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  for(;;) {
    int n, p;
    cin >> n >> p;
    if(n == 0) break;
    int st[n];
    for(int i = 0; i < n; i++) {
      st[i] = 0;
    }
    int c = 0;
    for(;;) {
      if(p>0) {
        st[c++]++;
        p--;
      } else {
        int nz = 0;
        for(int i = 0; i < n; i++) {
          if(st[i] != 0) nz++;
        }
        if(nz == 1) {
          cout << ((c + n - 1) % n) << endl;
          break;
        }
        p = st[c];
        st[c++] = 0;
      }
      c %= n;
    }
  }
  return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m),n||m){
        vector<int>V(n,0);
        int cnt=0,b=m;
        while(true){
            cnt%=n;
            if(b){
                b--;
                V[cnt]++;
            }
            else{
                b=V[cnt];
                V[cnt]=0;
            }
            if(V[cnt]==m){
                printf("%d\n",cnt);
                break;
            }
            cnt++;


        }
    }

}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
	int n, p;

	while(true){
		scanf("%d %d\n",&n, &p);
		if(n==0 && p==0){return 0;}
		int max = p;
		int* player = new int[n];
		for(int i=0; i<n; i++){ player[i] = 0;}
		
		for(int i=0; i<1000000; i++){
			if(p>0){
				p--;
				player[i%n]++;
			}
			else{
				p = player[i%n];
				player[i%n] =0;
			}
			if(player[i%n] == max){
				cout << i%n << endl;
				break;
			}
		}
		delete[] player;
	}	
}

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

int main(){

  int n,p;
  while(cin>>n>>p&&n!=0&&p!=0){
    int can[50];
    int kazu=p;

    // initialize
    for(int i=0;i<n;i++){
      can[i]=0;
    }
    int count=0;
    while(count<=1000000){
      if(kazu==can[count%n]){
        cout << count%n << endl;
        break;
      }
      if(p==0){
        p=can[count%n];
        can[count%n]=0;
      }else{
        p--;
        can[count%n]++;
      }
      count++;
    }
  }

  return 0;
}

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


int main()
{
    int n, p;
    while(cin >> n >> p && n && p) {
        int step = 0, wan = p;
        int have[n];
        fill(have, have + n, 0);
        while(true) {
            if(have[step % n] == p) 
                break;
            
            if(wan > 0) {
                have[step % n]++;
                wan--;
            } else {
                wan = have[step % n];
                have[step % n] = 0;
            }
            step++;
        }
        cout << (step % n) << endl;
    }
}

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

using namespace std;

  int n,p,a[51];

bool check(int x){
  for(int i=0;i<n;i++)if(a[i]>0 && i!=x)return false;
  return true;
}

int main(void){

  while(cin >> n >> p,n|p){
    fill(a,a+51,0);

    for(int i=0;i<1000001;i++){

      if(p>0)p--,a[i%n]++;
      else if(p==0 ){
	if(check(i%n)){
	  cout << i%n << endl;
	  break;
	}
	p=a[i%n],a[i%n]=0;
      }
    }
  }

  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for (int i=(n)-1;i>=0;i--)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define REPS(i,f,n) for (int i=(f)-1;i>=(n);i--)
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};

void slove(int n, int p){
	int tyawan = p;//??¶?¢??????\??£???????????°
	vint person(n);//????£????????????£???????????°
	rep(i, n) person[i] = 0;//?????????
	int cnt = 0;
	do{
		if (tyawan >= 1){//????????¨???
			person[cnt % n]++;//?????????????????£??????????????¨???????????????mod??§
			tyawan--;
		}else{
			tyawan += person[cnt % n];
			person[cnt % n] = 0;
		}
		cnt++;

	} while (person[cnt % n] + tyawan != p);

	printf("%d\n", cnt % n);
	return;
}

int main(void){
	int n, p;
	scanf("%d %d", &n, &p);
	while(n != 0 || p != 0){
		slove(n, p);
		scanf("%d %d", &n, &p);
	}

	return 0;
}

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

int main(void){
	int n, p;
	while(1){
		cin >> n >> p;
		if(n == 0 && p == 0) break;

		int ma[100] = {};

		int cnt = 0;
		int wan = p;
		while(1){
			if(wan != 0){
				ma[cnt]++;
				wan--;
				if(wan == 0 && ma[cnt] == p){
					cout << cnt << endl;
					break;
				}
			}else{
				wan = ma[cnt];
				ma[cnt] = 0;
			}
			cnt = (cnt + 1) % n;
			//cout << wan << endl;
		}
		
	}

	return 0;
}

C++: #include <bits/stdc++.h>
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;
typedef unsigned long long ULL;

#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 SORT(c) sort((c).begin(),(c).end())

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

const double EPS = 1e-10;
const double PI  = acos(-1.0);

#define CLR(a) memset((a), 0 ,sizeof(a))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int main() {
  int n, p;

  while(cin >> n >> p, n+p) {
    VI m(n);
    int s = p;

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

C++: #include<iostream>
using namespace std;
int main(){
  while(1){
    int n,a,b=0,c=0,k[51]={};
    cin>>n>>a;
    if(n+a==0)break;
    while(1){
      for(int i=0;i<n;i++){
	if(a!=0){
	  a--;
	  k[i]++;
	  if(a==0){
	    for(int j=0;j<n;j++)if(k[j]!=0)b++;
	    if(b==1){
	      cout<<i<<endl;
	      c++;
	    }
	    else b=0;
	  }
	}else{
	  a+=k[i];
	  k[i]=0;
	}
      }
      if(c==1)break;
    }
  }
  return 0;
}