int main()
{
	int weight[4],i,j,p;
	char name[4]={'z','q','s','l'};
	for(weight[0]=1;weight[0]<=5;weight[0]++)
		for(weight[1]=1;weight[1]<=5;weight[1]++)
			for(weight[2]=1;weight[2]<=5;weight[2]++)
			{
				weight[3]=weight[1]+weight[0]-weight[2];
				if((weight[0]+weight[3])>(weight[1]+weight[2])&&(weight[0]+weight[2])<weight[1])
				{
					for(i=1;i<4;i++)
						for(j=0;j<4-i;j++)
							if(weight[j]<weight[j+1])
							{
								p=weight[j];
								weight[j]=weight[j+1];
								weight[j+1]=p;
								p=name[j];
								name[j]=name[j+1];
								name[j+1]=p;
							}
					for(i=0;i<4;i++)
					{
						weight[i]*=10;
						cout<<name[i]<<" "<<weight[i]<<endl;
					}
				}

			}
	return 0;
}


int main()
{

		int z, q, s, l;
		int condition[4], i;
		char rank[4]= {0};

		for(z=1; z<=5; z++)
		{
			for(q=1; q<=5; q++)
			{
				if(z==q) continue;

				for(s=1; s<=5; s++)
				{
					if(s==z || s==q) continue;

					for(l=1; l<=5; l++)
					{
						if(l==s || l==q || l==z) continue;

						condition[1]= (z+q)==(s+l);
						condition[2]= (z+l)>(s+q);
						condition[3]= (z+s)<q;

						if(condition[1]+condition[2]+condition[3]==3)
						{
							rank[z]='z';
							rank[q]='q';
							rank[s]='s';
							rank[l]='l';

						for(i=5; i>=1; i--)
						{
							if(rank[i]!=0)
							{
								cout<<rank[i]<< " "<< i*10<<endl;
							}
						}

			}
			}

			}

			}

		}

return 0;

}


int main()
{
	int z, q, s, l;
	char a = 'z', b = 'q', c = 's', d = 'l';
	int temp;
	char chtemp;
	for (z = 1; z <= 5; z++)
	{
		for (q = 1; q <= 5; q++)
		{
			if (q == z)
				continue;
			for (s = 1; s <= 5; s++)
			{
				if (s == z || s == q)
					continue;
				for (l = 1; l <= 5; l++)
				{
					if (l == z && l == q && l == s)
						continue;
					if (z + q == s + l && z + l > s + q && z + s < q)
					{
						if (z > q)
						{
							temp = z;
							z = q;
							q = temp;
							chtemp = a;
							a = b;
							b = chtemp;
						}
						if (q > s)
						{
							temp = s;
							s = q;
							q = temp;
							chtemp = c;
							c = b;
							b = chtemp;
							if (z > q)
							{
								temp = z;
								z = q;
								q = temp;
								chtemp = a;
								a = b;
								b = chtemp;
							}
						}
						if (s > l)
						{
							temp = s;
							s = l;
							l = temp;
							chtemp = c;
							c = d;
							d = chtemp;
							if (q > s)
							{
								temp = s;
								s = q;
								q = temp;
								chtemp = c;
								c = b;
								b = chtemp;
								if (z > q)
								{
									temp = z;
									z = q;
									q = temp;
									chtemp = a;
									a = b;
									b = chtemp;
								}
							}
						}
											goto part1;
					}
				}
			}
		}
	}
	part1:
							cout << d << " " << l * 10 << endl
							 << c << " " << s * 10 << endl
							 << b << " " << q * 10 << endl
							 << a << " " << z * 10 << endl;
	return 0;
}


struct data;
typedef struct data *pdata;
struct data
{
    int x;
	pdata next;
};

pdata create()/*??head*/
{
	pdata p;
	p=(pdata)malloc(sizeof(struct data));
	p->next=NULL;
	return p;
}

void sort(pdata head,int n)/*head???????n???*/
{
	pdata p=NULL,q=NULL,s=NULL;
    int i;
	for (i=1;i<=n;i++)
	{
	s=(pdata)malloc(sizeof(struct data));
	scanf("%d",s->x);
    q=head;
	p=head->next;
	while (p)
	{
        if (p->x>=s->x)
			break;
		q=p;
		p=p->next;
	}
	q->next=s;
	s->next=p;
	}

}

void outputdata(pdata p)/*??head*/
{
    p=p->next;
	while(p->next)
	{
      printf("%d ",p->x);
	  p=p->next;
    }
	printf("%d",p->x);
}

pdata inputdata(int n)/*??head?????n???*/
{
	pdata q=NULL,p=NULL,head=NULL;
	int i;
	head=create();
	q=head;
	for (i=1;i<=n;i++)
	{p=(pdata)malloc(sizeof(struct data));
	if (p!=NULL)
	 {
	 scanf("%d",&p->x);
     p->next=NULL;
	 q->next=p;
	 q=p;
	}
	}
	return head;
}

void del(pdata p,int k)/*??head??k???*/
{
	pdata q=NULL;
	q=p;
	p=p->next;
	while(p)
		if (p->x==k)
		{
			q->next=p->next;
			free(p);
			p=q->next;
		}
		else
		{q=p;p=p->next;}
}

pdata rinputdata(int n)/*??head,????n???*/
{
	pdata q=NULL,p=NULL,head=NULL;
    int i;
	head=create();
	for (i=1;i<=n;i++)
	{
		p=(pdata)malloc(sizeof(struct data));
		if (p!=NULL)
		{
			scanf("%d",&p->x);
			p->next=q;
			q=p;
		}
	}
    head->next=q;
    return head;
}

void outputk(pdata p,int k)/*???k???*/
{
	int i=1;
    p=p->next;
	while(p)
	{
      if (i==k)
		 printf("%d",p->x);
	  p=p->next;
      i++;
	}
}

void change(pdata p,int n,int m)/*?head?n?????m???????*/
{
	int i=1;
	pdata q=NULL,s=NULL,head=NULL;
	q=p;
	head=p;
	p=p->next;
	while(p)
	{
		if (i==n-m+1)
			break;
		i++;
		q=p;
		p=p->next;
	}
	s=head;
	while(s->next)
		s=s->next;
	q->next=NULL;
	s->next=head->next;
	head->next=p;
}

int main()
{
printf("l 50\nq 40\nz 20\ns 10");
}


int main()
{
	struct weight
	{
		char name;
		int wei;
	};
	struct weight a1,a2,a3,a4;
	a1.name='z';
	a2.name='q';
	a3.name='s';
	a4.name='l';
	for(a1.wei=1;a1.wei<=5;a1.wei++)
		for(a2.wei=1;a2.wei<=5;a2.wei++)
		{
			if(a2.wei==a1.wei)
				continue;
			for(a3.wei=1;a3.wei<=5;a3.wei++)
			{
				if(a3.wei==a2.wei||a3.wei==a1.wei)
					continue;
				for(a4.wei=1;a4.wei<=5;a4.wei++)
				{
					if(a4.wei==a3.wei||a4.wei==a2.wei||a4.wei==a1.wei)
						continue;
					if(a1.wei+a2.wei==a3.wei+a4.wei&&a1.wei+a3.wei<a2.wei&&a1.wei+a4.wei>a2.wei+a3.wei)
					{
						for(int i=5;i>=1;i--)
						{
							if(a1.wei==i)
								cout<<a1.name<<" "<<(a1.wei*10)<<endl;
							if(a2.wei==i)
								cout<<a2.name<<" "<<(a2.wei*10)<<endl;
							if(a3.wei==i)
								cout<<a3.name<<" "<<(a3.wei*10)<<endl;
							if(a4.wei==i)
								cout<<a4.name <<" "<<(a4.wei*10)<<endl;
						}
					}
				}
			}
		}
		return 0;
}


int main()
{
  cout<<"l 50"<<endl;
  cout<<endl;
  cout<<"q 40"<<endl;
  cout<<endl;
  cout<<"z 20"<<endl;
  cout<<endl;
  cout<<"s 10"<<endl;
  return 0;
}


int main()
{
	int iz,iq,is,il,z,q,s,l;
	for(iz=10;iz<=50;iz+=10)

		for(iq=10;iq<=50;iq+=10)

			for(is=10;is<=50;is+=10)

				for(il=10;il<=50;il+=10)

					if((iz+iq==is+il)&&(iz+il>iq+is)&&(iz+is<iq))
					{
						z=iz;
						q=iq;
						s=is;
						l=il;
						break;
					}

	printf("l %d\n",l);
	printf("q %d\n",q);
	printf("z %d\n",z);
	printf("s %d\n",s);
    return 0;
}


int main()
{
	int weight[4]={1,1,1,1};//????/10
	char name[4]={'z','q','s','l'};
	/*????*/
	for(weight[0]=1;weight[0]<=5;++weight[0]){
		for(weight[1]=1;weight[1]<=5;++weight[1]){
			for(weight[2]=1;weight[2]<=5;++weight[2]){
				for(weight[3]=1;weight[3]<=5;++weight[3]){
					if(weight[0]==weight[1]||weight[0]==weight[2]||weight[0]==weight[3]||weight[1]==weight[2]||weight[1]==weight[3]||weight[2]==weight[3])
						continue;
					else if(weight[0]+weight[1]==weight[2]+weight[3]&&weight[0]+weight[3]>weight[2]+weight[1]&&weight[0]+weight[2]<weight[1])
						break;
				}
				if(weight[0]+weight[1]==weight[2]+weight[3]&&weight[0]+weight[3]>weight[2]+weight[1]&&weight[0]+weight[2]<weight[1])
						break;
			}
			if(weight[0]+weight[1]==weight[2]+weight[3]&&weight[0]+weight[3]>weight[2]+weight[1]&&weight[0]+weight[2]<weight[1])
						break;
		}
		if(weight[0]+weight[1]==weight[2]+weight[3]&&weight[0]+weight[3]>weight[2]+weight[1]&&weight[0]+weight[2]<weight[1])
						break;
	}
	/*??*/
	int max=0,t;//?????????
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)//???????
		{
			if(max<weight[j])
			{
				max=weight[j];
				t=j;
			}
		}
		cout<<name[t]<<' '<<weight[t]*10<<endl;
		weight[t]=0;//??????
		max=0;//???max
	}
	return 0;
}


int main()
{
  int z,q,s,l,i,j;
  char name[4]={'z','q','s','l'};
  int t;
  char tname;
  for(z=10;z<=50;z=z+10)
  {
    for(q=10;q<=50;q=q+10)
    {
        if(z==q) continue;
        for(s=10;s<=50;s=s+10)
       {
          if(z==s||q==s) continue;
                for(l=10;l<=50;l=l+10)
        {
          if(z==l||q==l||s==l) continue;
          else if((z+q)==(s+l)&&(z+l)>(s+q)&&(z+s)<q)
		  {  int result[4]={z,q,s,l};
		   for(i=3;i>0;i--)
			{
				for(j=i-1;j>=0;j--)
				{
				   if(result[i]>result[j])
				   {
				     t=result[i];
					 result[i]=result[j];
					 result[j]=t;
					 tname=name[i];
					 name[i]=name[j];
					 name[j]=tname;
				   }
				}
		    }
		   for(i=0;i<4;i++)
		   {
		    cout<<name[i]<<" "<<result[i]<<endl;
		   }
		  }
        }
      }
    }
  }
 return 0;
}


