semeru/Code-Code-CloneDetection-POJ104 huggingface dataset:
void main()
{
 int a[300];
 int c,p,i,j,k,n,m;
 while(scanf("%d%d",&n,&m)&&n!=0&&m!=0)
  {
    for(i=0;i<n;i++) a[i]=1;
    p=0;
    for(i=0;i<n;i++)
     {
       c=0;
       for(j=p;c<m;j=(j+1)%n)
         {if(a[j]==1) c++;
          if (c==m) 
          {a[j]=0;
           p=(j+1)%n;}
          }
        if(i==n-1) 
            {p=(p+n-1)%n;
             p++;}
         }
      printf("%d\n",p);
}}
void f(int n,int m)
{
	int i,x=0;
	for(i=2;i<=n;i++)
	{
		x=(x+m)%i;
	}
	printf("%d\n",x+1);
}
void main()
{
	int n[999],m[999],k=0;
	int i=0;
	scanf("%d%d",&n[i],&m[i]);
	while(n[i]!=0&&m[i]!=0)
	{
		scanf("%d%d",&n[i+1],&m[i+1]);
		i++;
	}
	k=i;
	for(i=0;i<k;i++)
		f(n[i],m[i]);
}
int main()
{
    int a[300],b,c,d,i,j,m,k,n;
    scanf("%d%d",&m,&n);
    while(m!=0&&n!=0)
    {
                 
     for(i=0;i<m;i++)
   {  a[i]=0;}
     i=0;
     j=0;
     for(b=0;j<m;b=(b+1)%m)
   {
    if(a[b]==0)
     {i++;}
     if(i==n)
     {i=0;
     j++;
     a[b]=j;}

}
  if(b==0)
          b=m;
 
     printf("%d\n",b);
     scanf("%d%d",&m,&n);}
    return 0;}
void main()
{int f(int n,int m);
int a[100],b[100],c[100],i;
for(i=0;i<100;i++)
{scanf("%d%d",&a[i],&b[i]);
if(a[i]==0&&b[i]==0)
break;
c[i]=f(a[i],b[i]);
}
for(i=0;a[i]!=0;i++)
printf("%d\n",c[i]);
}

int f(int n,int m)
{int z;
if(n==1)
z=1;
if(n!=1)
{z=(f(n-1,m)+m)%n;
if(z==0)
z=n;
}
return(z);
}
int ysf(int n,int m);
int main(){

	int nz[500],mz[500];
	int s=0;
	for (int i=0;i<500;i++){
		scanf("%d %d",&nz[i],&mz[i]);
		s++;
		if (nz[i]==0&&mz[i]==0){break;}}

	for (int i=0;i<s;i++){
		while (nz[i]!=0&&mz[i]!=0){
			printf("%d\n",ysf(nz[i],mz[i]));
			break;	
		}
	}
	return 0;
}

int ysf(int n,int m){
        if (n==1){
		    return 1;
		}else{
			return(ysf(n-1,m)+m-1)%n+1;
		}}
int main()
{
	int n[p],m[p],i;
	for(i=0;i<p;i++){
		scanf("%d %d",&n[i],&m[i]);
		if(n[i]==0&&m[i]==0){
			break;}}
     for(i=0;m[i]!=0&&n[i]!=0;i++){
	    int j,k=0;
		for(j=2;j<=n[i];j++){
			k=((m[i]%j)+k)%j;}
		if(n[i]==0&&m[i]==0){
			break;}
		printf("%d\n",k+1);
	
	}
    return 0;
}
int calculate(int n,int m)
{
	int num,monkey[1000]={0},*p;
	int i;
	num=n;
	for(i=0;i<n;i++)
	{
		monkey[i]=1;
	}
	p=&monkey[0]-1;

	while(num!=1)
	{
		for(i=1;i<=m;i++)
		{
			p+=1;
			if(p>monkey+n-1)
				p-=n;
			while(*p==0)
			{
				p+=1;
				if(p>monkey+n-1)
				    p-=n;
			}
		}
		*p=0;
		num-=1;
	}
	i=0;
	while(monkey[i]==0)
		i+=1;
	return(i+1);
}


