semeru/Code-Code-CloneDetection-POJ104 huggingface dataset:
main(){
	int k;
	int a[300][300];
		for(k=0;k<300;k++){
		int n,m,i,j;
		scanf("%d %d",&n,&m);
		if(n==0&&m==0) break;
		
		for(i=0;i<300;i++){
			for(j=0;j<300;j++){a[i][j]=0;}}
		for(i=0;i<n;i++) a[0][i]=i+1;
		for(i=1;i<n;i++){
			for(j=0;j<n-i;j++){
				if((m+j)<(n-i+1)) a[i][j]=a[i-1][m+j];
				else a[i][j]=a[i-1][(m+j)%(n-i+1)];}}
		printf("%d\n",a[n-1][0]);
			
			}
	

}
int main()
{
	int i,j,k,n,m,t;
	for(;;){
    scanf("%d",&n);
	scanf("%d",&m);
	if((n==0)&&(m==0))break;
	t=0;
	int nn[300]={0};
    for(i=1;i<n;i++){
		j=0;
		for(;;){
			if(t==n){
				for(k=0;k<n;k++){
					if(nn[k]==0){
				        t=k;
						break;
					}
				}
			}
			if(nn[t]==0)j=j+1;
			if(j==m){break;}
			t=t+1;
		}
		nn[t]=1;
	}
	for(i=0;i<n;i++){
		if(nn[i]==0)
			printf("%d\n",i+1);
	}
	}
	return 0; 
}
int main(int argc, char* argv[])
{
	int n,m,i,p,s;
	int x[300];
	while (1)
	{
	 scanf ("%d%d",&n,&m);
	 if (n==0) 
	 {break;}
     if (m==1) {printf ("%d\n",n);}
	 for (i=0;i<n;i++)
	 {
      x[i]=1;
	 }
	 s=0;
	 p=n;
	 while (p>1) 
	 {  
	   
	    for (i=0;i<n;i++)
		{
	     s=s+x[i];
	     if (s==m) 
		 {
	      s=0;
	      x[i]=0;
	   
		 }
	   
		}
		p=0;
	    for (i=0;i<n;i++)
		{
	    p=p+x[i];
	 
	 
		}
	 
	 
	  }
	
	   
	 
	
	 	for (i=0;i<n;i++)
		{
		if (x[i]==1) {i++;printf ("%d\n",i);}
	
	
		}
	
	
	
	 
	}
	
	return 0;
}
void main()
{
	int f(int m,int n);
	int m,n,k;
	scanf("%d%d",&n,&m);
	while (m!=0&&n!=0)
	{
	k=f(m,n);
	printf("%d\n",k);
	scanf("%d%d",&n,&m);
	}

}


int f(int m,int n)
{
	int k;
	if (n==1) k=1;
	else 
	{
		k=f(m,n-1);
		k=(k+m%n)%n;
		if (k==0) k+=n;
	}
	return k;
}
void king(int n,int m)
{
	int i,j=1,a[300],k=1;
	for(i=1;i<=n;i++)
		a[i]=i;
	while(n>1)
	{
		j=(j-1+m)%n;
		if(j!=0)
		{
			for(i=j;i<n;i++)
				a[i]=a[i+1];
		}
			else j=1;
			n=n-1;
	}
	printf("%d\n",a[1]);
}
void main()
{
	int n[30],m[30],i,j=0;
	do 
	{
		scanf("%d%d",&n[j],&m[j]);
		j++;
	}while(n[j-1]!=0&&m[j-1]!=0);
	for (i=0;i<j-1;i++)
		king(n[i],m[i]);
}
int mon[1000]={0};
void main()
{
	
	void fking(int a[1000],int k,int p);
	int n,m,j;
	
	while (1)
	{
	
	scanf("%d%d",&n,&m);
	if ((n==0)&&(m==0)) break;
	if (m==1) printf("%d\n",n);

	else 
	{for(j=1;j<=n;j++)
		mon[j]=j;
	fking(mon,n,m);
	}
	
}
}

