#include<iostream>
#include<cmath>
#include<string.h>
using namespace std;

int memo[1001][1001]={0};

int f(int n,int m){
	if(n==1){
		return 0;
	}
	else if(memo[n][m]!=0){
		return memo[n][m];
	}
	else{
		return memo[n][m]=(f(n-1,m)+m)%n;
	}
}

int main(){
	int n,m,ans;	
	while(cin>>n>>m,n&&m){
		ans=f(n,m);
		ans++;
		cout<<ans<<endl;
		memset(memo,0,sizeof(memo));
	}
}