semeru/Code-Code-CloneDetection-POJ104 huggingface dataset:
int main()
{
    int m[300],n[300],a[300];
    int i=0,j=0,k=0,r=0,t=0,q=0;
    for (k=0;k<300;k++)
    {
	    scanf("%d %d",&n[k],&m[k]);
	    if (n[k]==0){k=300;}
	    else r=r+1;
    }
    for (k=0;k<r;k++)
    {
        t=0;q=0;
        for (i=1;i<=n[k]+1;i++)
        {
            a[i]=i;
        }
        
        for (i=1;i<=n[k]+1;i++)
	    {
            if (i==n[k]+1){i=1;}
            if (a[i]!=0){t=t+1;}
            if (t==m[k]){a[i]=0;t=0;q=q+1;}
            if (q==n[k]-1){i=n[k]+2;q=0;}
        }
        for (j=1;j<=n[k];j++)
        {
            if (a[j]!=0)
            {
               printf("%d\n",a[j]);
            }
        }
    }
    return 0;
}
int main() {
	int i,j,k,m,n;
    for(j=1;j<101;j++){
        scanf(" %d %d", &n, &m);
        k=0;
        for(i=2;i<=n;i++){
            k=((m%i)+k)%i; 
        }
        k++;
        if(n!=0){printf("%d\n", k);}
    }
    return 0;
}
int main()
{
	int n=0,m=0,i=0,s=0;
	cin>>n>>m;
	while(n!=0)
	{
	for(i=2;i<=n;i++)
		{
           s=(s+m)%i;
	}cout<<s+1<<endl;
cin>>n>>m;
s=0;
}
}
//?????
int main()
{
	int a[300],n,m,i,j,k,sum;
	do//?????????
	{sum=0;
	cin>>n>>m; 
	k=n;//?k??????????
	for (i=0;i<n;i++) 
		a[i]=i+1;//?????
	do 
	{for (i=0;i<n;i++)
		{if (a[i]==0)//?????0????????
			{if (i==n-1) break;//?????????????????????
			else continue;//????????
			}
		sum=sum+1;//????0???????
		if (sum%m==0) 
			{a[i]=0;//?????sum??m???????????0
			k=k-1;//??????????-1
			}
		if (k==1) //?????????????????????
			{for (j=0;j<n;j++)
				 	if (a[j]!=0)			 
					{cout<<a[j]<<endl;
					break;//???????????
					}
			break;
			}
		else if (i==n-1) break;
		}
	}while (k>1);
	}while (n!=0&&m!=0);//?m=0?n=0???
	return 0;
}
main()
{     
      int m,n,j,i,k,c=0;
      int b[1000],a[1000];
      for(c=0;;c++)
      {
      scanf("%d %d",&n,&m);
      if(m==0&&n==0)
      break;
      else if(n==1)
      b[c]=n;
      else
      {
      for(i=0;i<=n-1;i++)
      a[i]=i+1;
      j=0;
      for(i=1;i<=n-1;i++)
      {
      j=(j+m-1)%(n-i+1);
      for(k=0;k<n-i;k++)
          {
          if(k<j)
          a[k]=a[k];
          else
          a[k]=a[k+1];
          }
      b[c]=a[0];
      }
      }
      }
      for(i=0;i<c;i++)
      {if(i==0)
      printf("%d",b[i]);
      else
      printf("\n%d",b[i]);
      }
      
      getchar();
      getchar();
        return 0;
      }
