semeru/Code-Code-CloneDetection-POJ104 huggingface dataset:
int main()
{
	int n,m;
	int i,j,a,b,sum;
	int sz[300];
	for(i=0;;i++){
		scanf("%d %d",&n,&m);
		if(n==0 && m==0){
			break;
		}else{
			sum=0;
			for(j=0;j<n;j++){
				sz[j]=j+1;
			}
			for(a=0,b=1;sum!=n-1;){
				if(b==m){
					sz[a]=0;
					sum++;
					b=0;
				}
				if(a!=n-1){
					a++;
				}else{
					a=0;
				}
				if(b!=m && sz[a]!=0){
					b++;
				}
			}
			for(a=0;a<n;a++){
				if(sz[a]!=0){
					printf("%d\n",sz[a]);
				}
			}
		}
	}
	return 0;
}
int f(int a,int n);
void main()
{
	int m,n,i,cut,count,a[400]={0};
	for(i=0;i<100000;i++)
	{
		scanf("%d%d",&n,&m);
		if(m==0&&n==0)
			break;
		else if(m==1)
			printf("%d\n",n);
		else 
        {
			for(i=1;i<=n;i++)
			{
				a[i]=1;
			}
			for(cut=1,count=n;count>1;count--)
			{
				for(i=1;i<=m-1;i++)
				{
					cut=f(cut+1,n);
					while(a[cut]==0)
					{
						cut=f(cut+1,n);
					}
				}
				a[cut]=0;
				while(a[cut]==0)
				{
					cut=f(cut+1,n);
				}
			}
			printf("%d\n",cut);
		}
	}
	
}
int f(int a,int n)
{
	if(a%n==0)
		return n;
	else 
		return a%n;
}
int main()
{
   int n,m,i,s;
   do
   {
       cin>>n>>m;
       if(n==0||m==0) break;
       else if(n==1) cout<<"1"<<endl;
       else
	   { s=0;
	     for (i=2; i<=n; i++) 
                s=(s+m)%i;
             printf ("%d\n",s+1);
	   }
   }
   while(n!=0&&m!=0);
   return 0;
}
int main()
{  int i,j,k,n,m,a[400],b[400];
   n=1;
   for (;n!=0;)
   {
   scanf("%d %d",&n,&m);
   if (n!=0){
   for (i=1;i<=n;i++) a[i]=i;
   k=m;
   if (k>n && k%n!=0) k=k%n;
   if (k>n && k%n==0) k=n;   
   
   for(;n!=1;)
     {for (i=k;i<n;i++)
        a[i]=a[i+1];

     n=n-1;
  //  for (i=1;i<=n;i++) printf("%d",a[i]);
  //   printf("end\n");
     k=k+m-1;
     if (k>n && k%n!=0) k=k%n;
     if (k>n && k%n==0) k=n;      
     
    
 //    printf("%d %d\n",n,k);
     }

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

}}    
 
    return 0;
}
int main(){
    int a[3000],b[3000],c[3000],x=0,i,j,s;
    for(j=0;j<3000;j++){ 
         s=0;
       scanf("%d%d",&a[j],&b[j]);
      if(a[j]==0) break;
       else{
         for (i=2; i<=a[j]; i++) s=(s+b[j])%i;
           x++;
           c[j]=s+1;
           } 
      }
      for(j=0;j<x;j++){
         printf("%d\n",c[j]);
      }
       return 0;
   }
