semeru/Code-Code-CloneDetection-POJ104 huggingface dataset:
struct results
{
	int num;
	struct results *next;
};

void print0(struct results *head)
{
   struct results *p;
   p=head;
	
   while(p!=NULL)
   {
		   printf("%d",p->num);
		   cout<<endl;
		   p=p->next;
	   }
}

int main()
{
	int n,m,i,g,s=0;

	struct results  *head,*p1,*p2;
	
	head=NULL;
	p1=(struct results*)malloc(LEN);
	p2=(struct results*)malloc(LEN);
	g=0;

	while(scanf("%d%d",&n,&m)!=EOF&&(m&&n)) 
	{ 
		if (m<=0||n>300)
		{
			printf("?????0<m,n<=300:");
			continue;
		}
		s=0;
		if(n==1) s=0;
		else {for(i=2;i<=n;i++)
		s=(s+m)%i;}
 		p1->num=s+1;
  	g=g+1;
 		if(g==1) head=p1;
 		else p2->next=p1;
 		p2=p1;
 		p1=(struct results*)malloc(LEN);
	}
	p2->next=NULL;

	print0(head);
	return 0;
}
main()
{
	int gg;
	for(gg=0;;gg++)
	{
	int n,k=0,i,m,j,a[301],kick=0;
	
	for(i=1;i<=301;i++)a[i]=1;
	scanf("%d %d",&n,&m);
	if(m==0)
	break;
	if(n==1)printf("1\n");
	else{
	for(j=0;;j++)
	{
		for(i=1;i<=n;i++)
		{
			if(a[i]==1)
			{k+=1;if(k==m){a[i]=0;kick+=1;k=0;if(kick==n-1){goto loop;}}}
		}
		
	}
	loop:for(i=1;i<=n;i++)
	{if(a[i]==1)printf("%d\n",i);}
	}	}	
			
    
}
int main()
{
int t=0;
int n[300],m[300];
for(int i=0;i<300;i++){
	scanf("%d",&n[i]);	
	scanf("%d",&m[i]);
		t++;
	if(m[i]==0||n[i]==0){goto next;}
}
next:for(int k0=0;k0<t-1;k0++){
		int s[300]={0};
	int k1=n[k0];
	int j=1;
	for(int h=0;h<n[k0];h=(h+1)%n[k0])
	{
		if(s[h]){
			continue;
		}
		
		if(j++==m[k0])
		{
			s[h]=1;
			k1--;
			j=1;
		}
		if(k1==0){
			printf("%d\n",h+1);
			break;
		}
	}
}
return 0;
}
int m;

