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++: /*Function Template*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pint;
const int MAX = 510000;
const int MOD = 1000000007;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define Rep(i, n) for(ll i = 1; i < (n); i++)
#define ALL(a)  (a).begin(),(a).end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll fac[MAX], finv[MAX], inv[MAX];


template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }

}
ll Len(ll n) {
  ll s=0;
  while(n!=0) s++, n/=10;
  return s;
}


ll Sint(ll n) {
  ll m=0,s=0,a=n;
  while(a!=0) s++, a/=10;
  for(ll i=s-1;i>=0;i--) m+=n/((ll)pow(10,i))-(n/((ll)pow(10,i+1)))*10;
  return m;
}


ll Svec(vector<ll> v){
  ll n=0;
  for(ll i=0;i<v.size();i++) n+=v[i];
  return n;
}


ll GCD(ll a,ll b) {
  return b ? GCD(b,a%b) : a;
}


ll LCM(ll a,ll b){
  return a/GCD(a,b)*b;
}


ll Factorial(ll n){
  ll m=1;
  while(n>=1) m*=n,n--;
  return m;
}

void runlength(string s,vector<pair<char,ll>> &p){
  ll x=1;
  if(s.size()==1){
    p.push_back(pair<char,ll>(s[0],1));
  }
  for(ll i=0;i<s.size()-1;i++){
    if(s[i]==s[i+1]){
      x++;
      if(i==s.size()-2){
        p.push_back(pair<char,ll>(s[i],x));
      }
    }else{
      p.push_back(pair<char,ll>(s[i],x));
      x=1;
      if(i==s.size()-2){
        p.push_back(pair<char,ll>(s[s.size()-1],x));
      }
    }
  }
}




ll COM(ll n,ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

const int MAX_N=100010;
vector<bool> sieve_of_eratosthenes(){
  vector<bool> isPrime(MAX_N+1,true);
  for(int i=2;i<=MAX_N;i++){
    if(isPrime[i]){
      for(int j=2*i;j<=MAX_N;j+=i){
        isPrime[j]=false;
      }
    }
  }
  return isPrime;
}

vector<pint> prime_factorize(ll n){
  vector<pint> ans;
  for(ll p=2;p<=sqrt(n);p++){
    if(n%p!=0) continue;
    ll cnt=0;
    while(n%p==0){
      n/=p;
      cnt++;
    }
    ans.push_back(make_pair(p,cnt));
  }
  if(n!=1) ans.push_back(make_pair(n,1));
  return ans;
}

/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
int main() {
  IOS;
  while(1){
    ll n,r;cin>>n>>r;
    if(n==0 && r==0) break;
    ll a[n];
    rep(i,n) a[i]=0;
    ll tmp=r;
    ll cur=0;
    while(1){
      if(tmp>0){
        a[cur]++;
        tmp--;
        if(a[cur]==r){
          cout<<cur<<endl;
          break;
        }
      }else if(a[cur]>0){
        tmp=a[cur];
        a[cur]=0;
      }
      cur++;
      cur%=n;
    }
  }
}

C++: #include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

int n, p;

int main() {
	while (cin>>n>>p, n|p){
		int wan = p;//椀に石を入れる
		int ans = -1;
		int vl[50] = {0};//町長候補を手ぶらにする
		for (int i = 0; i < 10000000; i++){
			if (wan > 0){//椀に石があるとき
				vl[i%n]++;//手持ちを増やす
				wan--;//椀の石を減らす
				if (vl[i%n] == p){//もし全て持っていたら
					ans = i%n;//町長候補をアンサーに入れる
					break;
				}
			}
			else{//石がないとき
				wan=vl[i%n];//椀に返す
				vl[i%n] = 0;
			}
		}
		cout << ans << endl;
	}
}

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

const int NMAX = 60;

int solve(int n, int p) {
  int candidates[NMAX];
  REP(i, n) { candidates[i] = 0; }
  int cup = p;
  int i = 0;
  while (true) {
    if (cup > 0) {
      cup--;
      candidates[i]++;
      if (candidates[i] == p) return i;
    } else {
      cup += candidates[i];
      candidates[i] = 0;
    }
    i = (i + 1) % n;
  }
}


int main() {
  int n, p;
  while (cin >> n >> p and n) {
    cout << solve(n, p) << endl;
  }
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1159

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
  int n, p;
  while (scanf("%d%d", &n, &p) != EOF && n && p) {
    int a[50];
    fill(a, a+50, 0);
    int cur = 0;
    int res = p;
    while(true) {
      if (res > 0) {
	a[cur]++;
	res--;
      } else {
	if (a[cur] == p) break;
	res = a[cur];
	a[cur] = 0;
      }
      cur = (cur+1)%n;
    }
    printf("%d\n", cur);
  }
}

C++: #include <stdio.h>

// target以外の候補者の石の数がすべて0の場合にtrueを返す
bool otherkouho(int *kouho, int target, int total)	{
	
	for (int i = 0; i <= total - 1; i++)	{
		
		if (i == target)	{
			continue;
		}
		
		if (kouho[i] > 0)	{
			return false;
		}
	}
	
	return true;
}

int main(void)	{
	
	int n, p, wan, turn;
	int kouho[50];
	
	while (true)	{
		
		scanf("%d %d", &n, &p);
		
		if (n == 0)	{ break; }

		// 諸々の初期化
		turn = 0;
		wan = p;
		for (int i = 0; i <= 49; i++)	{
			kouho[i] = 0;
		}
		
		while (true)	{
			
			if (wan > 0)	{
				
				kouho[turn]++;
				wan--;
				
				if (wan == 0)	{
					if (otherkouho(kouho, turn, n))	{
						break;
					}
				}
			} else	{
				if (kouho[turn] > 0)	{
					wan = kouho[turn];
					kouho[turn] = 0;
				}
			}
			
			turn++;
			
			if (turn >= n)	{
				turn = 0;
			}
		}
		
		printf("%d\n", turn);
	}
	
	return 0;
}

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

int stones[51];

int main(){
	while(true){
		for(int i = 0;i<=50;i++)stones[i]=0;
		int wanposi = 0;
		int n,p;
		cin >> n;
		cin >> p;
		if(n==0)break;
		int wan = p;
	
		while(true)
		{
			if(wan!=0){wan--;stones[wanposi]++;}
			else{wan=stones[wanposi];stones[wanposi]=0;}
		
			if(stones[wanposi]==p){break;}
			else if(wanposi<n-1){wanposi++;}
				else wanposi = 0;
		
		}
	cout << wanposi << endl;
	}
	return 0;
}

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

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

    cout << cnt << endl;
  }
}