int main()
{
    int m[100],n[100],s=0;
	int index=0;//????
    int loc;//????
    int t=0;
    int p;//?????
    for (s=0;s<=1000;s++) 
	{
		scanf("%d%d",&m[s],&n[s]);
		if ((m[s]==0)&&(n[s]==0)) break;
		int a[100][1000];
		for(int i=0;i<m[s];i++)
		{
			a[s][i]=1;
		}
        t=0;
	    index=0;
	    p=m[s];
        while(p>0)
		{
			if(a[s][index]==1)
			{   
                 ++t;
                 if(t%n[s]==0)
				 {
                       a[s][index]=0;
                       loc=index;
                       p--;
				 }
			}
            if(index < m[s]-1)
               index++;
            else
               index=0;
		}
        printf("%d\n",loc+1);
	}
	return 0;
}
int hs(int n,int m){
int i,k=0;
for(i=2;i<=n;i++){
	k=((m%i)+k)%i;}
return k;
}	
int main(){
	int m,n,h=0;
	int a[100],j=0,y=0;
	while(h==0){
	scanf("%d %d",&n,&m);
	if((n==0)&&(m==0)){
	h=1;
	}
	a[j]=hs(n,m)+1;
		j++;
		y+=1;
		}
	for(j=0;j<y-1;j++){
	printf("%d\n",a[j]);}
	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(){
int m,n,c[300],d,j;
for(j=0;j>-1;j++){
scanf("%d %d",&n,&m);
if(m==0) {d=j;  break;}
c[j]=f(m,n)+1;}

for(j=0;j<d;j++)
{printf("%d\n",c[j]);}

return 0;
}
void hou(int n,int m){
	
	
	int a[300];
	int *p,i,k=0,o=0;
	
	p=a+1;
	for(i=1;i<=n;i++)
		*(p++)=i;
	p=a+1;
	while(o<n-1){
		
		if(*p!=0)k++;
		if(k==m){
			*p=0;
			k=0;
		    o++;}//*???N???????????N?????
		if(p<a+n)p++;
		else p=a+1;}
		
	p=a+1;
	while(*p==0)p++;
	printf("%d\n",*p);
}
	
void main(){
	int a[20][2];
	int i,j;
	for(i=0;i<20;i++){
		scanf("%d%d",&a[i][0],&a[i][1]);
		if((a[i][0]==0)&&(a[i][1]==0))break;
	}
	for(j=0;j<i;j++)
		hou(a[j][0],a[j][1]);
}
int monkey(int n1,int m1);
int main()
{
	int m[301],n[301],j,i=0;
	while (1)
	{
		scanf("%d%d",&n[i],&m[i]);
		if(m[i]==0&&n[i]==0)
			break;
		i++;
	}
	for(j=0;j<i;j++)
	{
		printf("%d\n",monkey(n[j],m[j]));
	}
	return 0;
}
int monkey(int n1,int m1)
{
	int res[301],k;
	res[0]=0;
	res[1]=0;
	for(k=2;k<=n1;k++)
	res[k]=(res[k-1]+m1)%k;
	return res[n1]+1;
}
int main(){
    int a[300];
    int j,i,n,m,b,k,start,delenum;
    while(1){
    scanf("%d %d",&n,&m);
    if(m==0&&n==0){
        break;
    }
    b=n;
    for(i=0;i<b;i++){
        a[i]=i+1;
    }
    start=0;
    for(k=1;k<b;k++){
        delenum=(start-1+m)%n;
        for(j=delenum;j<n-1;j++){
            a[j]=a[j+1];
        }
            start=delenum;
            n--;
    }
    printf("%d\n",a[0]);
}
return 0;
}
int main()
{
    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)                                                    //??0?????
        {
            break;
        }
        num[row][0]=1;
        //?????????

        if(sz[row][0]==1)
        {
            result[row] = 1;
        }
        for(i=1; i<sz[row][0]; i++)
        {

            num[row][i]=(num[row][i-1]+sz[row][1])%(i+1);
            //n????????=??n-1????????+m??n??

            if(num[row][i]==0)                                                    //????=0?????n
            {
                num[row][i]=i+1;
            }
            result[row]=num[row][i];
            //?????result???
        }
        printf("%d\n",result[row]);

    }
    return 0;
}
int main()
{
	do
	{
		int n, m, a[500], flag, i, num = 0; 
		cin>>n>>m;
		flag = n;
		if(n == 0 && m == 0)
			break;
		for (i = 1; i <= n; i++)
		{
			a[i] = 1;
		}
		for (i = 1; i <= n; i++)
		{
			if (a[i] != 0)
			{
				num++;
				if (num == m)
				{
					if (flag != 1)
					{
						a[i] = 0;
						num = 0;					
						flag -= 1;
					}
					else
						break;
				}
			}
			if (i == n)
				i = 0;

		}

		for (i = 1; i <= n; i++)
		{
			if (a[i] == 1)
				cout<<i<<endl;
		}
	}while(1);
	return 0;
}
struct node
{
	int a;
	struct node *next;
};
void main()
{
	struct node *head;
	struct node *p1,*p2;
	int i;
	int n,m;
	while(1)
	{
		scanf("%d %d",&n,&m);
		if(n==0) 
			break;
		head=(struct node *)malloc(sizeof(struct node));
		p1=head;
		p1->a=1;
		for(i=2;i<=n;i++)
		{
			p2=(struct node *)malloc(sizeof(struct node));
			p2->a=i;
			p1->next=p2;
			p1=p2;
		}
		p1->next=head;
		while(p1->next!=p1)
		{
			for(i=1;i<m;i++)
				p1=p1->next;
			p1->next=p1->next->next;
		}
		printf("%d\n",p1->a);
	}

}
int ss(int a);
int main()
{
    int i=0,n,m,sum=0,count=0;
    int b[10000];
    for(i;;i++)
    {
        scanf("%d%d",&n,&m);
        if(n==0&&m==0){
            break;}
        int a[10000],p=0,j=0,k=0,number=0;
        for(j=0;j<n;j++)
        {
            a[j]=1;
        }
        while(number<n-1)
        {
            if(a[p]!=0)
            {
                k++;
                if(k==m)
                {
                    a[p]=0;
                    k=0;
                    number++;
                }
            }
            p=(p+1)%n;
        }
        for(j=0;j<n;j++)
        {
            if(a[j]!=0)
            {
                b[i]=j+1;
                count++;
            }
        }
    }
    for(i=0;i<count;i++)
    {
        printf("%d",b[i]);
        if(i!=count-1)
        printf("\n");
    }    
   
    return 0;
}
int main()
{
	int n=1,m=1;
	int res[100];
	int t=0;
    
    for(;t<100;t++){
            res[t]=0;
            }
    t=0; 
	
	for(;n!=0&&m!=0;){
                      
	scanf("%d%d",&n,&m);

	struct per{
		int a;
		int b;
	}*p;
        p=(struct per *)malloc(sizeof(per)*n);

	int i;

	for(i=0;i<n;i++){
		(p+i)->a=i+1;
		(p+i)->b=i+1;
	}

	int *out;
	out=(int *)malloc(sizeof(int)*n);

	for(i=0;i<n;i++){
		out[i]=0;
	}

	int r=n;
	int j=0;
	

	for(;out[n-1]==0;){
                                   
	    	    
		for(i=0;i<n;i++){
                         
            if((p+i)->b==0){
				continue;
			}
                         
			else if((p+i)->b-m*j==m){
				out[j]=(p+i)->a;
				(p+i)->b=0;
				j++;
			}
			else{
				(p+i)->b+=(n-j);
			}
		}		
	}
	
                   if(n!=0&&m!=0){
                   res[t]=out[n-1];

                   t++;}
         free(p);
	free(out);


        }
    
    for(t=0;res[t]!=0;t++){
                           printf("%d\n",res[t]);
    }

	return 0;
}
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;
		if(n==1) {
		printf("1\n");continue;}
		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-1;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)];}}
		if(m%2==1)printf("%d\n",a[n-2][1]);
		else{printf("%d\n",a[n-2][0]);}
			
			}
	

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

