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>

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;
      }else if(s[i]==pmax){
	break;
      }

      if(i = n-1)i = 0;
      else i++;
    }
    cout << i << endl;
  }

  return 0;
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;

#define gcu getchar_unlocked
#define ii inline int in
#define is inline bool scan
ii(int c) {
	int n = 0;
	bool minus = false; if (c == '-') minus = true, c = gcu();
	do {n = 10 * n + (c - '0'), c = gcu();} while (c >= '0');
	//return n; }
	return minus ? -n : n; }
ii() {return in(gcu());}
is(int &n) {int c=gcu();return c==EOF?false:(n=in(c),true);}
is(char &c){c=gcu();gcu();return c!=EOF;}
//void scan(string &s) {int c;do{c=gcu();s+=c;}while(!(c=='\n'||c==' '));}
#undef ii
#define pcu putchar_unlocked
template <typename T>
void out(T n) {
	static char buf[20];
	char *p = buf;
	if(n < 0) pcu('-'), n *= -1;
	if (!n) *p++ = '0';
	else while (n) *p++ = n % 10 + '0', n /= 10;
	while (p != buf) pcu(*--p); }
void out(const char *s){while(*s)pcu(*s++);}
void out(char c){pcu(c);}
//void out(string &s){for (char c: s) pcu(c);}
template <typename head, typename... tail>
void out(head&& h, tail&&... t){out(h);out(move(t)...);}
template <typename T>
//void out(vector<T> &v){for(T &x:v)out(' '),out(x);out('\n');}
void out(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');}
#undef svo

typedef vector<int> v;

int main() {
	int n, p;
	while ((n = in())) {
		p = in();
		v C(n);
		int b = p, res = -1;
		for (;;) {
			for (auto &c : C) {
				if (b) {
					b--, c++;
					if (c == p) {
						res = &c - &C[0];
						goto end;
					}
				} else
					b += c, c = 0;
			}
		}
end:
		out(res, '\n');
	}
}

C++: # include <algorithm>
# include <cassert>
# include <cctype>
# include <cstdio>
# include <cstdlib>
# include <cstring>
# include <cmath>
# include <iostream>
# include <map>
# include <queue>
# include <set>
# include <sstream>
# include <stack>
# include <string>
# include <vector>
# include <iomanip>

# define rep(i, n) for (int i = 0; i < (int)(n); i++)
# define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
# define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;

void show_all(vector<int> &d) {
  rep (i, d.size()) {
    cout << d[i] << ' ';
  }
  cout << endl;
}


bool convergence(vector<int> d) {
  int nonzero = 0;
  for (int _d : d) {
    if (_d != 0) nonzero++;
  }
  return nonzero == 1;
}

int main() {
  int n, p;

  while (cin >> n >> p && n && p) {
    int i = 0;

    vector<int> d(n+1);
    for (int j=0; j<n; j++) d[j] = 0;
    d[n] = p;

    while (true) {
      // cout << "wan: " << i << endl;
      // show_all(d);

      // ???????????´??°
      if (d[n] > 0) {
        d[n]--;
        d[i]++;
      } else if (d[n] == 0) {
        d[n] += d[i];
        d[i] = 0;
      }


      // ????????????
      if (convergence(d)) {
        cout << i << endl;
        // show_all(d);
        break;
      }

      // ???????????????????????´??°
      if (i == n-1) {
        i = 0;
      } else {
        i++;
      }
    }
  }
}

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

using namespace std;

struct count_op
{
  bool operator()( int i )
  {
    return i > 0;
  }
};


int main()
{
  for(;;) {
    int n, p;
    cin >> n >> p;

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

    vector<int> temoto( n, 0 );


    for(;;) {
      for( int i=0; i<n; ++i ) {
	if ( p > 0 ) {
	  --p;
	  ++temoto[i];

	  if ( p == 0 ) {
	    int const other = count_if( temoto.begin(), temoto.end(), count_op() );
	    if ( other == 1 ) {
	      cout << i << endl;
	      goto ee;
	    }
	  }
	} else {
	  p += temoto[i];
	  temoto[i] = 0;
	}
      }
    }
ee:;
  }
}

C++: #include<iostream>
using namespace std;
const int MAX = 50;
int main()
{
	while (true)
	{
		int n, p;
		cin >> n >> p;
		if (!n && !p)
			break;
		int pOfCan[MAX] = { 0 };
		int pOfBowl = p;
		int winIdx = -1;
		int idx = 0;
		bool flag = false;
		while (true)
		{
			for (int i = 0; i < n; i++)
				if (pOfCan[i] == p)
				{
					winIdx = i;
					flag = true;
					break;
				}
			if (flag)
				break;
			if (idx > n - 1)
				idx = 0;
			if (pOfBowl > 0)
			{
				pOfCan[idx]++;
				pOfBowl--;
			}
			else if (pOfBowl == 0)
			{
				pOfBowl = pOfCan[idx];
				pOfCan[idx] = 0;
			}
			idx++;
		}
		cout << winIdx << endl;
	}
}

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



int main(){
  while(1){


    
  int n;
  int p;
  cin>>n>>p;
  if(n==0 && p==0){break;}
  int al=p;
  int a[100]={};
  int j=-1;
  while(j==-1){
    for(int i=0;i<n;i++){
      if(p>0){p--; a[i]++; if(a[i]==al){j=i; break;}}
      else{p+=a[i]; a[i]=0;}
    }
  }
  cout<<j<<endl;





  }
  
  return 0;
}

C++: #include <iostream>

using namespace std;

int main(){
  int n,p;
  cin >> n >> p;
  while(1){
    if(n==0)break;
    int have[n]={},i=0,con=0,non=n;
    while(1){
      if(p>0){
        if(have[i]==0)non--;
        have[i]++;
        p--;
        if(p==0 && non==n-1)break;
      }else{
        if(have[i]>0){
          p+=have[i];
          have[i]=0;
          non++;
        }
      }
      i++;
      if(i==n)i=0;
    }
    cout << i << endl;
    cin >> n >> p;
  }
  return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
  int n,p,i,m,a=0,I;
  int box[100]={};
  while(1){
    cin>>n>>p;
    a=0;
    if(n==0&&p==0) break;
    else{
      while(1){

	for(i=0;i<n;i++){
	  m=i;
	  if(p>0){
	    box[i]+=1;
	    p-=1;
	  }
	  else if(p==0){
	    for(I=0;I<n;I++){
	      if(box[I]==0) a++;
	    }
	    if(a==n-1) {
	      break;
	    }
	    else {
	      p=box[m];
	      box[m]=0;
	      a=0;
	    }
	  }
	 
	}
	if(a==n-1) break;
      }
      cout<<m-1<<endl;
          }  
    for(i=0;i<n;i++){
      box[i]=0;
    }
  }
  return 0;
}

C++: #include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<sstream>
#include<iostream>
using namespace std;
int main(void){

    int n,p;
    while(cin>>n>>p,n|p){
        int a[100] = {};
        int q = p;
        int now = 0;
        while(true){
            if(q>0){
                a[now]++;
                q--;
                if(a[now] == p){
                    cout<<now<<endl;
                    goto next;
                }
            }
            else{
                q = a[now];
                a[now] = 0;
            }
            now++;
            if(now == n)now -= n;
        }
        next:;
    }

    return 0;
}

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

#define REP(i, n) for(int (i)=0; (i)<(n); (i)++)
#define ALL(v) (v).begin(), (v).end()
#define INF 1e09

using namespace std;

int main(){
  int n, p;
  while(cin >> n and cin >> p and n + p){
    vector<int> a(n, 0);
    int m = p;
    bool flag = true;
    while(flag)
      REP(i, n){
        if(m > 0){ a[i%n]++; m--; }
        else { m = a[i%n]; a[i%n] = 0; }
        if(a[i%n] == p) { cout << i % n << endl; flag = false; break; }
      }
  }
}