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++: //#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
constexpr int PI = (2*acos(0.0));
constexpr int EPS = (1e-9);
constexpr int MOD = (int)(1e9+7);
constexpr int INF = 100000000;
#pragma endregion

#define int long long


signed main()
{
    int n, p;
    while(cin>>n>>p, n) {
        stack<int> w;
        for(int i=0; i<p; ++i) w.push(1);
        int memo[n] = {0};
        int c = 0;
        while(true) {
            if(w.empty()) {
                for(int i=0; i<memo[c]; ++i) w.push(1);
                memo[c] = 0;
                c  = (c+1)%n;
                continue;
            }
            w.pop();
            memo[c]++;
            if(memo[c]==p) {
                cout << c << endl;
                break;
            }
            c  = (c+1)%n;
        }
    }
    return 0;
}

C++: #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 <fstream>
#include <queue>
#include <complex>

#define INF 100000000
#define EPS 1e-9

using namespace std;

typedef long long ll;  

#define MAX_N 50
#define MAX_P 50

int n, p;

int temoto[MAX_N];

vector<int> ans_list;

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

    int sum = p;
    memset(temoto, 0, sizeof(temoto));

    int turn = 0;
    
    while(true){
      if(p > 0){
	temoto[turn%n]++;
	p--;
	if(p == 0 && temoto[turn%n] == sum)
	  break;
      }
      else{
	p += temoto[turn%n];
	temoto[turn%n] = 0;
      }
      turn++;
    }

    ans_list.push_back(turn%n);
    
  }

  for(int i = 0; i < ans_list.size(); i++){
    cout << ans_list[i] << endl;
  }
  
  
  return 0;
  
}

C++: #include <iostream>
using namespace std;
int can[51];

int game(int n,int p){
	int x=p;
	while(1){
		for(int i=0;i<n;i++){
			if(p==0){
				p=can[i];
				can[i]=0;
			}else{
				p--;
				can[i]++;
			}
			if(x==can[i])return i;
		}
	}
}


int main(){
	int p,n;
	while(1){
		cin >>n>>p;
		if(n==0&&p==0)break;
		for(int i=0;i<n;i++){
			can[i]=0;
		}
		int ans = game(n,p);
		cout <<ans <<endl;
	}
	return 0;
}

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

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

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

using namespace std;

int main(void){
    int n,q,w;
    int i;
    int p[50];
    while(true){
        cin >> n >> q;
        if(n == 0) break;
        for(i = 0;i < 50;i++) p[i] = 0;
        i = 0;w = q;
        while(true){
            if(w){
                p[i]++;
                w--;
            }
            else{
                if(p[i] == q){
                    cout << i << endl;
                    break;
                }
                else{
                    w = p[i];
                    p[i] = 0;
                }
            }
            i = (i+1)%n;
        }
    }
}

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

int main(){
  int n, m, p;
  int i, k, wan, j;



    while(1){
      int temp[60] = {}, ans = -1;
      cin>>n>>p;
      
      if(n == 0 && p == 0) break;
      
      wan = p;

      //  cout<<n<<endl;
      while(1){//cout<<"QQQQQQQQQQQQQQQQQQQQQQQ"<<endl;cout<<"wan = "<<wan<<endl;



	
	for(i = 0; i < n; i++){//cin>>n;cout<<"NNNNNN"<<endl;
	  if(wan >= 1){//cout<<"wan = "<<wan<<endl;
	    if(wan == 1){//cout<<"CCCCCCCCCCCCCCCCCCCCCCC"<<endl;cout<<"wan = "<<wan<<endl;
	      // k = 0;
	      // cin>>n;
	      /* for(j = 0; j < n; j++){cout<<"BBBBBBBBBBBBBBBBBBBBB"<<endl;
		if(temp[j] >= 1){//cout<<temp[j];
		  k = 1;cout<<"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZ  temp[j] ="<<temp[j]<<" j ="<<j<<endl;
		  break;
		}//cin>>n;
		}*/ wan--;
	      temp[i]++;
	      if(temp[i] == p){//cout<<"DDDDDDDDDDDDDDDD"<<endl;cout<<"wan = "<<wan<<endl;
		  ans = i;
		  break;
	      }//cin>>n;
	      
	    } else {//cout<<"EEEEEEEEEEEEEEEEEEEE"<<endl;
	      // cout<<"wan = "<<wan<<endl;
	      temp[i]++;//cout<<temp[i]<<" "<<i<<endl;
	      wan--;//cin>>n;
	    }
	    
	  } else {//cout<<"AAAAAAAAAAA"<<endl;cout<<"wan = "<<wan<<endl;
	    wan += temp[i];//cin>>n;
	    temp[i] = 0;
	  }
	  
	}





	if(ans != -1)break;//cout<<"RRRRR"<<endl;cout<<"wan = "<<wan<<endl;

	
      }
     
      
       cout<<ans<<endl;//cout<<"HHHHHHHHHHHH"<<endl;;
    }
  
  
  
  return 0;
}