void fking(int a[1000],int k,int p)
{
	int b[1000];int i;
	if(k>=p)
	{
		for(i=1;i<=k-p;i++)
			b[i]=a[p+i];
		for(i=k-p+1;i<=k-1;i++)
			b[i]=a[i+p-k];
		fking(b,k-1,p);
	}
	else
		if(k>0&&k<p)
		{
			for(i=1;i<=k-p%k;i++)
				b[i]=a[p%k+i];
			for(i=k-p%k+1;i<=k-1;i++)
				b[i]=a[i-k+p%k];
			if(k>=2)
				fking(b,k-1,p);
			else
				printf("%d\n",b[1]);

		}
}
int main()
{int n,m,a[301],k,i,j,num;
for(int p=0;p<100;p++)

{cin>>n;
cin>>m;
if(n!=0)
{for(i=1;i<=n;i++)
{a[i]=1;}
j=0;
k=0;
for(i=1;i<=n;i++)
{
  if(a[i]==1)
  {
    j=j+a[i];
    if(j==m)
       {
		 j=0;
         a[i]=0;
         k++;
	   }
    if(k==n)
     {
		 num=i;break;
	}
  }
  if(i==n)
  i=0;
}
cout<<num<<endl;}}
return 0;
}
int f(int n,int m)
	{
		if(n==1)
			return(1);
		else
			return((f(n-1,m)+m-1)%n+1);
	}
int main()
{	int a[300],b[300];
		int i,n;
		for(i=0;;i++)
		{
			scanf("%d %d ",&a[i],&b[i]);
			if(a[i]==0&&b[i]==0)
				break;
		}
		n=i;
		for(i=0;i<n;i++)
			printf("%d\n",f(a[i],b[i]));
}
int main()
{
    int *a[20000];
    int i,m,n;
    int *k;
    for(;;)
    {
        scanf("%d %d",&n,&m);
        if(n==0) return 0;
        for(i=1;i<n;i++) a[i]=&a[i+1];
        a[n]=&a[1];
        k=&a[1];
        if (m==1) printf("%d\n",n);
        else {
        while(k!=*k)
        {
            for(i=1;i<m-1;i++) k=*k;
            *k=*(int*)(*k);
            k=*k;
        }
        printf("%d\n",k-(int *)&a[1]+1);
        }
    }
}
int main(void)
{
	int m=1,n=1;
	int*p;
	int i,j,k;
	
	while(m!=0&&n!=0)
	{
	scanf("%d%d",&n,&m);
	if(m==0||n==0)
		break;
	
	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==0)
		{
			printf("%d\n",i+1);
			break;
		}
	}
	}
	free(p);
		return 0;
}
struct node{
    int data;
	struct node *next;
};

struct link{
   struct node *head;
   struct node *tail;
};


//allocate a node for the link
int init_link( struct link *plink )
{
    struct node *p = (struct node *)malloc( sizeof(struct node) ); //dynamic allocate memory
	if ( p==NULL )
		return -1;
	p->data = 0;
	p->next = NULL;
	plink->head = p;
	plink->tail = p;
	return 0;
}

//insert a node with data = value at the end of the list 
int insert_link( struct link *plink ,int value )
{ 
	struct node *p = (struct node *)malloc( sizeof(struct node) ); //dynamic allocate memory
	if ( p==NULL )
		return -1;
	p->data = value;
	p->next = NULL;
	plink->tail->next = p;
	plink->tail = p;
    
	return 0;
}

//delete the next node of *pnode from the linklist
//if  *pnode is the last node then delete the first node
int delete_link( struct link *plink,struct node *pnode)
{
    // if delete the first node
	//if ( plink->head == pnode ) 
    struct node *p = pnode->next;
	if ( p == NULL ){ //delete the first node 
      pnode = plink->head;
	  p = plink->head->next;
	  pnode->next = pnode->next->next;
	} else if ( plink->tail == p ){
	  pnode->next = NULL;
	  plink->tail = pnode; 
	}
	else {
      pnode->next = pnode->next->next;
	}
    
	free(p);
	return 0;
    
}

