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 <bits/stdc++.h>
using namespace std;

int ppl[55]={0};
int n,p,now,num;

int main(){
  int i,j,k;
  while(1){
    cin >> n >> p;
    if(n+p==0)break;
    now = p;
    num = 0;
    while(1){
      if(now>0){
	ppl[num]++;
	now--;
      }
      else if(ppl[num]==p)break;
      else{
	now = ppl[num];
	ppl[num]=0;
      }
      num++;
      num%=n;
    }
    printf("%d\n",num);
    for(i=0;i<n;i++)ppl[i]=0;
  }
  return 0;
}

C++: #include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;

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

    int a[n], wan = p;
    memset(a, 0, sizeof(a));
    int i = 0;
    while (true) {
      if (wan == 0) {
	wan = a[i];
	a[i] = 0;
      }else {
	a[i]++;
	wan--;
	if (a[i] == p) {
	  std::cout << i << std::endl;
	  break;
	}
      }
      i = (i+1)%n;
    }
  }
}

C++: #include <stdio.h>
int main()
{
  int i=0, l,  p, q, j=0;
  int d[50], dd[50];

  for ( i = 0; i < 50; i++)
    {
      dd[i] = 0;
    }
  while(1)
    {
      for ( i = 0; i < 50; i++)
	{
	  d[i] = 0;
	}   
      scanf("%d %d", &l, &p);
      if( l == 0 && p == 0 ) break;
      q = p;
      while(1)
	{
     	  if ( i >= l ) i = 0;
	  if ( p > 0 )
	    {
	      d[i] += 1;
	      p--;
	      if ( d[i] == q && p == 0)
		{
		  dd[j] = i;
		  j++;
		  break;
		}  
	    }
	  else if ( p == 0 )
	    {
	      p = d[i];
	      d[i] = 0;
	    }
	  i++;
	}
    }
  for ( i = 0; i < j; i++)
    {
      printf("%d", dd[i]);
      printf("\n");
    }
  return 0;
}