void main()
{
	int n,m,king;
	do{
		scanf("%d%d",&n,&m);
		if(n!=0&&m!=0)
		{
		    king=calculate(n,m);
		    printf("%d\n",king);
		}
	}while(n!=0&&m!=0);
}
main()
{  int n,m,i,j,k;
   int a[1000];
 
 
 for (;;)
  { 
   scanf("%d %d",&n,&m);
   if(n==0&&m==0) break;
   else
{  for(i=1;i<=n;i++)
         a[i]=i;
   for(k=1,i=1;i<=n-1;i++)
   { k=(k+m-2)%(n+1-i)+1;
      for(j=k;j<=n-i;j++)
      a[j]=a[j+1];  }
 printf("%d\n",a[1]); 
}
}

}
main()
{
	int m[301];
	int n[301];
	int o[301];
	int a[301][301];
	int i,j,k=0,p,q,r;
	for(i=1;;i++)
	{
		scanf("%d %d",&n[i],&m[i]);
		if(n[i]==0&&m[i]==0)break;
		k++;
	}
	for(i=1;i<=k;i++)
	{
		for(j=1;j<=n[i];j++)
		{
			a[i][j]=j;
		}
	}
	for(i=1;i<=k;i++)
	{
		for(j=0,q=1,p=1;p<n[i];)
		{
			j++;
			if(j==n[i]+1)j=1;
			if(a[i][j]!=0)q++;
			if(q==m[i]+1)
			{
				q=1;
				a[i][j]=0;
				p++;
			}
		}
		for(r=1;r<=n[i];r++)
		{
			o[i]+=a[i][r];
		}
		printf("%d\n",o[i]);
	}
}
int main(){
int i ;
int n[100],m[100],r,row=0;
do{
	row++;
	scanf("%d %d",&n[row],&m[row]);
	
}while(n[row]!=0&&m[row]!=0);

int a[300];

int j = 0,k=0,L;
for(r=1; r < row; r++){
	for(i = 1; i<= n[r]; i++){
		a[i] = i;
	}

	for(i = 1; i <= n[r]; i++){
		if(a[i]!=0){
			j++;
			if(j==m[r]){
				a[i] = 0;
				k++;
				j=0;
			}
		}
		if(i == n[r]){
			i = 0;
		}
		if(k==n[r]-1){
			for( i =1; i <= n[r]; i++){
				if(a[i]!=0){L= a[i];}
			}
			break;
		}
	}
	j=0;
	k=0;
	printf("%d\n",L);
}


return 0;
}
int main()
{
    int m,n,i,y=0,h,x=1;
    for(h=1;h<=20;h++)
    
{
    scanf("%d %d",&n,&m);
    if(m!=0&&n>=2)
    
{
    for(i=2;i<=n;i++)
    y=(y+m)%i;
    printf("%d\n",y+1);
    y=0;
}
    else if(n==1)
    printf("%d\n",x);
    else if(m==0&&n==0) 
    break;
}
 
}
int main()
{
	int a[400];
	int n,m,i,j,count;
	cin >> n >> m;
	for (i = 0; i < 400; i++)
		a[i] = 1;
	count = 0;
	i = 0;
	j = 0;
	while (n != 0)
	{
		while (count < (n - 1))
		{
			i++;
			if (i == (n + 1))
			{
				i = 1;
			}
			if (a[i] != 0)
			{
				j++;
				if(j == m)
				{
					a[i] = 0;
					j = 0;
					count++;
				}
			}
		}
		for (i = 1; i <= n; i++)
		{
			if (a[i] != 0)
			{
				cout << i << endl;
				break;
			}
		}
		for (i = 0; i < 400; i++)
		    a[i] = 1;
		cin >> n >> m;
		count = 0;
		i = 0;
		j = 0;
	}
	return 0;
}
int main(int argc, char* argv[])
{
	int m,n,k,i,turn,baoshu,ing,p;
	long begin,end;
	
	int monkey[300];
	int king[2];
	while(1){
	scanf("%d%d",&n,&m);//??n?m
	if(n!=0){
		for(i=0;i<n;i++){
	monkey[i]=i+1;}//????????monkey[300]???
		if(m==1){
		printf("%d\n",n);}
		else{
		for(i=0,k=0;i<n;i++){
			if((i+1)%m!=0){
		monkey[k]=i+1;
		k++;}}
		king[0]=0;
		king[1]=k;//?????????k?
	
		for(turn=2,begin=n+1,ing=0;ing!=1;turn++){//??????
			if(turn==2){
				end=begin+king[1];}//????????n+1??????n+k?
			else{
			begin=end;
			end=begin+ing;}
		for(baoshu=begin,ing=0;baoshu<end;baoshu++){
			if(baoshu%m!=0){
				p=baoshu-begin;
	    monkey[ing]=monkey[p];
		ing++;}}}//ing?????????????
		printf("%d\n",monkey[0]);}}
		
	else{
		break;}}
	return 0;}