int destroy_link( struct link *plink )
{
	struct node *p = plink->head ;
	struct node *q;
	while( p != NULL )
	{
       q = p->next ;
	   free(p);
	   p = q;
	}
	plink->head = NULL;
	plink->tail = NULL;
	return 0;
}

// slove problem 
// n the number of monkeys
// m the count number
int solve( int n,int m)
{
	int i,j,result;
	struct link link1;
	struct node *p;
	// p mark the position count begins
	// p mark the position to delete
	// p point to the pre node of the node to be deleted
    init_link( &link1 );
    for(i=1;i<=n;i++)
		insert_link(&link1,i);
    p = link1.head;
	for(i=1;i<n;i++)
	{   
		//count
		for(j=1;j<m;j++)
		{
           if ( p->next == NULL )
			  p = link1.head->next;
		   else
              p = p->next;
		} 
		//delete
        delete_link( &link1,p);
	}
    // at last left one 
    result = link1.head->next->data;
	destroy_link( &link1 );
    return result;

}

int main()
{
    int n,m,k;
	struct link link1;
	struct node *p;
    init_link( &link1 );
	scanf("%d %d",&n,&m);
    while( n || m )
	{ 
        k=solve(n,m);
		insert_link(&link1,k);
		scanf("%d %d",&n,&m);
	}
    p = link1.head;
	while( p->next != NULL )
    {
		printf("%d\n",p->next->data );
		p = p->next ;
	}
	destroy_link( &link1 );
	
	return 0;
}
int main()
{
	int i,j,num,king,check[350]={0},m,n,count=0;
	for(;;)
	{
		cin>>n>>m;
		if(m==0&&n==0)
			break;
		for(i=1;i<320;i++)
			check[i]=1;
		count=0;
		for(i=1;;i++)
		{
			if(i==n+1)
				i=1;
			if(check[i]==1)
				count++;
			else
				continue;
			if(count==m)
			{
				count=0;
				check[i]=0;
			}
			for(j=1;j<=n;j++)
			{
				if(check[j]==1)
				{
					num++;
					king=j;
				}
			}
			if(num==1)
			{
				cout<<king<<endl;
				break;
			}
			num=0;
		}
	}
	return 0;
}
int n,m;
int flag[400];

int inc(int x){
	if(++x==n+1)
		x=1;
	return x;
}

int main(){

	//freopen("a.txt","r",stdin);

	while(scanf("%d%d",&n,&m),n||m){
		memset(flag,0,sizeof flag);
		int cnt=n,now=0;
		while(cnt>1){
			for(int i=0;i<m;i++)
				for(now=inc(now);flag[now];now=inc(now));
			flag[now]=1;
			cnt--;
			
		}
		for(now=inc(now);flag[now];now=inc(now));
		printf("%d\n",now);
	}

	return 0;
}
int main()
{
	int n,m,i,j,k,r,p;
	int a[300];
	for(j=0;j<300;j++)
		a[j]=1;
	for(j=1;;j++)
	{

		r=0;
		k=0;
		scanf("%d%d",&n,&m);
	    for(p=0;p<300;p++)
		    a[p]=1;
		if(n==0&&m==0)
			break;
		else
		{
			for(i=0;i<n;i++)
			{
				if(a[i]==1)
				{
					k=0;
				    r++;
				}
				else
					k++;
				if(r==m)
				{
					a[i]=0;
					r=0;
					k=0;
				}
				if(i==n-1)
					i=-1;
				if(k==n-1)
				{
					if(i<n-1)
						i++;
					break;
				}
			}
			printf("%d\n",i+1);
		}
	}
	return 0;
}
int joseph(int n,int m)
{
	int mon[300];
	int i,j,f;

	for (i=0;i<n;i++)
		mon[i]=i+1;//????? ?n?
	for (i=-1,j=0;n!=1;)
	{
		i=i+1;
		j=j+1;
		if (i>=n)
			i=0;
		if (j==m)
		{
			for (f=i;f<n-1;f++)
				mon[f]=mon[f+1];
			n=n-1;
			j=0;
			i=i-1;
		}
	}
	return mon[0];
}


