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

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