int main(){
	int y[300],n[300],m[300];
	int i,k,z;
	k=0;
	while(1){
		scanf("%d %d",&n[k],&m[k]);
		if((n[k]==0)||(m[k]==0))
			break;
		k++;
	}
	for(z=0;z<k;z++){
		y[z]=0;
		for(i=1;i<n[z];i++){
			y[z]=(y[z]+m[z])%(i+1);
		}
		printf("%d\n",y[z]+1);
	}
	return 0;
}
main()
{
    int n[20],m[20],z[201];
    int i,j,k,l,t;
    for(i=0;;i++)
    {
        scanf("%d %d",&n[i],&m[i]);
        if(n[i]==0&&m[i]==0)
        {break;}
    }

    for(i=0;n[i]!=0||m[i]!=0;i++)
    {
        int z[201]={0},t;
        for(j=0;j<n[i];j++)
        {
            z[j]=j+1;
        }
        t=n[i];
        k=0;
        for(j=1;j<=t-1;j++,n[i]--)                //????j<=n[i]-1?n[i]???!!
        {
            k=(m[i]+k-1)%n[i];
            for(l=k;z[l]!=0;l++)              //??????    ????'0' !!!!
            {
                z[l]=z[l+1];
            }
        }
        printf("%d\n",z[0]);
    }
}
main()
{
	for (int i=0;;i++)
	{ 
    int m,n;
   scanf("%d%d",&m,&n);
    if (m==0)
    break;
    else
    {
	int a[1000];
    for(int i=0;i<m;i++)
        a[i]=1;
    int index=0;
    int loc;
    int t=0;
    int p=m;
    while(p>0)
    {
        if(a[index]==1)
        {    
            ++t;
            if(t%n==0)
            {
                a[index]=0;
                loc=index;
                p--;
            } 
        }
        if(index < m-1)
            index++;
        else
            index=0;
    }
    printf("%d\n",loc+1);
   }
   }
}
void monkey(int a,int b)
{
	int t[500],i,j,q=0,time;
    //sc("%d%d",&a,&b);
	
	for(i=0;i<500;i++)
		t[i]=-3;
	for(i=0;i<a;i++)
		t[i]=0;

    for(i=0;i<a-1;i++)
	{	
		time=0;
		for(j=0;;j++)
		{
			if(q>=a)
				q=q-a;

			if(t[q]==0)
				time++;
			q++;
			if(time==b)
			{
				t[q-1]=1;
				break;
			}
			
		}
	}
	for(i=0;i<a;i++)
		if(t[i]==0)
			printf("%d\n",i+1);
}
void main()
{
	int c[100],d[100],i,j;
	for(i=0;i<100;i++)
	{
		c[i]=-1;
		d[i]=-1;
	}
	i=0;
	while(!(c[i-1]==0&&d[i-1]==0))
	{
		scanf("%d%d",&c[i],&d[i]);
		i++;
	}
	for(j=0;j<i-1;j++)
	{
		monkey(c[j],d[j]);
	}
}
void p_king(int m,int n)
{
	int i,j,k,a[300];
	int *p;
	for(p=a+1,i=1;p<=a+n;*p++=i++);
	for(i=0,j=0,k=0,p=a;k<n-1;)
	{
		i=i%n+1;
		if(*(p+i)!=0)
		{
			j++;
			if(j%m==0) {k++;*(p+i)=0;}
		}
	}
	for(i=1;;i++)
		if(a[i]!=0) {printf("%d\n",a[i]);break;}
}
void main()
{
	int i,m[20],n[20];
	for(i=0;;i++)
	{
	scanf("%d%d",&n[i],&m[i]);
	if(m[i]==0 && n[i]==0) break;
	}
	for(i=0;;i++)
	{
		if(m[i]==0 && n[i]==0) break;
		p_king(m[i],n[i]);
	}
}
int main()
{
	int monkey[500], r[500],i,j,m,n,left,count,time=0;

		/*
		*	?????
		*	monkey???????????
		*	r???????????????????????
		*	left????????
		*	count????
		*	time??????????????0 0?
		*/

	scanf("%d%d", &m, &n);
	while(m!=0 && n!= 0)
	{
		//??????????????
		for(i=0; i<m; i++)
		{
			monkey[i] = 1;
		}

		left = m;
		count = 0;
		while(left > 1)
		{
			for(i=0; i<m; i++)
			{
				if(monkey[i])
					count ++;
				if(count == n)
				{
					monkey[i] = 0;
					left --;
					if(left == 1) break;
					count = 0;
				}
			}
		}
		
		//?????????????????
		for(i=0; i<m; i++)
		{
			if(monkey[i]) 
				r[time] = i;
		}

		scanf("%d%d", &m, &n);
		time ++;
	}

	for(i=0; i<time; i++)
	{
		printf("%d\n", r[i]+1);
	}
	
	return 0;
}
int main()
{
	int m,n,i,n0,p1,m0;
	int a[301];
	while(scanf("%d%d",&n,&m),n!=0)
	{
		for(i=0;i<n;i++)
			a[i] = i+1;
		n0 = n;		
		p1 = 0;
		while(n0>1)
		{
			m0 = m;
			while(m0!=1)
			{
				if(a[(p1+1)%n])
				{
					m0--;					
				}
				p1=(p1+1)%n;

			}
			a[p1] = 0;
			while(a[p1%n]==0)
			{
				p1=(p1+1)%n;					
			}
			n0 --;
			
		}
		
		printf("%d\n",a[p1]);
		
	}
	
	return 0;
}
int main()
{
	
				//i??????
	while (1)
	{
		int m,n,j=1,count,k = 1 ,i=0,a[301] = {0};
		cin>> n>>m;
		if (n ==0)break;
		count=n;
		j=0;
		while(count > 1)
		{
			i++;
			while(1)
			{
				if (k > n) k-=n;
				if (!a[k]) break;
				k++;
			}
			if (i == m)
			{
				a[k] = 1;
				i = 0;
				count--;
				k++;
				continue;
			}
			k++;
		}
		for (i = 1; i <= n; i++)
		{	
			if (!a[i]) cout << i << endl;
		}
	}
	return 0;
}
void ysf(int n,int m)
{
	int i,j,b[300]={0},num,p;
	num=0;
	for(i=1;i<=n-1;i++)
	{
		for(j=1;j<=m;j++)
		{
			num=num+1;
			if(num>n)
					num=num-n;
			while(b[num]==1)
			{
				num=num+1;
				if(num>n)
					num=num-n;
			}
		}
		b[num]=1;
	}
	for(i=1;i<=n;i++)
	{
		if(b[i]==0)
		{
			printf("%d\n",i);
			break;
		}
	}
}
void main()
{
	int i,j,n[100],m[100];
	for(i=0;;i++)
	{
		scanf("%d%d",&n[i],&m[i]);
		if(m[i]==0)
			break;
	}
	for(j=0;j<i;j++)
		ysf(n[j],m[j]);
}
int josafe(int n,int m) 
 {
  int jo[1000],i,j,count,s;
  for(i=0;i<n;i++)
   jo[i]=1;
  j=-1;
  for(i=1;i<=n-1;i++)
  {count=0;
   while(count<m)
    {j=(j+1)%n;
     count=count+jo[j];
    }
    jo[j]=0;
    }
 for(i=0;i<n;i++)
   if(jo[i]==1)
   {s=i+1;
   break;}
  return(s);
  }
