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>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;

int main(){
	int n,p,max;
	int stone[51]={};
	int cup;
	bool flag;
	while(1){
		cin >> n >> p;
		max = p;
		for(int i=0;i<n;i++){
			stone[i]=0;
		}
		cup =0;
		if(n==0&&p==0)break;
		while(1){
			if(p>0){
				p-=1;
				stone[cup]+=1;
				if(cup!=n-1)cup+=1;
				else cup = 0;
			}else{
				flag =0;
				for(int i=0;i<n;i++){
					if(stone[i]!=0&&stone[i]!=max){
						flag =1;
					}
				}
				if(flag==0){
					if(cup==0){
						cout << n-1 << endl;
					}else{
						cout << cup-1 <<endl;
					}
					break;
				}else{
					if(stone[cup]>0){
						p+=stone[cup];
						stone[cup]=0;
						if(cup!=n-1)cup+=1;
						else cup = 0;
					}else{
						if(cup!=n-1)cup+=1;
						else cup = 0;
					}
				}
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <queue>
#include <map>
using namespace std;

int main(){
int m,p;

while(cin>>m>>p && m!=0){
int a[55]={0};
int no=0,is=p;
while(1){
if(is>0){
is--;
a[no]++;
}
else if(a[no]>0){
is=is+a[no];
a[no]=0;
}
//cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<" "<<is<<" "<<no<<endl;
if(a[no]==p) break;

no=no+1;
no=no%m;

//cout<<no<<" "<<is<<endl;

}
cout<<no<<endl;
}

return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  long long int n=0,p=0,count=0,answer=0;



while(1){
cin >> n >> p;
vector<int> v(n,0);
if(n==0&&p==0)
  return 0;
  while(1){
    for(int i=0;i<n;i++){
      if(p==0){

        if(v[i]!=0){
          p+=v[i];
          v[i]=0;
          count=0;
        }else{
          count++;
        }

      }else{
        v[i]++;
        p--;
        count=0;
      }
      if(count==n-1){
      //  cout << count << endl;
        break;
      }
    }if(count==n-1){
    //  cout << count << endl;
      break;
    }


  }


  for(int i=0;i<n;i++){
    if(v[i]!=0){
      answer = i;
    }
  }

  cout<<answer<<endl;
}
}

C++: #include<iostream>
using namespace std;
main(){int n,p;while(cin>>n>>p&&n&&p){int num=0,p2=p,a[50]={};while(a[(num-1)%n]!=p){if(p2)p2--,a[num%n]++;else p2=a[num%n],a[num%n]=0;num++;}cout<<(num-1)%n<<endl;}}

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

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

typedef long long LL;

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

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


int main(void){

        int p=0;
        int n=0;
        int cad[50];

        while(1){

                int i,j=0;
                int cad[50];
                int sum=0;
                

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

        for(i=0; i<50; i++){
                cad[i] = 0;
        }

      
        while(1){
              
        if(p != 0){
                cad[j] = cad[j] + 1;
                p = p-1;
              
                if(sum == cad[j])break;

        }else if(p == 0){
                
                if(cad[j] != 0){
                        p = cad[j] + p;
                        cad[j] = 0;
                        
                }
        }
        j++;
        j = j%n;
        }

        cout <<  j <<endl;

}

        return 0;

}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n,p;
  while(cin >> n >> p, n!=0 && p!=0){
    int s[n];
    int index = 0;
    int stone = p;
    for(int i=0;i<n;i++){
      s[i] = 0;
    }
    while(true){
      if(stone > 0){
	s[index]++;
	stone--;
      }else if(stone == 0){
	stone = s[index];
	s[index] = 0;
      }
    
      if(s[index] == p) break;
      if(index == n-1){
	index = 0;
      }else{
	index++;
      }
    }
    cout << index << endl;
  }
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <array>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <valarray>
#include <utility>
#include <tuple>
#include <cmath>
#include <random>

// boost
//#define USE_BOOST_IN_CODE
#ifdef USE_BOOST_IN_CODE
#include <boost/range.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/adaptor/strided.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/format.hpp>
#endif


using ll = long long;

//vector
template <class T>
using vec = std::vector<T>;

template <class T>
using vv = vec<vec<T>>;

//namespace std
//{
//    template<>
//    class hash<Key>
//    {
//    public:
//        size_t operator() (const Key& key) const
//        {
//            size_t seed = 0;
//            auto i_hash = hash<int>()(key.i_th);
//            auto w_hash = hash<int>()(key.W);
//
//            seed ^= i_hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
//            seed ^= w_hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
//
//            return seed;
//        }
//    };
//}

void solve()
{
    using namespace std;
    int n;

    while (cin >> n)
    {
        if (n == 0)
            break;

        int p;
        cin >> p;

        vec<int> v(n);
        int stoneNum = p;

        for (int i = 0;; i = (i + 1) % n)
        {
            if (stoneNum == 0)
            {
                stoneNum += v[i];
                v[i] = 0;
            }
            else if (stoneNum == 1 && stoneNum + v[i] == p)
            {
                cout << i << endl;
                break;
            }
            else
            {
                ++v[i];
                --stoneNum;
            }
        }
    }

}

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

C++: #include<iostream>
#include<cstdio>

using namespace std;
int N[50];
int main(){
  int n,p,p1;
 
  int i=0;
  while(true){
    cin>>n>>p;
    //ishi
    p1=p;
    if(n==0&&p==0)break;
    i=0;
    while(true){
      if(i==n)i=0;
      if(p1==0){
	p1=N[i];
	N[i]=0;
      }
      else {
	p1--;
	N[i]++;
	if(N[i]==p)break;
      }
     i++;
    }
    cout<<i<<endl; 
    N[i]=0;
    /*
    for(int i=0;i<n;i++){
      cout<<"person"<<i<<" "<<N[i]<<endl;
      }*/
  }
  return 0;
  
}

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

int main(){
	vector<int> ansvec;

	for(;;){
		int n, p;
		cin >> n >> p;
		if (n == 0) break;
		
		int bowlp = p;
		int turn = 0;
		int mayors[50]{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 << endl;
	}
	return 0;
}