void main()
{
	int n,m,a[321],i,k,count;
	

	do
	{
		scanf("%d%d",&n,&m);
		if (m==0&&n==0) break;

		k=1;
		for (i=1;i<=n;i++)
			a[i]=0;

		for (i=1;i<n;i++)
		{
			count=0;
			do 
			{
				if(a[k]==0)count++;
				if (count!=m)k++;
				if (k>n)k=1;
			}while(count<m);
			a[k]=1;
		
		}

		for(i=1;i<=n;i++)
			if (a[i]==0)
				printf("%d",i);
		printf("\n");
	
	}while(1);
}
int jian(int n,int m)
{
	int i,j,s,w,p;
	int a[301]={0};
	int b[300]={0};
	for(i=0;i<n;i++)
	{
		a[i]=i+1;
	}
	w=n;
	for(i=0;i<w-1;i++)
	{
		s=m%n-1;
		if(s==-1 && m<n) s=m-1;
		else if(s==-1 && m>=n) s=n-1;
		a[s]=a[300];
		if(s==0)
		{
			for(j=0;j<n;j++)
			{
				a[j]=a[j+1];
			}
		}
		else
		{
		for(j=0;j<s;j++)
		{
			b[j]=a[j];
		};
		for(j=s+1;j<n;j++)
		{
			a[j-s-1]=a[j];
		}
		j=0;
		for(j=0;j<s;j++)
		{
			p=n-s+j-1;
			a[p]=b[j];
		}
		}
		n=n-1;
	}
	printf("%d\n",a[0]);
	return(0);
}
void main()
{
	int a[100]={0},b[100]={0};
	int i,j;
	for(i=0;;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
		if(a[i]==0) break;
	}
	for(j=0;j<i;j++)
	{
		jian(a[j],b[j]);
	}
}
/*
 *La vie est ailleurs
 *PROB: Joseph Problems
 *LANG: c
 *IDE: Dev-C++ 4.9.9.2
 *Compiler: gcc
 *ID: 00910049
 *Coding by Xu Zhendong
 */
 


struct Per
{
	int num;
	struct Per *next;
};

int main()
{
	int n, m, i;
	//printf("Please input 'n' and 'm', seperated by space.\nExample: 6 2\n");
	struct Per *head;
	struct Per *p;
	while (1)
{
	scanf("%d %d", &n, &m);
	if (n ==0 || m ==0) break;
	if (n == 1 || m == 1)
	{
		printf ("%d\n", n);
		continue;
	}
	
	head = (struct Per*) malloc (LEN);
	p = head;
	//head -> num = 1;
	for (i = 1; i <= n; i ++)
	{
		p -> num = i;
		if (i == n)
		{
			p -> next = head;
		}
		else
		{
			p -> next = (struct Per*) malloc (LEN);
			p = p -> next;
		}
	}
	p = head;
	/*
	for (i = 1; i <= n; i ++)
	{
		printf("%d",p -> num);
		p = p-> next;
	}
	*/
	
	while (p != p -> next)
	{
		for (i = 1; i < m; i ++)
		{
			if (i == (m - 1))
			{
				p -> next = p -> next -> next;
				p = p -> next;
			}
			else p = p -> next;
		}
	}

	printf("%d\n",p -> num);
	}
	getchar();
	getchar();
	return 0;
}
int main()
{
int n,m,i,a;
while(1)
{cin>>n;
cin>>m;
if(n==0)break;
else{a=0;
if(n>1)
for(i=2;i<=n;i++)
a=(a+m)%i;
cout<<(a+1)<<endl;
}}
return 0;}
int main()
{
	int i,m,n,t,s,a[301];
	for(;;)
	{   scanf("%d %d",&n,&m);
	    if(n==0||m==0)break;
	    for(i=1;i<=n;i++)a[i]=0;
		t=n;s=0;
		for(i=0;t>0;)
			{i++;if(i==n+1)i=1;
			if(a[i]==0)
			{s=s+1;
			if(s%m==0)
			{a[i]=-1;t=t-1;}}}
		printf("%d\n",i);}}
main()
{
	int i,j,k,l,n,m,a[301],s;
	for(;scanf("%d %d",&n,&m)!=EOF;)
	{
		if(n!=0)
		{
			for(l=1;l<=300;l++)
		        a[l]=l;
			for(j=1;j<n;j++)
			{
				for(s=0;s==0;)
				{
					for(i=1;i<=n;i++)
					{
					if(a[i]==m) {a[i]=0;s++;break;}
					}
					if(s==0)
					{
						for(k=1;k<=n;k++)
						{
							if(a[k]!=0) a[k]=a[k]+n-j+1;
						}
					}		
				}//zhao a[i]!=0
				for(k=i+1,s=0;k<=n;k++)
				{
					if(a[k]!=0)
						a[k]=++s;
				}
				for(k=1;k<i;k++)
				{
					if(a[k]!=0)
					    a[k]=++s;
				}
			}
			for(j=1;j<=n;j++)
			{
				if(a[j]!=0) printf("%d\n",j);
			}
		}
	}
}
int king(int M,int N)
{
    int k=0;
    for(int i=2;i<=M;i++)
    k=(k+N)%i;
    return ++k;
} 
int main()
{
    int n,m,a[100],i=1,x;
    while(scanf("%d%d",&n,&m)&&n&&m)
    {
                                    if(m==0&&n==0)
                                    break;
                                    else
                                    a[i]=king(n,m); 
                                    x=i;
                                    i++;
    }
    for(i=1;i<=x;i++)
    {
               cout<<a[i]<<endl;
    }
    return 0;
}
int a[301];
int b[301];
int select(int n, int m){int tp=0;
                         for(int i=1;i<=n;i++){
                                 b[i]=i;a[i]=i;}/* give number*/
                                 for(int i=1;i<=n;i++){
                                 tp =  m%(n+1-i);
                                 if(tp!=0) {for(int j=1;j<=n-i-tp+1;j++){a[j] = b[j+tp];}
                                            for(int j=n-i-tp+2;j<=n-i;j++){a[j]=b[j-(n-i-tp+1)];}}
                                            for(int j=1;j<=n+1-i;j++){b[j]=a[j];}}
                                
                                 return a[1];
                                 }
                                 


