module c;

import std.conv, std.stdio;
import std.algorithm, std.array, std.string, std.range;

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