import std.algorithm;
import std.array;
import std.conv;
import std.stdio;

void main()
{
  long[] n_and_k;
  long n;
  long k;
  
  n_and_k = readln.split.to!(long[]);
  n = n_and_k[0];
  k = n_and_k[1];
  
  writeln(min(n % k, k - n % k));
}
