#include <iostream> #include <cstdio> using namespace std; int main() { int n, k; while ( cin >> n >> k && n && k ) { if ( 1 == n ) { cout << "1" << endl; continue; } int p[n + 1], kill, temp = 1, total = n; for ( int i = 1; i <= n; i++ ) p[i] = i; while ( total > 1 ) { int count = 0; while ( count < k ) { if ( temp > n ) temp = 1; if ( p[temp] ) count++; temp++; } kill = temp - 1; p[kill] = 0; count = 0; while ( count < k ) { if ( temp > n ) temp = 1; if ( p[temp] ) count++; temp++; } p[kill] = p[temp - 1]; p[temp - 1] = 0; temp = kill + 1; total--; } if ( p[kill] == 1 ) cout << "1" << endl; else cout << n - ( p[kill] - 1 ) + 1 << endl; } return 0; }