void main()
{
 int i,a[1000],b[1000],j,c[1000];
 for(i=0;;i++)
  {scanf("%d %d",&a[i],&b[i]);
  if(a[i]==0&&b[i]==0)
   break;
  else  c[i]=josafe(a[i],b[i]);}
 for(j=0;j<i;j++)
  printf("%d\n",c[j]);
}
struct monkey
	{
		int num;
		struct monkey *next;
	};
void main()
{
	int kingf(int n,int m);
	int m=1,n=1;
	int king;
	for(;;)
	{
		scanf("%d%d",&n,&m);
		if(m==0&&n==0)break;
		if(m==1)printf("%d\n",n);
		else
		{
			king=kingf(n,m);
			printf("%d\n",king);
		}
	}
}
int kingf(int n,int m)
{
	int i;
	struct monkey monkey[800];
	struct monkey *p1=&monkey[0],*p2=&monkey[0];

	for(i=0;i<n-1;i++)
	{
		monkey[i].next=&monkey[i+1];
		monkey[i].num=i+1;
	}
	monkey[i].next=&monkey[0];
	monkey[i].num=i+1;

	for(;p1!=p1->next;p1=p1->next)
	{	
		for(i=1;i<m-1;p1=p1->next,i++);
		p2=p1;
		p2=p2->next;
		p1->next=p2->next;
	}
	return p1->num;	
}
void main() 
{ 
	int n,m,i,a,b;
          a=0;
          b=0;
	for(;;b++)
	{
		scanf("%d",&n);
		scanf("%d",&m);
		if(n==0 && m==0)
			break;
		for(i=2;i<=n; i++)
			a=(a+m)%i;
			printf("%d\n",a+1);
			a=0;
	}
return 0;
}
void main()
{
	int i,k,f[301],n,m;
	scanf("%d%d",&n,&m);
	while (n*m!=0)
	{
		f[1]=0;
		for (i=2;i<=n;i++)
		{
			k=m%i;
			f[i]=(f[i-1]+k)%i;
		}
		printf("%d\n",f[n]+1);
		scanf("%d%d",&n,&m);
	}
}
int main()
{
  while(1)
  {
	int  n,m;
	scanf("%d %d",&n,&m);
	if(n==0&&m==0)
	{
	  break;
	}
	else
	{
	  //int m1;
	  int i,j=0,sum=0;
	  int *a=(int*)malloc(sizeof(int)*n);
	  for(i=0;i<n;i++)
	  {
	    a[i]=1;
	  }
	  for(i=0;1;i++)
	  {
	    while(i>n-1)
	    {
	      i=i%n;
	    }
	    sum=sum+a[i];
	   // m1=m;
	    //while(m1>(n-j))
	   // {
	   //   m1%=(n-j);
	   // }
	    if(m==sum)
	    {
	      a[i]=0;
		  j++;
		  sum=0;
	    }
	    if(j==n-1)
	    {
	      break;
	    }
	  }
	  for(i=0;i<n;i++)
	  {
	    if(a[i]>0)
	    {
	      printf("%d\n",i+1);
		  break;
	    }
	  }
            free(a);
	}
  }
	return 0;
}
int main()
{	
	for(;;)
	{
	int m,n,i,j,k,t=0;
	scanf("%d%d",&n,&m);
	if(n==0)break;
	int a[n];
	for(i=0;i<n;i++)
	a[i]=0;
	for(i=1;t<n-1;)
	{
		for(j=0;j<m;)
		{
		if(a[i-1]==0)j++;
		i++;
		if(i>n)i%=n;
		}
		t++;
		if(i!=1)a[i-2]=1;
		else a[n-1]=1;	
	}
	for(i=0;i<n;i++)
	{
		if(a[i]==0)printf("%d\n",i+1);
	}
	}
}
int main(int argc, char *argv[])
{
  int a[300000],n[1000],m[1000],answer[1000];
  int k=0,i,j,l,x=0,t;
  while(x==0)
  {
      k++;
      scanf("%d%d",&n[k],&m[k]);
      if(n[k]==0&&m[k]==0) x=1;
  }
  for(i=1;i<=k-1;i++)
  {
      for(t=1;t<=300;t++) a[t]=t;
      j=1;
      l=n[i]+1;
      while(j<l-1)
      {
          if(j%m[i]==0) j++;
          else
          {
              a[l]=a[j];
              j++;
              l++;
          }
      }
      answer[i]=a[j];
  }
  for(i=1;i<=k-1;i++) printf("%d\n",answer[i]);
  return 0;
}
int ysf(int m,int n)
{
    int *p;
    int i,j,k;
    p=(int*)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
    {
         p[i]=0;
    }
    k=n;
    j=1;
    for(i=0;i<n;i=(i+1)%n)
    {
         if(p[i])
         {
              continue;
         }
         if(j++==m)
         
         {
                   p[i]=1;
                   k--;
                   j=1;
         }
         if(k==1)
         {
              break;
         }
   }
   i=0;
   for(;i<n;i++)
   {
                if(p[i]==0)
                break;
   }
   free(p);
   return i+1;
    
}
int main()
{
    int n, m;
    scanf("%d%d",&n,&m);
    while(m!=0||n!=0)
    {
                     

         printf("%d\n",ysf(m,n));   
         scanf("%d%d",&n,&m);
    }
                  
}
void main()
{
    int i,m,n,s;
    for (s=1;s<100;s++)
{
    scanf("%d%d",&m,&n);
    if (m==0&&n==0) break;
    int a[1000];
    for(i=0;i<m;i++)
    {
        a[i]=1;
    }
    int index=0;//????
    int loc;//????
    int t=0;
    int p=m;//?????
    while(p>0)
    {
        if(a[index]==1)
        {   
            ++t;
            if(t%n==0)
            {
                a[index]=0;
                loc=index;
                p--;
            }
 
        }
        if(index < m-1)
            index++;
        else
            index=0;
    }
    printf("%d\n",loc+1);
}}
int main(void)
{
int i,j;
int s = 0;
int n;
int m;
for(j=1;j<=9;j++){
scanf("%d%d\n", &n,&m);
for (i = 1; i <= n; i++)
{
s = (s + m) % i;
}

printf("%d\n", s + 1);}

return 0;
}
int main()	{
	int a[100], b[100];
	int i, p = 1, q = 1;
	for (i = 0; p != 0 && q != 0; i ++)	{
		scanf("%d%d", &p, &q);
		a[i] = p; b[i] = q;
	};
	int j, aa, bb;
	int monkey[500];
	for (j = 0; j < i - 1; j ++)	{
		aa = a[j]; bb = b[j];
		int sum = 0;
		int ptr, s = 0;
		for (ptr = 0; ptr < aa; ptr ++)	monkey[ptr] = 1;
		for (ptr = 1; sum < aa - 1; ptr = (ptr + 1) % aa)	{
			if (monkey[ptr] == 1)	{
				s ++;
				if (s == bb)	{
					monkey[ptr] = 0;
					sum ++;
					s = 0;
				};
			};
		};
		int ans;
		for (ptr = 0; ptr < aa; ptr ++)	if (monkey[ptr] == 1)	ans = ptr;
		if (ans == 0)	ans = aa;
		printf("%d\n", ans);
	};
	return 0;
};
int main()
{
	int m,n,a,b,c;
	int hz[300];
	scanf("%d%d",&n,&m);
	while((n!=0)&&(m!=0))
	{
		c=0;
		for(a=2;a<=n;a++)
		{
			c=(c+m)%a;
		}
		printf("%d\n",c+1);
		scanf("%d%d",&n,&m);
	}
	return 0;
}
int hezi(int a[])
{
	int m,i=1;
	while(1)
	{
		if(a[i]!=0)
		{
			m=a[i];
			break;
		}
		i=i+1;
	}
	return m;
}
int pan(int n,int m)
{
	int a[301],b[301],i,k,l,p,s=0,r;
	for(i=0;i<=n;i++) a[i]=i;
	while(1)
	{
		l=1;
		k=0;
		while(m*l+s<=n)
		{
			a[m*l+s]=0;
			l=l+1;
			k=k+1;
			if(n-k==1)
			{
				a[1]=hezi(a);
				break;
			}
		}
		if(n-k==1) break;
		p=m*l+s-n;
		n=n-k;
		s=p%n;
		if(s==0) s=n;
		s=s-m;
		r=1;
		for(i=1;i<=n+k;i++)
		{
			if(a[i]!=0)
			{
				b[r]=a[i];
				r=r+1;
			}
		}
		for(i=1;i<=n;i++) a[i]=b[i];
	}
	return a[1];
}
int main()
{
	int n,m,a[20],i=0;
	while(1)
	{
	    scanf("%d %d",&n,&m);
	    if(m+n==0) 
		{
		    a[i]=0;
		    break;
		}
	    a[i]=pan(n,m);
	    i=i+1;
	}
	for(i=0;a[i]!=0;i++) printf("%d\n",a[i]);
	return 1;
}
int sz[301];
int m,n;
int remove(int k);
int main(){
	int i,k;
	scanf("%d%d",&n,&m);
	while(m||n){
		for(i=0;i<n;i++){
			sz[i]=i+1;
		}
		for(k=0;n>1;n--){
			k=remove(k);
		}
		printf("%d\n",sz[0]);
		scanf("%d%d",&n,&m);
	}
	return 0;
}
int remove(int k){
	int i,t;
	t=(m+k-1)%n;
	for(i=t+1;i<n;i++){
		sz[i-1]=sz[i];
	}
	return t%(n-1);
}
int N,M;
 
