include <stdio.h>
#include <stdlib.h>
 
int main(void){
  long long int n, k, i, f = 0, s, t;
  
  scanf("%d %d", &n, &k);
  
  n = (n % k);
  s = n;
  
  do{
    t = n-k;
    n = abs(n);
    
    if(n < s){
      s = n;
    }else if(n == s){
      f = 1;
    }
  }while(f == 0);
  
  printf("%d", s);
  
  return 0;
}