void doublesort(int a,int b,int c,int d)
{
	int result[4]={a,b,c,d};
	char name[4]={'z','q','s','l'};

	for(int i=0;i<4;i++)
	{
		for(int j=i;j<4;j++)
		{
			if(result[i]<result[j])
			{
				swap(result[i],result[j]);
				swap(name[i],name[j]);
			}
		}
	}

	for(int i=0;i<4;i++)
	{
		cout<<name[i]<<" "<<10*result[i]<<endl;
	}
}

int main()
{
	int z,q,s,l;
	for(z=1;z<=5;z++)
	{
		for(q=1;q<=5;q++)
		{
			for(s=1;s<=5;s++)
			{
				for(l=1;l<=5;l++)
				{
					if(z+q==s+l&&z+l>s+q&&z+s<q&&z!=q!=s!=l)
					{
						doublesort(z,q,s,l);
					}
				}
			}
		}
	}
	return 0;
}


struct weight
{
    char name;
    int weight;
};
int main()
{
    int a,b,c,d,i,j,t1;
    char t2;
    struct weight f[4];
    for(a=1;a<=5;a++)
        for(b=1;b<=5;b++)
            for(c=1;c<=5;c++)
                for(d=1;d<=5;d++)
                {
                    if((a!=b) && (b!=c) && (c!=d) && (d!=a))
                        if((a+b)==(c+d))
                            if((a+d)>(c+b))
                                if((a+c)<b)
                                {
                                    f[0].name='z';
                                    f[0].weight=a;
                                    f[1].name='q';
                                    f[1].weight=b;
                                    f[2].name='s';
                                    f[2].weight=c;
                                    f[3].name='l';
                                    f[3].weight=d;
                                    for(i=0;i<4;i++)
                                        for(j=i+1;j<4;j++)
                                            if(f[j].weight>f[i].weight)
                                            {
                                                t1=f[j].weight;
                                                t2=f[j].name;
                                                f[j].weight=f[i].weight;
                                                f[j].name=f[i].name;
                                                f[i].weight=t1;
                                                f[i].name=t2;
                                            }
                                    for(i=0;i<4;i++)
                                        printf("%c %d\n",f[i].name,10*f[i].weight);
                                }
                }
    return 0;
}


int differ(int a,int b,int c,int d);
int main()
{
	int z,q,s,l;
	for(z=10;z<=50;z+=10)
		for(q=10;q<=50;q+=10)
			for(s=10;s<=50;s+=10)
				for(l=10;l<=50;l+=10)
					if(differ(z,q,s,l)==0)
						if(z+q==s+l)
							if(z+l>s+q)
								if(z+s<q)
									cout<<"l "<<l<<endl<<"q "<<q<<endl<<"z "<<z<<endl<<"s "<<s<<endl;
	return 0;
}

int differ(int a,int b,int c,int d)
{
	int p=1;
	p*=(a-b);
	p*=(a-c);
	p*=(a-d);
	p*=(b-c);
	p*=(b-d);
	p*=(c-d);
	if(p==0)
		return 1;
	else
		return 0;
}


int main()
{
	int z,q,s,l,i;
	char weight[6];
	for(z=1;z<6;z++)
	{
		for(q=1;q<6;q++)
		{
			if(z==q)
				continue;
			else
				for(s=1;s<6;s++)
				{
					if(s==z||s==q)
						continue;
					else
						for(l=1;l<6;l++)
						{
							if(l==z||l==q||l==s)
								continue;
							else
								if((z+q)==(s+l)&&(z+l)>(s+q)&&(z+s)<q)
								{
									weight[z]='z';
									weight[q]='q';
									weight[s]='s';
									weight[l]='l';
									for(i=5;i>0;i--)
									{
										if(i==z||i==q||i==s||i==l)
											cout<<weight[i]<<" "<<i*10<<endl;
										else
											continue;
									}
								}
						}
				}
		}
	}
	return 0;
}


struct student
	{
		int weigh;
	};
int main()
{

	int a=0,b=0,c=0,d=0;
	struct student zhao,qian,sun,li;
	zhao.weigh=a;
	qian.weigh=b;
	sun.weigh=c;
	li.weigh=d;
	for(a=10;a<=50;a=a+10)
		for(b=10;b<=50;b=b+10)
			for(c=10;c<=50;c=c+10)
				for(d=10;d<=50;d=d+10)
				{
					if(a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d)
						if(a+d>c+b)
							if(a+c<b)
								if((a+b)==(c+d))
								{
									printf("l %d\nq %d\nz %d\ns %d",d,b,a,c);
									printf("\n");
								}
				}
				return 0;
}


int main( )
{
	int zhao, qian, sun, li, i;
	char weight[51];
	for(i = 0; i < 51; i++)
	{
		weight[i] = ' ';
	}
	for(zhao = 10; zhao <= 50; zhao=zhao+10)
		for(qian = 10; qian <= 50; qian=qian+10)
		{
			if(zhao != qian)
				for(sun = 10; sun <= 50; sun=sun+10)
				{
					if(zhao != sun && qian != sun)
					{
						for(li = 10; li <= 50; li=li+10)
							if(zhao != li && qian != li && sun != li)
								if(((zhao+qian)==(sun+li))&&((zhao+li)>(sun+qian))&&((zhao+sun)<qian))
								{
									weight[zhao] = 'z';
									weight[qian] = 'q';
									weight[sun] = 's';
									weight[li] = 'l';
									for(i = 50; i > 0; i--)
									{
										if(weight[i] != ' ')
											cout<<weight[i]<<" "<<i<<endl;
									}
								}
					}
				}
		}
		return 0;
}


int compare (int z, int q, int s, int l, int i)
{
	int a[4] = {z, q, s, l};
int j, k, t;
for (j = 4; j > 0; j--)
	for (k = 1; k < j; k++)
	{
	if(a[k] > a[k - 1])
	   {
		t = a[k];
		a[k] = a[k - 1];
		a[k - 1] = t;
	   }
	}
return (a[i - 1]);
}
int fout( int z, int q, int s, int l, int sum)
{
if(sum == z) cout << "z";
if(sum == q) cout << "q";
if(sum == s) cout << "s";
if(sum == l) cout << "l";
return 0;
}
int main()
{
int z, q, s, l, i, sum;
for(z = 10; z < 60; z += 10)
for(q = 10; q < 60; q += 10)
	for(s = 10; s < 60; s += 10)
			for(l = 10; l < 60; l += 10)
			if( (z - q) * (z - s) * (z - l) * (q - s) * (q - l) * (s - l) != 0
			    && z + q == s + l
				&& z + l > s + q
				&& z + s < q )
{
for(i = 1; i <= 4; i++)
      {
	  sum = compare(z, q, s, l, i);
	  fout(z, q, s, l, sum);
	  cout << " " << sum <<endl;
      }
}
return 0;
}


//
//  main.cpp
//  poj
//
//  Created by ChuPan,Wong on 9/27/13.
//  Copyright (c) 2013 ChuPan,Wong. All rights reserved.
//

int main(int argc, const char * argv[])
{
    //  manual computation shows that there is only one solution for this question
    cout<<"l 50"<<endl;
    cout<<"q 40"<<endl;
    cout<<"z 20"<<endl;
    cout<<"s 10"<<endl;
        return 0;
}


int main()
{
    int i,j,z,q,s,l;
    printf("l 50\nq 40\nz 20\ns 10\n");
}


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


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 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, 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;
}


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,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;
}


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];
        }
    }
}


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);
}


/*
 *La vie est ailleurs
 *PROB: Joseph Problems
 *LANG: c
 *IDE: Dev-C++ 4.9.9.2
 *Compiler: gcc
 *ID: 00910049
 *Coding by Xu Zhendong
 */

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

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

	head = (struct Per*) malloc (LEN);
	p = head;
	//head -> num = 1;
	for (i = 1; i <= n; i ++)
	{
		p -> num = i;
		if (i == n)
		{
			p -> next = head;
		}
		else
		{
			p -> next = (struct Per*) malloc (LEN);
			p = p -> next;
		}
	}
	p = head;
	/*
	for (i = 1; i <= n; i ++)
	{
		printf("%d",p -> num);
		p = p-> next;
	}
	*/

	while (p != p -> next)
	{
		for (i = 1; i < m; i ++)
		{
			if (i == (m - 1))
			{
				p -> next = p -> next -> next;
				p = p -> next;
			}
			else p = p -> next;
		}
	}

	printf("%d\n",p -> num);
	}
	getchar();
	getchar();
	return 0;
}


int main()
{
int n,m,i,a;
while(1)
{cin>>n;
cin>>m;
if(n==0)break;
else{a=0;
if(n>1)
for(i=2;i<=n;i++)
a=(a+m)%i;
cout<<(a+1)<<endl;
}}
return 0;}


int king(int M,int N)
{
    int k=0;
    for(int i=2;i<=M;i++)
    k=(k+N)%i;
    return ++k;
}
int main()
{
    int n,m,a[100],i=1,x;
    while(scanf("%d%d",&n,&m)&&n&&m)
    {
                                    if(m==0&&n==0)
                                    break;
                                    else
                                    a[i]=king(n,m);
                                    x=i;
                                    i++;
    }
    for(i=1;i<=x;i++)
    {
               cout<<a[i]<<endl;
    }
    return 0;
}


int main()
{
	int num[1000],i,cnt,jsh=0,m,n;
	scanf("%d %d",&n,&m);
	while(n!=0)
	{
		for(i=0;i<=n-1;i++)
			num[i]=i+1;
		for(i=0;i<=n-1;i++)

		{

			cnt=0;
			for(cnt=0;cnt<m;cnt++)
			{
				while(num[jsh]==0)
					jsh=(jsh+1)%n;
				jsh=(jsh+1)%n;
			}
			jsh=jsh-1;
			if(jsh<0)
				jsh=n-1;
			if(i==n-1)
				printf("%d\n",num[jsh]);
			num[jsh]=0;
		}
        scanf("%d %d",&n,&m);
		jsh=0;
	}
	return 0;
}


void ysfwt(int n,int m)
{
	int i,s=0;
	for(i=2;i<=n;i++) s=(s+m)%i;
    printf("%d\n",s+1);
}
void main()
{
	int j,n,m;
	for(j=0;;j++)
	{scanf("%d %d",&n,&m);
	if(m!=0&&n!=0) ysfwt(n,m);
	else break;}
}