int main(){
    int i;
    int alive,count;
    int x[320];
    
    while (1){
        scanf("%d%d",&N,&M);
        if (N==0 && M==0) break;
        alive = N;
        count = 0;
        memset(x,0,sizeof(x));
        for (i = 1; alive > 1 ;i = i%N+1){
            if (!x[i]){
                count++;
                if (count == M){
                    x[i] = 1;
                    alive --;
                    count = 0;
                }
            }
        }
        for (i = 1; i <= N ;i++)
           if(!x[i]) printf("%d\n",i);
    }

    return 0;
}
int main()
{int n=0,p,i,j,t,k;
int sl[301],cz[301];
for(i=0;i<301;i++)
	{scanf("%d%d",&(sl[i]),&(cz[i]));
		if(sl[i]==0){break;}
		n=n+1;
	}
	k=1;
for(t=0;t<n;t++){
	for(i=1;i<=sl[t];i++){
		k=(k+cz[t]-1)%i+1;}
	printf("%d\n",k);
	}
return 0;
}
main()
{
      int a[300],i,j,k,m,n;
      scanf("%d %d",&n,&m);     
      while(m+n!=0)
      {
       for(i=1;i<=n;i++)
          a[i]=i;
          k=1;
          while(n>1)
          {
              k=(k+m-1)%n;
              if(k==0)  k=n;
              for(i=k;i<n;i++)
                 a[i]=a[i+1];
              n--;
          }
          printf("%d\n",a[1]);
          scanf("%d %d",&n,&m);
      }       
}
int fun(int m,int a[],int n,int*p)
{
	int*q,i;
	if (n==1)
		return (a[0]);
	else
	{
		p=p-1;
		for(i=1;i<=m;i++)
		{
			if (p==a+n-1)
				p=a;
			else
				p++;
		}
		for(q=p;q<a+n-1;q++)
			*q=*(q+1);
		return (fun(m,a,n-1,p));
	}
}
void main()
{
	int m[100],n[100],i,count,j;
	int*p;
	int a[1000];
	for(i=0;;i++)
	{
		
		scanf("%d%d",&n[i],&m[i]);
		if (n[i]==0)
			break;
	}
	count=i-1;
	for(i=0;i<=count;i++)
	{
		for(j=0;j<1000;j++)
			a[j]=j+1;
		p=a;
		printf("%d\n",fun(m[i],a,n[i],p));
	}
}
struct monkey
{
	int x;
	struct monkey *l,*r;
};

