#include<stdio.h>
#include<math.h>

int main(){
  int n,k;
  int t;
  int ans1,ans2;

  scanf("%d %d", &n, &k);

  if(k > n){
    t = k; k = n; n = t;
  }

  ans1 = n % k;
  ans2 = abs(ans1 - k);

  if(ans1 > ans2) printf("%d\n", ans2);

  else printf("%d\n", ans1);

  return 0;
}