main()
{   int n,m;
 while(1){
    scanf("%d %d",&n,&m);
    if(m==0&&n==0){break;}
    printf("%d\n",select(n,m)) ;}
      }
void main()
{
	int f,m,n,i,t=0,a[100];
	do
	{
		f=1;
		scanf("%d %d",&n,&m);
		for(i=2;i<=n;i++)
			f=(f+m-1)%i+1;
		a[t]=f;
		t++;
	}
	while(m!=0||n!=0);
	for(i=0;i<t-1;i++)
		printf("%d\n",a[i]);
}
void main()
{
	int n,m,i,j,k,l;
	int a[400]={0};
	scanf("%d%d",&n,&m);
                  
	while(n>0)
	{i=1;
		j=0;
		k=0;
            for(l=1;l<=n;l++)
			a[l]=0;
        while(k<(n-1))
		{
		j=0;
		while(j<m)
		{
			if(a[i]==0) 
				{
j++;
}

			i++;

			if(i>n)i=1;
		}
		if(i==1) a[n]=1;
		else a[i-1]=1;
		k++;
		}
		for(l=1;l<=n;l++)
			if(a[l]==0)printf("%d\n",l);
	scanf("%d%d",&n,&m);	
	}
}
int main()
{
   int a[300],b[300],n,i,num=0,*p,j,m,l;
   for(i=0;;i++)
   {
	   scanf("%d%d",&a[i],&b[i]);
       num=num+1;
	   if(a[i]==0&&b[i]==0)
		   break;
   }
   num=num-1;
   for(i=0;i<=num-1;i++)
   {
	   int c[300];
	   p=c;
	   for(j=0;j<a[i];j++)
		   *(p+j)=j+1;
	   j=0;m=0;l=0;
	   while(l<a[i]-1)
	   {
		   if(*(p+j)!=0)
			   m++;
		   if(m==b[i])
		   {
			   *(p+j)=0;
			   m=0;
			   l++;
		   }
		   j++;
		   if(j==a[i])
			   j=0;
	   }
	   while(*p==0)
		   p++;
	   printf("%d\n",*p);
   }
   return 0;
}
int main()
{
	int num[1000],i,cnt,jsh=0,m,n;
	scanf("%d %d",&n,&m);
	while(n!=0)
	{
		for(i=0;i<=n-1;i++)
			num[i]=i+1;
		for(i=0;i<=n-1;i++)

		{
			
			cnt=0;
			for(cnt=0;cnt<m;cnt++)
			{
				while(num[jsh]==0)
					jsh=(jsh+1)%n;
				jsh=(jsh+1)%n;
			}
			jsh=jsh-1;
			if(jsh<0)
				jsh=n-1;
			if(i==n-1)
				printf("%d\n",num[jsh]);
			num[jsh]=0;
		}
        scanf("%d %d",&n,&m); 
		jsh=0;
	}
	return 0;
}
int remain(int start[],int n,int m)
{	int k,i,j,d;
	d=n-1;
	for(k=1;k<=(n-1);k++)
	{
		for(i=0,j=0;j<m;)
		{
			++i;
			if(start[(d+i)%n]!=0)
				j++;
		}
		d+=i;
		start[d%n]=0;
	}
	for(i=0;i<n;i++)
	{
		if(start[i]!=0)
			break;
	}
	return(i+1);
}
int main()
{
	int k,n[100],m[100],i,j;
	scanf("%d %d",&n[0],&m[0]);
	for(k=1;n[k-1]!=0&&m[k-1]!=0;k++)
		scanf("%d %d",&n[k],&m[k]);
	int a[100][300];
	for(j=0;j<k-1;j++)
	{
		for(i=0;i<n[j];i++)
			a[j][i]=1;
	}
	for(i=0;i<k-1;i++)
		printf("%d\n",remain(a[i],n[i],m[i]));
	return(0);
}
int main(){
    int i,j,k,f,m[50]={0},n[50]={0},b[300]={0};
    
    for(k=0;k<50;k++){
         scanf("%d%d",&n[k],&m[k]);
         if(m[k]==0)
             break;      //??????????m=0?????
    }   

    for(k=0;n[k]>0;k++){          //????
         f=0;                    //??????f=0
         for(i=0;i<n[k];i++)
             b[i]=0;              //?????b???????????0

         for(i=0;i<n[k];i++){
             for(j=0;j<m[k];j++){
                 f=(f+1)%n[k];//?????m??????????????????
                 while(b[f]==1)
                 f=(f+1)%n[k];//?b[f]??=1?????????
             }   
             b[f]=1;   //?????????1                      
         } 
         if(f==0)
             f=n[k];//??????????1?f??0????????????n
         printf("%d\n",f); //??????????1?f???????? 
    }

    return 0;
}
void ysfwt(int n,int m)
{ 
	int i,s=0;  
	for(i=2;i<=n;i++) s=(s+m)%i; 
    printf("%d\n",s+1);
}
void main()
{
	int j,n,m;
	for(j=0;;j++)
	{scanf("%d %d",&n,&m);
	if(m!=0&&n!=0) ysfwt(n,m);
	else break;}
}
void main()
{
	int a[30],b[30],c[300],d[300],i,n,m,x;
	for(i=0;i<30;i++)
	{
		scanf("%d %d",&a[i],&b[i]);
		if(a[i]==0)
		{
			n=i;
			break;
		}
	}
	for(i=0;i<n;i++)
	{
		for(m=1;m<=a[i];m++)c[m]=m;
		while(a[i]>=1)
		{
			x=(b[i]-1)%a[i]+1;
			for(m=1;m<=a[i];m++)
				d[m]=c[m];
			for(m=1;m<=a[i]-x;m++)
				c[m]=d[m+x];
			for(m=a[i]-x+1;m<=a[i]-1;m++)
				c[m]=d[m-a[i]+x];
			if(a[i]==1)
				printf("%d\n",c[1]);
			a[i]--;
		}
	}
}
int main ()
{
	int n,m,i;
	while(1)
	{
		scanf("%d %d",&n,&m);
		if(n==0) break;
		int a[300];
		for(i=0;i<n;i++)
			a[i]=1;
		int j=0,k,l;
		for(l=1;l<n;l++)
		{
			k=0;
			while(k!=m)
			{
				if(a[j]!=0) k++;
				j++;
				if(j>=n) j=0;
			}
			if(j==0) a[n-1]=0;
			else a[j-1]=0;
		}
		for(j=0;j<n;j++)
		{
			if(a[j]==1) {printf("%d\n",j+1);break;}
		}
	}
}
main()
{
    int s,j,n,tao,m,x=0;
    for(;;)
    {
        int a[305]={0};
        scanf("%d %d",&n,&m);
        if(n==0||m==0) break;
        
        for(s=1,j=0,tao=0;tao!=n-1;s++)
        {
              if(s==n+1) s=1;
              if(a[s]==1) continue;
              j++;
              if(j==m){tao++;j=0;a[s]=1;}
        }
        for(s=1;s<=n;s++)
        {
           if(a[s]==0)
           {printf("%d\n",s);break;}
        }
    }
}
int main(){
	int*p;
	int a,i,j,k,y;
	int n[100],m[100],x[100];
	for(a=0;a<100;a++){
	scanf("%d%d",&n[a],&m[a]);
	if(n[a]==0&&m[a]==0){
		y=a;
		break;
	}
	}
	for(a=0;a<y;a++){
		p=(int*)malloc(sizeof(int[100])*n[a]);
		for(i=0;i<n[a];i++)
			p[i]=0;
		k=n[a];
		j=1;
		for(i=0;i<n[a];i=(i+1)%n[a])
		{
			if(p[i])
				continue;
			if(j++==m[a]){
				p[i]=1;
				k--;
				j=1;
			}
			if(k==0){
				x[a]=i+1;
				break;
			}
		}
	}
	for(int b=0;b<y;b++)
		printf("%d\n",x[b]);
	free(p);
	return 0;
}
main()
{
	int a[300][2],i,j,s[300]={0},k=1,x=0,p,q;
	for(i=0;;i++)
	{
		scanf("%d",&p);
		scanf("%d",&q);
		if(p==0)
		{
			break;
		}
		else
		{
			a[i][0]=p;
			a[i][1]=q;
			x=x+1;
		}
	}
	for(i=0;i<x;i++)
	{
		for(j=2;j<=a[i][0];j++)
			s[i]=(s[i]+a[i][1])%j;
		printf("%d\n",s[i]+1);
	}
}
int a[305];
int main()
{
	int n,m;
	cin>>n>>m;
	while(n!=0&&m!=0){
		memset(a,0,sizeof(a));
		int step=0;
		int flag=0;
		int i=1;
		while(flag<n-1){
			step=0;
			for(;i<=n;i=i%n+1){
				if(a[i]==0)	step++;
				if(step==m){
					a[i]=1;
					break;
				}
			}
			flag++;	
		}
		for(int j=1;j<=n;j++)
			if(a[j]==0){
				cout<<j<<endl;
				break;
			}
		cin>>n>>m;
	}
	return 0;
}
int main()
{
	int a[301],m[10000],n[10000],i,j,k,t=0,s,b=0;
	for(i=0;;i++)
	{
		scanf("%d %d",&n[i],&m[i]);
		if(n[i]==0 && m[i]==0) break;
		t=t+1;
	}
	for(i=0;i<t;i++)
	{
		for(j=1;j<=300;j++)
		    a[j]=1;
		s=0;
		b=n[i];
		while(b>1)
		{
		    k=0;
	        while(k!=m[i])
			{
				s+=1;
			    if(s>n[i]) s-=n[i];
				if(a[s]!=0) k+=1;
	        }
			a[s]=0;
			b-=1;
		}
		for(j=1;j<=n[i];j++)
			if(a[j]!=0) printf("%d\n",j);
	}
	return 0;
}
int main()
{
	int m,n,k,i,b;
	for(i=0;i<20000;i++){
	    scanf("%d%d",&n,&m);	
	    if(m==0&&n==0)
			break;
		else {
	       if(n==1)
			  printf("%d\n",n);
		   else{
		      k=0;
		      for(b=2;b<=n;b++){
			     k=((m%b)+k)%b;
			  }
		      printf("%d\n",k+1);
		   }
		}
	}
	return 0;
}
int judge(int j,int n)   //?judge????????j?n??j?0//
{  
   if(j==n)   j=0;   
   return j;
}
int choose(int n,int m)
{   
	int i,j=0,k;
	int a[301]={0};
	for(k=0;k<n-1;k++)    //??n-1??k?????????//
	{
	  for(i=0;i<m;i++)     //???????m???i????//
	  {   
		for(;a[judge(j,n)+1]!=0;)
                  {
                     j=judge(j,n)+1; 
                  }
                  	j=judge(j,n)+1;  
      } 
	  a[j]=1; 
	}
	for(k=1;a[k]!=0;)
	{k=judge(k,n)+1;}
    return k;
}
	  