int guowang(int n,int m)
{
	int wang,i,j;
	struct monkey *p,*s,*t;
	p=(struct monkey *)malloc(n*sizeof(monkey));
	for(i=1;i<n-1;i++)
	{
		(p+i)->x=i+1;
		(p+i)->l=p+i-1;
		(p+i)->r=p+i+1;
	}
	p->x=1;
	(p+n-1)->x=n;
	p->l=p+n-1;
	p->r=p+1;
	(p+n-1)->l=p+n-2;
	(p+n-1)->r=p;
	for(i=1;i<n;i++)
	{
		s=p;
		for(j=1;j<m;j++)
			s=s->r;
		
		s->l->r=s->r;
		
		s->r->l=s->l;
		p=s->r;
	}
	wang=p->x;
	return wang;
}


int main()
{
	int n[100],m[100],l,a[100],i;
	for(l=0;;l++)
	{
		scanf("%d %d",&n[l],&m[l]);
		if(n[l]==0&&m[l]==0)
			break;
		else
			a[l]=guowang(n[l],m[l]);
	}
	
	for(i=0;i<l;i++)
		printf("%d\n",a[i]);
}
void main()
{
int n[800], m[800], i, k,l=0,s=0;
for(k=0;1;k++)
{
	scanf("%d %d", &n[k], &m[k]);
	if(n[k]==0&&m[k]==0)
	break;
}
for(k=0;1;k++)
{if(n[k]==0&&m[k]==0)
break;
else if(n[k]==1)
printf("%d\n",n[k]);
else
{	for (i=2; i<=n[k]; i++) s=(s+m[k])%i;
printf ("%d\n", s+1);
s=0;}
}
}
int Joseph(int n,int m)
{
	int l[300];
	int i,j;
	for(i=0;i<n-1;i++)
	{
		l[i] = i+1;
	}
	l[n-1] = 0;
	do
	{
		for(j=1;j<m;j++)
		{
			i = l[i];
		}
		l[i] = l[l[i]];
	}
	while(l[i] != i);
	return(i+1);
}
void main()
{
	int n,m;
	while(1)
	{
		scanf("%d %d",&n,&m);
		if(n==0 && m==0)
			break;
		printf("%d\n",Joseph(n,m));
	}
}
struct monkey
{
	int num;
	int nextmonk;
};