int a[305];
int main()
{
	int n,m;
	cin>>n>>m;
	while(n!=0&&m!=0){
		memset(a,0,sizeof(a));
		int step=0;
		int flag=0;
		int i=1;
		while(flag<n-1){
			step=0;
			for(;i<=n;i=i%n+1){
				if(a[i]==0)	step++;
				if(step==m){
					a[i]=1;
					break;
				}
			}
			flag++;
		}
		for(int j=1;j<=n;j++)
			if(a[j]==0){
				cout<<j<<endl;
				break;
			}
		cin>>n>>m;
	}
	return 0;
}


int main(){
    int N,m;
    while(true){
        int count=0,number=0;
        cin>>N>>m;
        if(N==0&&m==0)break;
        const int n=N;
        int a[n+1];
        memset(a,0,sizeof a);
        for(int i=1;;++i){
            if(i>n) i=i-n;
            if(a[i]==1)continue;
            else{
                ++count;
                //cout<<"i="<<i<<" count="<<count<<" a["<<i<<"]="<<a[i]endl;

            }
            if(number==n-1){break;}
            if(count==m){
                number++;
                count=0;
                a[i]=1;
                //cout<<"number="<<number<<"a["<<i<<"]="<<a[i]<<endl

            }
        }
    for(int i=1;i<=n;++i)
    if(a[i]==0)cout<<i<<endl;
    }
    return 0;
}


int kingmonkey(int n,int m);
int main()
{
	int n,m;
	int result;
	for(;;)
	{
		scanf("%d %d",&n,&m);
		if((n==0)&&(m==0))
		{
			break;
		}
		else
		{
			result=kingmonkey(n,m);
			printf("%d\n",result);
		}
	}
	return 0;
}
/*
n??????m????
?????kingmonkey(n,m)=(kingmonkey(n-1,m)+m)%n
?????king(n-1,m)?????????
*/
int kingmonkey(int n,int m)
{
	if(n==1)
	{
		return 1;
	}
	/*??
	if(monkey[n]==0)
	{
		monkey[n]=(kingmonkey(n-1,m)+m+1)%n;
	}
	*/
	else
	{
		int tmp=(kingmonkey(n-1,m)+m);
		if ((tmp%n)==0)
		{
			tmp=n;
		}
		else if(tmp>n)
		{
			tmp=tmp%n;
		}
		return tmp;
	}
}


struct king{int num;struct king *next;};
void p_king(int n,int m)
{
	int i;
	struct king *p1,*p2,*p;
	p1=p=(struct king *)malloc(LEN);
	for(i=0;i<n;i++)
	{
		p1->num=i+1;
		if(i==n-1) p1->next=p;
		else p1->next=(struct king *)malloc(LEN);
		p2=p1,p1=p1->next;
	}
	for(p1=p;n!=1;n--)
	{
		for(i=1;i<(m-1)%n+1;i++)
			p2=p1,p1=p1->next;
		p2->next=p1=p1->next;
	}
	printf("%d\n",p1->num);
}

void main()
{
	int i,n[300],m[300];
	scanf("%d%d",&n[0],&m[0]);
	for(i=1;m[i-1]&&n[i-1];i++)
		scanf("%d%d",&n[i],&m[i]);
	for(i=0;m[i]&&n[i];i++)
		p_king(n[i],m[i]);
}


int main()
{
	int king(int n, int m);
	int n,m;

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

int king(int n,int m)
{
	int z;
	if (n==1) return 1;
	if (n>1)
	{
		z=(m%n+king(n-1,m))%n;
		if (z==0) z=n;
		return z;
	}
}


void main()
{
	int m,n;
	int *p;
	int i,j,k;
	for(;;)
	{
		scanf("%d%d",&n,&m);
		if(m==0)break;
		else if(n==1) printf("1\n");

		else
		{p=(int*)malloc(sizeof(int)*(n));
	for(i=0;i<n;i++)
		p[i]=0;
	k=n;
	j=1;
	for(i=0;i<n;i=(i+1)%n)
	{

		if(p[i])continue;

		if(j++==m)
		{
			k--;
			p[i]=1;
			j=1;
		}

		if(k==0)
		{
			printf("%d\n",i+1);break;
		}

	}free(p);
	}
	}
return;
}


int main () {
	int n, m;
	while (true) {
		scanf("%d%d", &n, &m);
		if (n == 0 && m == 0) {
			break;
		}
		int count = n;
		int currentIndex = 0;
		int flags[302];
		for (int i = 0; i < n; i++) {
			flags[i] = i + 1;
		}
		while (count != 1) {
			int currentNumber = 1;
			while (currentNumber != m) {
				currentNumber++;
				currentIndex = (currentIndex + 1) % count;
			}

			// delete the element in currentIndex.
			for (int i = currentIndex + 1; i < count; i++) {
				flags[i - 1] = flags[i];
			}
			count--;
		}
		printf("%d\n", flags[0]);
	}
}


int King(int a,int b)
{
  int monkey,num,i,j,countor;

/*scanf("%d%d",&monkey,&num);*/
monkey=a;num=b;
int*Monkey =(int*)malloc(monkey*sizeof(int));
for(i=0;i<monkey;i++)
{
    *(Monkey+i)=0;
   // printf("%d\n",*(Monkey+i));
   }

for(i=0,j=0,countor=1;j<monkey-1;)
 {
     if(*(Monkey+i%monkey)==-1){i++;continue;}
     if(countor==num)
        {
            *(Monkey+i%monkey)=-1;
           // printf("No.%d is %d\n",i%monkey+1,*(Monkey+i%monkey));
            j++;
            i++;
            countor=1;
            //printf("i=%d j=%d countor=%d\n",i,j,countor);
            continue;
            }
     i++;
     countor++;
     //printf("//i=%d j=%d countor=%d\n",i,j,countor);
     }

//for(i=0;i<monkey;i++) printf("%d\n",*(Monkey+i));
 for(i=0;i<monkey;i++)
 {
        if(*(Monkey+i)!=-1)
            return (i+1);
 }
free (Monkey);
}
int main(){
    int king[100];
    int a,b,n=0,k=0;
    scanf("%d%d",&a,&b);
    while(a!=0&&b!=0)
    {
       king[n]=King(a,b);
       n++;
        scanf("%d%d",&a,&b);

    }
    for(k=0;k<n;k++) printf("%d\n",*(king+k));
    return 0;
    }


const int MAX_NUM=300;
int anLoop[MAX_NUM+10];
int main()
{
	int n,m;
	while(1)
	{
		cin>>n>>m;
		if(n==0&&m==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;
		}
	}

	return 0;
}


/*
 * YueSeFuProblem.cpp
 *
 *  Created on: 2010-11-14
 *      Author: user
 */

int main() {
	while (true) {
		int n, m, i;
		cin >> n >> m;
		if (n == 0 && m == 0)
			break;
		int f[n + 1];
		for (i = 1; i < n; i++) {
			f[i] = i + 1;
		}
		f[n] = 1;

		int t = 1;

		while (true) {
			if (m == 1) {
				cout << n << endl;
				break;
			} else {
				for (i = 1; i < m - 1; i++) {
					t = f[t];
				}
				f[t] = f[f[t]];
				t = f[t];
				if (f[t] == t) {
					cout << t << endl;
					break;
				}
			}
		}
	}

	return 0;
}


int number(int x,int y)
{
	int k,num;
	if(x==1) num=1;
	k=y%x;
	if(x==2)
	{
		if(k==1) num=2;
	    else num=1;
	}
	if(x>2)
	{
	    num=number(x-1,y)-x+k;
		if(num<=0) num+=x;
	}
	return num;
}

void main()
{
	int a[1000],b[1000],n=0,i;
    for(n=0;;n++)

	{	scanf("%d %d",&a[n],&b[n]);
		if(a[n]==0&&b[n]==0)break;}
    for(i=0;i<n;i++)
		printf("%d\n",number(a[i],b[i]));
}


int main()
{
	int a[301], n, m, i, count = 0, zong = 0;
	while(1)
	{
		cin >> n >> m;
		if (n == 0 && m == 0)
			break;
		for (i = 1; i <= n; i++)
			a[i] = 1;
		i = 1;
		while(1)
		{
			if (a[i] == 1)
			{
				count++;
				if (zong == n - 1)
				{
					cout << i << endl;
					count = 0;
					zong = 0;
					break;
				}
				if(count == m)
				{
					zong++;
					a[i] = 0;
					count = 0;
					if (i < n)
						i++;
					else if (i == n)
							i = 1;
				}
				else
				{
					if (i < n)
						i++;
					else if (i == n)
							i = 1;
				}
			}
			else if (i == n)
				i = 1;
				 else
					i++;
		}
	}
	return 0;
}


int f(int x,int y)
{
    int k=0,i;
    for(i=2;i<=x;i++)
    {
        k=(k+y)%i;
    }
    return k+1;
}
main()
{
      int n[20],m[20],i,king;
      for(i=0;i<=19;i++)
      {
           scanf("%d %d",&n[i],&m[i]);
           king=f(n[i],m[i]);
           if(n[i]!=0)
              printf("%d\n",king);
           else
              break;
      }
}


int f(int x,int y);
int main()
{
    int i,a,d,m,n;
    int ad[999];
    int sd[999];
    for(i=1;i>0;i++)
    {
        scanf("%d%d\n",&ad[i],&sd[i]);
        if(ad[i]==0&&sd[i]==0)
        {
            break;
        }

         printf("%d\n",f(ad[i],sd[i]));
    }
    return 0;
}

int f(int x,int y) {
   int i;
   int k=0;
   for(i=2;i<=x;i++)
   {k=((y%i)+k)%i;}
  return k+1;
}


long int num(int a,int b)
{
    long int i;
    if(a==1) i=1;
    if(a==2)
    {
        if(b%2==0) i=1;
        else i=2;
    }
    if(a>2)
        i = (num(a-1,b)+b%a)%a;
    if(i==0) i=a;
    return i;
}
int main()
{
   int c;
   for(c=1;c!=0;)
   {
    long int n,m,k;
    scanf("%ld%ld",&n,&m);
    c=n;
    if(c==0) break;
    k=num(n,m);
    if(k==0)
       printf("%ld\n",n);
    else
       printf("%ld\n",k);
    }

}


int main()
{
  int dawang(int n,int m);
  int a,b,boss;
  scanf("%d %d",&a,&b);
  while (a!=0)
  {
     boss=dawang(a,b);
  printf("%d\n",boss);
  scanf("%d %d",&a,&b);
  }
//  scanf("\n");
}
int dawang(int n,int m)
{
 int after[400];
 int i,j,pre,t;
 for (i=0;i<n;i++) after[i]=i+1;
 after[n]=1;
 pre=n;
 t=1;
 while (after[pre]!=pre)
 {
    for (i=1;i<m;i++)
    {
      pre=t;
      t=after[pre];
    }
    after[pre]=after[t];
	t=after[pre];
 }
return(t);
}


void Left(int *ptr,int hd,int tl);

int main()
{
    int n,m,m1;
    int a[301],result[100];
    int i,j=0,k;
    while(1)
    {
       scanf("%d%d",&n,&m);
       if(n==0&&m==0) break;

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

       k=1;
       for(i=n;i>1;i--)
       {
          if(m%n!=0) m1=m%n;
          else m1=n;
          if(k-1+m1<=n) k+=m1-1;
          else k=k-1+m1-n;
          Left(a,k,n);
          n--;
       }
       j++;
       result[j]=a[1];
    }

    for(i=1;i<=j;i++) printf("%d\n",result[i]);

  return 0;
}

void Left(int *ptr,int hd,int tl)
{
   int i;
   for(i=hd;i<tl;i++)
   {
      ptr[i]=ptr[i+1];
   }
   ptr[tl]=0;
}


int king(int n,int m)
{
	int num,k,i,j,total=0;
	int a[300];

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

		num=0;
		do
		{

			if(j%n!=0)
			{
	           if(a[j%n]!=0)
				num++;
			}
			else
			{
				if(a[n]!=0)
					num++;
			}
		    if(num<m)
				j++;
		}while(num<m);
		if(j%n!=0)

		{
			a[j%n]=0;total++;
		}
		else
		{
			a[n]=0;total++;
		}
	if(total==n) break;
	}
	if(j%n!=0)
        return(j%n);
	else
	    return(n);
}
void main()
{
	int l,n,m,c[20],s;
	for(l=0;;l++)
	{
		scanf("%d %d",&n,&m);

		if(n==0) {s=l;c[l]=0;break;}
		c[l]=king(n,m);

	}
	l=0;
	for(l=0;l<s;l++)
	{

		printf("%d\n",c[l]);
	}

}


