#include <stdio.h>

int main(int argc, char const *argv[]) {
  int N, K;
  int former;

  scanf("%d", &N);
  scanf("%d", &K);

  former = N - 1;
  while (N != former) {
    former = N;
    if (N > K) {
      N = N - K;
    }else{
      N = K - N;
    }
  }

  printf("%d\n", N);

  return 0;
}
