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 <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )

#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

int solve( const int n, const int p )
{
	int cup = p;
	VI have( n, 0 );
	for ( int i = 0; ; i++, i %= n )
	{
		if ( cup )
		{
			have[i]++;
			cup--;
		}
		else
		{
			cup = have[i];
			have[i] = 0;
		}

		if ( have[i] == p )
		{
			return i;
		}
	}

	return -1;
}

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

	while ( true )
	{
		int n, p;;
		cin >> n >> p;

		if ( !( n | p ) )
		{
			break;
		}

		cout << solve( n, p ) << endl;
	}

	return 0;
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

ll n,p;

int main(){
    while(cin >> n >> p && n){
        vector<ll> cnt(n,0);
        int now = 0;
        while(1){
            if(p != 0){
                cnt[now]++;
                p--;
                if(p == 0){
                    bool f = true;
                    rep(i,n){
                        if(i != now)f &= (cnt[i] == 0);
                    }
                    if(f){
                        cout << now << endl;
                        break;
                    }
                }
            }
            else {
                p += cnt[now];
                cnt[now] = 0;
            }
            now = (now+1)%n;
        }
    }
}

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

int main(){

  while(1){

    int n, p, cnt = 0, win, sum = 0;
    int human[51] = {0};

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

    while(1){
      cnt = 0;
      if(p == 0){
	for(int i = 0; i < n; i++){
	  if(human[i] == 0){
	    cnt++;
	  }else if(human[i] != 0) win = i;
	}
	if(cnt == n - 1){
	  cout << win << endl;
	  break;
	}else {
	  //cout << "B";
	  p = human[sum];
	  human[sum] = 0;
	  sum++;
	  cnt = 0;
	  if(sum > n - 1) sum = 0;
	}
      }else {
	p -= 1;
	//cout << "A";
	human[sum] += 1;
	sum++;
	if(sum > n - 1) sum = 0;
      }
    }
  }
  return 0;
}

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

int main() {
	while(true) {
		int n, p, i = 0;
		cin >> n >> p;
		if(n == 0 && p == 0) break;
		vector<int> player;
		player.resize(n+1);
		while(true) {
			if(p > 0) {
				if(p == 1) {
					bool flag = true;
					for(int j=0; j<n; j++) if(i != j && player[j] != 0) flag = false;
					if(flag) break;
				}
				player[i]++;
				p--;
			} else {
				p += player[i];
				player[i] = 0;
			}
			i++;
			if(i >= n) i = 0;
		}
		cout << i << endl;
	}
	
	return 0;
}

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

#define FOR(i, a, b) for(long long int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LLINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;


int main(void) {
	
	while (1) {
		bool flag = false;
		int n, p;
		int now;
		int pos = 0;
		int a[51] = {};

		cin >> n >> p;
		if (n == 0 && p == 0) {
			break;
		}
		now = p;
		while (1) {
			if (now > 0) {
				now--;
				a[pos]++;
				
			}
			else {
				FOR(i, 0, n - 1) {
					if (a[i] == p) {
						cout << i << endl;
						flag = true;
						break;
					}
				}
				now += a[pos];
				a[pos] = 0;
			}
			pos++;
			if (pos == n) {
				pos = 0;
			}
			if (flag == true) {
				break;
			}
		}
	}

}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define unsigned long long 
#define rep(i,a,b) for(ull (i)=(a);(i)<(b);i++)
using namespace std;
int n, p, man[60], wan;


int main(void){
	while (true){
		memset(man, 0, sizeof(man));
		cin >> n >> p;
		if (n == 0)return(0);
		wan = p;
		int i = 0;
		while (true){
			if (wan > 0){
				man[i]++;
				wan--;
				if (man[i] == p)break;
			}
			else
			{
				wan = man[i];
				man[i] = 0;
			}

			i = (i + 1) % n;
		}
		cout << i << endl;



	}



}

C++: #include <iostream>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;

int main(){
  while(1){
    int N;
    cin >> N;
    if(N==0) break;
    int x,y,map[100][100]={};
    rep(i,N){
      cin >> x >> y;
      map[y][x] = 1;
    }
    int Rx = 10,Ry = 10,tr = 0;
    int M;
    cin >> M;
    rep(i,M){
      string v;
      int d;
      cin >> v >> d;
      if(v == "N"){
	rep(j,d){ 
	  Ry++;
	  tr += map[Ry][Rx];
	}
      }
      else if(v == "E"){
	rep(j,d){ 
	  Rx++;
	  tr += map[Ry][Rx];
	}
      }
      else if(v == "S"){
	rep(j,d){ 
	  Ry--;
	  tr += map[Ry][Rx];
	}
      }
      else if(v == "W"){
	rep(j,d){ 
	  Rx--;
	  tr += map[Ry][Rx];
	}
      }
    }
    if(tr == N) cout << "Yes\n";
    else cout << "No\n";
  }
  return 0;
}

C++: #include<cstdio>
int main(void) {
	int n, p, k;
	bool v;
	int a[100];
	while (true) {
		scanf("%d %d", &n, &p);
		if (n == 0 && p == 0)return 0;
		for (int i = 0; i < n;i++) a[i] = 0;
		k = p;
		v = false;
		while (true) {
			for (int i = 0; i < n;i++) {
				if (k == 0) {
					k = a[i];
					a[i] = 0;
				}
				else {
					k--;
					a[i]++;
					if (a[i] == p) {
						printf("%d\n", i);
						v = true;
						break;
					}
				}
			}
			if (v)break;
		}

	}
	return 0;
}