int f(int x) {
    // pick the last monkey.
    if (x == 1)
        return 1;
    // who's kicked off?
    int k = m % x;
    if (k == 0)
    {
        // kick off the n-th monkey, so it is exactly f(x-1)
        return f(x-1);
    } else {
        int t = f(x-1);
        if (t <= x - k)
            return k + t;
        else
            return t - (x-k);
    }

}
int main() {
    int n;
    while (true) {
        scanf("%d%d", &n, &m);
        if (n == 0) break;
        printf("%d\n", f(n));
    }
}
int main()
{
    for (;;)
    {
        int n,m,sum,dp[301]={0},pos=0;
        cin>>n>>m;
        if ((n==0)&&(m==0)) break;
        for (int i=0;i<(n-1);i++)
        {
            sum=0;
            for (;sum<m;)
            {
                pos++;
                if (pos>n) pos=pos%n;
                if (dp[pos]==0) sum++;
            }
            dp[pos]=1;
        }
        for (int i=1;i<=n;i++)
          if (dp[i]==0) 
          {
            cout<<i<<endl;
            break;
          }
    }
}
int main()
{
        int m = 0, n = 0, i = 0, pre = 0;
        int numberOut = 0, in[300], count = 0;
        scanf("%d%d", &n, &m);
        while (!(m == 0 && n == 0))
        {
               for (i = 0; i < n; i++)
               {
                       in[i] = 1;
               }
               i = 0;
               while (numberOut < n - 1)
               {
                       while (count < m)
                       {
                               if (in[i] == 1)
                               {
                                      count++;
                               }
                               pre = i;
                               i = (i + 1) % n;
                       }
                       in[pre] = 0;
                       numberOut++;
                       count = 0;
               }
               for (i = 0; i < n; i++)
               {
                       if (in[i] == 1)
                               printf("%d\n", i + 1);
               }
               numberOut = 0;
               scanf("%d%d", &n, &m);
        }
        return 0;
}
void main()
{
	int n, m;
	int i, j, m1, cur, t;
	int next[10000];
	int b=0;
	while (1)
	{
		scanf("%d %d", &n, &m);
		if (n==0 && m==0)
			break;
		//??????????????
		for (i=1;i<=n;i++)
			next[i]=i+1;
		next[n]=1;
		//?????????cur
		cur=1;
		//?n-1??????????cur==next[cur]???????????
		for (i=1;i<=n-1;i++)
		{
			//???????
			m1=m%(n-i+1);
			if (m1==0) m1=n-i+1;
			else if (m1==1) m1=n-i+1+1;
			//??
			for (j=0;j<m1-2;j++)
				cur=next[cur];
			//??cur???????
			t=next[next[cur]];
			next[next[cur]]=0;
			next[cur]=t;
			//?????????1??
			cur=next[cur];
		}
		if (b) printf("\n");
		printf("%d", cur);
		b=1;
	}
}
int f(int m,int n)
{
	int s=0,i=0;
	int *pa,*p;
	pa=(int*)malloc(sizeof(int)*n);
	for(p=pa;p<pa+n;p++)
	{
	   *p=1;
	}
	p=pa;
	while(s!=n*m)
	{
		s=s+*(p+i%n);
		if(s%m==0)
		{
			*(p+i%n)=0;
		}
		i++;
	}
	if(i%n==0)
		i=n;
	else
		i=i%n;
	return(i);
}
void main()
{
	while(1)
	{
	int m,n;
	scanf("%d%d",&n,&m);
	if(m>0&&n>0)
		printf("%d\n",f(m,n));
	else
		break;
	}
}
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;
		t=s->l;
		t->r=s->r;
		t=s->r;
		t->l=s->l;
		p=t;
	}
	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]);
}
int a[300];
int main()
{
	int n,m,s,k,p;
	int i,j;
	while(cin>>n>>m,n||m)
	{
		for(i=0;i<n;i++)
		a[i]=1;
		p=n;
		if(n==1)cout<<1<<endl;
		else
		{
		a[(m-1)%p]=0;
		s=((m-1)%p+1)%p;
		for(n--;n>1;n--){
			k=m;
			while(k>1){
				if(a[s]==1){k--;}
				s++;
				s%=p;
			}	
			while(a[s]==0)
			{s++;s%=p;}
			a[s]=0;//cout<<s<<endl;
		}
		for(i=0;i<p;i++)
		if(a[i])
		cout<<i+1<<endl;
	}
	}
	return 0;
}
int main()
{
 int n,m;
 int i,j,k;
 int a[350];
 while(1)
 {
  scanf("%d %d",&n,&m);
  if(n==0&&m==0)break;
  else
  {
   for(i=0;i<n;i++)
   a[i]=0;
   i=0;
   k=0;          
   for(j=0;j<n;j++)
   {
     if(!a[j]){k++;}
     if(k==m)
     {
      a[j]=1;
      k=0;
      i++;
     }
     if(i==n-1)break;
     if(j==n-1)j=-1;
   }
   for(i=0;i<n;i++)
    if(!a[i])
    printf("%d\n",i+1); 
  }
 }
}
int main ()
{
	int m = 1, n = 1;
	while (1)
	{
		cin >> n >> m;
		if (m == 0 && n == 0)
			break;
		int a[301] = {0}, i = 0, num = 0, baoshu = 0;
		while (num != n - 1)
		{
			i++;
			if (a[i] == 0)
				baoshu++;
			if (baoshu == m)
			{
				a[i] = 1;
				baoshu = 0;
				num++;
			}
			if (i == n)
				i = 0;
		}
		for (i = 1; i <= n; i++)
		{
			if (a[i] == 0)
				cout << i << endl;
		}
	}
	return 0;
}
int main(void)
{
	int m,n;
	int *p;
	int i,j,k,l;
	while(1){
	   scanf("%d%d",&n,&m);
	   if(n==0&&m==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){          /*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;
}
int main()
{
	int N, M, i, cur, leave;
	int a[301];
	while(1)
	{
		scanf("%d %d", &N, &M);
		if (N==0 && M==0)
			break;

		for (i=1; i<=N; i++)
			a[i] = i;
		cur = 1;
		while(1)
		{
			if (N==1)
				break;
			leave = (cur+M-1)%N;
			if (leave==0)
			{
				leave=N;
				cur=1;
			}
			else
				cur=leave;
			for (i=leave; i<N; i++)
				a[i] = a[i+1];
			N--;
		}
		printf("%d\n", a[1]);
	}
	return 0;
}
int main()
{
	int	m, n;
	do
	{
		int	p[301] = {0}, q = 0, count = 0;
		cin >> n >> m;	//??n?0??? 
		if (n == 0){break;}
		for (int i = 1; ; i ++)
		{
			if (i == n + 1)	//????n+1,?????1 
			{
				i = 1;
			}
			if (p[i] == 1)	//????????????????? 
			{
				continue;
			}
			q ++;		//???????????????(q++); 
			if (q == m)	//???????m 
			{
				p[i] = 1;	//???? 
				count ++;	//??????+1 
				if(count == n)	//?????????? 
				{
					cout << i << endl;//?????????? 
					break;
				}
				//????????????????;
				q = 0;	 
				continue;
			}
		}
	}while(1);
return 0;
}
main()
{
      char n[300]={'\0'};
      int m,i,j,k,d;
      int x=1;
      int num;
      
      for(x=1;x<=2;)
      {
                    scanf("%d %d",&num,&m);
                    if(num==0&&m==0)
                    break;
                    if(num==1)
                    {
                              printf("1\n");
                              continue;
                    }
      for(i=0;i<=num-1;i++)
      {
              n[i]='1';
      }
      int count=0;j=1;i=0;
      for(d=1;;d++)
      {
              
              j=0;
            for(;i<=num;)
            {               
                              if(n[i]!='\0')
                              {             j=j+1;
                                           if(j==m)
                                           { n[i]='\0';
                                           count++;
                                            break;}
                                            if(i==num-1&&j<m)
                                            {
                                            i=-1;
                                            }
                                
                                }
                                i=i+1;
                                if(i>=num)
                                i=0;
                   
            }
            if(count==num-1)
            break;
      }
      for( i=0;i<=num;i++)
      {
                         if(n[i]!='\0')
                         break;
      }
      printf("%d\n",i+1);
      }
}
int main()
{

    while(1)
    {
        int m;
        int n;
        int i=0;
        int a=0;
        int b=0;
        int sz[1000];
        int numofking=0;
        int c=0;
        scanf("%d%d",&n,&m);
        if(n==0||m==0)
            break;
        c=n;
        for(i=0; i<n; i++)
        {
            sz[i]=1;
        }
        while(n>1)
        {
            while(a<m)
            {
                if(sz[b]==1)
                {
                    a=a+1;
                    if(a==m)
                        sz[b]=0;
                }
                b++;
                b=b%c;
            }
            a=0;
            n=n-1;
        }
        for(i=0; i<c; i++)
        {
            if(sz[i]==1)
            {
                numofking=i+1;
                break;
            }
        }
        printf("%d\n",numofking);
    }
    return 0;
}
main()
{
	for (int j=0;;j++){
	int m,n;
	scanf("%d%d",&n,&m);
	if(m==0) break;else{	
	int k=0;
	for (int i=2;i<=n;i++)
		k=((m%i)+k)%i;
	printf("%d\n",k+1);
	}
}
}
struct monkey
{
	int num;
	struct monkey *next;
};

int monkeyking(int n,int m,struct monkey *temp)
{
    int i;  
	while(temp->next!=temp)
	{
		if(m==1)
			return(n);
		for(i=0;i<m-2;i++)
		{
			temp=temp->next;
		}
		temp->next=(temp->next)->next;
		temp=temp->next;
	}
	return(temp->num);
}

void main()
{
	struct monkey *temp=NULL,*pt=NULL,*head=NULL;
	int i,m=1,n=1;
	while(m!=0&&n!=0)
	{
		scanf("%d %d",&n,&m);
		if(n==0&&m==0)
			break;
		for(i=0;i<n;i++)
		{
			pt=(struct monkey *)malloc(sizeof(struct monkey));
			if(i==0)
			{
				head=pt;
				temp=pt;
			}
			else
			{
				temp->next=pt;
				temp=pt;
			}
			pt->num=i+1;
		}
		pt->next=head;
		printf("%d\n",monkeyking(n,m,head));
	}
}
main()
{
	int m,n,h[301],i,k,num,leave,last;
	do
	{
	scanf("%d%d",&n,&m);
	for(i=0;i<=n;i++) h[i]=1;
	num=0;
	k=0;
	leave=n;
	do
	{
		if(h[num]==1)
		if(k!=m)
		{
			k++;
		}
		else
		{
			k=1;
			h[num]=0;
			leave--;
			last=num;
		}
		if(num!=n) num++;
		else num=1;
	}while(leave>0);
	if(last>0) printf("%d\n",last);
	}while(last>0);
	return(0);
}
int main(int argc, char *argv[])
{
	int m[10000], n[10000], s[10000], i;
	for (i=0;;i++)
	{
		cin >> n[i];
		cin >> m[i];
		if (n[i]==0&&m[i]==0)  break;
	}
	for (int j=0;j<i;j++)
	{   s[j]=0;
		for (int k=2;k<=n[j];k++)
		{
			s[j]=(s[j]+m[j])%k;
		}
	}
	for (int j=0;j<i;j++)
	{
		cout << s[j]+1 << endl;
	}
}
void main()
   {int n,m,i,j,ms,s[400]={0},x[400];
    for (;;)
    {for (i=1;i<=400;i++)
	     s[i]=0;
	scanf("%d", &n);
	scanf("%d", &m);
	if (n==0&&m==0)
		break;
	for (i=1,j=1;i<=m*(n-1);i++)
	{x[j]=i;
	 if (x[j]%m==0)
		 s[j]=1;
	 for (;;)
		 {if (j==n)
		     j=1;
		  else j=j+1;
		  if (s[j]==0)
			  break;}
	}
	for (i=1;i<=n;i++)
		if (s[i]==0)
		printf("%d\n", i);
	}
	
   }
int main()
{
	int i,j,COUNT=0,m[300],n[300],STAT[300][300],sum[300],SUM[300];
	/*??m,n*/
	for(i=0;i<300;i++)
	{
		scanf("%d%d",&n[i],&m[i]);
		COUNT++;
		sum[i]=0;
		SUM[i]=n[i];
		if(m[i]==0&&n[i]==0)
		{
			break;
		}
	}

	for(i=0;i<COUNT-1;i++)
	{
		for(j=0;j<n[i];j++)
		{
			STAT[i][j]=1;              //???STAT;
		}
		while(SUM[i]>1)
		{
			for(j=0;j<n[i];j++)
			{
				sum[i]+=STAT[i][j];
			    if(sum[i]==m[i])
				{
				   sum[i]=0;          
				   STAT[i][j]=0;
				   SUM[i]=SUM[i]-1;   //??m[i]???;
				}
			    if(SUM[i]==1)
				{
					break;
				}
			}
		}
		for(j=0;j<n[i];j++)
		{
			if(STAT[i][j]==1)
			{
				printf("%d\n",j+1);    //??;
			}
		}
	}
    return 0;
}
int main(){
    int sz[300];
    int n,m,i,j,c,p;
	for(j=0;;j++){
		p=0;
		scanf("%d%d",&n,&m);
		if(n==0){
			break;}
        for(i=0;i<n;i++){
            sz[i]=i+1;
        }
        for(i=0;i<n;i++){
			c=0;
            while(c<m){
                while(sz[p]==0)
                p=(p+1)%n;
                c++;
                p=(p+1)%n;
            }
            p--;
            if(p<0)
                p=n-1;
            if(i==n-1)
                printf("%d\n",sz[p]);
            sz[p]=0;
           }
      }
      return 0;
}
int main(){
    int t;
    for(t=0;1;t++){
    int n,m,a[301],i,r,x,y=0;
    scanf("%d%d",&n,&m);
    if(n==0&&m==0){
                   break;
                   }
    x=n;
    for(i=0;i<n;i++){
                     a[i]=i;
                     }
    for(i=0;1;i++){
              if(a[i]==i){
                          y++;
                          }
              if(y==m){
                       a[i]=-1;
                       y=0;
                       x--;
                       }
              if(x==1){
                       for(r=0;r<n;r++){
                                        if(a[r]!=-1){
                                                    printf("%d\n",a[r]+1);
                                                    }
                                        } 
                       break;
                       }
              if(i==n-1){
                         i=-1;
                         }
              
              
              }
              }
    return 0;
}
int main()
{
	int n,m,i,j,k;
	int a[300];
	for(k=1;;k++){
	scanf("%d %d\n",&n,&m);
	if(n==0&&m==0)
		break;
	for(i=0;i<n;i++)
		a[i]=i+1;
	i=0;
	while(n!=0)
	{
		i=i+m-1;
	for(j=i%n;j<n-1;j++)
		a[j]=a[j+1];
		i=i%n;
		n--;
	}
	printf("%d\n",a[0]);
	}
	return 0;
}
int main()
{
	int n,m,i,j,a[301],c=1,b[100],k=0,d;
	cin>>m>>n;
	while(m!=0)
	{
	for(i=1;i<=m;i++)
	    {a[i]=i;}
	for(j=0;j<(m-1);j++)
	{
		c=(c+n-1)%(m-j);
	    if(c==0) c=m-j;
	    for(i=c;i<(m-j);i++)
		{a[i]=a[i+1];}
	}   
	    b[k]=a[1];
		k=k+1;
		cin>>m>>n;
		c=1;
	}
	for(d=0;d<k;d++)
	{cout<<b[d]<<endl;
	 cout<<endl;}	
	return 0;
}
int ysf(int n,int m)
{int i,s=n,h=0;
int a[n+1];
for(i=0;i<=n;i++) a[i]=1;for (i=1;s>1;i++)
{i=i%n;
if (a[i]==0) continue;
else h++;
if (h==m) {s--;a[i]=0;h=0;}
}
for (i=1;i<=n;i++)
if (a[i]==1) return i;
}
int main ()
{int a0,b,c;
while (1)
{scanf ("%d %d",&a0,&b);
if (a0==0 && b==0) break;
c=ysf(a0,b);
printf ("%d\n",c);
}
return 0;
}
void main()
{
	int n,m,i,j,a[300];
	int *p,*end;
	scanf("%d%d",&n,&m);
	while(m||n)
	{
		if(n==1)
		{printf("1\n");
		goto loop;
		}
		end=a+n;
		for(p=a;p<end;p++)
			*p=1;
		p=a-1;
		for(i=1;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				p++;
				if(p>=end)p-=n;
				if(*p==0)j--;
			}
			*p=0;
		}
		
     for(i=1;i<=n;i++)
			if(a[i-1]!=0)printf("%d\n",i);
loop:scanf("%d%d",&n,&m);
	}

}
struct monkey
{
	int n;
	int m;
};
struct monkey mon[100];
void main()
{    int i=0,j;
     int b[100];
	 int wang(int a,int c);
	do
	{ scanf("%d%d",&mon[i].n,&mon[i].m);
	i++;}while(mon[i-1].n!=0&&mon[i-1].m!=0);
	for(j=0;j<i-1;j++)
	{
		b[j]=wang(mon[j].n,mon[j].m);
	}
	for(j=0;j<i-1;j++)
		printf("%d\n",b[j]);
}
int wang(int a,int c)
{
	int s=0,i;
	for(i=2;i<=a;i++)
		s=(s+c)%i;
	return(s+1);
}
int main()
{
	int n,m;
	do
	{	
		scanf("%d%d",&n,&m);
		if(n==0)
			break;

		int a[301],b[301],i,j,r;
		for(i=1;i<=n;i++)
			a[i]=b[i]=i;
		for(i=1;i<=n-1;i++)
		{
			r=m%(n+1-i);
			if(r!=0)
			{
				for(j=1;j<=n-i;j++)
				{
					if(j!=n+1-i-r)
						a[j]=b[(j+r)%(n+1-i)];
					else
						a[j]=b[n+1-i];
				}
				for(j=1;j<=n-i;j++)
					b[j]=a[j];
			}
		}

		printf("%d\n",a[1]);
	}
	while(n!=0);

	return 0;
}
main()
{
  int m=1,n,i,j,k,r=0,T;
  while(m!=0)
   {
     scanf("%d %d",&m,&n);if(m==0)break;
     int monkey[309];
     for(i=0;i<m;i++)
       monkey[i]=1;
     T=0;
     for(i=1;i<m;i++)
       {
         for(j=1;j<=n;)
           {
             if(monkey[T]==1)
               {j=j+1;}
             T=(T+1)%m;
           }
         monkey[(T-1+m)%m]=0;//printf(" %d",T-1);
       }
     for(i=0;i<m;i++)
        {
          if(monkey[i])
            {
             printf("%d\n",i+1);
             //break;
             }

        }
}}
int main()
{
	int a[301] = {0}, b[301] = {0}, i, n, m, k, t, count;
	while (cin >> n >> m)
	{
		if (n == 0 && m == 0)
		{
			break;
		}
		for (i = 1; i <= n; i ++)
		{
			a[i] = 1;
		}
		k = 0;
		t = 0;
		count = 1;
		while (count <= n)
		{
			k ++;
			if (k == n + 1)
			{
				k = 1;
			}
			if (a[k] != 0)
			{
				t ++;
				if (t == m)
				{
					a[k] = 0;
					b[count] = k;
					count ++;
					t = 0;
				}
			}
		}
		cout << b[n] << endl;
	}
	return 0;
}
int main()
{
	int a[301],n,m;
	while(1)
	{
		int i,j=0,k=0;
	cin>>n>>m;
	if(n==0)
		break;
	for(i=1;i<=n;i++)
	    a[i]=1;
	for(i=1;i<=n;i++)
	{
			j=j+a[i];
			if(j==m)
			{
				a[i]=0;
				j=0;
				k++;
			}
		if(k==n)
		{
			cout<<i<<endl;
			break;
		}
		if(i==n)
			i=0;
	}
	}
	return 0;
}
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(void)
{
  int m[100],n[100],i=0,z;
  while(1){
    scanf("%d%d",&n[i],&m[i]);
    if(m[i]==0&&n[i]==0) break;
    i++;
    z=i;
  }
  for(i=0;i<z;i++){
    printf("%d\n",f(m[i],n[i])+1);
  }
  return 0;
}
int main(){
   int n[300],m[300],i,j;
   for(i=0;;i++){
      scanf("%d %d",&n[i],&m[i]);
      if(n[i]==0){
         break;
      }else{
         int f[300];
         f[1]=0;
         for(j=1;j<=n[i];j++){
            f[j+1]=(f[j]+m[i])%(j+1);
         }
         printf("%d\n",f[n[i]]+1);
      }
   }
   return 0;
}
/*?????????????????????????????????????????????????????????????????????1???????????????????????????????????????????????????*/
//Goofy 2010.11.9
int main()
{
	int n[300], N, m, temp1, temp2, i, k;//temp1,????temp2??????
	for(;;)//???????00
	{
		for ( i=0; i<300; i++)//???????
			n[i]=i+1;
		cin>>N>>m;
		if ( N!=0 && m!=0)
		{
			for(;;)//?????
			{
				if ( N!=1)
				{
					temp1=m % N-1;//??????
					if ( temp1 != -1)
					{
						for ( i=temp1; i<N; i++)//???????????.????
							n[i]=n[i+1];
						N--;
						for ( i=0; i<N-temp1; i++)//?????N-1-temp1???
						{
							temp2=n[N-1];//??????
							for ( k=N-1; k>0 ; k--)//??????
								n[k]=n[k-1];
							n[0]=temp2;
						}
					}
					else
						N--;
				}
				else
					break;
				cout<<endl;
			}
			cout<<n[0]<<endl;;
		}
		else
			break;
	}
	return 0;
}
int main(){
	int n,m;
	while(true)
	{
		scanf("%d%d",&n,&m);
		if(n==0 && m==0) break;
		int out[301];
		for(int i=0; i<n; i++) out[i] = 0;
		int cur_id = -1;
		// ??n-1???
		for(int i=0; i<n-1; i++) {
			int count = 0;
			while(count < m) {
				cur_id = (cur_id+1) % n;
				if(out[cur_id] == 0) {
					count += 1;
				}
			}
			out[cur_id] = 1;
		}
		for(int i=0; i<n; i++) {
			if(out[i] == 0) {
				printf("%d\n", i+1);
			}
		}
	}
	return 0;
}
int main()
{
	int king(int n,int m);
	int n,m,i,cycle;
         cycle=0;
	int k[30]={0};
	while(scanf("%d %d",&n,&m))
	{
		if((n!=0)&&(m!=0))
		{
			k[cycle]=king(n,m);
			cycle++;		
		}
		else
			break;
	}

	for(i=0;i<cycle;i++)
		printf("%d\n",k[i]);

	return 0;
}


int king(int n,int m)
{
	int s,b,i,j,k;
    int*a=(int*)malloc(sizeof(int)*n);
         s=0;
         b=1;
         i=0;
	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=s+a[j];
	 }

	 for(i=0;i<n;i++)
	 {
		 if(a[i]==1)
			 k=i+1;	
	 }
	 return k;
	 free (a);
}
int main()
{
    int m,k,n,a[300],i,j,h;
    scanf("%d%d",&n,&m);
    do
    {
    for(j=1;j<=n;j++)
        a[j]=j;
        k=1;
        for(i=1;i<=n-1;i++)
        {
            k=k+m-1;
            while(k>(n-i+1))
            k=k-n+i-1;
            if(k==(n-i+1))
                a[k]=a[k+1];
            else
            {
            for(h=k;h<=n-i;h++)
            a[h]=a[h+1];
            }
        }
    printf("%d\n",a[1]);
        scanf("%d%d",&n,&m);
    }
    while(n!=0);
    return 0;
}
int main()
{
  int n, m, i, s=0;
  while(cin>>n>>m && n!=0 && m!=0)
  {
          s=0;
          for(i=2; i<=n; i++) 
              s=(s+m)%i;
          cout<<s+1<<endl;
  }
  return 0;
}
int main()
{
    int m[1000],n[1000],i,j,k,s=0,d,w[1000],c,g,f,v,e;
   for(k=0;;k++)
{
    scanf("%d %d",&n[k],&m[k]);
   c=n[k]+1;
    int a[c];
    for(i=0;i<=n[k];i++)
    a[i]=1;
    j=0;i=n[k]-1;v=0;
    while(v<n[k]-1)
    {if(j==m[k])
     {
          a[i]=0;
          j=0;
          v++; 
             
     }
     else 
     {     i++;
     if(i==n[k])
      i=i-n[k];
      j=j+a[i];
      }
     
     
     }
     for(i=0;i<n[k];i++)
     if(a[i]==1)
     w[k]=i+1;
      g = n[k];
      f = m[k];
      if(g==0&&f==0)
      {e=k;
      break;} 
}
      for(k=0;k<e;k++)
     printf("%d\n",w[k]);
    getchar();
     getchar();
}
int f(int m,int n)
{
    int a;
    if(n==1) a=1;
    else a=(f(m,n-1)+m)%n;
    if(a==0) a=n;
  
    return a;
}
    
