'use strict';

var Main = function Main(input) {
  var input_sp = input.split(' ').map(function (e) {
    return BigInt(e);
  });
  var N = input_sp[0];
  var K = input_sp[1];
  var x_prev = N;
  var x = N;
  var result;

  while (true) {
    x = x_prev - K;
    x = x > 0 ? x : BigInt(-1) * x;

    if (x === K) {
      result = 0;
      break;
    }

    if (x < K) {
      if (x < K / BigInt(2)) {
        result = x;
        break;
      }
    }

    x_prev = x;
  }

  console.log(Number(result));
};

Main(require("fs").readFileSync("/dev/stdin", "utf8"));