#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
 
int main()
{
	ll N,K,x,y;
	cin >> N >> K;
	
	x = N%K;
	y = (x - K)*(-1);

	cout << min(x,y);

	cin >> N;
	return 0;
}