main()
{
    int i,m,n;
    for(i=1;;i++)
    {
          scanf("%d %d",&n,&m);
          if(m!=0) printf("%d\n",f(m,n));
          else     break;
    }
 
}
int f(int n, int m)
{
  if (n == 1)
  {
    return 1;
  }
  else
	  return (f(n - 1, m) + m - 1) % n + 1;
}
int main()
{
	int i,m, n,a[20000]={0},c=0;
  for(i=1;i<20000;i++)
  {
	  scanf("%d %d",&n,&m);
	  if(n!=0&&m!=0)
	  {
		   a[i]=f(n,m);
		   c++;
	  }
	  else
		  break;
  }
  for(i=1;i<=c;i++)
  {
	  printf("%d\n",a[i]);
  }
  return 0;
}
int main()
{
	int a,b;
	int i,j,k;
	int x[300]={0},y[300]={0};
	for(;;){
		scanf("%d%d",&a,&b);
		if(a==0)
			break;
		else{
			int n=a;
			for(i=0;i<n;i++){
				x[i]=i+1;
			}
			do{
				j=b%n;
				for(i=0;i<n-1;i++){
					y[i]=x[(j+i)%n];
				}
				for(i=0;i<n-1;i++){
					x[i]=y[i];
				}
				n=n-1;
			}
			while(n>1);
			printf("%d\n",x[0]);
		}
	}
	return 0;
}
int main()
{
	int n, m, a[301] = {0}, k, i, j;
     while(cin >> n >> m && n != 0)
	 {
        for(i = 1; i <= n; i++)
		    a[i] = 1;
	    j = 0;
		k = 0;
		for (i = 1; i <= n+1; i++)
		{
			if (a[i] == 1)
				j = j+ a[i];
			if (j == m)
			{
				j =0;
				a[i] =0;
				k++;
			}
			if (k == n)
			{
				cout << i << endl;
				break;
			}
			if (i == n+1)
				i =0;
		}

	 }
	return 0;
}
void main()
{
	int a[100][300],b[100],c[100],i=0,j=0,k=0,m=0,n=0;
	do{
		scanf("%d %d",&b[i],&c[i]);
		i++;
	}
	while(b[i-1]!=0&&c[i-1]!=0);
	m=i-1;
	for(i=0;i<m;i++)
	{
		for(j=1;j<=b[i];j++)
		{
			a[i][j-1]=j;
		}
	}
	for(i=0;i<m;i++)
	{
		n=b[i];
		for(j=0,k=0;n>1;j++)
		{
            if(j>=b[i]) j=j-b[i];
			if(a[i][j]!=0) k++;
			if(k==c[i])
			{
				a[i][j]=0;
				n=n-1;
				k=0;
			}	
		}
	}
	for(i=0;i<m;i++)
		for(j=0;j<b[i];j++)
			if(a[i][j]!=0) printf("%d\n",a[i][j]);
	
}
int aLoop[MAX_NUM+10];
main()
{
	int n,m,i;
	while(1){
		scanf("%d%d",&n,&m);
		if(n==0)
			break;
		for(i=0;i<n;i++)
			aLoop[i]=i+1;
		int nPtr=0;
		for(i=0;i<n;i++){

			int nCounted=0;
			while(nCounted<m){
				while(aLoop[nPtr]==0)
					nPtr=(nPtr+1)%n;
				nCounted++;
				nPtr=(nPtr+1)%n;
			}
			nPtr--;
			if(nPtr<0)
				nPtr=n-1;
			if(i==n-1)
				printf("%d\n",aLoop[nPtr]);
			aLoop[nPtr]=0;
		}
	}
}