void main()
{
	int i,j,t,a,n[301],m[301],r[300];
	struct student *head;
	struct student *p1,*p2;
	for(t=0;;t++)
	{
		scanf("%d %d",&n[t],&m[t]);
		if(n[t]==0 && m[t]==0)
			break;		
	}
	for(i=0;i<t;i++)
	{
		a=1;
		p1=p2=(struct student *)malloc(LEN);
		p1->num=a;
		head=p1;
		while(a<n[i])
		{
			if(a==1) head=p1;
			else p2->next=p1;
			p2=p1;
			p1=(struct student *)malloc(LEN);
			p1->num=a+1;
			a++;
		}
		p2->next=p1;
		p1->next=head;
		for(;n[i]>1;n[i]--)
		{
			p1=p2=head;
			for(j=1;j<m[i];j++)
			{
				p2=p1;
				p1=p1->next;
			}
			p2->next=p1->next;
			head=p1->next;
		}
		r[i]=head->num;
	}
	for(i=0;i<t;i++)
	{
		printf("%d\n",r[i]);
	}
}
main()
{
      int m,n,a[301],c,d=0,e=0,N;//e ???? 
      for (N=0; ;N++)
 {
      scanf("%d %d",&n,&m);
      if (n==0)
      break;
      else ;
      for(c=1;c<=n;c++)
       a[c]=1;
       c=0;
       e=0;
     while (e<n-1)
      { d=0;
        while(d<m)
           { 
            c++;
            if (c==n+1)
            c=1;
            else ;
            if (a[c]==1)
            d++;
            else ;
           }
        a[c]=0;
        e++;
      }
      //for (c=1;c<=n;c++)
      // printf("%d\n",a[c]);
      for (c=1;c<=n;c++)
      {
          if (a[c]==1)
          {printf("%d\n",c);break;}
          else ;
          }
 }
}
int main(){
	int n=1,m=1,a[301],b[301],i,c,d,e[301],f=0;
	while(n!=0&&m!=0){
	scanf("%d%d",&n,&m);
	if((n==0)&&(m==0)){break;}
	f++;
	for(i=0;i<n;i++){
		a[i+1]=i+1;
	}
	for(i=1;i<n;i++){
		c=m%(n-i+1);
		for(d=1;d<=(n-i+1-c);d++){
			b[d]=a[c+d];
		}
		for(d=1;d<c;d++){
			b[n-i+1-c+d]=a[d];
		}
		for(d=0;d<(n-i);d++){
			a[d+1]=b[d+1];}
	}
	e[f]=a[1];
	}
	for(d=1;d<=f;d++){
		printf("%d\n",e[d]);
	}
	return 0;
}
int read();
void circle(int t);
void print();
void baoshu(int p,int q);
int n[100],m[100],a[300],ans[100],num,i;
int main()
{
    read();
    for (i=1;i<=num;i++)
    {
        circle(i);
    }
    print();
    return 0;
}
int read()
{
    i=1;
    scanf("%d%d",&n[i],&m[i]);
    while (n[i]!=0&&m!=0)
    {
        i++;
        scanf("%d%d",&n[i],&m[i]);
    }
    num=i-1;
    return 0;
}
void circle(int t)
{
    int j;
    for (j=1;j<=n[t];j++)
    {
        a[j]=1;
    }
    baoshu(n[t],1);
    return;
}
void baoshu(int p,int q)
{
    int zz,j,k,z;
    if (p==1)
    {
        ans[i]=q;
        return;
    }
    else{
    zz=q;
    j=1;
    while (j<m[i])
    {
        j++;
        if (zz<n[i])
        {
            z=zz;
            for (k=zz+1;k<=n[i];k++)
             {
               if (a[k]==1){
                   zz=k;
                   break;
               }
             }
             if (z==zz)
             {
               for (k=1;k<=zz;k++) 
               {
                  if (a[k]==1){
                      zz=k;
                      break;
                  }
               }
             }
        }else {
            for (k=1;k<=n[i];k++) 
            {
               if (a[k]==1){
                   zz=k;
                   break;
               }
            }
        }
    }
    a[zz]=0;
    z=zz;
    for (k=z;k<=n[i];k++)
             {
               if (a[k]==1){
                   zz=k;
                   break;
               }
             }
    if (z==zz)
    {
      for (k=1;k<z;k++)
             {
               if (a[k]==1){
                   zz=k;
                   break;
               }
             }
    }
    baoshu (p-1,zz);
  }
  return;
}
void print()
{
    for (i=1;i<=num;i++)
    {
        printf("%d\n",ans[i]);
    }
    return;
}
void main()
{
	int n,m,i,j;
	int a[500];
	while (1)
	{
	scanf("%d%d",&n,&m);
	if (n==0&&m==0) break;
	for (i=0;i<n;i++)
	{
		a[i]=i+1;
	}
    j=1;
	while(n!=1)
	{
		j--;
		for (i=1;i<=m;i++)
		{
			if (j+1>n) j=1;
			else j++;
		}
		for (i=j;i<=n-1;i++)
			a[i-1]=a[i];
		n--;
	}
	printf("%d\n",a[0]);
	}
}
int main(void)
{   int f[100],h[100];
    int m,n,b,c,d,e,g,i,j;
    i=0;
    scanf("%d%d",f+i,h+i);
    while(f[i]!=0&&h[i]!=0)
    {i++,scanf("%d%d",f+i,h+i);}
    
    
    g=0;
    while(f[g]!=0&&h[g]!=0)
    {n=f[g];
     m=h[g];
    int a[n];
    for(i=0;i<n;i++)
     {a[i]=i+1;}
    for(j=0;j<n-1;j++)
     {
      b=m%(n-j);
      if(b==0)
       {b=n-j;c=0;
      while(a[c]!=b)
       c++;
       a[c]=0; 
       }
      else
      {
      c=0;
      while(a[c]!=b)
       c++;
      a[c]=0;
      for(d=c+1,i=1;d<n;d++)
        {
        if(a[d]!=0)
         {a[d]=i;i++;}
        }
      for(e=c-1,i=n-j-1;e>=0;e--)
        {
        if(a[e]!=0)
         {a[e]=i;i--;}
        }
      }     
     }
     i=0;
     while(a[i]!=1)
       i++;
    printf("%d\n",i+1); 
    g++;
    }
          
}
//******************************************
//*???????.cpp                     **
//*??: ??     1100012808              **
//*???2011.12.17                       **
//******************************************
int main()  //???
{        //?????
	int flag[301] = {0}, i, n, j = 0, m, count, k, t;
	while (cin >> n)
	{
		memset(flag, 0, sizeof(flag));
		cin >> m;
		j = 0;
		if (m == 0 && n == 0)
			break;
		if (m == 1)
		{
			cout << n << endl;
			continue;
		}
		for (i = 1; i <= n; i++)
		{
			count = 0;
			if (flag[i] == 0)
			{
				j++;
				if (j == m)
				{
					j = 0;
					flag[i] = 1;
				}
			}
			if (i == n)
			{
				i = 0;
				for (k = 1; k <= n; k++)
					if (flag[k] == 0)
					{
						count++;
						t = k;
					}
				if (count == 1)
					break;
			}
		}
		cout << t << endl;
	}
	return 0;
} //?????
int main()
{
    int n,m,i,j,pos,f[400];
    while (scanf("%d %d",&n,&m),n|m)
    {
        memset(f,0,sizeof(f));
        pos=0;
        for (i=1;i<=n;i++)
        {
            j=0;
            while (j<m)
            {
				pos++;
				if (pos>n)pos=1;
                if (f[pos]==0)
                    j++;
                
            }
            //printf("!%d\n",pos);
            f[pos]=1;
        }
        printf("%d\n",pos);
    }
    return 0;
}
void main()
{
	int a[400]={0};
	int m,n,i,out,sum;

	while(1){
		scanf("%d %d",&n,&m);
		if(!m*!n) break;
		out=m;
		sum=n;
		for(i=1;i<=n;i++) a[i]=1;
		for(i=1;sum>1;){
			
			if(!a[i]){
				i++;
				if(i>n) i=1;
				continue;
			}
			else out--;
			if(!out){
				a[i]=0;
				sum--;
				out=m;
			}
			i++;
			if(i>n) i=1;
		}
		for(i=1;i<=n;i++) if(a[i]) break;
		printf("%d\n",i);


	}
}
int main()
{
	int m,n,a[1000],l,i,j;
	while(1)
	{ scanf("%d %d",&n,&m);
	if((n==0)&&(m==0))
		break;
    for(i=1;i<=n;i++)
		a[i]=i;
	l=(m-1)%n+1;
	for(i=1;i<=n-1;i++)
	{    for(j=1;j<=n;j++)
	  if(j>=l)a[j]=a[j+1];
	  l=(l+m-2)%(n-i)+1;
	  
	  }
	printf("%d\n",a[1]);
	}
	return 0;
}
int main()
{
	int n, m, houzi[301], k, i, number;
	while (cin >> n >> m && n != 0)
	{
		number = n;
		k = 0; i = 0;
		for (int j = 1; j <= n; j++)
		{
			houzi[j] = 0;
		}
		while(number > 0)
		{
			k++;         
			i++;
			while (houzi[i]==1)
			{
				i++;
			}
			if (i > n)
			{
				i = 0;
				k--;
				continue;
			}
			if ((k == m) && (houzi[i]==0))
			{
				houzi[i] = 1;  
				number--;
				k=0;
				if (number == 0)
				{
					cout << i << endl;
				}
			}
		}
	}
	return 0;
}
int main()
{
	
	int n[100];
	int m[100];
	int i,t=0;
	int j;
	
	for(i=0;i<=100;i++)
	{
		scanf("%d",&n[i]);
		scanf("%d",&m[i]);
		t++;
		if(n[i]==0&&m[i]==0)
		{
			break;
		}
	}
	for(i=0;i<t-1;i++)
	{
		int a[300];
		for (j=0;j<n[i];j++)
		{
			a[j]=j+1;
		}
		int nPtr=0;
		for(j=0;j<n[i];j++)
		{
			int nCount=0;
			while(nCount<m[i])
			{
				while(a[nPtr]==0)
				{
					nPtr=(nPtr+1)%n[i];
				}
				nCount++;
			    nPtr=(nPtr+1)%n[i];
			}
			nPtr--;
			if(nPtr<0)
			{
				nPtr=n[i]-1;
			}
			if(j==n[i]-1)
			{
				printf("%d\n",a[nPtr]);
			}
			a[nPtr]=0;
		}
		
	}
	
	return 0;

}
int main() {
     int m,n,i,j,s;
     int a[301];
    while(1){
        scanf("%d%d",&n,&m);
     if(m==0&&n==0)break;
           s=n;
      for(i=1;i<=n;i++){
         a[i]=0;
  }
         i=0;j=0;
while(i<n){
    i++;
     if(a[i]==1){
         if(i==n)
            i=0;
       continue;
    }
   j++;
  if(j==m){
        a[i]=1;
        s--;
        j=0;
      }
      if(i==n)i=0;
         if(s==1)break;
       }
      for(i=1;i<=n;i++){
           if(a[i]==0) printf("%d\n",i);
       }
   }
     return 0;
   }
