#include int *S; int *K; int n,k; struct kto { int subor; int kolky; kto():subor(-1),kolky(-1){} kto(int s, int k) { subor=s; kolky=k; } int kam() { return K[subor]+kolky; } }; kto *pole; int odkial; int treba(int i) { kto cluster=pole[i]; if(cluster.subor==-1 || cluster.kam() == i)return 0; if(pole[cluster.kam()].subor==-1) { pole[cluster.kam()]=cluster; pole[i].subor=-1; return 1;//na volne policko } else if(cluster.kam()==odkial) { pole[cluster.kam()]=cluster; pole[i].subor=-1; return 2;//na spracuvane policko } else { int p=treba(cluster.kam())+1; pole[cluster.kam()]=cluster; if(i!=odkial)pole[i].subor=-1; return p; } } int main(){ while(1){ cin>>n>>k; if(n==0 &&k==0) break; pole=new kto[n]; S=new int[k]; K=new int[k]; K[0]=0; for(int i=0;i>S[i]; for(int j=0;j>cluster; pole[cluster-1]=kto(i,j); } if(i)K[i]=K[i-1]+S[i-1]; } int moves=0; for(int i=0;i