int main()
{
   int n[100],m[100],i,num;
   for(i=0;;i++)
   {
    scanf("%d %d",&n[i],&m[i]);
	if (n[i]==0)  break;
   }
   num=i;
   for(i=0;i<num;i++)
   {
	   printf("%d\n",choose(n[i],m[i]));
   }
   return 0;
}
int main(){
    int N,m;
    while(true){
        int count=0,number=0;
        cin>>N>>m;
        if(N==0&&m==0)break;
        const int n=N;
        int a[n+1];
        memset(a,0,sizeof a);
        for(int i=1;;++i){
            if(i>n) i=i-n;
            if(a[i]==1)continue;
            else{
                ++count;
                //cout<<"i="<<i<<" count="<<count<<" a["<<i<<"]="<<a[i]endl;
                
            }
            if(number==n-1){break;}
            if(count==m){
                number++;
                count=0;
                a[i]=1;
                //cout<<"number="<<number<<"a["<<i<<"]="<<a[i]<<endl
                
            }
        }
    for(int i=1;i<=n;++i)
    if(a[i]==0)cout<<i<<endl;
    }
    return 0;
}
int choose(int n,int m)
{
	int a[MAX];
	int i,j,k;
	for(i=1;i<=n;i++)
		a[i]=1;
	a[n+1]=0;
	i=0;j=0;k=0;
	while(k<n)
	{
		i++;
		while(a[i]==0)
		{
			i++;
			if(i>n) i=1;
		}
		j++;
		if(j==m)
		{	
			a[i]=0;
			j=0;k++;	
		}
	}
	return i;
}
void main()
{
	int m,n;
	scanf("%d %d",&n,&m);
	while(m!=0||n!=0)
	{
		printf("%d\n",choose(n,m));
		scanf("%d %d",&n,&m);
	}

}
int main()
{
	int n,m,i,k,j,s,count=0;
	int a[100];
	for(k=0;;k++)
	{
		scanf("%d %d",&n,&m);
		if(n==0)
			break;
		for(s=0,i=2;i<=n;i++)
		{
			s=(s+m)%i;
		}
		a[k]=s+1;
		count++;
	}
	for(j=1;j<=count;j++)
	{
		printf("%d\n",a[j-1]);
	}
}
int main()
{
	int N=1000;
	for (int h=0;h<=N;h++)
{
    int a[300];
    int i,j,t;
    for (i=0;i<300;i++)
    	a[i]=0;
    int n,m;
    scanf ("%d%d",&n,&m);
    i=0;
	t=1;
	j=n;
    for (n;n>1;)
    {
    	if (i<j)
    	{
    		if (a[i]==0)
    		{
    			if (t==m)
    			{
    				a[i]=1;
    				t=1;
    				n--;
    			}
    			else
    			{
    				t++;
    				i++;
    			}
    		}
    		else
    		{
    			i++;
    		}
    	}
    	else
    	{
    		i=0;
    	}
    }
    for (i=0;i<j;i++)
    {
    	if (a[i]==0)
    	{
    		printf ("%d\n",i+1);
    	}
    }
if(n==0&&m==0)
break;
}
}
int kingmonkey(int n,int m);
int main()
{
	int n,m;
	int result;
	for(;;)
	{
		scanf("%d %d",&n,&m);
		if((n==0)&&(m==0))
		{
			break;
		}
		else
		{
			result=kingmonkey(n,m);
			printf("%d\n",result);
		}
	}
	return 0;
}
/*
n??????m????
?????kingmonkey(n,m)=(kingmonkey(n-1,m)+m)%n
?????king(n-1,m)?????????
*/
int kingmonkey(int n,int m)
{
	if(n==1)
	{
		return 1;
	}
	/*??
	if(monkey[n]==0)
	{
		monkey[n]=(kingmonkey(n-1,m)+m+1)%n;
	}
	*/
	else
	{
		int tmp=(kingmonkey(n-1,m)+m);
		if ((tmp%n)==0)
		{
			tmp=n;
		}
		else if(tmp>n)
		{
			tmp=tmp%n;
		}
		return tmp;
	}
}
struct king{int num;struct king *next;};
void p_king(int n,int m)
{
	int i;
	struct king *p1,*p2,*p;
	p1=p=(struct king *)malloc(LEN);
	for(i=0;i<n;i++)
	{
		p1->num=i+1;
		if(i==n-1) p1->next=p;
		else p1->next=(struct king *)malloc(LEN);
		p2=p1,p1=p1->next;
	}
	for(p1=p;n!=1;n--)
	{
		for(i=1;i<(m-1)%n+1;i++)
			p2=p1,p1=p1->next;
		p2->next=p1=p1->next;
	}
	printf("%d\n",p1->num);
}
	
