#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<complex>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef complex<double> P;

#define MAX 1010

int josephus(int n, int k, int s) {
  long x = k * s;
  while (x > n)
    x = ((x - n) * k - 1) / (k - 1);
  return (int) x - 1;
}

int main(){
  int n, m;
  while(cin>>n>>m, n+m)
    cout << josephus(n, m, n) + 1 << endl;
  return 0;
}