void main()
{
	int n[100],m[100],t,king;
	int i;

	for (i=0;;i++)
	{
		scanf("%d %d",&n[i],&m[i]);
		if (n[i]==0)
			break;
	}
	t=i;
	for (i=0;i<t;i++)
	{
		king=joseph(n[i],m[i]);
		printf("%d",king);
		if (i!=t-1)
			printf("\n");
	}
}
struct monkey 
{
int a;
struct monkey *next;
};
int main()
{
int n,m;
struct monkey *creat (int n);
struct monkey *delet(struct monkey *head,int m,int n);
scanf("%d%d",&n,&m);
if(n!=0)
do
{
	if(m!=1)
	{
		struct monkey * head;
head= creat(n);
head= delet(head,m,n);
printf("%d\n",head->a);
	}
	else printf("%d\n",n);
scanf("%d%d",&n,&m);
}
while(n!=0);




return 0;
}



struct monkey *creat (int n)
{
struct monkey *p1,*p2,*head;
int i;
head=p1=(struct monkey *)malloc(LEN);
for(i=0;i<n;i++)
{
p1->a=i+1;
p2=p1;
p1=(struct monkey *)malloc(LEN);
p2->next=p1;

}
p2->next=head;

return (head);
}
struct monkey *delet(struct monkey *head,int m,int n)
{
  struct monkey *p2=head;
  int i;
    while(head->next!=head)
  {
  
  if((m!=2)&&(m<=n))
  {
	  for(i=0;i<m-1;i++)
  { 
      p2=head;  
	  head=head->next;
  
  }
	  p2->next=head->next;
  head=head->next;
  }
  else
  {
	  if(m>n)
	  {
		   for(i=0;i<m-1;i++)
  { 
      p2=head;  
	  head=head->next;
  
  }
	  
	  p2->next=head->next;
      head=head->next;
	  }
	  else
  { 
	  p2=head;head=head->next;p2->next=head->next;head=head->next;}
  }
  n=n-1;
	}
return (head);
}
int main()
{
    int n,m;
    int a[N];
    int i,j,r;
    int t;

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

        for(i=1;i<=n;i++)
        {
            a[i]=i;
        }

        for(r=0;r<n-1;r++)
        {
            if(r==0)
                i=0;
            else
                i--;

            for(j=1;j<=m;j++)
            {
                if(i==n-r)
                {
                    i=1;
                }
                else
                {
                    i++;
                }
            }

            //printf("%d %d\n",i,a[i]);

            for(j=i;j<n-r;j++)
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }

        }

        printf("%d\n",a[1]);
    }

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

   
             getchar();getchar();getchar();
             }
void main()
{void search(int m,int n);
	int m,n,k=1,i,j=0,*p,a[300],b[10][2];
	for(i=0;;i++)
	{
	scanf("%d %d",&b[i][1],&b[i][2]);
	if(b[i][1]==0)break;
	}
for(j=0;j<i;j++)
	search(b[j][1],b[j][2]);
}