int num(int n,int m);
void main()
{   int x,y;
	while(scanf("%d%d",&x,&y)==2)
	{
       if(x==0&&y==0)
		   break;
	  else
		  printf("%d\n",num(x,y));

	}

}

int num(int n,int m)
{
	int x=0,i=0,flag=0;
	int str[301];

	for(i=1;i<=n;i++)
		str[i]=1;
	while(flag<n)
	{
		i=1;
		for(i=1;i<=n;i++)
		{
			if(str[i]==1)
			{	x+=1;
		    	if(x==m)
				{
				  str[i]=0;
		   	      x=0;
			      flag+=1;
				}
			}
			if(flag==n)
			{
			    return(i);
				break;
			}

		}
	}

}


struct mon
{
	int num;
	struct mon*next;
};
void monkey(int m,int n)
{
	struct mon a[300],*p1,*p2;
	int i,j;
	for(i=1;i<=n;i++)
	{
		a[i].num=i;
		a[i].next=a+i+1;
	}
	a[n].next=a+1;
	p1=a+1;
	for(;n>1;n--)
	{
		for(j=1;j<m;j++)
		{
			p2=p1;
			p1=(*p2).next;
		}
		(*p2).next=(*p1).next;
		p1=(*p1).next;
	}
	printf("%d\n",(*p1).num);
}
int main()
{
	int n,m;
	while(1)
	{
		scanf("%d%d",&n,&m);
		if(m==0&&n==0) break;
		monkey(m,n);
	}
	return 0;
}


int main(){

	int n, m, id[N], num_monkey;

	int i;

	while(1){
		scanf("%d %d", &n, &m);

		if(n != 0 && m != 0){

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

			num_monkey = n;
			int index = 0;
			int count = 1;
			while(true){
				while(num_monkey > 1){
					if(id[index] != 0){
						if(count != m){
							count++;
							index = (index+1)%n;
						}else{
							id[index] = 0;
							count = 1;
							num_monkey--;
						}
					}else{
						index = (index+1)%n;
					}
				}

				for(i = 0; i < n; i++){
					if(id[i] != 0){
						printf("%d\n", id[i]);
						break;
					}
				}

				break;
			}
		}else{
			break;
		}
	}

	return 0;
}


int josephus0(int n, int m)
{
	if (n == 2) return (m%2) ? 2 : 1;
	int v = (m+josephus0(n-1,m)) % n;
	if (v == 0) v = n;
	return v;
}
int josephus(int n, int m)
{
	if (m == 1) return n;
	if (n == 1) return 1;
	if (m >=n) return josephus0(n,m);
	int l = (n/m)*m;
	int j = josephus(n - (n/m), m);
	if (j <= n-l) return l+j;
	j -= n-l;
	int t = (j/(m-1))*m;
	if ((j % (m-1)) == 0) return t-1;
	return t + (j % (m-1));
}
int main()
{
int n , k;
while(2 == scanf("%d %d",&n ,&k))
{
   if(n == 0 && k == 0)break;
    printf("%d\n",josephus(n , k));
}
 return 0;
}


int main()
{
	int count,a[300],n,m,i,exist;
	while(1)
	{
	scanf("%d%d",&n,&m);
	if (n==0&&m==0) break;
	exist=n;
	for (i=0;i<n;i++)
		 a[i]=1;
	i=0;count=0;
	while(exist>1)
	{
		if(a[i]!=0) {count++;}
		if (count==m) {a[i]=0;exist--;count=0;}
		i++;
		if (i==n) i=0;
	}
	for (i=0;i<n;i++)
		 if (a[i]!=0) printf("%d\n",i+1);
	}
	return 0;
}


void main()
{
	int a[301],b[301];
	int i,j,n,m,p,t;
	int line,num;
	for(;;)
	{
	scanf("%d %d",&n,&m);
	if(n==0&&m==0)break;
	else
	{
	for(i=1;i<=n;i++)
		a[i]=i;
	for(num=0;num<n-1;num++)
	{
	    p=n-num;
	    for(i=1;;i++)
		{
			if(p>=m)break;
		    p=p+n-num;
		}
		if(i==1) line=m;
		else line=n-num-p%m;
	    for(j=1;j<=n-num-1;j++)
		{
	 	   if(line+j>n-num)
		    	t=line+j-(n-num);
		   else t=line+j;
		   b[j]=a[t];
		}
		for(j=1;j<=n-num-1;j++)
			a[j]=b[j];
	}
    printf("%d\n",a[1]);
	}
	}
}


