#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void){
  long int initial_value = 0;
  long int drawn_number = 0;
  long int distance = 0;
  scanf("%ld %ld",&initial_value, &drawn_number);
  if (initial_value > drawn_number){
    distance = initial_value % drawn_number;
  } else {
    distance = drawn_number;
  }
  long int revice_distance = abs(initial_value - distance);
  long int min_number = fmin(distance, revice_distance);
  min_number = fmin(min_number, initial_value);
  printf("%ld",min_number);
  return 0;
}