semeru/Code-Code-CloneDetection-POJ104 huggingface dataset:
int main(){
    int a[1000],n[1000],m[1000],i,j,k,l;
    for(i=0;i<998;i++){
        scanf("%d%d",&n[i],&m[i]);
        if(n[i]==0){
            break;
        }
    }
    for(l=0;l<998;l++){
        if(n[l]==0){
            break;
        }
        for(i=1;i<=n[l];i++){
            a[i]=1;
        }
        j=0;
        for(i=1;i<=n[l]-1;i++){
            k=0;
            while(k<m[l]){
                j++;
                if(j>n[l]){
                    j=0;
                }
                if(a[j]==1){
                    k++;
                }
            }
            a[j]=0;
        }
        for(i=1;i<=n[l];i++){
            if(a[i]==1){
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}
void main()
{
	int n, m;
	int i, j, k;
	int num;
	int a[500];

	scanf("%d%d", &n, &m);
	while((n > 0) && (m > 0))
	{
		for(i = 1; i <= n; i++)
			a[i] = 0;
		num = 0;
		for(i = 1; i < n; i++)
		{
			for(j = 1; j <= m; j++)
			{
				num = num % n + 1;
				while(a[num] == 1)
					num = num % n + 1;
			}
			a[num] = 1;
		}
		for(i = 1; i <= n; i++)
			if(a[i] == 0)
				printf("%d\n", i);
		scanf("%d%d", &n, &m);
	}
}
int main()
{
   int i,j,k,n,c,d,m;
   int a[100][2],b[300][2];
   i=0;
   j=0;
   k=0;
   n=0;
   d=0;
   c=0;
   m=1;
   do {
       scanf("%d%d",&a[i][0],&a[i][1]);   
       i++;
       }   
   while (a[i-1][0]!=0&&a[i-1][1]!=0);
   
    for(j=0;j<i-1;j++)
    {
       for(k=0;k<a[j][0];k++)
       {
        b[k][0]=k+1;
        b[k][1]=1;
        }
        while(m<=a[j][1])
        {  
           if(b[n][1]!=0)
           {  
           if(m!=a[j][1])
           b[n][1]=1;
           else
           {
             b[n][1]=0;
             m=0;
             c++;
             }
             m++;
           }
           else;
           
           if(n<a[j][0]-1)
             n++;
             else
             n=0;
           
             if(c==a[j][0]-1)
             {
             for(d=0;d<a[j][0];d++)
             {
                if(b[d][1]==1)
                printf("%d\n",b[d][0]);
                else;
             }
             break;
             }
             else;    
        }
        m=1;  
        n=0;
        c=0;
        
            
    }   
    
    
    
    getchar();
    getchar();
    
}
int f(int m,int n){
    int i;
    int k=0;
    for(i=2;i<=n;i++)
    k=((m%i)+k)%i;
    return k;
}
int main(){
    int m,n;
    m=1;
    n=1;
    while((m>0)&&(n>0)){
    scanf("%d%d",&m,&n);
    if((m>0)&&(n>0)){
        printf("%d\n",f(n,m)+1);
    }
    }
    return 0;
}
void main()
{
	int n[100],m[100],p=1,i,j,k=0,s,t;
	int a[300];
	do
	{
		scanf("%d%d",&n[p],&m[p]);
		p++;
	}while(n[p-1]!=0&&m[p-1]!=0);
        for(i=1;i<p-1;i++)
	{
		j=0;
		for(t=1;t<=n[i];t++)
		     a[t]=1;
                s=n[i];
		for(j++;j<=n[i];j++)
		{
			if(s==1)break;
			k=a[j]+k;
			if(k==m[i])
			{
				s=s-1;
				k=0;
				a[j]=0;
				if(j==n[i])
		  		   j=0;
                                if(s>1)
				   continue;
				else
				   break;  
			}
			if(j==n[i])
			   j=0;
		}
		for(j=1;a[j]==0;)
			j++;
		printf("%d\n",j);
	}
}
struct moncky
{
	int seq;
	struct moncky *next;
};
struct monckycircle
{
	int allnumber;
	struct moncky *curp;
};
struct monckycircle *creat(int);
void cry_123(struct monckycircle *,int);

int main()
{
	int n_all,n_unlucky;
	struct monckycircle *circlename;
	scanf("%d%d",&n_all,&n_unlucky);
	while(n_all)
	{
		if(n_unlucky==1)
			printf("%d\n",n_all);
		else
		{
			circlename=creat(n_all);	
			while(circlename->allnumber!=0)
				cry_123(circlename,n_unlucky);
			printf("%d\n",circlename->curp->seq);
			
		}
		scanf("%d%d",&n_all,&n_unlucky);
	}
	return 0;
}
struct monckycircle *creat(int allnum)
{
	int i=1;
	struct monckycircle *p;
	struct moncky *head,*now;
	p=(struct monckycircle *)malloc(sizeof(struct monckycircle));
	now=head=(struct moncky *)malloc(sizeof(struct moncky));
	p->allnumber=allnum;
	p->curp=head;
	while(i<allnum)
	{
		now->seq=i;
		now->next=(struct moncky *)malloc(sizeof(struct moncky));
		now=now->next;
		i++;
	}
	now->seq=i;
	now->next=head;
	return p;
}
void cry_123(struct monckycircle *circlename,int unlucky)
{
	int i=1;
	struct moncky *p,*aux;
	p=circlename->curp;
	for(i=1;i!=unlucky;i++)
		p=(i==1?p:p->next);
	aux=p->next->next;
	
	p->next=aux;
	circlename->allnumber--;
	circlename->curp=p->next;
}
int main()
{int a[300],m[20],n[20],i,j,k,p,q,b,c,x[300],y,e;
for(k=0;;k++)
	{scanf("%d %d",&n[k],&m[k]);
if(m[k]==0)break;}
for(i=0;i<k;i++)
{ j=0;for(j=0;j<n[i];j++)
{a[j]=j+1;}
for(;n[i]>1;n[i]--)
{p=m[i]%n[i];
c=n[i]-p;
for(y=0,b=0;b<p-1;b++,y++)
{x[y]=a[b];}

for(e=0;p<n[i];e++,p++)
{a[e]=a[p];}
for(q=0;c<n[i]-1;c++,q++)
{a[c]=x[q];}}


printf("%d\n",a[0]);}}
int main(){
    int bianhao[300];
    int tuichu[300];
    int i,j,k,p,m,n,x,y;
    scanf("%d %d",&n,&m);
    bianhao[0]=n;
    tuichu[0]=m;
    i=1; 
    while(n!=0||m!=0){
      scanf("%d %d",&n,&m);
      bianhao[i]=n;
      tuichu[i]=m;
      i++;                  
    }
    for(j=0;j<i-1;j++){
      int yuanxulie[300];
      int xinxulie[300];
      for(k=0;k<bianhao[j];k++){
      yuanxulie[k]=k+1;                    
      }
      x=bianhao[j];
      for(k=0;k<bianhao[j]-1;k++){
         p=tuichu[j]%x;
         for(y=0;y<x-1;y++){
           if (y<x-p){xinxulie[y]=yuanxulie[p+y];}
           if (y>x-p-1){xinxulie[y]=yuanxulie[y+p-x];}
         }
         for(y=0;y<x-1;y++){
         yuanxulie[y]=xinxulie[y];                  
         }
         x=x-1;
      }
      printf("%d\n",yuanxulie[0]);              
    }
     return 0;      
}
int main()
{
	int n,m,i,j;
	int houwang(int n,int m);
	int y[100];
	for(i=0;i<100;i++)
	{
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)
			break;
		else 
		    y[i]=houwang(n,m);	
	}
	for(j=0;j<i;j++)
		printf("%d\n",y[j]);
	return 0;
}

int houwang(int n,int m)
{
	int i,j=0,k=0;
	int x[301];
	for(i=0;i<301;i++)
		x[i]=1;
	for(i=0;;i++)
	{
		if(x[i]==1)
		{
			j++;
	     	if(j%m==0)
			{
			    x[i]=0;
			    k++;
			}
		}
		if(k==n-1)
			break;
		if(i==n-1)
			i=-1;
	}
	for(i=0;i<n;i++)
		if(x[i]==1)
			break;
	return(i+1);
}
main()
{
	int i=1,j,tot,rem,cur,rou,n[100],m[100],a[301];
	do 
	{
		scanf("%d %d",&n[i],&m[i]);
		i++;
	}
	while (n[i-1]!=0);
	tot=i-2;
	for (i=1;i<=tot;i++)
	{
		rem=n[i];
		cur=0;
		for (j=1;j<=n[i];j++)
			a[j]=1;
		while (rem>1)
		{
			rou=0;
			while (rou<m[i])
			{
				rou++;
				do
				{
					cur++;
					if (cur>n[i]) cur-=n[i];
				}
				while (a[cur]==0);
			}
			a[cur]=0;
			rem--;
		}
		for (j=1;j<=n[i];j++)
		if (a[j]==1) 
		{
			printf("%d\n",j);
			break;
		}
	}
	return 0;
}
int main(){
	int a[max],n,m,i,x,y;
	while(1){
		scanf("%d%d",&n,&m);
		if (n==0)
			break;
		for (i=0;i<n;i++)
			a[i]=i+1;
		x=0;
		for (i=0;i<n;i++){
			y=0;
			while(y<m){
				while(a[x]==0)
					x=(x+1)%n;
				y++;
				x=(x+1)%n;
			}
			x--;
			if(x<0)
				x=n-1;
			if (i==n-1)
				printf("%d\n",a[x]);
			a[x]=0;
		}
	}
	return 0;
}
int main()
{
	int a[301]={0};
	int i,n,m;
	while(cin >>n >>m)
	{
		int t=1,count=0,x=0;
		if (m==0&&n==0) continue;
	for(i=1;i<=n;i++)
	   a[i]=i;
	while(count<=n-1)
	{
	for(i=1;i<=n;i++)
	{
		if(a[i]!=0)
		{
			if(t%m==0)
			{a[i]=0;
			x=i;
			count++;}
			t++;
		}
	}
	}
	cout <<x<<endl;
	}
	return 0;
}
int main()
{
int n,a[300],m,t,s,h;
scanf("%d %d",&n,&m);
while(m!=0&&n!=0)
{
	s=1;
	h=0;
for(t=0;t<n;t++)
a[t]=t+1;
t=0;
while(h<n-1)
{
	if(a[t])
	{
	if(s==m)a[t]=0,s=0,h++;
	s++;
	}
	t++;
	if(t==n)t=0;
}
t=0;
while(a[t]==0)t++;
printf("%d\n",a[t]);	
scanf("%d %d",&n,&m);
}
return 0;
}
int main() 
{ 
	int n, m, i, s=0;
    while(1)
	{
		s=0;
	scanf("%d%d", &n, &m);
    if(n==0)
		break;
	if(n==1)
		printf("1\n");
	else 
	{
		for (i=2; i<=n; i++)
		s=(s+m)%i;    
		printf ("%d\n", s+1);
	}
	
	
	}
	return 0;
}
main()
{
    int a[300];
    int b[20];
    int c[20];
    int i,j,k,l,n,m,x;
    int s,t;
    int p,q;
    int count=0;
    s=0;
    t=0;
    for(i=0;;i++)
    {
        scanf("%d %d",&n,&m);
        b[s]=n;
        c[s]=m;
        if((n==0)&&(m==0))
        {
            break;
        }
        s++;
    }
    for(j=0;j<=s-1;j++)
    {
        p=b[j];
        q=c[j];
        if(p==1)
        {
            x=1;
            printf("%d\n",x);
        }
        if(p>1)
        {
        for(k=0;k<=p-1;k++)
        {
            a[k]=k+1;
        }
        for(l=0;;l++)
        {
            t=(q+t-1)%(p-count);
            for(i=t;i<=p-count-2;i++)
            {
                a[i]=a[i+1];
            }
            count++;
            if(count==p-1)
            {
                break;
            }
        }
        printf("%d\n",a[0]);
        t=0;
        count=0;
    }
    }
}
int main()
{
	int a[300];
	int i,j,k,m,n;
	int nbegin,ncounted;
	while(m!=0&&n!=0)
	{
		scanf("%d%d",&n,&m);
		for(i=0;i<n;i++)
		{
			a[i]=1;
		}
		nbegin=0;
		for(i=0;i<n;i++)
		{
			ncounted=0;
			for(j=nbegin;ncounted<m;j=(j+1)%n)
			{
				if(a[j]==1)ncounted++;
				if(ncounted==m)
				{
					a[j]=0;
					nbegin=(j+1)%n;
				}
			}
			if(i==n-1)
			{
				nbegin=(nbegin+n-1)%n;
				printf("%d\n",nbegin+1);
			}
		}
	}
	return 0;
}
void main()
{
	int m,n;
	int king(int m,int n);
	while(n!=0&&m!=0)
	{
		scanf("%d %d",&n,&m);
		if(n!=0&&m!=0)
		printf("%d\n",king(m,n)+1);
	}
}

int king(int m,int n)
{
	int i;
	int k=0;
	for(i=2;i<=n;i++)
	k=((m%i)+k)%i;
	return k;
}
int n,m;
struct mon
{
	int i;
	struct mon *next;
};
struct mon *creat(int n)
{
	struct mon *head,*p1,*p2;
	int t;
	p1=p2=(struct mon *)malloc(sizeof(struct mon));
	head=p1;
	for(t=1;t<n;t++)
	{
		p1->i=t;
		p1=(struct mon *)malloc(sizeof(struct mon));
		p2->next=p1;
		p2=p1;
	}
	p1->i=t;
	p1->next=head;
	return head;
}
int find(struct mon *head,int m)
{
	struct mon *p1=head,*p2=head;
	int i=1;
	for(p1=p2=head;;i++)
	{
		if(m==1)
			return n;
		if(i%m!=0)
		{
			p2=p1;
			p1=p1->next;
		}
		else if(i%m==0)
		{
			p2->next=p1->next;
			p1=p1->next;
		}
		if(p1==p1->next)
			return p1->i;
	}
}
void main()
{
	struct mon *head;
	scanf("%d%d",&n,&m);
	while(n!=0&&m!=0)
	{
		head=creat(n);
		printf("%d\n",find(head,m));
		scanf("%d%d",&n,&m);
	}
}
int main()
{
int n[100],m[100],i,j,s[100];
for(i=1;n[i-1]!=0,m[i-1]!=0;i++){
  scanf("%d %d", &(n[i]),&(m[i]));
}
for(i=1;n[i-1]!=0,m[i-1]!=0;i++){
	s[i]=0;
	for(j=2;j<=n[i];j++) {
		s[i]=(s[i]+m[i])%j;
	}
	if(n[i]!=0,m[i]!=0){
    printf("%d\n", s[i]+1);
	}
}
return 0;
}
int main()
{
    int n,m;
    int i,k=1,p=0,a[1000];
    for(;;)
    {    
        scanf("%d %d",&n,&m);
        if(m==0&&n==0) break;
        memset( a , 0 , sizeof(a));
        k=1,p=0;
        for(i=1;;i++)
        {
            if(i>n) i=i%n;
            if(p==n-1) break;
            if(a[i]==1) continue;
            
            if(k==m)
            {
                a[i]=1;
                p++;    
                k=1;
            }
            else k++;
            //printf("%d %d %d\n",a[1],a[2],a[3]);
        }
        for(i=1;i<=n;i++)
        {
           if(a[i]==0)
            {
                printf("%d\n",i);
                
                break;
            }
        }
    }
    return 0;
}
int main()
{
	int a[300]={0},n[300],m[300],i,j,b,k=0,x=0,y=0;
	for(i=0;;i++)
	{
		k=k+1;
		scanf("%d%d",&n[i],&m[i]);
		if(n[i]==0&&m[i]==0)break;
	}
	for(b=0,x=0,y=0;;b++)
	{
		x=0;
		for(i=0;;i++)
		{
			if(a[i]==0)	
			{
				x=x+1;
				if(x==m[b]){a[i]=1;x=0;}
			}
			if(i==n[b]-1)i=-1;
			for(j=0,y=0;j<n[b];j++)
			{
				y=y+a[j];
			}
			if(y==n[b]-1)break;
		
		
		}
	
		for(i=0;i<n[b];i++)
		{
			if(a[i]==0)
			{
				printf("%d\n",i+1);
				break;
			}
		}
		for(i=0;i<300;i++)a[i]=0;
	
		if(n[b+1]==0&&m[b+1]==0)break;
	}
	return 0;
}
int main()
{
	int a[300],b,c=0,d,e,f,g,h,i,j,p=1,m,k,l,n,s=0;
	for(;;)
	{scanf("%d %d",&n,&m);
	c=0;
	p=1;
    s=0;
	if(m==0 && n==0) break;
	for(i=0;i<=n-1;i++)	{a[i]=i+1;}
	for(i=n;i<=299;i++)	{a[i]=0;}
	for(;;)
	{	if((p+m)%n==1 || (p+m)%n==0 ) {p=(p+m)%n+n-1;}
	    else {p=(p+m)%n-1;}
	   	if(p==0) break;
		for(j=p-1;j<=n-1;j++)
		{a[j]=a[j+1];}
		n=n-1;
	}
	printf("%d\n",a[0]);
	}
	return 0;
}
main()
{
    int a[300]={0},b[300]={0};
    int i,j;
    int n=1,m=1;
    while ((n!=0)&&(m!=0))
    {
    scanf("%d %d",&n,&m);
    if ((n!=0)&&(m!=0)){
    for(i=0;i<=n-1;i++)
    {
       a[i]=i+1;

    }
    for(j=n;j>=1;j--)
    {
      for(i=0;i<=j-2;i++)
      {
        b[i]=a[(m%j+i)%j];

      }
      for(i=0;i<=n-2;i++)
      {
        a[i]=b[i];
      }


    }
    printf("%d\n",a[0]);
    }
    }
}
int main()
{
    int m[30000],n[300],i,k,s[3000],j;
    i=0;
    do
    {
        i++;
        scanf("%d %d",&n[i],&m[i]);
        
    }
    while(n[i]!=0&&m[i]!=0);
    for(k=1;k<i;k++)   s[k]=0;
    for(k=1;k<i;k++)
    {
        for(j=2;j<=n[k];j++)  s[k]=(s[k]+m[k])%j;
        
    }
    for(k=1;k<i;k++)  printf("%d\n",s[k]+1);
    return 0;
}
int main(){
  int n,m,count,number,del,i,j;
  int monkeys[302],result[100];
  scanf("%d%d",&n,&m);
  j=0;
  while(m>0&&n>0){
    j++;
	for(i=1;i<=n;i++){
      monkeys[i]=1;
    }  
    number=0;
    count=0;
    del=0;
    while(del<n-1){
      number++;
      if(number>n){
        number=1;
      }
      if(monkeys[number]==1){
        count++;
      }
      if(count==m){
        monkeys[number]=0;
        del++;
        count=0;
      }
    }
    number=1;
    while(1){
      if(monkeys[number]==1){
        break;
      }
      number++;
    }
    result[j]=number;
  scanf("%d%d",&n,&m);
  }
  for(i=1;i<=j;i++){
    printf("%d\n",result[i]);
  }
  return 0;
}
static struct str 
{
  int num;
  struct str *next;
}s1[301],s2[301],*p1,*p2;//?0?8?0?5?0?8?0?2?0?7?0?3?0?3?0?7?0?2?0?9301  ?0?5?0?6?0?6?0?2  ?0?3·?0?4?0?2?0?9?0?7±í

int chq(int n,int k)
	{
	  int i,j;
	  if (n==1)
	  {
	     return 1;
	  }
	  if (k==1)
	  {
	     return n;
	  }
	  for (i=1;i<=n-1;i++)
	  {
	     for(j=1;j<=k;j++)
			 {
			   p2=p1;p1=p1->next;
			 }		
		 p2->next=p1->next;
		 p1=p2;
	  }
	  return (p1->num);

	}
int main()
	{
	int i,j,n,m,e;
	  for (i=0;i<=300;i++)
	  {
	    s1[i].num=i;
		s1[i].next=&s2[i+1];
	  }
	  while (1)
	  {
	    scanf("%d%d",&n,&m);
	  if (n==0&&m==0)
	  {
	     break;
	  }
	  for (j=0;j<=n;j++)
		  {
		     s2[j]=s1[j];
		  }
		  s2[n].next=&s2[1];
		  p1=&s2[0];
		  e=chq(n,m);
		  printf("%d\n",e);
		   
		
	  }
	  
	  
	   return 0;
	}
int main()
{
	int m,n,i,j,k,num,s[300],b[300],q,w;
	for(i=1;;i++)
	{
    scanf("%d%d",&s[i],&b[i]);
	if(s[i]==0){w=i-1;break;}
    }
	int x[w];

  for(q=1;q<=w;q++)
  {
	int a[s[q]+1];
	for(i=1;i<=s[q];i++)
	{
		a[i]=1;
    }
	j=0;
	k=0;
for(i=1;i<=s[q];i++)
{
	if(a[i]==1)
	{
		j++;
		if(j==b[q])
		{
			a[i]=0;
			j=0;
			k++;
        }
		if(k==s[q])
		{
			x[q]=i;
			break;
        }
    }
	if(i==s[q])
	i=0;
}

  }
  for(q=1;q<=w;q++)
  {printf("%d\n",x[q]);
}

}
int  main()

{

int n, m, i, s=0,a=1;
while(1)
{

cin>>n>>m;
if((n+m)==0)
    break;
if(n>=2)

{for (i=2; i<=n; i++)
    s=(s+m)%i;


cout<<s+1<<endl;
s=0;
}
if (n==1)
cout<<a<<endl;


}
return 0;
}
int main()
{
	int m,n,i=0,num=0,p=1,flag[301]={0};
	for(;;)
	{
		scanf("%d %d",&n,&m);
		if(m==0 && n==0)
			return 0;
		for(;;)
		{
			i++;
			if(p>n)
				p=1;
			if(flag[p]==1)
			{
				i--;
				p++;
				continue;
			}
			if(i==m)
			{
				flag[p]=1;
				i=0;
				num++;
			}
			p++;
			if(num==n-1)
			{
				for(i=1;i<=n;i++)
				{
					if(flag[i]==0)
					{
						printf("%d\n",i);
						break;
					}
				}
				break;
			}
		}
		num=0;
		for(i=0;i<301;i++)
			flag[i]=0;
		i=0;
		p=1;
	}
}
int function(int n,int m)
{
	int a[1000];
	int i,j,k,s;
    for(i=0;i<n;i++)
	{
		a[i]=i+1;
	}
	i=-1;
	for(s=n;s>1;s--)
	{
		i=(i+m) % s;
		k=a[i];
		for(j=i+1;j<n;j++)a[j-1]=a[j];
		{
			a[n-1]=k;
		}
		i--;
	}

	return a[0];
}
void main()
{
	int n,m,l,i,king[100];
	l=0;
	while(1)
	{
		scanf("%d %d",&n,&m);		
		if(m==0 && n==0)
		{
			break;
		}
		king[l++]=function(n,m);
	}
	for(i=0;i<l;i++)
	{
		printf("%d\n",king[i]);
	}
}
void main()
{
     int g(int m,int s,int t);        /*????*/
     void f(int a[],int k,int t);         /*????*/
     int a[301]={0},b[301]={0},c[100][2]={0},m,n,t,k,j,i=0;  /*???a???????k????????*/
     do
     {
         scanf("%d %d",&c[i][0],&c[i][1]);         /*????????????c[100][2]?*/
         i++;
     }while(c[i-1][0]!=0);       /*?????????*/
     k=i-1;                           /*k?????????*/
     for(j=1;j<=k;j++)
     {
         n=c[j-1][0];m=c[j-1][1];
         for(i=0;i<300;i++)
         {a[i]=0;b[i]=0;}            /*????a?b??*/
         for(i=0;i<=n-1;i++)
         {
              a[i]=i+1;              /*???a????????????*/
         }
         t=n;i=0;                    /*?t?????????*/
         while(a[1]!=0)                   /*?a[1]==0???????????a[0]???*/
         {
              f(a,g(m,b[i],t),t);
              i++;
              if(g(m,b[i-1],t)==(t-1)/*??????????????*/)                           
                   b[i]=0;/*????a[0]????????*/
              else
                   b[i]=g(m,b[i-1],t);
              t--;
         }
         if(j<k)
              printf("%d\n",a[0]);
         else
              printf("%d",a[0]);
     }
}
 
int g(int m,int s,int t)         /*??g??????m??a[s]???????t???????????c???????????????*/
{
     int c;
     if((s+m)%t==0)
         c=t-1;
     else
         c=(s+m)%t-1;
     return c;
}
 
void f(int a[],int k,int t)      /*??f??????m???????a[k])?????a[c]=a[c+1];a[c+1]=a[c+2];a[c+2]=a[c+3];??  a[t-1]=a[t]=0?t??????k???????????*/
{
     int i;
     for(i=k;i<=t;i++)
     {
         a[i]=a[i+1];
     }
}
/**
 * ???5.cpp
 * ????????
 * ??????
 * ???2010?11?14?
 */
int main()
{
    int n,m,i,a[301];    //???a???????
	while(cin>>n>>m && n!=0 && m!=0)  
	{
		int k=0,count=n;  //?k?????????count???????
		for(i=1;i<=n;i++) //????????1,??????
		{a[i]=1;}
		i=1;              //?????i=1??
		while(count!=1)   //?count==1??
		{
			if(a[i]==1)   //????????????1
			{
				k++;
				if(k%m==0)  //??????m?????????????0????-1
				{a[i]=0;count--;}
			}

            if(i==n)    //?????????????????
			{i=1;}
			else        //??????????????
			{i++;} 
			
		}
		for(i=1;i<=n;i++) //??????????????????????
		{
			if(a[i]==1)
				cout<<i<<endl;
		}
	}
	return 0;
}
int main(){
 int i,j,num,king;
 int n[100],m[100];
 int rema(int x,int y);
 num=0;
 for (i=0;;i++){
     scanf("%d %d",&n[i],&m[i]);
     num++;
     if (n[i]==0&&m[i]==0){
              break;
     }
 }
 for (j=0;j<num-1;j++){  
      king=rema(n[j],m[j]) ;         
      printf("%d\n",king);
 }   
return 0;    
}

int rema(int x,int y){                 
    int shu[300],dele,rem;
    for (int t=0;t<x;t++){
        shu[t]=t+1;         
        }        
     if (x==1){
                     return 1;
                     }         
    rem=x;                 
    if(y%rem==0){           
                 dele=rem;
     }
    else {
                 dele=y%rem;     
    }  
    for (int a=0;a<x;a++){  
                  if (shu[a]!=0){     
                                 dele--; 
                   }              
                  if(dele==0){
                                 shu[a]=0;
                                 rem--;         
                               if(y%rem==0){
                                            dele=rem;
                                }
                               else {
                                            dele=y%rem;
                                }  
                   }
                  if(a==x-1){
                                 a=-1;
                   }    
                  if (rem==1){
                             break;
                  }
    }
    for (int k=0;k<x;k++){
        if (shu[k]!=0){
             return shu[k];
        }
    }
}
int f(int a[300],int n,int m)
{int l,i,k,sum,j=1;
for(i=0;i<n;i++)
  a[i]=1;
  for(k=1;k<n;k++)
  {for(sum=0;sum<m;j++)
  {i=(j-1)%n;
    sum=sum+a[i];
	 }
    a[i]=0;
  }
  for(l=0;l<n;l++)
	  if(a[l])
		return(l+1) ;
  
}
  void main()
  {
	  int p,k=0,n,m,a[300],b[100];
    scanf("%d %d",&n,&m);
	while(n!=0)
	{b[k]=f(a,n,m);
	scanf("%d %d",&n,&m);
	k++;
	}
 for(p=0;p<k;p++)
	printf("%d\n",b[p]);
  
  }
int f(int m,int n){
	int i;
	int k=0;
	for(i=2;i<=n;i++){
		k=((m%i)+k)%i;
	}
	return k;
}
int main(){
	int m,n,i,c,N[100],M[100],r[100],stop;
	c=0;
	for(i=0;;i++){
		scanf("%d %d",&n,&m);
		if(m==0&&n==0){
			break;
		}
		c++;
		N[i]=n;
		M[i]=m;
		r[i]=f(M[i],N[i])+1;
	}
	for(i=0;i<c;i++){
		printf("%d\n",r[i]);
	}
	scanf("%d",&stop);
	return 0;
}
void main()
{
    int effect(int n,int m);
    int b[20],c[20],j=0,k;
    for (k=1;k<=20;k++){
    scanf("%d %d",&b[k],&c[k]);
    if (b[k]!=0 || c[k]!=0)
	        j++;
    else break;}
	for (k=1;k<=j;k++)
		printf("%d\n",effect(b[k],c[k]));
}

int effect(int n,int m)
{
    int i,s=0;
    for (i=2; i<=n; i++) 
		s=(s+m)%i;
    return(s+1);        
}
void f(int n,int m)
{
	int i,k=0,t=0,a[300],*p;for(i=0;i<300;i++) a[i]=i+1;
    p=a;	
	i=0;
	while(t<n-1)
	{
		if(*(p+i)!=0) k++;
		if(k==m) {*(p+i)=0;t++;k=0;}
		i++;
		if(i==n) i=0;
	}
	while(*p==0) p++;
	printf("%d\n",*p);k=0;t=0;
}
void main()
{
	int n,m;
	
	for(;;)
	{
	scanf("%d%d",&n,&m);if(n==0)break;
	f(n,m);
	}
}
void main()
{int a,b,i,j;
 while(1)
 {
	 scanf("%d %d",&a,&b);
     if(a==0&&b==0)break;
 
	 int f[301]={0},n;
 
	 n=a;
 
	 for(j=1;j<=n;j++)
 
	 {f[j]=(f[j-1]+b)%j;
     if(f[j]==0)
      f[j]=j;}
 printf("%d\n",f[n]);
}
}
int main()
{
 int n[10000],m[10000],i,x,y,j,k,p,a[300];
 scanf("%d %d",&n[0],&m[0]);
 for(x=1;n[x-1]+m[x-1]>0;x++)
  scanf("%d %d",&n[x],&m[x]); 
 for(y=0;y<x-1;y++)
 {
 
 
 for(i=1;i<=n[y];i++)
  a[i]=i;
 j=1;
 for(k=n[y];k>1;k--)
 {
  p=m[y]%k;
  j=j+p-1;
  if(j>=k)
   j=j%k;
  if(j>0)
  {
   for(i=j;i<k;i++)
   a[i]=a[i+1];  
  }
  else
   j++;
 }
 printf("%d\n",a[1]);

 }
 getchar();
 getchar();
 return 0;    
}
main()
{
     int n=1, m=1;
     while(n!=0 && m!=0)
     {
         int i,s=0;
         scanf("%d%d",&n,&m);
         if(n==0 && m==0) break;
         else
         {
             int *p,k,all;
             p=(int *)malloc(n*sizeof(int));
             for(i=0;i<n;i++) p[i]=i+1;
             k=0;
             for(i=1,all=0;;i++)
             {
                  if(i%m==0) {p[k]=0;all++;}
                  if(all==n-1) {for(i=0;i<n;i++) if(p[i]!=0) printf("%d\n",p[i]);break;}
                  do{k=(k+1)%n;}while(p[k]==0);
             }         
         }
     }
     
}
main()
{int x=0,y,i,z,m,n,a,b=0; 
	while(b==0)
	{scanf("%d %d",&n,&m);
	int c[n];
	for(i=1;i<=n;i++)
	c[i]=i;
	if(n==0)
	b=1;
	else
	{x=0;
		while(n>=2)
	{a=m+x;
	x=a%n;
	if(x==0)
	x=n;
	for(z=x;z<n;z++)
	c[z]=c[z+1];
	n=n-1;
	x--;
	}
	printf("%d\n",c[1]);
		}
}
}
int main(){
	int n[300],m[3000],i,s[300],p,x;
	for(x=0;x<300;x++){
		s[x]=0;
		}
	for(p=0;;p++){
		scanf("%d%d",&n[p],&m[p]);
		if(n[p]!=0 && m[p]!=0){
	         for(i=2;i<=n[p];i++){
		        s[p]=(s[p]+m[p])%i;}
			    printf("%d\n",s[p]+1);
			}
		else{
			break;
			}
		}
	return 0;
}
main()
 { 
   int n, m, i, s;          
   while (scanf ("%d %d",&n,&m)&&n!=0&&m!=0)
   {
	s=0;     
   	for (i=2; i<=n; i++) 
   	s=(s+m)%i;    
   	printf ("%d\n", s+1);	
   } 
 }
//******************************************************************************
//??????????                                                         *
//????wayne                                                                *
//?????10.23                                                              *
//******************************************************************************
int main()
{
	int n,m,a[301],i,j,t,k;
	while (cin>>n>>m&&n!=0)        //??????????????
	{
       for (k=1;k<=n;k++)     
		   a[k]=1;                 //??????????1
	   i=1;
	   for (j=n;j>1;j--)           //j?????????????1???????
	   {
		   t=0;
	        while (t<m)            //?????m?
			{
		        if (a[i]!=0)       //??????????
				{
			      t=t+1;           //??????
			      i=i+1;           //??????
				}
		        else i=i+1;
		        if (i>n)  i=i-n;    //?????????
			}
			if (i==1)  a[n]=0;       
		    else a[i-1]=0;           //?m??????
	   }
	   for (k=1;k<=n;k++)
		   if (a[k]!=0)
		   {cout<<k<<endl;           //???????????????
		   break;}
	}
	return 0;
}