//#include<stdlib.h>
//#include<math.h>
int main()
{
	int n,m,i,j;
	int a[301];
	while(true){
		scanf("%d %d",&n,&m);
		if(n==0)
			break;
		for(i=1;i<n;i++)
			a[i]=i;
		a[0]=n;
		j=1;
		while(n>1){
			j=(m+j-1)%n;

			for(i=j;i<n-1;i++)
				a[i]=a[i+1];
			n--;
		}
		printf("%d\n",a[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 a,b,i,re;
	for(i=0;;i++){
		scanf("%d%d",&b,&a);
		if(a==0)
			break;
		re=f(a,b)+1;
		printf("%d\n",re);
	}
	return 0;

}


int main()
{
	int n,m,i,k,cnt;
	int num[330];
	while(1)
	{
		cin >> n >> m;
		if(!n)
			break;
		cnt = n;
		for(i = 1; i <= n; i++)
			num[i] = 1;
		k = 1;
		i = 1;
		while(cnt > 1)
		{
			if(i > n)
				i = 1;
			if (num[i] == 0)
				i++;
			else
				if(k == m)
				{
					num[i] = 0;
				    cnt --;
				    k = 1;
				    i++;
				}
				else
				{
					i++;
					k++;
				}
		}
		for(i = 1; i <= n; i++)
			if(num[i] != 0)
			{
				cout << i << endl;
				break;
			}
	}
    return 0;
}


int main()
{
	int i,m,n,k,j,l;
	scanf("%d %d",&n,&m);
	while (m>0)
	{i=3;
	   l=fmod(m,2);
	   while (i<=n)
	   {
		   l=fmod(m+l,i);
		   i++;
	   }
		printf("%d\n",l+1);
		scanf("%d %d",&n,&m);
	}
	return 7;
}


int asdf(int n,int m)
{
        int *a = (int*)malloc(n*sizeof(int));
        int ret=0;
        int i=0;
        int left=n;
        int temp = 0;
        int inc=0;

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

        i=0;

        while(a[1]!=0)
        {
                temp = (i + m)%left;
                if(0==temp)
                {
                        temp = left;
                }
                else
                {
                        for(inc=temp;inc<left;inc++)
                        {
                                a[inc-1]=a[inc];

                        }
                }
                i = temp-1;
                a[left-1]=0;
                left--;
        }
        ret = a[0];

        free(a);
        return ret;
}

int main()
{
        int n,m;
        int res[MAX_SIZE];
        int counter =0;
        memset(res,0,sizeof(res));

        for(;counter<MAX_SIZE;counter++)
        {
                scanf("%d %d",&n,&m);
                if(n==0)
                {
                        break;
                }
                res[counter]=asdf(n,m);
        }
        for(counter =0;(counter<MAX_SIZE)&&(res[counter]!=0);counter++)
        {
                printf("%d\n",res[counter]);
        }
        return 0;
}


int monkey[300];

int main()
{
    void getking(int n, int m);
    int n = -1, m;

    while(n != 0)
    {
            scanf("%d %d", &n, &m);

            if(n != 0)
                 getking(n, m);
    }

    return 0;
}

void getking(int n, int m)
{

     int i, left, delete_n, count = 0;

     for(i = 0; i <= n - 1; i++)
           monkey[i] = 1;

     left = n;
     i = n - 1;
     while(left > 1)
     {
                while(count < m)
                {

                            if(i + 1 >= n)
                                 i = 0;
                            else
                                 i = i + 1;
                            count = count + (monkey[i] == 1);
                }

                monkey[i] = 0;
                left = left - 1;
                count = 0;
     }

     for(i = 0; i <= n - 1; i++)
           if(monkey[i] == 1)
                        printf("%d\n", i + 1);
}


int procedure (int n , int m);

int main()
{
	int ans[100];
	int n,m,t,s;
	t=0;
	while(1)
	{
		scanf("%d %d",&n,&m);
		if(n==0 && m==0)
		{
			break;
		}
		else
		{
			ans[t]=procedure(n,m);
			t++;
		}
	}
	for(s=0;s<t;s++)
	{
		printf("%d\n",ans[s]);
	}
	return 0;
}

int procedure (int n , int m)
{
	int num=n;
	int i,j,k,r;
	int sz[300];
	for(i=0;i<n;i++)
	{
		sz[i]=0;
	}
	i=0;
	for(k=num;k>1;k--)
	{
		j=0;
		while(j<m)
		{
			if(i==n)
			{
				i=0;
			}
			if(j==m-1 && sz[i]==0)
			{
				sz[i]=1;
				break;
			}
			if(sz[i]==0)
			{
				j=j+1;
			}
			i=i+1;
			continue;
		}
	}
	for(r=0;r<n;r++)
	{
		if(sz[r]==0)
		{
			return r+1;
		}
	}
}


struct monkey
{
	int num;
	struct monkey *next;
};
struct monkey *del(struct monkey *head,int x)
{
	struct monkey *p1,*p2;
	p1=head;
	while(x!=p1->num&&p1->next!=NULL)
	{
		p2=p1;p1=p1->next;
	}
	if(x==p1->num)
	{
		if(p1==head)
		head=p1->next;
		else p2->next=p1->next;
	}
	return(head);
}

void main()
{
	struct monkey *del(struct monkey *head,int x);
	struct monkey *head,*p1,*p2;
	int i,j,k=1,t,n[100],m[100],count=0,p;
	for(t=0;;t++)
	{

		scanf("%d %d",&n[t],&m[t]);
		if(n[t]==0)break;
	}
	p=t-1;
	//printf("%d",p);
	for(t=0;t<=p;t++)
	{
		count=0;k=1;
		p1=p2=head=(struct monkey*)malloc(LEN);
		p1->num=1;
		for(i=2;i<=n[t];i++)
		{
			p1=(struct monkey*)malloc(LEN);
			p1->num=i;
			p2->next=p1;
			p2=p1;
		}
		p2->next=NULL;
		p1=head;
		for(i=1;;i++)
		{
			if(k==m[t])
			{
				del(head,p1->num);
				count++;
				head=del(head,p1->num);
				k=0;
			}
			if(p1->next!=NULL)
			p1=p1->next;
			else
			p1=head;
			k++;
			if(count==n[t]-1)
				break;
		}
		printf("%d\n",head->num);
	}
}


struct hou
{int k;
struct hou *next;
};
int main(int argc, char *argv[])
{int i,j,k,n,m;
struct hou *head,*p1,*p2;

while(1)
{
        scanf("%d%d",&n,&m);
        if(n==0&&m==0)
        break;
        head=(struct hou*)malloc(sizeof(struct hou));
p1=head;
        for(i=1;i<=n;i++)
        {if(i==n)
        {p1->k=n;p1->next=head;}
        else
        {p2=p1;
        p1=(struct hou*)malloc(sizeof(struct hou));
        p2->k=i;
        p2->next=p1;
        }}
        p1=head;
        p2=head;
        for(i=1;i<=n-1;i++)
        {
                           for(j=1;j<=m-1;j++)
                           {p2=p1;
                           p1=p1->next;}
                           p2->next=p1->next;
                           p1=p1->next;
        }
        printf("%d\n",p1->k);
}

    return 0;
}


int main()
{
    int n, m, ans;
    int solve(int n, int m);
    scanf("%d %d",&n, &m);
    while (!(n==0 && m==0))
    {
        ans = solve(n, m);
        if (!ans) ans = n;
        printf("%d\n", ans);
        scanf("%d %d", &n, &m);
    }
    return 0;
}
int solve(int n, int m)
{
    int ans;
    if (n==1) return 1;
    ans = (m % n + solve(n-1, m)) % n;
    if (!ans) ans = n;
    return ans;
}


struct monkey
{
	int num;
	struct monkey *next;
}monkey[300];
void main()
{
	struct monkey *head,*p,*q;
	int n,i,m,j,k;
	for(i=0;i<300;i++)
		monkey[i].num=i+1;
	for(j=0;;j++)
	{
	    scanf("%d %d",&n,&m);
		if(n==0&&m==0)
			break;
	    head=&monkey[0];
	    for(i=0;i<n-1;i++)
		{
		    monkey[i].next=&monkey[i+1];
		}
    	monkey[n-1].next=&monkey[0];
    	p=head;
		if(m!=1)
		{
			while(p!=p->next)
			{
			    for(k=0;k<m-2;k++)
				{
				    p=p->next;
				}
	            q=p->next;
	            p->next=q->next;
			    p=q->next;
			}
			printf("%d\n",p->num);
		}
		if(m==1)
		{
			printf("%d\n",monkey[n-1].num);
		}
	}
}


int main()
{
	int count = 0;
	int count1 = 0;
	int m,n;
	int temp;
	int inputcount=0;
	int result[102400];
	scanf("%d",&n);
	scanf("%d",&m);
while(m!=0 && n!=0)
{
        if(m==1)
        {
           result[inputcount] = n;
           inputcount++;
	   scanf("%d",&n);
	   scanf("%d",&m);
           continue;
        }
	int *monkey = (int*)malloc(sizeof(int)*n);
	int *tempmonkey = (int*)malloc(sizeof(int)*n);
	for(count=0; count<n; count++)
		monkey[count]= count+1;
    while(m<n)
	{
		temp = n/m;
        for(count=temp*m;count<n;count++)
			tempmonkey[count-temp*m]=monkey[count];
		for(count=0,count1=0;count<temp*m;count++)
		{
			if((count+1)%m)
			{
				tempmonkey[n-temp*m+count1]=monkey[count];
				count1++;
			}
		}
		n = n-temp;
		for(count=0;count<n;count++)
			monkey[count]=tempmonkey[count];
	}
	while(n>1)
	{
		temp = (m-1)%n+1;
		for(count=temp;count<n;count++)
		  tempmonkey[count-temp] = monkey[count];
		for(count=0;count<temp-1;count++)
		  tempmonkey[n-temp+count]=monkey[count];
        n--;
		for(count=0;count<n;count++)
			monkey[count]=tempmonkey[count];
	}
	result[inputcount]=monkey[0];
    inputcount++;
	scanf("%d",&n);
	scanf("%d",&m);
	free(monkey);
	free(tempmonkey);
}
for(count=0; count<inputcount; count++)
  printf("%d\n",result[count]);
	return 0;
}


struct person
{
	int number;
	int nextp;
}link[300];
int king(int n,int m)
{
	int i,t,h;
	for(i=1;i<=n;i++)
	{
		if(i==n)
			link[i].nextp=1;
		else
			link[i].nextp=i+1;
		link[i].number=i;
	}
	t=0;
	h=n;
	while(t<n-1)
	{i=0;
	while(i!=m)
	{
		h=link[h].nextp;
		if(link[h].number)
			i++;
	}
	link[h].number=0;
	t++;
	}
	for(i=1;i<=n;i++)
	{
		if(link[i].number)
			return(link[i].number);
	}
}
void main()
{
	int king(int a,int b);
	int a[1000],k,i,b[1000]={0};
	for(i=0;;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
		if(a[i]==0) break;
	}
	k=i;
	for(i=0;i<k;i++)
	{
		printf("%d\n",king(a[i],b[i]));
	}
}


//??????????...?????????--by ??? 2011.11.10
struct stu
{
    int num;
    struct stu * next;
};
int tiren(int x,int n,int m,struct stu * p)//???? x?????
{
    int i;
    struct stu * pp;
    if (x!=n)
    {
        for (i=1;i<=m-1;i++)
        p=(*p).next;
        pp=(*p).next;
        (*p).next=(*pp).next;
        tiren(x+1,n,m,p);
    }
    else
    printf("%d\n",(*p).num);
    return 0;
}

main()
{
    int n,m,i;
    struct stu * head,* p;
    scanf("%d %d",&n,&m);
    while ((n!=0))
    {
    head=p=(struct stu *)malloc(L);
    for (i=2;i<=n;i++)
    {
        (*p).num=i-1;
        p=(*p).next=(struct stu *)malloc(L);

    }
    (*p).next=head;
    (*p).num=n;
    tiren(1,n,m,p);
    scanf("%d %d",&n,&m);
    }

}


void main()
{
	typedef struct {
     int data;
	 int next;
	}Link;

   Link a[Max];
   int n,m;
   int i;
   int p;

 while(1){
   scanf("%d%d",&n,&m);
   if(n==0||m==0)
	   exit(0);
   for(i=0;i<=n;i++)
	   a[i].data=i,a[i].next=i+1;
   a[n].next=1;
   a[0].data=n;
   p=n;
   while(a[0].data>1)
   {

      for(i=1;i<m;i++)
		  p=a[p].next;
      a[p].next=a[a[p].next].next;
	  a[0].data--;

   }

   printf("%d\n",a[p].data);
 }

}


int main()
{
  int a,n,m,i;
  while(cin>>n>>m&&m!=0)
  { a=0;
    for(i=2;i<=n;i++) a=(a+m)%i;
    cout<<a+1<<endl;
  }
  return 0;
}


 int main()
  {
    int sum,test,i,last,j;
    int mon[300];
    int num=0,count=0;
    for(j=1; ;j++)
 {num=0;count=0;
 scanf("%d%d",&sum,&test);
 if (sum==0&&test==0) break;
 for(i=0;i<sum;i++)
        mon[i]=i+1;
    while(num<sum)
   {   for(i=0;i<sum;i++)
   {   if(mon[i]!=0)
                      {   count=count+1;
                          if(count==test)
                              {   num=num+1;
                                  if(num==sum)
                                  last=mon[i];
                                  mon[i]=0;
                                  count=0;
                              }
                      }
 }
 }
   printf("%d\n",last);
 }
  return 0;
 }


int chs(int *p0,int num,int *ptr,int skip);

int main()
{
  int n=0,m=0;
  int ppl[1000],king;
  int i;
  scanf("%d%d",&n,&m);
  while(n!=0&&m!=0)
  {
    for(i=0;i<n;i++)
    {
      ppl[i]=i+1;
    }
    for(i=n;i<1000;i++)
    {
      ppl[i]=0;
    }
    king=chs(ppl,n,ppl,m);
    printf("%d\n",king);
    scanf("%d%d",&n,&m);
  }
  return 0;
}

int chs(int *p0,int num,int *pchs,int skip)
{
  int *i=NULL;

  //????
  pchs=pchs+skip-1;
  while(pchs-p0>=num)
  {
    pchs=pchs-num;
  }

  //????
  if(num==1)
  {
    return *pchs;
  }
  //????
  else
  {
    //??
    if(pchs-p0==num-1)
    {
      return chs(p0,num-1,p0,skip);
    }
    //???
    else
    {
      for(i=pchs;i-p0<num-1;i++)
      {
        *i=*(i+1);
      }
      return chs(p0,num-1,pchs,skip);
    }
  }
}


int check(int N,int p)
{
	int k[400];
	int i,j,count=0;
	for(i=0;i<N;i++)
		k[i]=1;
	i=-1;
	for(j=1;j<N;j++)
	{
		count=0;
		do
		{
			i=(i+1)%N;
			count=count+k[i];
		}while(count<p);
		count=0;
		k[i]=0;
	}
	j=0;
	while(k[j]!=1)j++;
	return(j+1);
}

void main()
{
	int n,m,res,i;
	while(scanf("%d %d", &n, &m) && n != 0 && m != 0)
	{
		res=check(n,m);
		printf("%d\n",res);
	}
}


main()
{
    int m,n,count,j;
    scanf("%d %d",&m,&n);
    while (m != 0)
    {
        int monkey[310] = {0};
        for (int i = 0;i <= m-1;i ++)
            monkey[i]=2;
        j = -1;
        for (int i = 1;i <= m-1;i ++)
        {
            count = 1;
            while (count <= n)
            {
                j ++;
                switch(monkey[j])
                {
                    case 0:j = -1;break;
                    case 1:break;
                    case 2:count++;break;
                }

            }
            monkey[j] = 1;
        }
        for (int i = 0;i <= m - 1;i ++)
        {
             if (monkey[i]==2)
                printf("%d\n",i+1);
        }
        scanf("%d %d",&m,&n);
    }
}


int main(int argc, char* argv[])
{
	int n,m;
	int*p;
	int i,j,k;
   for(int a=0;a<344;i++)
   {
	 scanf("%d%d",&n,&m);
	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;
		}
	}
    if(n==0||m==0) break;
	free(p);
   }
	return 0;
}


int fun(int m,int n)
{
	int i,j=0,k,e;
	int r[300]={0};
	k=m;
	while(k>0)
	{
		for(i=0;i<m;i++)
	   {
		  if(r[i]<0)continue;
   	           r[i]=0;
	   }
		for(i=0;i<m;i++)
		{
			if(r[i]<0)continue;
			j++;
			r[i]=r[i]+j;
			if(r[i]%n==0)
			{
				r[i]=-1;
				k--;e=i;
			}
		}

	}
	return e+1;
}
int main()
{
int m,n,k;
while(1){
scanf("%d %d",&m,&n);
if(m==0&&n==0)break;
printf("%d\n",fun(m,n));
}
return 0;
}


int main()
{
	int n,m,i,j,a,k;
	while(cin>>n>>m&&m!=0)
	{
		a=0;
		for(i=2;i<=n;i++)
		{
			a=(a+m)%i;
		}
		cout<<a+1<<endl;
	}
    return 0;
}


/*****************************************************************/
/* ????????                                              */
/*    ???c[i][j]??????????????????????  */
/* c[i][j]=(c[i-1][j]+j) mod i???i??????j??????? */
/* ?????????                                            */
/*    ??i?1?N???????O(n)                              */
/* ??????c[N][M]                                           */
/* ?? 2010-11-29                                               */
/*****************************************************************/

//????????

//?????????
typedef struct inputandoutput{
	int n;
	int m;
	int king;
	struct inputandoutput  * next;
}LinkIO;

//??????????
typedef struct{
	LinkIO * head;
	LinkIO * tail;
	int    total;
}LinkIOHead;

//n????m??
int n,m;

//????????
int CheckInput(int num,int total){
	if (num<0)
	{
		return ISNOT_POSITIVEINT;
	}
	else if (num>total)
	{
		return ISMORETHAN_THREEHUNDRED;
	}
	else
	{
		return ISCORRECT;
	}
}

//???????
int GetUserInput(int * n,int * m){
	int checkresult=0;
	scanf("%d %d",n,m);
    checkresult=CheckInput(*n,300);
	if (checkresult==ISNOT_POSITIVEINT)
	{

		printf("error!\n");
	}
	else if (checkresult==ISMORETHAN_THREEHUNDRED)
	{

		printf("error!\n");
	}
	else
	{
		checkresult=CheckInput(*m,300);
		if (checkresult==ISNOT_POSITIVEINT)
		{
			printf("error!\n");
		}
		else if (checkresult==ISMORETHAN_THREEHUNDRED)
		{

			printf("error!\n");
		}
		else
		{
			if (*n==0 && *m==0)
			{

			}
			else if (*n==0 || *m==0)
			{
				printf("error!\n");
			}
			else{
				return 1;
			}
		}
	}
	return 0;
}

//??????n?m?????
void InsertIntoLinkIO( LinkIOHead * iohead ,int n,int m)
{
	LinkIO *p;
	p=(LinkIO *) malloc(sizeof(LinkIO));
	if (p==NULL)
	{
		printf("error!");
		printf("error,n=%d,m=%d!\n",n,m);
		return;
	}
	p->n=n;
	p->m=m;
	p->king=0;
	p->next =NULL;
	if (iohead->head==NULL && iohead->tail==NULL)
	{
		iohead->head=p;
		iohead->tail=p;
		iohead->total=1;
	}
	else
	{
		iohead->tail->next=p;
		iohead->tail=p;
		iohead->total=iohead->total+1;
	}
}

//????
int GetKing( int kn, int km )
{
	int king=0,i=0,j=0;
	int kresult=0;
	for (i=2;i<=kn;i++)
	{
		kresult=(kresult+km) % i;
	}
	return kresult+1;
}

//???
int main(){
	int inputresult=0;
	int king=0;
	LinkIOHead *iohead;
	LinkIO *p;

	iohead=(LinkIOHead *)malloc(sizeof(LinkIOHead));
	if (iohead==NULL)
	{
		printf("error!");
		return 0;
	}
	iohead->head=NULL;
	iohead->tail=NULL;
	iohead->total=0;

	inputresult=GetUserInput(&n,&m);
	while (inputresult==1)
	{
		InsertIntoLinkIO(iohead,n,m);
		inputresult=GetUserInput(&n,&m);
	}

	p=iohead->head;
	while (p!=NULL)
	{
		king=GetKing(p->n,p->m);
		p->king=king;
		p=p->next;
	}

	p=iohead->head;
	while (p!=NULL)
	{
		printf("%d\n",p->king);
		p=p->next;
	}

	if (iohead!=NULL)
	{
		free(iohead);
	}
	if (p!=NULL)
	{
		free(p);
	}

	return 0;
}


struct number{
  int num;
  struct number *next;
};
int main(void)
{
  int n,m,i;
  struct number *p,*head,*q;
  while(scanf("%d%d",&n,&m)&&n>0&&m>0){
    p=(struct number *) malloc(LEN);
    head=p;
    for(i=0;i<n-1;i++){
      p->num=i+1;
      p->next=(struct number *) malloc(LEN);
      p=p->next;
    }
    p->num=n;
    p->next=head;
    q=p;
    while(p->next!=p){
      for(i=0;i<m-1;i++) p=p->next;
      q=p;
      p=p->next;
      q->next=p->next;
      p=q;
    }
    printf("%d\n",p->num);
  }
  return 0;
}


int queue[90010];//??

int main()
{
   // freopen("1.in","r",stdin);
   // freopen("1.out","w",stdout);
    int n,m;
    while(1)
    {
    cin>>n>>m;
    if(n==0&&m==0)  break;
    int front=0,rear=n;//???????????????????
    for(int i=0;i<n;i++)
      queue[i]=i+1;
    for(int i=0;i<n-1;i++)
    {
            for(int j=1;j<m;j++)
            {
                    queue[rear++]=queue[front];
                    front++;
            }
            front++;
    }
    cout<<queue[front]<<endl;//???????????????
    }
    return 0;
}


struct mon
{
	int a;
	struct mon *next;
};

struct mon *app(int n)
{
	struct mon *newnode, *head, *thisnode;
	int i;

	for(i=1;i<=n;i++)
	{
		newnode=(struct mon *)malloc(sizeof(struct mon));
		newnode->a =i;
		if(i==1)
		{
			head=newnode;
			thisnode=head;
		}
		else
		{
			thisnode->next =newnode;
			thisnode=newnode;
		}

	}
	thisnode->next =head;
	return head;
}

void shuchu(int n, int m)
{
	struct mon *head;
	int i;
	head=app(n);

	do
	{
	for(i=1;i<m-1;i++)
		head=head->next ;
	head->next =head->next ->next ;
	head=head->next ;
	}while(head->next !=head);

	printf("%d\n",head->a );

}

int main()
{
	int n,m,sign=1;

	for(;sign;)
	{
		scanf("%d%d",&n, &m);
		if(m==1)
			printf("%d\n",n);
		else
		{
		if(n!=0)
			shuchu(n, m);
		else sign=0;
		}
	}

	return 0;
}


/*??*/
int king;
int monkeynum(int [],int);
void main()
{
	int m[20],n[20],i,j,k,start,casenum,monkey[300];
	for(i=0;;i++)
	{
		scanf("%d %d",&n[i],&m[i]);
		if(!m[i]) break;
	}
	casenum=i;
	for(i=0;i<casenum;i++)
	{
		for(j=0;j<300;j++)
			monkey[j]=0;
		for(j=0;j<n[i];j++)
			monkey[j]=1;
		start=0;
		while(monkeynum(monkey,n[i])!=1)
		{
				/*printf("%d\n",monkeynum(monkey,n[i]));*/
			k=m[i];
			while(k>0)
			{
				if(start==n[i]) start=0;
					/*printf("start=%d",start);
					getchar();*/
				if(monkey[start]) --k;
					/*printf("k=%d",k);
					getchar();*/
				++start;
			}
			monkey[start-1]=0;
				/*printf("kill monkey %d",start);
				getchar();
				printf("start=%d",start);
				getchar();*/
		}
		printf("%d",king);
		if(i<casenum-1) printf("\n");
	}
}
int monkeynum(int monkey[],int a)
{
	int i,num=0;
	for(i=0;i<a;i++)
		if(monkey[i])
		{
			num++;
			king=i+1;
		}
	return num;
}


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;
}


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()
{
        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;
}


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 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()
{

    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;
}


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);
}


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;
	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 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 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;
		}
	}
}


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;
}


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;
}


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,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();
             }


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;
}


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;
}
}
}


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 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;
}