void main()
{
	int baoshu(int,int);
	int i,x;        
	int n[50]={0},m[50]={0},w[50]={0};
	for(i=0;;i++)
	{
		scanf("%d %d",&n[i],&m[i]);
		if((n[i]==0)&&(m[i]==0))
			break;
	}
	x=i;
	for(i=0;i<x;i++)
	{
		w[i]=baoshu(n[i],m[i]);
	}
	for(i=0;i<x;i++)
		printf("%d\n",w[i]);
}
int baoshu(int n,int m)
{
	int i,k,r;                       //k:???????,r:???????
	int num[300]={0},*p;
	p=num;
	for(i=0;i<n;i++)
		*(p+i)=i+1;
	i=0;
	k=0;
	r=0;
	while(r<n-1)
	{
		if(*(p+i)!=0)
			k++;
		if(k==m)
		{
			*(p+i)=0;
			k=0;
			r++;
		}
		i++;
		if(i==n)
			i=0;
	}
	while(*p==0)
		p++;
	return(*p);
}
int main() 
{
	int n,m,counter=0,num,a[300],i;
	cin>>n>>m;	
	do
	{
		for (i=0;i<300;i++)
			a[i]=1;
		num=n;
		i=-1;
		while (num>1)
		{
			while (counter<m)
			{
				i++;
				if (i>=n) i=0;
				if (a[i]==1) counter++;
			}
			a[i]=0;
			num--;
			counter=0;
		}
		for (i=0;i<n;i++)
			if (a[i]!=0) cout<<(i+1)<<endl;
		cin>>n>>m;
	}
	while (n!=0 && m!=0);
	return 0;
}
int main()
{
	int c,outnum=0,m=1,n,i,a[300];;
	
	while(m!=0)
		{
			scanf("%d %d",&n,&m);
			if(m==0)break;
			else
			{
				for(i=0;i<300;i++)
				{
					a[i]=0;
				}
			outnum=0;c=0;
			while(outnum<n-1)
	{
		for(i=0;i<=n-1;i++)
		{
			if(a[i]==0)
				{c++;
			if(c==m)
			{
				a[i]=1;
				outnum++;
				c=0;
			}}}}
	for(i=0;i<=n;i++)
	{
		if(a[i]==0)
		{
			if(m==1)printf("%d\n",i);
			else printf("%d\n",i+1);
			break;
		}
	}
	}
	}

	return 0;
}
//**********************************
//*  ????homework_9.cpp        *
//*  ??????1200012884        * 
//*  ?????2012?10?31?      *
//*  ??????????          *
//**********************************

