import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main() {
  long n, k;
  readf (" %d %d", &n, &k);
  //x := abs (x - k)
  n %= k;
  n = min (n, abs (n - k));
  writeln (n);
}

