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 <vector>
using namespace std;

int main(){
	int mayors[50]{0};
	vector<int> ansvec;

	for(;;){
		int n, p;
		cin >> n >> p;
		if (n == 0) break;

		for (int i = 0; i < n; i++) mayors[i] = 0;
		int bowlp = p;
		int turn = 0;
		for(;;){
			if (bowlp == 1 && mayors[turn] == p - 1) break;
			else if (bowlp == 0){
				bowlp += mayors[turn];
				mayors[turn] = 0;
			}
			else{
				mayors[turn]++;
				bowlp--;
			}

			if (turn == n - 1) turn = 0;
			else turn++;
		}
		ansvec.push_back(turn);
	}

	for (int ans : ansvec){
		cout << ans << "\n";
	}
	cout << flush;
	return 0;
}

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

#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define FOR(itr,data) for(__typeof((data).begin()) itr=(data).begin();itr!=(data).end();++itr)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

typedef unsigned long long UInt64;
typedef long long Int64;

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

using namespace std;

typedef pair<int ,int > P;

int n,p;

bool check(int *a) {
    int cnt=0;
    rep(i,n) if(a[i]!=0) cnt++;
    return cnt==1;
}

void solve() {
    int ary[100]={};
    int wan=p;

    int index = 0;
    while(wan!=0 || !check(ary)) {
        if(wan==0) {
            wan = ary[index];
            ary[index] = 0;
        }
        else {
            wan--;
            ary[index]++;
        }
        index++;
        index%=n;
    }
    index=index+n-1;
    index%=n;
    cout<<index<<endl;


}

int main() {
    while(cin>>n>>p){
        if(!(n+p)) return 0;
        solve();
    }
    return 0;
}

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

int main(){
  while(1){
    int n,p;
    cin>>n>>p;
    if(n==0&&p==0)return 0;
    vector<int> a(n,0);
    int pool=p;
    int i=0;
    while(1){
      if(pool==0){
        pool+=a[i];
        a[i]=0;
      }else{
        pool--;
        a[i]++;
      }
      if(a[i]==p)break;
      i = (i+1)%n;
    }
/*
    while(1){
      for(i = 0;i<n;i++){
        if(pool==0){
          pool+=a[i];
          a[i]=0;
        }else{
          pool--;
          a[i]++;
        }
        if(a[i]==p)break;
      }
      if(a[i]==p)break;
    }
    */
    cout<<i<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  while(1) {
    int n, p; cin >> n >> p;
    if(n == 0) break;
    vector<int> v(n, 0);
    int win = -1;
    int pos = 0;
    while(1) {
      if(p > 0) {
        v[pos]++;
        p--;
        if(p == 0) {
          bool ok = true;
          for(int i=0;i<n;++i) {
            if(i == pos) continue;
            if(v[i] != 0) {
              ok = false;
              break;
            }
          }
          if(ok) {
            win = pos;
            break;
          }
        }
      } else {
        p = v[pos];
        v[pos] = 0;
      }
      pos = (pos + 1) % n;
    }
    cout << win << endl;
  }
}

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

using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
using pii=pair<int,int>;

#define REP(i,a) for(int i=0;i<a;i++)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define rREP(i,a) for(int i=(a)-1;i>=0;i--)
#define rFOR(i,a,b) for(int i=(a)-1;i>=(b);i--)
#define out(S) cout<<(S)<<endl;
#define beginend(v) (v).begin(),(v).end()
#define IfOut(condition,text) if(condition){out(text);return 0;}
#define IfOutElse(condition,if_text,else_text) if(condition){out(if_text);return 0;}else{out(else_text);return 0;}
#define mod(s) ((s)%(ll)(1e9+7))
ll modpow(ll i, ll j) { ll tmp = 1; while (j) { if (j % 2)tmp = mod(tmp*i); i = mod(i*i); j /= 2; }return tmp; }
#define divmod(a,b) (mod(a*modpow((ll)b,(ll)(1e9+5))))
#define pb(i) push_back(i)
#define fst first
#define snd second
#define Foreach(item,collection) for(auto item:collection)
#define INF 1e10
#define removeAll(vec,deleg) auto itr=remove_if(beginend(vec),deleg);vec.erase(itr,vec.end());
void removeAt(string& s, int index) { s.erase(index, 1); }
template<typename T> void removeAt(vector<T>& v, int index) { v.erase(v.begin() + index); }
ll manhattanDistance(ll x1, ll y1, ll x2, ll y2) { return (abs(x2 - x1) + abs(y2 - y1)); }
vector<ll> sieveOfEratosthenes(int max) {ll prime;double sqrtmax = sqrt(max);vector<ll> primeVec, searchVec;FOR(i, 2, max + 1) searchVec.push_back(i);do{prime = searchVec[0];primeVec.push_back(prime);auto itr = remove_if(beginend(searchVec), [=](ll x)->bool {return x%prime == 0; });searchVec.erase(itr, searchVec.end());} while (prime < sqrtmax);primeVec.reserve(primeVec.size() + searchVec.size());primeVec.insert(primeVec.end(), beginend(searchVec));return primeVec;}
ll sum(vi v) {ll sum=0;Foreach(i,v){sum+=i;}return sum;}
#define ShowAll(v) Foreach(i,v) out(i);


int main(){
    vi ans;
    int n,p;
    while (cin>>n>>p,!(!n&&!p))
    {
        int stones=p;
        vi members(n);
        int index=0;
        while (1)
        {
            if(stones>0){
                members[index]++;stones--;
                if(stones==0&&members[index]==p) break;
            }
            else{
                stones=members[index];members[index]=0;
            }
            index++;
            index%=n;
        }
        ans.pb(index);
    }    
    ShowAll(ans);
    return 0;
}

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

bool solve(){
    int n, p, r;
    cin >> n >> p;
    if(n == 0) return false;
    vector<int> a(n);
    r = p;
    for(int i=0;;i=(i+1)%n){
        if(r>0){
            a[i]++;
            r--;
        }else{
            r += a[i];
            a[i] = 0;
        }
        if(a[i] == p){
            cout << i << endl;
            break;
        }
    }
    return true;
}

int main(){
    while(solve());
    return 0;
}