void search(int m,int n)
{int k=0,i,j=0,*p,a[300];
	p=a;
	for(i=0;i<m;i++)
		*(p+i)=i+1;
	i=0;
	
	while(j<m-1)
	{
		if(*(p+i)!=0)k++;
	if(k==n)
	{
		*(p+i)=0;
		k=0;
		j++;
	}
	i++;
	if(i==m)i=0;
	}
	while(*p==0)p++;
     printf("%d\n",*p);
}
//????4_1.cpp
//???2013?10?
//????????
int main()
{
	int j;
	for(j=1;;j++)
	{
	int a[301],i,m,n,k,t;
	cin>>n>>m;
	if((m==0)&&(n==0))
		break;
	for(i=1;i<=n;i++)          //?a[i]??????
		a[i]=1;
	t=n;
	k=0;
	i=0;
	while(n>1)                //?????????????????
	{
		k=k+1;
		i=i+1;
		int i0=i;
		for(;;i0++)
		{
		if(i0>t)
			i0=i0-t;
		if(a[i0]==1)
		   { 
			i=i0;
		    break;
		   }
		}
		if(k==m)
		{
			a[i]=0;
			k=0;
			n=n-1;
		}
	}
	for(i=1;i<=t;i++)        //????????????????
	{
		if(a[i]==1)
			cout<<i<<endl;
	}
	}
	return 0;
}
void main()
{
	int i,j,m,n,l,k,r=0,a[301],c[301],b[1000];
	for (j=1;;j++)
	{
		scanf("%d %d",&n,&m);
		if(n==0) break;
		for (i=1;i<=n;i++)
		{
			a[i]=i;
		}
		for (l=n;l>1;l--)
		{
			if(m%l==0) 
			{
				continue;
			}
			else 
			k=m%l;
			for(i=1;i<=l-k;i++)
			{
				c[i]=a[k+i];
			}
			for(i=l-k+1;i<=l-1;i++)
			{
				c[i]=a[i-l+k];
			}
			for(i=1;i<=l-1;i++)
			{
				a[i]=c[i];
			}
		}
		b[j]=a[1];
		r++;
	}
	for(j=1;j<=r;j++)
	{
		printf("%d\n",b[j]);
	}
}
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 s=0,b=1,i=0,j,king;
         int a[301];
	for(i=0;i<n;i++)
		a[i]=1;
	i=0;

	 while(s!=1)
	 {   
		 s=0;
		 if(i==n)
		 {
			 i=0;
			 continue;
		 }
		 else
		 {
		     if(a[i]==0)
			 {
			     i++;
			     continue;
			 }
		     else
			 {
	 	         if(b!=m)
				 {
			        b++;             
				 }
		         else 
				 {
			         b=1;
			         a[i]=0;
				 }
			     i++;
			 }
		 }
		 for(j=0;j<n;j++)
			 s+=a[j];
	 }

	 for(i=0;i<n;i++)
	 {
		 if(a[i]==1)
			 king=i+1;	
	 }
	 return king;
}
int main(int argc, char* argv[])
{
	int n,m,k,i,j,p,num,q;
	int a[301];
	for(;;)
	{
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)
			break;
		else if(m==1)
			printf("%d\n",n);
		else
		{
    	    k=n+1;num=0;q=n+1;
   	        if(n>=m)
			{
	            for(i=1;i<n+1;i++)
				{
		            a[i]=i;
				}
	            while(num<n-1)
				{
	                for(j=1;j<n+1;j++)
					{
		                if(a[j]!=0&&a[j]%m==0)
						{
	                         a[j]=0;
				             num++;
						}
						else;
					}
	                for(p=1;p<n+1;p++)
					{
	                     if(a[p]==0)
						 {
		   	                  continue;
						 }
	   	                 else
						 {
			                 a[p]=k;
	        	             k++;
						 }
					}
	
				}
			}
	        else
			{
 	            for(i=1;i<n+1;i++)
				{
		         	a[i]=i;
				}
	            while(num<n-1)
				{
                    for(i=1;i<n+1;i++)
					{
                         if(a[i]==0)
						 {
		   	                  continue;
						 }
	   	                 else
						 {
			                 a[i]=q;
			                	q++;
						 }
					}
			        for(j=1;j<n+1;j++)
					{
			             if(a[j]!=0&&a[j]%m==0)
						 {
				              a[j]=0;
				              num++;
						 }
					}
				}
			} 
            for(i=2;i<n+2;i++)
			{
	        	if(a[i-1]!=0)
				{
		        	break;
				}
			}
	        printf("%d\n",i-1);
		}
	}
	return 0;
}
main()
{
      int a,b,c[10000],d,e,f[100000];
      scanf("%d%d",&a,&b);
      while(a!=0 || b!=0)
      {
      for(d=1;d<=a;d++)
      c[d]=d;
      for(;a>=1;a--)
      {  
         e=b%a;
         if(e==0)
         e=a;
          for(d=1;d<=a;d++)
          f[d]=c[d];
         for(d=1;d<=(a-e);d++)
           c[d]=c[e+d];
         for(d=a-e+1;d<=a-1;d++)
           c[d]=f[d-(a-e)];
      }
      printf("%d\n",c[1]);
      scanf("%d%d",&a,&b);
      } 
     
      getchar();
      getchar();
      }
