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;

#define pb         push_back

const int INF = 1000000000;
const int MOD = 1000000007;

int main(void) {
	while(1){
		int n, p;
		int st[100] = {0};
		cin >> n >> p;
		if(n == 0 && p == 0) break;

		int i = 0;
		int wan = p;
		while(1){
			if(wan > 0){
				st[i]++;
				wan--;
				if(st[i] == p){
					printf("%d\n", i);
					break;
				}
			}else{
				wan += st[i];
				st[i] = 0;
			}
			/*
			for(int j=0; j<n; j++){
				printf("%d:%d\n", j, st[j]);
			}
			puts("");
			*/
			i++;
			i%=n;
		}
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,s,e) for(int (i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)
#define RFOR(i,e,s) for(int (i)=(e)-1;(i)>=(int)(s);(i)--)
#define RREP(i,e) RFOR(i,e,0)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;
typedef priority_queue<int> PQI;
typedef priority_queue<PII> PQII;

const int N = 50;
int c[N];

int main() {
  int n, p;
  while (1) {
    scanf("%d%d ", &n, &p);
    if (!n && !p) break;
    memset(c, 0, sizeof(c));
    int i = 0, ap = p;
    while (1) {
      if (p == 0) {
        p += c[i];
        c[i] = 0;
      } else {
        p--;
        c[i]++;
      }
      if (c[i] == ap) {
        printf("%d\n", i);
        break;
      }
      i = (i+1)%n;
    }
  }
  return 0;
}

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

void main() {
	int n, p;

	while (1) {
		int flag = 0;
		int player[50] = { 0 };
		cin >> n >> p;
		if (n == 0 && p == 0) break;
		int p_max = p;

		while (1)
		{
			for (int j = 0; j < n; j++) {
				if (p != 0) player[j] += 1, p--;
				else p = player[j], player[j] = 0;
				if (player[j] == p_max) {
					flag = -1;
					cout << j << endl;
					break;
				}
			}
			if (flag == -1) break;
		}
	}
}

C++: #include<iostream>
#include<vector>

using namespace std;

int main(){
  int n,p;
  cin >> n >> p;
  vector<int> win;
  while(n){
    vector<int> may(n);
    int cal = p;
    int flag = 1;
  while(flag){
    for(int i = 0; i < n; i++){
      if(cal){
        cal--;
        may[i]++;
        if(may[i] == p){
          win.push_back(i);
          flag = 0;
        }
      }
      else if(may[i]){
        cal = may[i];
        may[i] = 0;
      }
    }
  }
  cin >> n >> p;
}
for(int i = 0; i < win.size();i++)
  cout << win[i] << endl;
}

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

int main(){
	int N,P,R,P2,P3[100],A;
	R=0;
	while(true){
		cin>>N>>P;
		P2=P;
		A=0;
		memset(P3,0,sizeof(P3));
		if(N==0 && P==0){
			break;
		}
		while(true){
			for(int i=0;i<N;i++){
				if(P3[i]==P){
					R=i;
					goto Exit;
				}
			}
			if(P2==0){
				P2=P3[A%N];
				P3[A%N]=0;
			}
			else{
				P2 -= 1;
				P3[A%N] += 1;
			}
			A++;
		}
		Exit:
		cout<<R<<endl;
	}
	return 0;
}

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

bool allZero(vector<int> s, int cur) {
	for (int i = 0; i < s.size(); i++) {
		if (s[i] > 0 && i != cur) return false;
	}
	
	return true;
}
int main()
{
	// cinはscanfより基本的に遅い
	// sync_with_studio()を事前に書いておくとcinでも速度向上が期待出来る！

	bool flg = false;
	int n, p, cur;
	int curP;
	vector<int> s; 
	
	while (cin >> n >> p) {
		if (n == 0 && p == 0) {
			break;
		}

		
		cur = 0;
		curP = p;
		s.clear();
		s.resize(n);

		// 現在の候補者が碗から石を取り出す
		// 番号が降ってあるわけではない！
		while (true) {
			// 現在ターンである候補者が1つ取り出す
			if (curP > 0) {
				s[cur]++;
				curP--;
			} else if (curP == 0 && s[cur] > 0) {// 手元に小石があれば全て碗の中に入れる
				curP += s[cur];
				s[cur] = 0;
			}
			
			// 条件を満たして入れば終了
			if (curP == 0) {
				// && allZero(s, cur)) {
				flg = true;
				for (int i = 0; i < s.size(); i++) {
					if (s[i] > 0 && i != cur) flg = false;// 条件満たさず
				}
				if (flg) {
					cout << cur << endl;
					break;
				}
				
			}

			// 次の候補者へ
			cur++;
			if (cur == n) cur = 0;
		}
	
		
	}

	return 0;
}

C++: #include <iostream>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i,k,n) for(int i = k; i < n; i++)
#define mmax(start, end) *max_element(start, end)
#define mmin(start, end) *min_element(start, end)
#define argmax max_element
#define argmin min_element
#define push push_back
#define pop pop_back
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef string str;

int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n,p;

    while(cin >> n >> p,n){
        int a[55] = {};
        rep(i,n){
            a[i] = p/n + ((p%n>i)? 1 : 0);
        }
        // rep(i,n) cout << a[i] << " ";
        // cout << endl;
        int t = p%n;
        int owan = 0;
        int ans = -1;
        while(true){
            if(owan>0) {
                owan -=1;
                a[t]+=1;
                if(a[t]>=p){
                    ans = t;
                    break;
                }
            }
            else if(a[t]>0){
                owan = a[t];
                a[t] = 0;
            }
            t = (t+1)%n;
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include "bits/stdc++.h"
#define int long long
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
#define all(a) (a).begin(),(a).end()
using namespace std;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const long double EPS = 1e-8;
using vi = vector <int>;
using vvi = vector <vi>;

signed main() {
	int n, p;
	while (cin >> n >> p, n) {
		int wan = p;
		int step = 0;
		int have[n] = {};
		int ans;
		while (1) {
			rep (i, n) {
				if (wan != 0) {
					wan--;
					have[i]++;
					if (!wan) {
						bool flag = true;
						rep (j, n) {
							if (j == i) continue;
							if (have[j]) {
								flag = false;
							}
						}
						if (flag) {
							cout << i << endl;
							goto end;
						}
					}
				} else {
					wan += have[i];
					have[i] = 0;
				}
			}
			step++;
		}
	end:;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cstdlib>
#include<cassert>
#include<random>
using namespace std;

const int e=1e9+7;

int cut(int n){
    if(n>255){n=255;}
    if(n<0){n=0;}
    return n;
}

int dp[25000][256];


int main(){
    while(1){
    
        
        
    int n,m;
    fill(&dp[0][0],&dp[24999][256],e);
    vector<int> in,c;
    cin>>n>>m;
        if(n==0 && m==0){break;}
    for(int i=0;i<m;i++){
        int d;
        cin>>d;
        c.push_back(d);
    }
    for(int i=0;i<n;i++){
        int d;
        cin>>d;
        in.push_back(d);
    }
    dp[0][128]=0;
    
    for(int i=0;i<n;i++){
        for(int t=0;t<256;t++){
            for(int h=0;h<c.size();h++){
                if(dp[i][t]!=e){
                    dp[i+1][cut(t+c[h])]=min(dp[i+1][cut(t+c[h])],dp[i][t]+(int)pow((in[i]-cut(t+c[h])),2));
                }
            }
        }
    }
    int ans=e;
    for(int i=0;i<256;i++){
        ans=min(ans,dp[n][i]);
    }
    cout<<ans<<endl;
    
        
        
    }
    return 0;
}