#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

int main(){
  int n,m,i;
  bool check[1010];

  for(;;){
    scanf("%d %d",&n,&m); if(n == 0 && m == 0) break;
    rep(i,n) check[i] = true;

    int cnt = 0,ans;
    for(i = 1; i < 50000000; i++){
      if(check[i%n]) cnt++;
      if(cnt == m){
        if(check[i%n]){
          check[i%n] = false;
          ans = i%n;
          cnt = 0;
        }
      }
    }

    printf("%d\n",ans);
  }

  return 0;
}