const int MAX_NUM = 300;
int anLoop[MAX_NUM + 10];
int main()
{
int n,m;
while (1) {
scanf("%d %d",&n,&m);
if (n==0) break;
for (int i=0; i<n; i++) anLoop[i] = i + 1;
int nPtr = 0;

for (int i=0; i<n; i++) {
int nCounted = 0;
while (nCounted < m) {
while (anLoop[nPtr] == 0) nPtr = (nPtr + 1) % n;
nCounted++;
nPtr = (nPtr + 1) % n;
}
nPtr--;
if (nPtr < 0) nPtr = n - 1;
if (i == n-1)
cout << anLoop[nPtr] << endl;
anLoop[nPtr] = 0;
}
}
}
int main()
{
    int i;
    int n[10000],m[10000];
    int sum,k,q,f;
    int p=0; 
  for (i=1;i<10000;i++)
       {
        scanf("%d %d",&n[i],&m[i]);
        if (n[i]==0)
        {break; }
        p++;
       }
    i=1;
    int y;       
    int a[10000];
    while (i<=p)
       {
         if (n[i]==1||m[i]==1)
         printf("%d\n",n[i]);
         else
         {for (y=0;y<10000;y++)
           { a[y]=1;}
         sum=0;
         k=0;
         q=0;
         while (q!=n[i]-1)
         {
          sum=sum+a[k];
          if (sum==m[i])
             {a[k]=0;
              q++;
              sum=0;}
          if (a[k]!=0)
              {f=k+1;}
              k++;
          if (k>=n[i])
             {k=0;} 
         }
         printf("%d\n",f);}
         i++;    
       }
}
main()
{
	int a[80][2],i,j,s;
	for(j=0;;j++)
	{
		s=0;
		scanf("%d%d", &a[j][0], &a[j][1]);
		if(a[j][0]==0) break;
	for (i=2; i<=a[j][0]; i++)
		s=(s+a[j][1])%i;
	printf("%d\n",s+1);
	}
}
int main ()
{
	int n, m, i, j, count, p, xh;
	int a[310]; // a??? ????????????
	int b[310]; // b???? ??????? ?????????????i???????????
    // ?????????????count?1
	while (cin)
	{
		cin >> n >> m;
		if ((n == 0) && (m == 0)) return 0;
		for (i = 1; i <= n; i++) a[i] = 1;
		count = n;
		p = 1;
		while (count != 1)
		{
			// ????????b??
			xh = 1;
			while (xh <= m)
			{
				if (a[p] == 1) xh++;
				if (p <= n - 1) p++;
				else p = (p + 1) % n;
			}
			if (p == 1) a[n] = 0;
			else a[p - 1] = 0;
			count = count - 1;
		}
		for (i = 1; i <= n; i++)
		{
			if (a[i] == 1) cout << i << endl;
		}
	}
	return 0;
}
void main()
{int i,k,q[100],t,r,n[100],m[100],num[300],*p;
for(t=0;t<100;t++)
{ scanf("%d",&n[t]);
  scanf("%d",&m[t]);
 if(n[t]==0&&m[t]==0)
  break;
}
 
for(r=0;r<t;r++)
{p=num;
 for(i=0;i<n[r];i++)
 *(p+i)=i+1;
  i=0;
  k=0;
  q[r]=0;
 while(q[r]<(n[r]-1))
  {if(*(p+i)!=0)
      k++;
   if(k==m[r])
   {*(p+i)=0;
    k=0;
    q[r]++;
    }
    i++;
    if(i==n[r])
    i=0;
    }
while(*p==0)
p++;
printf("%d\n",*p);
}
}
main()
{   int cn(int x,int y);
    int i,q,p, m[2000],n[2000];
    for(i=1;i<=999;i++)
    {scanf("%d %d",&n[i],&m[i]);
    q=q+1;
    if(n[i]==0&&m[i]==0)
    break;
   if (n[i]==1)
    printf("1\n");
    else 
    cn(n[i],m[i]);
}

}



    int cn(int x,int y)
    {int i,j,p,k,m,d[2000][1000];
         k=0;m=0;
        for(i=1;i<=1999;i++)
        {for(j=1;j<=x;j++)
        {d[i][j]=1;
       }}
        
        for(i=1;i<=1999;i++)
        {       for(j=1;j<=x;j++)
               {        if(d[i][j]==1)
                        k=k+1;
                       if(k==y)
                      {     for(p=1;p<=1999;p++)
                            {d[p][j]=0;}
                            k=0;
                            m=m+1;
             
                           if(m==(x-1))
                           {goto end;}
                      }
        }   }
        end:
            for(i=1;i<=x;i++)
            {if(d[1][i]==1)
            { p=i;
             printf("%d\n",i);
            break;}}
        
        }
