/* In the name of Allah */

#include <bits/stdc++.h>
using namespace std;

// const int INF = 20000000;

#define ll long long
#define _SetBit(A, k) (A[(k / 32)] |= (1 << (k % 32)))
#define _ClearBit(A, k) (A[(k / 32)] &= ~(1 << (k % 32)))
#define _CheckBit(A, k) (A[(k / 32)] & (1 << (k % 32)))
#define SetBit(x, k) (x |= (1 << k))
#define ClearBit(x, k) (x &= ~(1 << k))
#define CheckBit(x, k) (x & (1 << k))

int main()
{
  unsigned ll n, k;
  cin >> n >> k;
  while (n > k)
  {
    n = n % k;
  }

  if (n < k - n)
    cout << n << "\n";
  else
    cout << k - n << "\n";

  return 0;
}