int ysf(int n,int m);
int main(){

	int nz[500],mz[500];
	int s=0;
	for (int i=0;i<500;i++){
		scanf("%d %d",&nz[i],&mz[i]);
		s++;
		if (nz[i]==0&&mz[i]==0){break;}}

	for (int i=0;i<s;i++){
		while (nz[i]!=0&&mz[i]!=0){
			printf("%d\n",ysf(nz[i],mz[i]));
			break;
		}
	}
	return 0;
}

int ysf(int n,int m){
        if (n==1){
		    return 1;
		}else{
			return(ysf(n-1,m)+m-1)%n+1;
		}}


int calculate(int n,int m)
{
	int num,monkey[1000]={0},*p;
	int i;
	num=n;
	for(i=0;i<n;i++)
	{
		monkey[i]=1;
	}
	p=&monkey[0]-1;

	while(num!=1)
	{
		for(i=1;i<=m;i++)
		{
			p+=1;
			if(p>monkey+n-1)
				p-=n;
			while(*p==0)
			{
				p+=1;
				if(p>monkey+n-1)
				    p-=n;
			}
		}
		*p=0;
		num-=1;
	}
	i=0;
	while(monkey[i]==0)
		i+=1;
	return(i+1);
}

void main()
{
	int n,m,king;
	do{
		scanf("%d%d",&n,&m);
		if(n!=0&&m!=0)
		{
		    king=calculate(n,m);
		    printf("%d\n",king);
		}
	}while(n!=0&&m!=0);
}


