import java.util.*;

public class Main 
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		if(n > k)
		{ 
			long c = n%k;
			long d = Math.abs(c - k);
			
			if(c < d)
			{
				System.out.println(c);
			}
			else
			{
				System.out.println(d);
			}
			
			
			System.out.println();
		}
		else
		{
			long a = Math.abs(n - k);
			long b = Math.abs(a - k);
			
			if(a < b)
			{
				System.out.println(a);
			}
			else
			{
				System.out.println(b);
			}
		}
			
		
		scan.close();
	}
} import java.util.*; util.* java public class Main 
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		if(n > k)
		{ 
			long c = n%k;
			long d = Math.abs(c - k);
			
			if(c < d)
			{
				System.out.println(c);
			}
			else
			{
				System.out.println(d);
			}
			
			
			System.out.println();
		}
		else
		{
			long a = Math.abs(n - k);
			long b = Math.abs(a - k);
			
			if(a < b)
			{
				System.out.println(a);
			}
			else
			{
				System.out.println(b);
			}
		}
			
		
		scan.close();
	}
} Main public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		if(n > k)
		{ 
			long c = n%k;
			long d = Math.abs(c - k);
			
			if(c < d)
			{
				System.out.println(c);
			}
			else
			{
				System.out.println(d);
			}
			
			
			System.out.println();
		}
		else
		{
			long a = Math.abs(n - k);
			long b = Math.abs(a - k);
			
			if(a < b)
			{
				System.out.println(a);
			}
			else
			{
				System.out.println(b);
			}
		}
			
		
		scan.close();
	} main {
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		if(n > k)
		{ 
			long c = n%k;
			long d = Math.abs(c - k);
			
			if(c < d)
			{
				System.out.println(c);
			}
			else
			{
				System.out.println(d);
			}
			
			
			System.out.println();
		}
		else
		{
			long a = Math.abs(n - k);
			long b = Math.abs(a - k);
			
			if(a < b)
			{
				System.out.println(a);
			}
			else
			{
				System.out.println(b);
			}
		}
			
		
		scan.close();
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long n = scan.nextLong(); n scan.nextLong() scan.nextLong scan long k = scan.nextLong(); k scan.nextLong() scan.nextLong scan if(n > k)
		{ 
			long c = n%k;
			long d = Math.abs(c - k);
			
			if(c < d)
			{
				System.out.println(c);
			}
			else
			{
				System.out.println(d);
			}
			
			
			System.out.println();
		}
		else
		{
			long a = Math.abs(n - k);
			long b = Math.abs(a - k);
			
			if(a < b)
			{
				System.out.println(a);
			}
			else
			{
				System.out.println(b);
			}
		} n > k n k { 
			long c = n%k;
			long d = Math.abs(c - k);
			
			if(c < d)
			{
				System.out.println(c);
			}
			else
			{
				System.out.println(d);
			}
			
			
			System.out.println();
		} long c = n%k; c n%k n k long d = Math.abs(c - k); d Math.abs(c - k) Math.abs Math c - k c k if(c < d)
			{
				System.out.println(c);
			}
			else
			{
				System.out.println(d);
			} c < d c d {
				System.out.println(c);
			} System.out.println(c) System.out.println System.out System System.out c {
				System.out.println(d);
			} System.out.println(d) System.out.println System.out System System.out d System.out.println() System.out.println System.out System System.out {
			long a = Math.abs(n - k);
			long b = Math.abs(a - k);
			
			if(a < b)
			{
				System.out.println(a);
			}
			else
			{
				System.out.println(b);
			}
		} long a = Math.abs(n - k); a Math.abs(n - k) Math.abs Math n - k n k long b = Math.abs(a - k); b Math.abs(a - k) Math.abs Math a - k a k if(a < b)
			{
				System.out.println(a);
			}
			else
			{
				System.out.println(b);
			} a < b a b {
				System.out.println(a);
			} System.out.println(a) System.out.println System.out System System.out a {
				System.out.println(b);
			} System.out.println(b) System.out.println System.out System System.out b scan.close() scan.close scan String[] args args public class Main 
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		if(n > k)
		{ 
			long c = n%k;
			long d = Math.abs(c - k);
			
			if(c < d)
			{
				System.out.println(c);
			}
			else
			{
				System.out.println(d);
			}
			
			
			System.out.println();
		}
		else
		{
			long a = Math.abs(n - k);
			long b = Math.abs(a - k);
			
			if(a < b)
			{
				System.out.println(a);
			}
			else
			{
				System.out.println(b);
			}
		}
			
		
		scan.close();
	}
} public class Main 
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		if(n > k)
		{ 
			long c = n%k;
			long d = Math.abs(c - k);
			
			if(c < d)
			{
				System.out.println(c);
			}
			else
			{
				System.out.println(d);
			}
			
			
			System.out.println();
		}
		else
		{
			long a = Math.abs(n - k);
			long b = Math.abs(a - k);
			
			if(a < b)
			{
				System.out.println(a);
			}
			else
			{
				System.out.println(b);
			}
		}
			
		
		scan.close();
	}
} Main
import java.util.*;

public class Main 
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		if(n > k)
		{ 
			long c = n%k;
			long d = Math.abs(c - k);
			
			if(c < d)
			{
				System.out.println(c);
			}
			else
			{
				System.out.println(d);
			}
			
			
			System.out.println();
		}
		else
		{
			long a = Math.abs(n - k);
			long b = Math.abs(a - k);
			
			if(a < b)
			{
				System.out.println(a);
			}
			else
			{
				System.out.println(b);
			}
		}
			
		
		scan.close();
	}
}