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 <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <memory.h>
#include <functional>

using namespace std;

//FILE * in = freopen("A", "r", stdin);

int n, m;

void process() {
	int data[100];
	memset(data, 0, sizeof(data));

	int cnt = m, cur = 0;;
	while(true) {
		if(cnt > 0) {
			--cnt;
			data[cur]++;

			if(cnt == 0 && data[cur] == m) {
				printf("%d\n", cur);
				break;
			}
		}
		else {
			cnt = data[cur];
			data[cur] = 0;
		}
		cur = (cur + 1) % n;
	}
}

int main() {
	while(true){
		scanf("%d%d", &n, &m);
		if(n == 0) break;
		process();
	}
	return 0;
}

C++: #include <iostream>
#define REP(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;
 
int N, P, a[50];
 
int main(void) {
  while(cin >> N >> P, N && P) {
    REP(i, 0, N) a[i] = 0;
 
    for(int i = 0; true; i = (i + 1) % N) {
      if(P == 0) {
        P = a[i];
        a[i] = 0;
      } else {
        P--;
        a[i]++;
        if(P == 0) {
          bool fin = true;
          REP(j, 0, N) if(j != i && a[j] != 0) fin = false;
          if(fin) {
            cout << i << endl;
            break;
          }
        }
      }
    }
  }
 
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define _A_ true
#define _3_ true
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;

int main(){
  int a,b,rem;
  int persons[60];
  while(_A_){
    scanf("%d %d",&a,&b);
    if(!(a|b))break;
    rep(i,60)persons[i] = 0;
    int Index=0;
    rem = b;
    while(_3_){
      if(Index%a == 0)Index = 0; 
 
      if(rem == 1 && persons[Index]+1 == b){
	cout << Index << endl;
	break;
      }
      else if(rem != 0){
	persons[Index]++;
	rem--;
	Index++;
      }
      else if(rem == 0){
	rem=persons[Index];
	persons[Index] = 0;
	Index++;
      }    
    }
  }
  return 0;
}

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

#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define ALL( a )        (a).begin(), (a).end()             

int main() {
	int n, p;

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

		vector<int> stone( n );
		int j = 0, ok = p;
		while( 1 ){
			bool flag = false;
			for( int i = 0; i < p; i++ ) { stone[j % n]++; j++; }
			p = 0;
			while( 1 ) {
				if( stone[j % n] > 0 ) {
					if( stone[j % n] == ok ) { cout << j % n << endl; flag = true; }
					p = stone[j % n];
					stone[j % n] = 0;
					j++;
					break;
				}
				j++;
			}
			if( flag ) break;
		}	
	}

}

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

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

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

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

int main(){
  int n,p;
  while(1){
  cin>>n>>p;
  if(n+p==0) break;
  int a[50]={},s=p;
  int i=0,vic,f=0;
  while(1){
    if(i==n) i=0;
    if(f==1) break;

    if(p<=0){
	if(a[i]==s){
	  f=1;
	  vic=i;
	}

      if(p==0){
	  p+=a[i];
	  a[i]=0;
	} 
    }

    else{
      a[i]++;
      p--;
    }
  i++;
  }

  cout<<vic<<endl;
  }
  return 0;
}

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

int main() {
	int n, p;
	while( cin >> n >> p, n || p ) {
		vector<int>data( n );
		int now = 0, P = p;
		while( 1 ) {

			for( size_t i = 0; i < n; i++ ) {
				data[i] += p / n;
			}

			p %= n;

			for( size_t i = 0; i < p; i++ ) {
				now++;
				now %= n;
				data[now]++;
			}

			now++; now %= n;
			while( !data[now] ) {
				now++; now %= n;
			}
			p = data[now];
			if( P == p ) {
				break;
			}
			data[now] = 0;
			//cout << p << P << endl;
			//cout << data[0] << data[1] << data[2] << endl;
		}
		now--;
		if( now < 0 ) {
			now = n - 1;
		}
		cout << now << endl;
	}
}

C++: #include <iostream>

int main(void){
  while(1){
int stonNum[51] = {0};
    int p, n;
    std::cin >> n >> p;
    int first = p;
    if(n == 0 && p == 0)  return 0;
    int i=0;
    while(1){
      bool flag = true;
      if(p > 0){
        stonNum[i]++;
        p--;
        if(stonNum[i] == first){
        std::cout << i << std::endl;
        break;
        }
      }else{
        p += stonNum[i];
        stonNum[i] = 0;
      }
      if(i == n-1)  i = 0;
      else i++;
    }
  }
}

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

		}
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); ++(i))
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, int> pli;
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T>bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod*mod;
constexpr int MAX = 15000010;

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

C++: #include <vector>
#include <iostream>
using namespace std;
int n, m;
int main() {
	while (cin >> n >> m, n) {
		int s = m;
		vector<int> v(n, 0);
		for (int i = 0; ; i = (i + 1) % n) {
			if (m != 0) v[i]++, m--;
			else m = v[i], v[i] = 0;
			if (m == 0 && s - m - v[i] == 0) {
				cout << i << endl;
				break;
			}
		}
	}
	return 0;
}