#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define ll long long

ll mabs(ll a, ll b)
{
  return (a > b) ? a - b : b - a;
}

int main(void)
{
  ll n, k, m;
  scanf("%lld%lld", &n, &k);
  if (n >= k)
  {
    ll a = n / k;
    n = n - (a * k);
  }
  m = mabs(n, k);
  if (m < n)
    printf("%lld\n", m);
  else
    printf("%lld\n", n);

  return 0;
}