int main()
{
    int b[1000];              //b???????????????n(?0??),???????m
    int x=0;
    scanf ("%d %d",&b[0],&b[1]);   //x????x????????
    
    
    
    for (x=0;b[x]!=0;x=x+2)
    {
        int n,m,d=0,i=0,k=0;               //n??????m???????
        n=b[x];
        m=b[x+1];
        int a[999];
        
        
        for (i=0;i<n;i++)                //??n?a??????i=n
            a[i]=i+1;
        
        for (i=0;d<n-1;i++)
        {
            if (i==n)
                i=0;
            
            while (a[i]==0)
            {
                i++;
                if (i==n)
                    i=0;
            }
            
            k++;                           //k?????n?
            
            if (k==m)
            {
                a[i]=0;
                k=0;
                d++;
            }
            //printf("%d %d %d %d %d %d\n",a[0],a[1],a[2],a[3],a[4],a[5]);
            
        }
        
        
        for (i=0;i<n;i++)
        {
            if (a[i]!=0)
            printf ("%d\n",a[i]);
        }
        
        
        scanf ("%d %d",&b[x+2],&b[x+3]);
    }
    return 0;
}
struct Node
	{
       int number;
	   struct Node * next;
	};
void main()
{
	
	struct Node *linkhead,*a,*linktail;
	int n,m,i,j,count=0;
	int king[100];
	while(1)
	{
	scanf("%d%d",&n,&m);
	if((n==0)&&(m==0))
	    break;
	for(i=1;i<=n;i++)
	{
        if(i==1)
		{a=(struct Node *)malloc(1*sizeof(struct Node));
		 a->number=i;
		 linkhead=a;
		 linktail=a;
		}
		else
		{
			a=(struct Node *)malloc(1*sizeof(struct Node));
            a->number=i;
			linktail->next=a;
			linktail=a;
		}
	}
	linktail->next=linkhead;
	linkhead=linktail;
	for(i=1;i<=(n-1);i++)
	{
	for(j=0;j<(m-1);j++)
	{
		linkhead=linkhead->next;
	}
    a=linkhead->next;
	linkhead->next=a->next;
	free(a);
	}
	king[count]=linkhead->number;
	count++;
	free(linkhead);
}
	for(i=0;i<count;i++)
	{
		printf("%d\n",king[i]);
	}
}
void shu(int a,int b)
{
	int monkey[300];
	int i,j,count;
	for(i=0;i<a;i++)
		monkey[i]=1;
	i=-1;
	for(j=1;j<a;j++)
	{
		count=0;
        do
		{
			i=(i+1)%a;
			count=count+monkey[i];
		}while(count<b);
		monkey[i]=0;
	}
    for(j=0;j<a;j++)
	{
		if(monkey[j]!=0)  {printf("%d\n",j+1);break;}
	}
}
void main()
{
	int n,m;
	while(1)
	{
		scanf("%d %d",&n,&m);
		if(n>0)
			shu(n,m);
		else break;
	}
}
struct jiegou
{
	int a;
	struct jiegou *next;
};
void main()
{
	struct jiegou *p1,*p2,*head,*p;
	int n,m;
    int i;
    int count=1;
	while(1)
	{
        scanf("%d%d",&n,&m);
		if(n==0)
			break;
		p1=(struct jiegou *)malloc(sizeof(struct jiegou));
        for(i=0;i<n;i++)
		{
			if(i==0)
              head=p1,p2=p1,p1->a=1;
			else
			{
				p1=(struct jiegou *)malloc(sizeof(struct jiegou));
				p1->a=i+1;
				p2->next=p1;
				p2=p1;
			}
		}
		p2->next=head;
        
        p=head;
		while(head->next!=head)
		{
		if(m!=1)
		{
			count++;
			if(count==m)
		   {
			   if(p->next!=head)
                 p->next=(p->next)->next;

			   else
			   {
				    p->next=(p->next)->next;
					head=p;
			   }
			 count=1;
			 
		   }
		   
           p=p->next;
		}
		else
		{
			printf("%d\n",n);
			break;
		}
		     
		   
		}
		if(m!=1)
		printf("%d\n",head->a);
	}

}
int main()
{
	int num[300] = {0};//??????0 
	int total[300] = {0};//? n
	int countNum[300] = {0};//? m
	int n = 1 ;
	int m = 1 ;
	int nn = 0 ;//?????
	while(n!=0&&m!=0)
	{
		cin >> n >> m ;
		total[nn] = n ;
		countNum[nn] = m ;
		nn ++ ;
	}
	int q ;
	for(q = 0 ; q < nn ; q++)//????
	{
		n = total[q] ;
		m = countNum[q] ;
		int i ;
		for( i= 0 ; i < n ; i ++)
		{
			num[i] = 1 ;//?1???????????
		}
		int p = 0 ;//????????
		int k ;
		int n1 = n ;
		while(n1 > 1)
		{
			for(k = 1 ; k <= m ; k ++)//k??????
			{
				if(k == m)//?m???
				{
					while(num[p] != 1)//????????
					{
						p ++;
						if(p >= n)
							p = 0 ;
					}
					num[p] = 2 ;
					p ++ ;			
					if(p >= n)//????????????
						p = 0 ;
				}else{
					while(num[p] != 1)//????????
					{
						p ++;
						if(p >= n)
							p = 0 ;					
					}
					p++;
					if(p >= n)
						p = 0 ;	
				}
			}
			n1 -- ;		
		}
		for(i = 0 ; i < n ; i ++)
		{
			if(num[i]==1)
				cout<<(i+1)<<endl ;
		}
	}
	
}
int main(int argc, char* argv[])
{
	int sz[100][2],num[100][100],result[100];
	int row,col,i;

	for(row=0;row<100;row++)
	{
		for(col=0;col<2;col++)
		{
			scanf("%d",&sz[row][col]);
			
		}
		if(sz[row][1]==0)
		{
			break;
		}
	}

	for(row=0;row<100;row++)
	{	
		if(sz[row][0]==0)
		{
			break;
		}
		num[row][0]=1;
		
		if(sz[row][0]!=1)
		{
			for(i=1;i<sz[row][0];i++)
			{
			    num[row][i]=(num[row][i-1]+sz[row][1])%(i+1);
			
			    if(num[row][i]==0)
				{	
					num[row][i]=i+1;
				}
				result[row]=num[row][i];
			}
		}else{
			result[row]=1;
		}	

		printf("%d\n",result[row]);
	
	}
	return 0;
}