void countoff(int n,int m)
{
	struct monkey *monk
		=(struct monkey *) malloc(LEN*n);
	int i,j,sum=n,last;

	for(i=1;i<=n;i++){
		monk[i].num=i;
		monk[i].nextmonk=i+1;
	}
	monk[n].nextmonk=1;
	last=i=n;

	while(sum>1){
		j=0;
		while(j!=m){
			i=monk[i].nextmonk;
			if(monk[i].num){
				j++;
				if(j!=m) last=monk[i].num;
			}
		}
		monk[i].num=0;
		sum--;
	}
	printf("%d\n",last);
}



void main()
{
	int n,m;
	while(1){
		scanf("%d %d",&n,&m);
		if(m==0&&n==0) break;
		countoff(n,m);
	}
}
int main()
{
	int m,n;
	int in[320];   //????
	int i;
	int num;        //????
	int last;
	while(1>0)
	{
		scanf("%d%d",&n,&m);
		if((n==0)||(m==0))
			break;
		else
		{
			for(i=1;i<=n;i++)
			{
				in[i]=i;   //?????
			//	printf("%d\n",in[i]);  //debug
			}
			//??????
			last=1;
			num=n;
			while(num!=1)
			{
				last+=m-1;   //??????
			//	printf("%d",last);   //debug
				while(last>num)
				{
					last=last-num;
				}
			//	printf("   %d\n",last);//debug
				num--;
				for(i=last;i<=n;i++)
				{
					in[i]=in[i+1];
				}
			//	printf("\n%d",num);  //debug
			/*	for(i=1;i<=num;i++)
				{
					printf(" %d",in[i]);
				}
				printf("\n");  */   //debug
			}
			printf("%d\n",in[1]);
		}
	}
	return 0;
}
int main(){
    int m,n,i,x,e,max;
    while(e==e){
    scanf("%d %d",&n,&m);
    if(n==0&&m==0)break;
    int c[n+1];
    for(i=1;i<=n;i++)
        c[i]=i;
       x=n;
for(i=1;;) {
     for(i=1;i<=n;i++)
        if(c[i]%m!=0){
           x++;
           c[i]=x;
        }
    max=1;
    for(i=1;i<=n;i++)
       if(c[i]>c[max])
         max=i;
    e=0;
    for(i=1;i<=n;i++)
        if(c[i]%m==0)
            e=e+1;
      if(e==n)break;

}
printf("%d\n",max);}
return 0;
}
main(){
      int a[301];
      int b[301];
      int i,j,n,m,c,x;
      scanf("%d %d",&n,&m);
      while(n!=0){
      for(i=1;i<=n;i++)a[i]=i;
      x=n;
      for(i=1;i<n;i++){c=m%x;if(c==0)c=x;for(j=1;j<=(x-c);j++)b[j]=a[j+c];for(j=1;j<=c-1;j++)b[x-c+j]=a[j];x=x-1;
      for(j=1;j<=x;j++)a[j]=b[j];
                       
                       }
                       printf("%d\n",a[1]);
                      scanf("%d %d",&n,&m); }
      
       }