void main()
{
	int i,n[300],m[300];
	scanf("%d%d",&n[0],&m[0]);
	for(i=1;m[i-1]&&n[i-1];i++)
		scanf("%d%d",&n[i],&m[i]);
	for(i=0;m[i]&&n[i];i++)
		p_king(n[i],m[i]);
}
int main()
{
	int m,n,i,x=0;
	do {scanf("%d %d",&n,&m);
	if(m==0&&n==0) break;
	x=1;
	for(i=2;i<=n;i++)
	{
		x=(x+m)%i;
		if(x==0)
			x=x+i;
	}
	printf("%d\n",x);}
	while (0==0);
	return 0;
}
main ()
{
  int m=1,n,i,j,k; 
  int a[301],b[301];
  while(m!=0) 
   {
    scanf("%d%d",&n,&m);
 
    for(i=0;i<301;i++)
       a[i]=1,b[i]=0;
    for(i=1;i<n;i++)
     {
      j=1;
      k=0;
      while(k<m)
       {
         if (a[(j+b[i-1])%n])//???????????? 
              k++;
         if (k==m)
           {
              a[(j+b[i-1])%n]=0;//?m?????????0????????     
              b[i]=(j+b[i-1])%n;//?????????? 
           }
         j++;
       }
     }
   for (i=0;i<n;i++)
     if(a[i])
       {
         if (i==0)
            printf("%d\n",n);
         else
            printf("%d\n",i);
         break;
       }
}  
}
main()
{
      for(int i=0;i<100;i++)
      {
                   int n,m,j,p;
                   scanf("%d%d",&n,&p);
                   int a[1000],b[1000];
                   a[0]=0;
                   b[0]=0;
                   m=p;
                   if(n==0&&m==0)return 0;
                   else
                   {
                       for(j=1;j<=n;j++)
                       a[j]=j;
                       int k;
                       for(k=n;k>0;k--)
                       {               
                                       if(p>k)
                                       m=p%k;
                                       for(j=1;j<k;j++)
                                       {
                                                        if(m+j<=k)
                                                        b[j]=a[m+j];
                                                        else
                                                        b[j]=a[m+j-k];
                                       }
                                       for(int m=1;m<=k-1;m++)
                                       a[m]=b[m];
                       }
                       printf("%d\n",a[1]);
                                          }
      }
}
int f(int m,int n)
    {
        int i;
        int k=0;

        { for(i=2;i<=m;i++)
        k=((n%i)+k)%i;}
        return k;
        }
        int main()
        {
            int m[100],n[100];
            int j,h;
            for(j=1;j<=100;j++)
            {scanf("%d %d",&m[j],&n[j]);
            if(m[j]==0&&n[j]==0)
            break;}
             for(h=1;h<j;h++)
            printf("%d\n",f(m[h],n[h])+1);
            getchar();
            getchar();
            return 0;
            }
