//for joseph problem#include <iostream>usingnamespacestd;intjoseph(intn,intm){intflat[300];for(inti=0;i<n;i++)flat[i]=1;for(inti=0,count=n,mod=m-1;count!=1;i=((++i)%n)){if(flat[i]==1){if(mod==0){flat[i]=0;count--;}mod=(mod-1)%m;}}for(inti=0;i<n;i++){if(flat[i]==1)returni+1;}}intmain(){intset_n[100],set_m[100];intcount=0;while(true){intn,m;cin>>n>>m;if((n==0)&&(m==0)){break;}else{set_n[count]=n;set_m[count]=m;count++;}}for(inti=0;i<count;i++)cout<<joseph(set_n[i],set_m[i])<<endl;return0;}