#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Rep(i, N) for(int i = 0; i < N; i++)
#define Reps(i, x, N) for(int i = x; i < N; i++)

const int LLINF = 1LL << 60;

signed main()
{
  int N, P;

  while(cin >> N >> P, N || P) {
    
    int w = P;
    int have[55] = {0};
    int i = 0;
    while(true) {
      if(w) have[i]++, w--;
      else w += have[i], have[i] = 0;
      if(have[i] == P) { cout << i << endl; break; }
      i++; if(i == N) i = 0;
    }

  }

  return 0;
}
/*** ?¬?????????? ***/

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

int main(){
	int n, p;
	int i;
	int count, size;
	int max, num;
	
	cin >> n >> p;
	while(n!=0 || p!=0){
		vector<int> v;
		for(i=0; i<n; ++i) v.push_back(0);
		count=0;
		size=p;
		
		while(1){
			for(i=size; 0<i; --i){
				++v[count];
				count=(count+1)%n;
			}
			
			max=0;
			num=-1;
			for(i=0; i<n; ++i){
				if(max<v[i]){
					max=v[i];
					num=i;
				}
			}
			if(max==p) break;
			
			while(v[count]==0) count=(count+1)%n;
			size=v[count];
			v[count]=0;
			count=(count+1)%n;
		}
		cout << num << endl;
		
		cin >> n >> p;
	}
	return 0;
}