#include <iostream>
using namespace std;

int jos(int n, int k){
	int i;
	for( i = k-1 ; i<(k-1)*n ; i = (k*i)/(k-1)+1 );
	return (k*n-i);
}

int main(){
	int n,k;
	while( cin >> n >> k , n || k ){
		cout << jos(n,k) << endl;
	}
}