int main()
{
	int n, m, i, count, temp;
	int a[10000];
	cin >> n >> m;
	while (n != 0 || m != 0)
	{
		memset(a, 0, sizeof(a));
		count = n;
		i = 1;
		while (count != 1)
		{
			temp = 0;
			while (temp != m - 1)
			{
				if (a[i] == 0)
					temp++;
				i++;
				if (i > n)
					i -= n;
			}
			while (a[i] == 1)
			{
				i++;
				if (i > n)
					i -= n;
			}
			a[i] = 1;
			count--;
		}
		for (i = 1;i <= n;i++)
			if (a[i] == 0)
				cout << i << endl;
		cin >> n >> m;
	}
	return 0;
}
int main()
{
	int year1,month1,day1;
	int year2,month2,day2;
	cin>>year1>>month1>>day1;
	cin>>year2>>month2>>day2;
	int days=0;
	int d1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	int d2[12]={31,29,31,30,31,30,31,31,30,31,30,31};
	if(year1==year2)
	{
		if((year1%4==0&&year2%100!=0)||year1%400==0)
		{
				if(month1==month2)
				 days+=day2-day1;
				else
				{
				days+=d2[month1-1]-day1+d2[month2-1]-day2;
				for(int i=month1;i<month2-1;i++)
				days+=d2[i];
				}
		 }
	    else 
		{	
			if(month1==month2)
		    days+=day2-day1;
		    else	
			{
			    days+=d1[month1-1]-day1+d1[month2-1]-day2;
                 for(int i=month1;i<month2-1;i++)
				days+=d1[i];
			}
		}
	}
	else
	{
		if((year1%4==0&&year1%100!=0)||year1%400==0)
		{
			days+=d2[month1-1]-day1;
            for(int i=month1;i<12;i++)
				days+=d2[i];
		}
		else 
				{
			days+=d1[month1-1]-day1;
            for(int i=month1;i<12;i++)
				days+=d1[i];
				}
		if((year2%4==0&&year2%100!=0)||year2%400==0)
		{
			days+=day2;
            for(int i=month2-2;i>=0;i--)
				days+=d2[i];
		}
		else 
				{
			days+=day2;
            for(int i=month2-2;i>=0;i--)
				days+=d1[i];
				}
		for(int i=year1+1;i<year2;i++)
		{
			if((i%4==0&&i%100!=0)||i%400==0)
				days+=366;
			else days+=365;

	}
	}
	cout<<days<<endl;
	
	return 0;
}
void main()
{
	int sy,sm,sd,ey,em,ed,year,a,b,c,sum;
	scanf("%d %d %d\n%d %d %d",&sy,&sm,&sd,&ey,&em,&ed);
    year=sy;
	c=(ey-sy)*365;
	switch(sm)
	{
	case 1:a=sd;break;
	case 2:a=sd+31;break;
	case 3:a=sd+59;break;
	case 4:a=sd+90;break;
	case 5:a=sd+120;break;
	case 6:a=sd+151;break;
	case 7:a=sd+181;break;
	case 8:a=sd+212;break;
	case 9:a=sd+243;break;
	case 10:a=sd+273;break;
	case 11:a=sd+304;break;
	default:a=sd+334;
	}
   switch(em)
	{
	case 1:b=ed;break;
	case 2:b=ed+31;break;
	case 3:b=ed+59;break;
	case 4:b=ed+90;break;
	case 5:b=ed+120;break;
	case 6:b=ed+151;break;
	case 7:b=ed+181;break;
	case 8:b=ed+212;break;
	case 9:b=ed+243;break;
	case 10:b=ed+273;break;
	case 11:b=ed+304;break;
	default:b=ed+334;
	}
   sum=c+b-a;
   while(year<=ey)
	   {
		   sum=sum+((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
		   year=year+1;
	   }
   sum=sum-(((sy % 4 == 0 && sy % 100 != 0 )|| sy % 400 == 0)&&sm>2);
   sum=sum-(((ey % 4 == 0 && ey % 100 != 0 )|| ey % 400 == 0)&&em<=2);
   printf("%d\n",sum);
}
int main()
{
	int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
	int y1,y2,m1,m2,d1,d2;
	int i,j,k,c=0,sum=0;
    cin>>y1>>m1>>d1>>y2>>m2>>d2;
    if(y1!=y2)
	{
	for(i=y1+1;i<=y2-1;i++)
	{
		if(i%4==0&&i%100!=0||i% 400 == 0)
		{
			c++;
		}
	}
	sum=sum+c*366+(y2-y1-1-c)*365;


	if(y1% 4 == 0 && y1% 100 != 0 || y1% 400 == 0)
	{
		for(i=m1+1;i<=12;i++)
		{
			sum=sum+b[i];
		}
		sum=sum+b[m1]-d1;
	}
	else
	{
		for(i=m1+1;i<=12;i++)
		{
			sum=sum+a[i];
		}
		sum=sum+a[m1]-d1;
	}
	
    if(y2% 4 == 0 && y2% 100 != 0||y2% 400 == 0)
	{
		for(i=1;i<=m2-1;i++)
		{
			sum=sum+b[i];
		}
		sum=sum+d2;
	}
	else
	{
		for(i=1;i<=m2-1;i++)
		{
			sum=sum+a[i];
		}
		sum=sum+d2;
	}
	
	}




	else
	{
		if(y1% 4 == 0 && y1% 100 != 0 || y1% 400 == 0)
	    {
		for(i=m1+1;i<=m2-1;i++)
		{
			sum=sum+b[i];
		}
		sum=sum+b[m1]-d1+d2;
		if(m1==m2)
		{
			sum=sum-b[m1];
		}
	
		}
		else
	    {
		for(i=m1+1;i<=m2-1;i++)
		{
			sum=sum+a[i];
		}
		sum=sum+a[m1]-d1+d2;
		if(m1==m2)
		{
			sum=sum-a[m1];
		}
	
	    }
		
	
	}




cout<<sum<<endl;
	
		
	return 0;
}
int main()
{
int year1,year2,month1,month2,day1,day2,sum1=0,sum2=0,leap1,leap2,c1,c2,a,b,x;
scanf("%d %d %d\n%d %d %d", &year1,&month1,&day1, &year2, &month2, &day2);
{
	switch(month1)
	{
case 1:sum1=0;break;
case 2:sum1=31;break;
case 3:sum1=59;break;
case 4:sum1=90;break;
case 5:sum1=120;break;
case 6:sum1=151;break;
case 7:sum1=181;break;
case 8:sum1=212;break;
case 9:sum1=243;break;
case 10:sum1=273;break;
case 11:sum1=304;break;
case 12:sum1=334;break;
default:printf("data error");break;
	}
sum1=sum1+day1;
if((year1%4==0 && year1 % 100 != 0) || (year1 % 400 == 0))
leap1=1;
else
leap1=0;
if(leap1==1&&month1>2)
sum1=sum1+1;
}
{
switch(month2)
{
case 1:sum2=0;break;
case 2:sum2=31;break;
case 3:sum2=59;break;
case 4:sum2=90;break;
case 5:sum2=120;break;
case 6:sum2=151;break;
case 7:sum2=181;break;
case 8:sum2=212;break;
case 9:sum2=243;break;
case 10:sum2=273;break;
case 11:sum2=304;break;
case 12:sum2=334;break;
default:printf("data error");break;
}
sum2=sum2+day2;
if((year2 % 4 == 0 && year2 % 100 != 0) || (year2 % 400 == 0))
leap2=1;
else
leap2=0;
if(leap2==1&&month2>2)
sum2=sum2+1;
}
c1=(year1-1)/4-(year1-1)/100+(year1-1)/400;
c2=(year2-1)/4-(year2-1)/100+(year2-1)/400;
a=(year1-1)*365+sum1+c1;
b=(year2-1)*365+sum2+c2;
x=b-a;
printf("%d\n",x);
return 0;
}