int main(int argc, char* argv[])
{
	int m,n,k,i,turn,baoshu,ing,p;
	long begin,end;

	int monkey[300];
	int king[2];
	while(1){
	scanf("%d%d",&n,&m);//??n?m
	if(n!=0){
		for(i=0;i<n;i++){
	monkey[i]=i+1;}//????????monkey[300]???
		if(m==1){
		printf("%d\n",n);}
		else{
		for(i=0,k=0;i<n;i++){
			if((i+1)%m!=0){
		monkey[k]=i+1;
		k++;}}
		king[0]=0;
		king[1]=k;//?????????k?

		for(turn=2,begin=n+1,ing=0;ing!=1;turn++){//??????
			if(turn==2){
				end=begin+king[1];}//????????n+1??????n+k?
			else{
			begin=end;
			end=begin+ing;}
		for(baoshu=begin,ing=0;baoshu<end;baoshu++){
			if(baoshu%m!=0){
				p=baoshu-begin;
	    monkey[ing]=monkey[p];
		ing++;}}}//ing?????????????
		printf("%d\n",monkey[0]);}}

	else{
		break;}}
	return 0;}


 main()
{
	for (int i=0;;i++)
	{
    int m,n;
   scanf("%d%d",&m,&n);
    if (m==0)
    break;
    else
    {
	int a[1000];
    for(int i=0;i<m;i++)
        a[i]=1;
    int index=0;
    int loc;
    int t=0;
    int p=m;
    while(p>0)
    {
        if(a[index]==1)
        {
            ++t;
            if(t%n==0)
            {
                a[index]=0;
                loc=index;
                p--;
            }
        }
        if(index < m-1)
            index++;
        else
            index=0;
    }
    printf("%d\n",loc+1);
   }
   }
}


struct monkey
	{
		int num;
		struct monkey *next;
	};
void main()
{
	int kingf(int n,int m);
	int m=1,n=1;
	int king;
	for(;;)
	{
		scanf("%d%d",&n,&m);
		if(m==0&&n==0)break;
		if(m==1)printf("%d\n",n);
		else
		{
			king=kingf(n,m);
			printf("%d\n",king);
		}
	}
}
int kingf(int n,int m)
{
	int i;
	struct monkey monkey[800];
	struct monkey *p1=&monkey[0],*p2=&monkey[0];

	for(i=0;i<n-1;i++)
	{
		monkey[i].next=&monkey[i+1];
		monkey[i].num=i+1;
	}
	monkey[i].next=&monkey[0];
	monkey[i].num=i+1;

	for(;p1!=p1->next;p1=p1->next)
	{
		for(i=1;i<m-1;p1=p1->next,i++);
		p2=p1;
		p2=p2->next;
		p1->next=p2->next;
	}
	return p1->num;
}


int ysf(int m,int n)
{
    int *p;
    int i,j,k;
    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==1)
         {
              break;
         }
   }
   i=0;
   for(;i<n;i++)
   {
                if(p[i]==0)
                break;
   }
   free(p);
   return i+1;

}
int main()
{
    int n, m;
    scanf("%d%d",&n,&m);
    while(m!=0||n!=0)
    {

         printf("%d\n",ysf(m,n));
         scanf("%d%d",&n,&m);
    }

}


int main(void)
{
int i,j;
int s = 0;
int n;
int m;
for(j=1;j<=9;j++){
scanf("%d%d\n", &n,&m);
for (i = 1; i <= n; i++)
{
s = (s + m) % i;
}

printf("%d\n", s + 1);}

return 0;
}


int main()	{
	int a[100], b[100];
	int i, p = 1, q = 1;
	for (i = 0; p != 0 && q != 0; i ++)	{
		scanf("%d%d", &p, &q);
		a[i] = p; b[i] = q;
	};
	int j, aa, bb;
	int monkey[500];
	for (j = 0; j < i - 1; j ++)	{
		aa = a[j]; bb = b[j];
		int sum = 0;
		int ptr, s = 0;
		for (ptr = 0; ptr < aa; ptr ++)	monkey[ptr] = 1;
		for (ptr = 1; sum < aa - 1; ptr = (ptr + 1) % aa)	{
			if (monkey[ptr] == 1)	{
				s ++;
				if (s == bb)	{
					monkey[ptr] = 0;
					sum ++;
					s = 0;
				};
			};
		};
		int ans;
		for (ptr = 0; ptr < aa; ptr ++)	if (monkey[ptr] == 1)	ans = ptr;
		if (ans == 0)	ans = aa;
		printf("%d\n", ans);
	};
	return 0;
};


int sz[301];
int m,n;
int remove(int k);
int main(){
	int i,k;
	scanf("%d%d",&n,&m);
	while(m||n){
		for(i=0;i<n;i++){
			sz[i]=i+1;
		}
		for(k=0;n>1;n--){
			k=remove(k);
		}
		printf("%d\n",sz[0]);
		scanf("%d%d",&n,&m);
	}
	return 0;
}
int remove(int k){
	int i,t;
	t=(m+k-1)%n;
	for(i=t+1;i<n;i++){
		sz[i-1]=sz[i];
	}
	return t%(n-1);
}


int N,M;

int main(){
    int i;
    int alive,count;
    int x[320];

    while (1){
        scanf("%d%d",&N,&M);
        if (N==0 && M==0) break;
        alive = N;
        count = 0;
        memset(x,0,sizeof(x));
        for (i = 1; alive > 1 ;i = i%N+1){
            if (!x[i]){
                count++;
                if (count == M){
                    x[i] = 1;
                    alive --;
                    count = 0;
                }
            }
        }
        for (i = 1; i <= N ;i++)
           if(!x[i]) printf("%d\n",i);
    }

    return 0;
}


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;

		s->l->r=s->r;

		s->r->l=s->l;
		p=s->r;
	}
	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 Joseph(int n,int m)
{
	int l[300];
	int i,j;
	for(i=0;i<n-1;i++)
	{
		l[i] = i+1;
	}
	l[n-1] = 0;
	do
	{
		for(j=1;j<m;j++)
		{
			i = l[i];
		}
		l[i] = l[l[i]];
	}
	while(l[i] != i);
	return(i+1);
}
void main()
{
	int n,m;
	while(1)
	{
		scanf("%d %d",&n,&m);
		if(n==0 && m==0)
			break;
		printf("%d\n",Joseph(n,m));
	}
}


struct monkey
{
	int num;
	int nextmonk;
};

void countoff(int n,int m)
{
	struct monkey *monk
		=(struct monkey *) malloc(LEN*n);
	int i,j,sum=n,last;

	for(i=1;i<=n;i++){
		monk[i].num=i;
		monk[i].nextmonk=i+1;
	}
	monk[n].nextmonk=1;
	last=i=n;

	while(sum>1){
		j=0;
		while(j!=m){
			i=monk[i].nextmonk;
			if(monk[i].num){
				j++;
				if(j!=m) last=monk[i].num;
			}
		}
		monk[i].num=0;
		sum--;
	}
	printf("%d\n",last);
}

void main()
{
	int n,m;
	while(1){
		scanf("%d %d",&n,&m);
		if(m==0&&n==0) break;
		countoff(n,m);
	}
}


int main()
{
	int m,n;
	int in[320];   //????
	int i;
	int num;        //????
	int last;
	while(1>0)
	{
		scanf("%d%d",&n,&m);
		if((n==0)||(m==0))
			break;
		else
		{
			for(i=1;i<=n;i++)
			{
				in[i]=i;   //?????
			//	printf("%d\n",in[i]);  //debug
			}
			//??????
			last=1;
			num=n;
			while(num!=1)
			{
				last+=m-1;   //??????
			//	printf("%d",last);   //debug
				while(last>num)
				{
					last=last-num;
				}
			//	printf("   %d\n",last);//debug
				num--;
				for(i=last;i<=n;i++)
				{
					in[i]=in[i+1];
				}
			//	printf("\n%d",num);  //debug
			/*	for(i=1;i<=num;i++)
				{
					printf(" %d",in[i]);
				}
				printf("\n");  */   //debug
			}
			printf("%d\n",in[1]);
		}
	}
	return 0;
}


int main()
{
int n, m, s;
while( cin >> n >> m && n )
{
s = 0;
for( int i = 2; i <= n; i ++ )
s = ( s + m ) % i;
cout << s + 1 << endl;
}
return 0;
}


struct monkey
	{
		int number;
		int selected;
		struct monkey *next;
	};
void maintoo(int,int);
void main()
{

	int n[20],m[20],i,j;
	for(i=0;;i++)
	{
		scanf("%d%d",&n[i],&m[i]);
		if(!m[i]) break;
	}
	for(j=0;j<i;j++)
		maintoo(n[j],m[j]);
}
void maintoo(int n,int m)
{
	struct monkey *p1,*p_this,*p_next;
	int i;
	p1=(struct monkey *)malloc(sizeof(struct monkey));
	p_this=p1;
	for(i=1;i<n;i++)
	{
		p_this->number=i;
		p_this->selected=1;
		p_next=(struct monkey *)malloc(sizeof(struct monkey));
		p_this->next=p_next;
		p_this=p_next;
	}
	p_this->number=n;
	p_this->selected=1;
	p_this->next=p1;
	do
	{
		i=0;
		do
		{
			p_this=p_this->next;
			if(p_this->selected) i++;
		}
		while(i<m);
		p_this->selected=0;
		n--;
	}
	while(n>0);
	printf("%d\n",p_this->number);
}


struct node
{
	int data;
	struct node *next;
};

int choose(int n, int m)
{
	int i=2;
	struct node *head;
	struct node *p1, *p2;
	head=(struct node*)malloc(len);
	head->data = 1;
	p1 = head;
	for(i=2; i<=n; i++)
	{
			p2 = (struct node*)malloc(len);
			p2->data = i;
			p1->next = p2;
			p1 = p2;
	}
	p1->next = head;
	i=1;

	while(head->next != head)
	{
		for(i=1; i<m; i++)
		{
			p1 = head;
			head = head->next;
		}
		p1->next = head->next;
		head = head->next;
	}
	return(head->data);
}

int main()
{
	int n=1, m=1, result;
	while(n!=0 && m != 0)
	{
		scanf("%d%d", &n, &m);
		if(n==0 && m==0)
			break;
		printf("%d\n", choose(n, m));
	}
	return 0;
}


int main()
{
	int n;
	int m;
	do
	{
		int *p;
		scanf("%d %d",&n,&m);
		if ( n == 0 )
		{
			break;
		}
		p = ( int * ) malloc ( sizeof (int) * n );

		int i = 0 ;
		for ( i = 0 ; i < n ; i++ )
		{
			*(p+i) = 1 ;
		}
		int count = 0;
		int count2 = n;
		i = 0;
		do
		{
			if ( *(p+i) != 0 )
			{
				count++;
				if ( count == m )
				{
					//printf("%d\n",i);
					count2--;
					count = 0;
					*(p+i) = 0;
				}
			}
			if ( i < n - 1 )
			{
				i++;
			}
			else
			{
				i = 0;
			}
			//printf("%d\n",i);
		}while(count2 > 1);
		i = 0 ;
		do
		{
			if ( *(p+i) == 1 )
			{
				printf("%d\n",i+1);
				break;
			}
			i++;
		}while ( i < n );
		free(p);
	}while ( n > 0 );
	return 0;
}


