#import <stdio.h>
#import <Foundation/Foundation.h>

int main(int argc, char **argv) {
  NSInteger N, K;
  scanf("%ld %ld", &N, &K);
  NSInteger mod1 = N % K;
  NSInteger mod2 = abs(mod1 - K);
  NSInteger result = MIN(mod1, mod2);
  printf("%ld", result);
}