#include<iostream>
using namespace std;

int main(){
  int n,p;
  int mayor[50],id,num;

  for(;;){
    cin >> n >> p;
    if(n==0 && p==0)break;
    
    for(int i=0;i<n;i++)mayor[i] = 0;
    id = 0; num = p;

    for(;;){
      if(num){
	mayor[id]++;
	num--;
      }else{
	num += mayor[id];
	mayor[id] = 0;
      }

      if(num==0 && mayor[id] == p){
	cout << id << endl;
	break;
      }

      id = (id+1) % n;
    }
  }
}