/**
 * AOJ #1159: Next Mayor (ICPC Domestic 2009)
 */

#include <stdio.h>

#define MAX_N 50

/**
 * Return the id of the winner of the game with n people and p pebbles.
 */
int winner(int n, int p)
{
  static int kept[MAX_N];
  int bowl, person, i;

  /* initialize */
  for (i = 0; i < MAX_N; i++) kept[i] = 0;
  bowl = p;
  person = 0; /* the next candidate */

  /* the game */
  for (;;) {
    if (bowl == 0) {
      bowl = kept[person];
      kept[person] = 0;
    } else {
      bowl--;
      kept[person]++;
      if (kept[person] == p) return person; /* winner */
    }
    person = (person + 1) % n;
  }
}

int main()
{
  int n, p;
  for (;;) {
    scanf("%d%d", &n, &p);
    if (n == 0 && p == 0) break;
    printf("%d\n", winner(n, p));
  }
  return 0;
}