int main()
{
	int king(int n, int m);
	int n,m;

	for (;;)
	{
		scanf("%d %d",&n,&m);
		if (n==0&&m==0)
			break;
		printf("%d\n",king(n,m));
	}
	return 0;
}

int king(int n,int m)
{
	int z;
	if (n==1) return 1;
	if (n>1)
	{
		z=(m%n+king(n-1,m))%n;
		if (z==0) z=n;
		return z;
	}
}
int main(){
	int sz[300]={0};
	int n,m,i,k,l,p;
	int king[100];
	for(p=1;p>0;p++){
		scanf("%d%d",&n,&m);
		if(m==0&&n==0)
			break;
		for(i=0;i<n;i++)
			sz[i]=1;
		i=-1;
		for(k=0;k<n;k++){
			l=0;
			while(l<m){
				if(i==n-1)
					i=0;
				else
					i++;
				if(sz[i]==1)
					l++;
				}
			sz[i]=0;
		}
		king[p]=i+1;
	}
	int x;
	for(x=1;x<p;x++)
		printf("%d\n",king[x]);
	return 0;
}
int yue(int n,int m)
{
	int b[100000]={0};
	
	int k,s=0,t,p;
	t=n;
	for(k=0,p=m;;k=(k+1)%n)
	{
		if(b[k]==0)
		{
			s++;
			if(s==p)
			{
				s=0;
				b[k]=1;
				t--;
				if(t!=0){
				if(m%t!=0)
				p=m%t;
				else
				p=t;
				}
				if(t==0)
				{	
				return (k+1);	 
				}
			}
		}
	}
}
int main()
{
	int i,l,x,y,t,a[100];
	for(i=0;;i++){
		scanf("%d %d",&x,&y);
		if(x==0&&y==0)
		break;
		else
	   a[i]=yue(x,y);
	}  
	for(t=i,i=0;i<t;i++)
	printf("%d\n",a[i]);
}
void main()
{
	int m,n;
	int *p;
	int i,j,k;
	for(;;)
	{
		scanf("%d%d",&n,&m);
		if(m==0)break;
		else if(n==1) printf("1\n");
	
		else
		{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)
		{
			k--;
			p[i]=1;
			j=1;
		}
	
		if(k==0)
		{
			printf("%d\n",i+1);break;
		}
		
	}free(p);
	}
	}
