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 <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <cctype>
#define rep(i,a,b) for(int i=(a);i<b;i++)
#define INF 1000000000
using namespace std;
bool debug=false;

int x[55];

void init(){
  for(int i=0;i<55;i++)x[i]=0;
}

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

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

    init();
    
    int tmp=p;
    
    for(int i=0;;i++){
      if(check(n,p))break;
      if(tmp==0){
	tmp=x[i%n];
	x[i%n]=0;
      }else{
	tmp--;
	x[i%n]++;
      }
      if(debug) for(int j=0;j<n;j++)cout<<x[j]<<" ";
      if(debug) cout<<endl;
    }
    
    for(int i=0;i<n;i++){
      if(x[i]==p){
	cout<<i<<endl;
	break;
      }
    }
  }
  return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
#define _ << " " <<
#define all(X) (X).begin(), (X).end()
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define Ti4 tuple<int, int, int, int>
#define Tlll tuple<ll, ll, ll>
#define PI 3.141592653589793

int main() {
    int n, p;
    while (cin >> n >> p) {
        if (n == 0) return 0;
        int b[n] = {}, cnt = 1;
        for (int i = 0; 1; i++) {
            if (p > 0) {
                b[i % n]++;  p--;
                cnt = 1;
            }
            else {
                if (b[i % n]) {
                    p = b[i % n];
                    b[i % n] = 0;
                    cnt = 1;
                }
                else {
                    cnt++;
                }
            }
            if (cnt == n) {
                cout << (i + 1) % n << endl;
                break;
            }
        }
    }
}

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

using namespace std;

int main(){
    int n,p;
    while(cin >> n >> p,n){
        vector<int> ca;
        for(int i=0;i<n;i++) ca.push_back(0);
        int s=p;
        for(int i=0;;i++){
            if(i>=n) i=0;
            if(p){
                p--;
                ca[i]++;
            } else {
                vector<int>::iterator it = find(ca.begin(),ca.end(),s);
                if(it != ca.end()){
                    cout << distance(ca.begin(), it) << endl;
                    break;
                }
                p = ca[i];
                ca[i] = 0;
            }
        }
    }
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <complex>
#include <math.h>

using namespace std;

//conversion
//------------------------------------------
inline long toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline long toLong(string s) {long v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#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())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(long i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const long double EPS = 1e-12;
const int INF = 10000;

struct edge{int to,cost;};
typedef pair<int,int> P;//firstは最短距離,secondは頂点番号

int main(){
	int n,p;
	int cnt[50];
	
	while(cin>>n>>p){
		if(n==0&&p==0)break;
		CLR(cnt);
		int wan=p;
		int num=0;
		while(1){
			
			if(wan!=0){
				cnt[num]++;
				wan--;
			}else{
				wan=cnt[num];
				cnt[num]=0;
			}
			//cout<<"!"<<num<<" "<<cnt[num]<<endl;
			if(cnt[num]==p)break;
			num++;
			num%=n;
			
		}
		cout<<num<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,p;
  while(cin>>n>>p,n){
    int v[51]={};
    int i=0,a=p;
    while(1){
      if(a==0){
        a=v[i], v[i]=0;
      }else if(a)
        a--, v[i]++;
      if(a==0 && v[i]==p){
        cout<<i<<endl;
        break;
      }
      i++;
      if(i==n) i=0;
    }
  }
  return 0;
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#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())

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

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

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

  int n, p, P;
  while(true){
	cin >> n >> p;
	if(n == 0) break;

	int idx = 0;
	P = p;
	VI has(n, 0);
	while(true){
	  if(p > 0){
		--p;
		has[idx]++;
	  }else
		if(has[idx] > 0){
		  p = has[idx];
		  has[idx] = 0;
		}

	  if(has[idx] == P) break;
	  idx = (idx+1) % n;
	}

	cout << idx << endl;
  }

  return 0;
}