import java.util.*;
public class Main{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		if(n % k < k - (n %k))
			k = n % k;
		else 
			k = k - (n %k);
		System.out.println(k);
	}
} import java.util.*; util.* java public class Main{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		if(n % k < k - (n %k))
			k = n % k;
		else 
			k = k - (n %k);
		System.out.println(k);
	}
} Main public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		if(n % k < k - (n %k))
			k = n % k;
		else 
			k = k - (n %k);
		System.out.println(k);
	} main {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		if(n % k < k - (n %k))
			k = n % k;
		else 
			k = k - (n %k);
		System.out.println(k);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n = sc.nextLong(); n sc.nextLong() sc.nextLong sc long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc if(n % k < k - (n %k))
			k = n % k;
		else 
			k = k - (n %k); n % k < k - (n %k) n % k n k k - (n %k) k (n %k) n k k = n % k k n % k n k k = k - (n %k) k k - (n %k) k (n %k) n k System.out.println(k) System.out.println System.out System System.out k String[] args args public class Main{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		if(n % k < k - (n %k))
			k = n % k;
		else 
			k = k - (n %k);
		System.out.println(k);
	}
} public class Main{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		if(n % k < k - (n %k))
			k = n % k;
		else 
			k = k - (n %k);
		System.out.println(k);
	}
} Main
import java.util.*;
public class Main{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		if(n % k < k - (n %k))
			k = n % k;
		else 
			k = k - (n %k);
		System.out.println(k);
	}
}