return;
}
int joseph(int x,int y)  
{
	int a[400]={1};
	int i=0,j=0,p=0,n=0,z;
	for(i=0;i<x;i++)
		a[i]=0;       
	for(j=0;;j++)
	{
		i=j%x;        
		if(a[i]==0)
		{
			p++;
		}
		if(p%y==0)
		{
			if(a[i]==0)n++;         
			a[i]=1;
		}
		if(n==x-1)
		{
			for(i=0;i<x;i++)
			{
				if(a[i]==0)
					z=i+1;   
			}
			break;     
		}
	}
	return z;
}
main()
{
	int m[100],n[100];     
  	int b,c=0,d=0;
	for(b=0; ;b++)
	{
		scanf("%d %d",&m[b],&n[b]);
		c++;
		if(m[b]==0&&n[b]==0)
			break;
	}
	for(b=0;b<c-1;b++)
	{
		if(m[b]==0)
			printf("1\n");
		else
		{
			d=joseph(m[b],n[b]);
			printf("%d\n",d);
		}
	}
	
}
int main () {
	int n, m;
	while (true) {
		scanf("%d%d", &n, &m);
		if (n == 0 && m == 0) {
			break;
		}
		int count = n;
		int currentIndex = 0;
		int flags[302];
		for (int i = 0; i < n; i++) {
			flags[i] = i + 1;
		}
		while (count != 1) {
			int currentNumber = 1;
			while (currentNumber != m) {
				currentNumber++;
				currentIndex = (currentIndex + 1) % count;
			}
			
			// delete the element in currentIndex.
			for (int i = currentIndex + 1; i < count; i++) {
				flags[i - 1] = flags[i];
			}
			count--;
		}
		printf("%d\n", flags[0]);
	}
}
void joseph(int n,int m)
{
	int i,a[301],k=0,num=0;
	for(i=1;i<=n;i++) a[i]=1;
	i=1;
	while(num<n-1)
	{
		if(a[i]==1)
		{
			k++;
			if(k==m)
			{a[i]=0;num++;k=0;}
		}
	    if(i==n) i=1;
		else i++;
	}
	for(i=1;i<=n;i++)
		if(a[i]==1) printf("%d\n",i);
}
void main()
{
	int n[20],m[20],i,num;
	for(i=0;i<20;i++)
	{n[i]=1;m[i]=1;}
	i=-1;
	while(m[i]!=0&&n[i]!=0)
	{
		i++;
		scanf("%d%d",&n[i],&m[i]);
	
	}
	num=i;
	for(i=0;i<num;i++) joseph(n[i],m[i]);
	
}
/*??????????????????????n????m??????????m%(n-1)????0----n-1????????????m%(n-1)+1??????????????????????n-1????m???????n-1??????s??????????n??????(s+m)%i??????,?????????????????????*/

