/*
	0085:Joseph's Potato
*/
#include	<iostream>
#include	<list>

using namespace std;

typedef pair<int, bool> P_IB;

int main(void) {
	while(1) {
		int n, m;
		list< P_IB > listPTT;
		
		cin >> n >> m;
		if(n == 0 && m == 0) { break; }
		for(int i = 0; i < n; i++) {
			listPTT.push_back( P_IB(i, true) );
		}
		list< P_IB >::iterator it = listPTT.begin();
		while(1) {
			for(int cost = m; cost > 0; cost--) {
				it++;
				if( it == listPTT.end() ) { it = listPTT.begin(); }
				if((*it).second == false) { cost++; }
			}
			/*cout << (*it).first << endl;*/
			if(n == 1) { break; }
			n--;
			(*it).second = false;
		}
		cout << (*it).first << endl;
	}
	return 0;
}