int main()
{
	int n,m,r,i;
	scanf("%d %d",&n,&m);
	while(n&&m)
	{
	if(n>=2)
	{
		for(i=2,r=1;i<=n;i++)
		{
			r=(r+m%i)%i;
			if(r==0)
				r=i;
		}
	}
	else if(n==1)
		r=1;
		printf("%d\n",r);
		scanf("%d %d",&n,&m);
	}
	return 0;
}
main()
{
int n,m,i,s=0;
for(;;)
{
scanf("%d %d",&n,&m);
if(n==0&&m==0) break;
if(n==1) printf("%d\n",n) ;
else
{
    s=0; 
for(i=2;i<=n;i++)
s=(s+m)%i;
printf("%d\n",s+1);
}

}

}
int main()
{
int n, m, s;
while( cin >> n >> m && n )
{
s = 0;
for( int i = 2; i <= n; i ++ )
s = ( s + m ) % i;
cout << s + 1 << endl;
}
return 0;
}
struct monkey
	{
		int number;
		int selected;
		struct monkey *next;
	};
void maintoo(int,int);
void main()
{
	
	int n[20],m[20],i,j;
	for(i=0;;i++)
	{
		scanf("%d%d",&n[i],&m[i]);
		if(!m[i]) break;
	}
	for(j=0;j<i;j++)
		maintoo(n[j],m[j]);
}
void maintoo(int n,int m)
{
	struct monkey *p1,*p_this,*p_next;
	int i;
	p1=(struct monkey *)malloc(sizeof(struct monkey));
	p_this=p1;
	for(i=1;i<n;i++)
	{
		p_this->number=i;
		p_this->selected=1;
		p_next=(struct monkey *)malloc(sizeof(struct monkey));
		p_this->next=p_next;
		p_this=p_next;
	}
	p_this->number=n;
	p_this->selected=1;
	p_this->next=p1;
	do
	{
		i=0;
		do
		{
			p_this=p_this->next;
			if(p_this->selected) i++;			
		}
		while(i<m);
		p_this->selected=0;
		n--;
	}
	while(n>0);
	printf("%d\n",p_this->number);
}
struct node
{
	int data;
	struct node *next;
};

int choose(int n, int m)
{
	int i=2;
	struct node *head;
	struct node *p1, *p2;
	head=(struct node*)malloc(len);	
	head->data = 1;	
	p1 = head;
	for(i=2; i<=n; i++)
	{	
			p2 = (struct node*)malloc(len);	
			p2->data = i;		
			p1->next = p2;	
			p1 = p2;	
	}
	p1->next = head;
	i=1;

	while(head->next != head)
	{
		for(i=1; i<m; i++)
		{
			p1 = head;
			head = head->next;
		}
		p1->next = head->next;
		head = head->next;
	}
	return(head->data);
}

int main()
{
	int n=1, m=1, result;
	while(n!=0 && m != 0)
	{
		scanf("%d%d", &n, &m);
		if(n==0 && m==0)
			break;
		printf("%d\n", choose(n, m));
	}
	return 0;
}
int main()
{
	int i,j,k,a=0;
	int m[100],n[100];

	for(i=0;;i++){
		scanf("%d%d",&n[i],&m[i]);
		if(n[i]==0&&m[i]==0){
			break;
		}
		a++;
	}
	for(i=0;i<a;i++){
		k=0;
		for(j=2;j<=n[i];j++){
			k=((m[i]%j)+k)%j;
		}
		printf("%d\n",k+1);
	}

	return 0;
}
int main()
{
	int a[301], n, m, k, t, count;
	
	cin >> n >> m;
	
	while((n != 0) && (m != 0))
	{
		for (k = 1; k <= n; k++)
			a[k] = 1; 
	
		k = 0; t = 0; count = 0; 
	
		while(count < n)
		{  	
			k++;
			if(k == n + 1)
				k = 1;
			if ( a[k] != 0 )
			{ 
				t++;
				if ( t == m )
				{ 
					a[k] = 0; t = 0;
					count++;
				}
			}
		}

		cout << k <<endl;
		
		cin >> n >> m;
	}

	return 0;
}