int main() {
    int n, m, i, s, j, counter=0;
    int answer[200];
    for (;;){
        scanf("%d%d", &n, &m);     
        if (n==0&&m==0) break;
        s=0;
        for (i=2; i<=n; i++) {     
           s=(s+m)%i;
        }
            answer[counter]=s+1;
            counter+=1;
    }
    for (j=0;j<counter;j++) {
        printf ("%d\n", answer[j]);
    }
    return 0;
}
int main(){
	int n[300],m[300],i,j,k[300];
	int x,y;
	int monkey[300][300];
	for(y=0;;y++){
	   scanf("%d%d",&n[y],&m[y]);
	   if(n[y]== 0&& m[y]==0){
		   break;
	   }
	   
	}

	for(x=0;x<y;x++){
		k[x]=0;
	   for(i=0;i<n[x];i++){
		   monkey[x][i]=i+1;
	   }
	   for(i=0,j=1;k[x]!=n[x]-1;){
		  if(j==m[x]){
			  monkey[x][i]=0;
			  k[x]++;
			  j=0;
		  }
		  if(i!=n[x]-1){
			  i++;
		  }else{
			  i=0;
		  }
		  if(j!=m[x]&&monkey[x][i]!=0){
			  j++;
		  }
	   }
	   for(i=0;i<n[x];i++){
		  if(monkey[x][i]!=0){
			  printf("%d\n",monkey[x][i]);
		  }
	   }
                                      
	}
   return 0;
}
int main()
{
 int a[100],b[100],num=0,i,j,n=0;
 for(i=0;i<100;i++){
 scanf("%d %d",&a[i],&b[i]);
 if(a[i]==0&&b[i]==0){
  n=i;
 break;
 }
 }
 for(i=0;i<n;i++){
  for(j=2;j<=a[i];j++){
  num=(num+b[i])%j;
  }
  if(i<n-1){
  printf("%d\n",num+1);
  }
  if(i==n-1){
  printf("%d",num+1);
  }
  num=0;
 }
 return 0;
}