// ???.cpp : ??????????????
//

int yuesefu(int key,int n);
int main()
{
	int monkeys[300];
	int tmp[300];
	for(int i=0;i<300;i++)
		tmp[i]=monkeys[i]=i;
	int key,num;
	int i=0;
	while(1)
	{
	scanf("%d %d",&num,&key);
	if(key==0)
		break;
    int final=yuesefu(key,num);
	if(i++)
	    printf("\n");
	printf("%d",final);

	}
	return 0;
}

int yuesefu(int m,int n)
{
	int leave=1;
	for (int i=2;i<=n;i++)
	{
		int j=((m-1)%i+leave-1)%(i-1)+1;
		if (j>=(m-1) % i+1)
          leave=j+1 ;
		else
		leave=j;
	}
	return leave;

}


int main()
{
    int i,count,out,n,m;

    count=0;
    out=0;

    while(1){
        scanf("%d%d",&n,&m);
        int num[n+1];
        if(m==0&&n==0)
        break;
        for(i=1;i<=n;i++){
      num[i]=0;
    }
    i=1;
    do
    {

        if(num[i]==0)
            count++;
        if(count==m){
            num[i]=1;
            out++;
            count=0;
        }
        if(i==n)
            i=0;
        i++;
    }
    while(out<n-1);
    for(i=1;i<=n;i++){
    if(num[i]==0){
    printf("%d\n",i);
    count=0;
    out=0;
    }
    }
    }
    return 0;
}


int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m))
    {
        if(n==0) break;
        int i=0,s=0;
        for(i=2;i<=n;i++)
                s=(s+m)%i;
                cout<<s+1<<endl;
    }
    return 0;
}


int main(int argc, char* argv[])
{
	int n,m;
	int x;
	for(x=0;;x++){
	scanf("%d%d",&n,&m);
	if(n==0&&m==0){
		break;
	}else{
	int sz[300]={0};

	int i;
	int j;
	int begin=0;
	for(i=n;i>1;i--){
		int M;
		M=m%i;
		if(M==0){
			M=i;              //??//
		}else{
			M=m%i;
		}
		int count=0;
		for(j=begin;j<n;j++){
			if(sz[j]==0){
				count++;
			}
		}

		int monkey_count=0;
		if(count>=M){
			for(j=begin;j<n;j++){
				if(sz[j]==0){
					monkey_count++;

				if(monkey_count==M){
					begin=j;
					sz[j]=1;
					break;
				}
				}
			}
		}else{
			for(j=begin;j<n;j++){
				if(sz[j]==0){
					monkey_count++;
				}
			}
			for(j=0;j<n;j++){
				if(sz[j]==0){
					monkey_count++;
					if(monkey_count==M){
						begin=j;
						sz[j]=1;
						break;
					}
				}
			}
		}
	}

	for(i=0;i<n;i++){
		if(sz[i]==0){
			printf("%d\n",i+1);
		}
	}
	}
	}

	return 0;
}


int main()
{
	int n[100],m[100];
	int a[300];
	int count=0;
	while(1)
	{
	    scanf("%d %d",n+count,m+count);
	    if(n[count]==0&&m[count]==0)
	    {
             break;
        }
        count++;
    }
    //count =count -1;
    int i=0;
    int j=0;
    int k=0;
    int l=0;
    int p=0;
    int kill=0;
    int kill2=0;
    int start=0;
    int now=0;
    int minmj=0;
    for(i=0;i<count;i++)
    {
        for(k=0;k<n[i];k++)
        {
            a[k]=1;
        }
        start=0;
        for(j=n[i];j>1;j--)
        {
            kill=(m[i])%j;
            if(kill==0)
            {
                minmj=m[i];
                if(j<minmj)
                {
                minmj=j;
                }
                kill=minmj;;
            }
            kill2=0;
            for(l=start;l<(start+n[i]);l++)
            {
                now=l%n[i];

                kill2=kill2+a[now];
                if(kill2==kill)
                {
                    a[now]=0;
                    for(p=now+1;p<(now+n[i]);p++)
                    {
                        if(a[p%n[i]]==1)
                        {
                            //printf("%d ",(p%n[i]+1));
                            start=p%n[i];
                            break;
                        }
                    }
                    //start=now+1;
                    //printf("%d",(now+1));
                    break;
                    //printf("%d\n",(l+1));
                }
            }
        }
        //printf("\n");
        for(k=0;k<n[i];k++)
        {
            if(a[k]==1)
            {
                printf("%d\n",(k+1));
            }
        }

    }

	return 0;
}


int j;
struct monkey//???????????????????
{
	int num;
	struct monkey *next;
};
struct monkey *create(int c)    //????create???????????????
{
	struct monkey *head,*p1,*p2;//??????monkey?????head?p1?p2
	int i;
	p1=p2=(struct monkey*)malloc(LEN);//(struct monkey*)malloc(LEN)???????p1?p2
	head=p1;                    //p1?????head
	head->num=1;                //?1???head??num
	for(i=1,p1->num=1;i<c;i++)
	{
		p1=(struct monkey*)malloc(LEN);
		p1->num=i+1;//???????
		p2->next=p1;//????????
		p2=p1;
	}
	p2->next=head;
	return head;//??head?num=1?next???????
}
struct monkey *findout(struct monkey *start,int d)//????findout?????????monkey
{
	int i;
	struct monkey *p;
	p=start;
	for(i=1;i<d-1;i++)
		p=p->next;//?start???????????????d-2??
	return p;//????????????
}
struct monkey *letout(struct monkey *last)
{
	struct monkey *out,*next;
	out=last->next;
	last->next=out->next;//????????????????????
	next=out->next;
	free(out);
	return next;
}
int main()
{
	int n[301],m[300],k=0,c,d;
	while(1)
	{
		scanf("%d %d",&n[k],&m[k]);//?????
		k++;
		if(n[k-1]==0&&m[k-1]==0)
			break;
	}
	for(j=0;j<k-1;j++)       //???j+1???
	{
		int i,king;
		struct monkey *p1,*p2;
		if(n[j]==1)
		{
			king=n[j];
		}
		else if(m[j]==1)
		{
			king=n[j];
		}
		else
		{
			c=n[j];
			p1=p2=create(c);
			for(i=1;i<c;i++)//??????
			{
				d=m[j];
				p2=findout(p1,d);
				p1=p2;
				p2=letout(p1);//p1?????last?p2????????????
				p1=p2;
			}
			king=p2->num;//p2????num???king
			free(p2);
		}
		printf("%d\n",king);
	}
return 0;
}


int main()
{
	int n, m;

	while (1) {
		cin >> n >> m;
		if (n == 0 && m == 0)
			break;
		int a[500];
		memset(a, 0, sizeof(a));
		int tmp = n, start = -1;

		while (tmp > 1) {
			int mtmp = (m-1) % tmp + 1;
			unsigned i = 0;
			while (i < mtmp) {
				start = (start+1) % n;
				while (a[start] == 1)
					start = (start+1) % n;
				++i;
			}
			a[start] = 1;
			--tmp;
		}
		for (unsigned i = 0; i < n; ++i) {
			//cout << a[i] << " ";
			if (a[i] != 1) {
				cout << i + 1 << endl;
				break;
			}
		}
	}
	return 0;
}


int bianhao(int m,int n);
int main(){
	int i,a,t;
	int m[10000],n[301];

	for(i=0;i>-1;i++){
		scanf("%d""%d",&n[i],&m[i]);
		if((m[i]==0)&&(n[i]==0)){
			break;
		}
	}
	for(t=0;t<i;t++){
		a=bianhao(m[t],n[t]);
        printf("%d\n",a);
	}
     return 0;
}

int bianhao(int m,int n)
{
    int s=0,i;
    for(i=2;i<=n;i++){
	    s=(s+m)%i;
	}
	s=s+1;
	return s;
}


int main(){
    int n[100],m[110];
    int i,j,k;
    int t,times;
    int a[3000],b[10000],c[3000];
    for(times=1;times<1000;times++)
    {scanf("%d %d\n",&n[times],&m[times]);if(n[times]==0)break;
               for(i=1;i<=n[times];i++) {a[i]=i;}
                if(n[times]==1)a[times]=1;
      while(n[times]>1){
       t=m[times]%n[times];
       i=1;
       n[times]--;
           for(i=1;i<=n[times];i++){
           if(i+t-1<=n[times])b[i]=a[t+i];
           else{b[i]=a[t+i-n[times]-1];}  }
           for(j=1;j<=n[times];j++)
              {a[j]=b[j];}
     }

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

              return 0;}


void main()
{
	int n[100],m[100];
	int i,j,y,x;

    struct monkey
	{
		int num;
		struct monkey *next;
	};
	struct monkey mon[300];
	struct monkey *head,*p;
	int k=-1;
		do
		{   k++;
          scanf("%d %d",&n[k],&m[k]);

		}
		while(n[k]!=0);
		for(i=0;i<k;i++)
		{
			for(j=0;j<n[i];j++)
			{
			mon[j].num=j+1;
			mon[j].next=&mon[j+1];
			}
			mon[n[i]-1].next=&mon[0];
			head=&mon[n[i]-1];
			 while((head->next)!=head)
			{
				x=1;
				while(x<m[i])
				{

					head=head->next;
					x++;
				}

				head->next=(head->next)->next;
			}
	      printf("%d\n",head->num);
		}
}


int main ()
{int n=1,m=1,king=0,left=0,count=0;
for (int i1=0;n!=0&&m!=0;i1++)
{  count=0;
int houzi[300];
    scanf ("%d%d\n",&n,&m);
    if (n==0&&m==0) {break;}
    left=n;
    for (int i2=0;i2<n;i2++)
    {
        houzi[i2]=1;
    }
    for (int i3=0;left!=1;i3++)
    {        if (i3>n-1) {i3=i3%(n);}
        if (houzi[i3]==0)
        {
            continue;
        }
        count++;

        if (count==m) {houzi[i3]=0;left--;count = 0;}
        if (count>m) {count=count%m;}
    }
    for (int i4=0;i4<n;i4++)
    {
        if (houzi[i4]==1) {king=i4+1;break;}
    }
    printf("%d\n",king);
}
    return 0;
}


int main()
{
	int m, n, i, j, count;
	while(1)
	{
		int flag[400] = {0};
		cin >> n >> m;
		if(n == 0 && m == 0)
			break;
		count = 0;
		i = 1;
		j = 1;
		while(1)
		{
			if(flag[j] == 0)
			{
				if(i == m)
				{
					flag[j] = -1;
					i = 1;
					count++;
				}
				else
					i++;
			}
			if(count == n)
			{
				cout << j << endl;
				break;
			}
			if(j == n)
				j = 1;
			else
				j++;
		}
	}
	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 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;
}


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);
	}

}


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]);
	}
}


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;
}


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 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,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;
}


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 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;
}