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<algorithm>
#include<cmath>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<utility>
#include<vector>
using namespace std;

typedef long long lint;

int main(){
    while(true){
        int n, p;
        cin >> n >> p;
        if(n == 0 && p == 0){
            break;
        }
        vector<int> c(n, 0);
        int i = 0, ans, last;
        while(true){
            i = i % n;
            if(p > 0){
                p--;
                c[i]++;
                last = i;
            }else if(c[i] > 0){
                if(i == last){
                    ans = i;
                    break;
                }
                p = c[i];
                c[i] = 0;
            }
            i++;
        }
        cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

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

// container util
#define all(x) (x).begin(), (x).end()

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

// typedef
typedef long long lint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

// constant
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};

// 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();
}

//
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//

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

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

      i = (i + 1) % n;
    }
  }

  return 0;
}

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

using namespace std;

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


int main(){

	while(1){

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

		vector<int> h(n,0);

		int wan = p;
		int t = 0;

		while(1){
			if(wan > 0){
				h[t%n]++;
				wan--;
				if(wan == 0 && h[t%n] == p){
					cout << t%n << endl;
					break;
				}
			}
			else{
				wan = h[t%n];
				h[t%n] = 0;
			}
			t++;
		}
	}

	return 0;
}

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

C++: #pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
typedef  unsigned long long int ull;
typedef pair<ll,ll> P;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
int main(){
    int n,p;
  while(1){
   cin>>n>>p;
   if(n==0||p==0)return 0;
   int sum=p;
   V<int> a(n,0);
   int i=0;
   while(1){
       if(a[i]==sum){
           cout<<i<<endl;
           break;
       }
       if(p==0){
           p+=a[i];
           a[i]=0;
       }
       else {
           a[i]++;
           p--;
       }
       i++;
       i%=n;
   }
  }
}

C++: #include <stdio.h>
#include <iostream>


#define rep(i,a,b)(long i=a;i<b;i++)

using namespace std;


int main(void){

	while(1){

	int ary[50]={0};
	int wan=0;
	int num;

	cin >> num >> wan;

	if(!num&&!wan)
		break;

	int i=0;
	while(1){
		if(wan){
			ary[i]++;
			wan--;

			if(!wan){
				bool flag=0;
				for(int j=0;j<num;j++){
					if(ary[j]&&j!=i){
						flag=1;break;
					};
				}
				if(!flag)
					break;
			}

		}else{
			wan=ary[i];
			ary[i]=0;
		}

		i++;
		if(i==num)
			i=0;
	
	}

	cout << i<<endl;

	}

	return 0;
}

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



using namespace std;
int n;
int p;
int member[50];
void solve(){
  int stone = p;
  long i = 0;
  while(true){
    //cout << "tarn" << i << "stone,0,1,2 ="<< stone <<"," <<member[0] << ","<< member[1]<< "," <<member[2] <<endl;

    if(stone == 1 && member[i%n] == p-1){
      cout << i%n << endl;
      member[i%n] = 0;
      break;
    }else if(stone > 0){
      member[i%n] ++;
      stone --;
      i++;
    }else{
      stone += member[i%n] ;
      member[i%n] = 0;
      i++;
    }
  }
}

int main(){

  while(true){
    cin >> n >> p;
    if(n == 0 && p == 0) break;
    solve();
  }

  return 0;
}

C++: #include<cmath>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cstdio>
#define rep(i,n) for(int i=0;i<n;i++)
#define fori(i,k,n) for(int i=k;i<n;i++)
using namespace std;
const int dx[4] = { 0, 1, 0, -1 }, dy[4] = { 1, 0, -1, 0 };
const int dX[8] = { 0, 1, 1, 1, 0, -1, -1, -1 }, dY[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };



int main()
{
	int n, p;
	cin >> n >> p;
	while (n != 0 || p != 0)
	{
		int ss[100] = {};
		int k = p;
		bool owari = true;
		while (owari)
		{
			for (int i = 0; i < n; i++)
			{
				if (k == 0)
				{
					k += ss[i];
					ss[i] = 0;
				}
				else
				{
					k--;
					ss[i]++;
					if (k == 0)
					{
						bool en = false;
						for (int l = 0; l < n; l++)
						{
							if (l == i)continue;
							if (ss[l] != 0)en = true;
						}
						if (en == false)
						{
							cout << i << endl;
							owari = false;
							break;
						}
					}
				}
			}
		}





		cin >> n >> p;
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
#define N 50

int main(){
	while(1){
		int n,m;
		int ca[N];
		cin >> n >> m;
		if(!n&&!m) break;
		fill(ca,ca+n,0);
		
		int wan = m;
		int k = 0;
		while(1){
			while(wan){
				ca[k]++;
				k++;
				wan--;
			}
			if(ca[k-1]==m){
				break;
			}else{
				wan = ca[k-1];
				ca[k-1] = 0;
			}
		}
		cout << k-1 << endl;
	}
	return 0;
}