import java.io.*;
import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;

public class Main
{	
	public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String []s=br.readLine().split(" ");
		
		long N=Long.parseLong(s[0]);
		long K=Long.parseLong(s[1]);
		
		out.println(min(N%K, abs(K-(N%K))));
			
		
		
	}
	
	
}

 import java.io.*; io.* java import java.util.*; util.* java import static java.lang.System.*; import static java.lang.System.*; import static java.lang.System.*; import static java.lang.Math.*; import static java.lang.Math.*; import static java.lang.Math.*; public class Main
{	
	public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String []s=br.readLine().split(" ");
		
		long N=Long.parseLong(s[0]);
		long K=Long.parseLong(s[1]);
		
		out.println(min(N%K, abs(K-(N%K))));
			
		
		
	}
	
	
} Main public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String []s=br.readLine().split(" ");
		
		long N=Long.parseLong(s[0]);
		long K=Long.parseLong(s[1]);
		
		out.println(min(N%K, abs(K-(N%K))));
			
		
		
	} main {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String []s=br.readLine().split(" ");
		
		long N=Long.parseLong(s[0]);
		long K=Long.parseLong(s[1]);
		
		out.println(min(N%K, abs(K-(N%K))));
			
		
		
	} BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String []s=br.readLine().split(" "); s br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long N=Long.parseLong(s[0]); N Long.parseLong(s[0]) Long.parseLong Long s[0] s 0 long K=Long.parseLong(s[1]); K Long.parseLong(s[1]) Long.parseLong Long s[1] s 1 out.println(min(N%K, abs(K-(N%K)))) out.println out min(N%K, abs(K-(N%K))) min N%K N K abs(K-(N%K)) abs K-(N%K) K (N%K) N K String []args args public class Main
{	
	public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String []s=br.readLine().split(" ");
		
		long N=Long.parseLong(s[0]);
		long K=Long.parseLong(s[1]);
		
		out.println(min(N%K, abs(K-(N%K))));
			
		
		
	}
	
	
} public class Main
{	
	public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String []s=br.readLine().split(" ");
		
		long N=Long.parseLong(s[0]);
		long K=Long.parseLong(s[1]);
		
		out.println(min(N%K, abs(K-(N%K))));
			
		
		
	}
	
	
} Main
import java.io.*;
import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;

public class Main
{	
	public static void main(String []args)throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String []s=br.readLine().split(" ");
		
		long N=Long.parseLong(s[0]);
		long K=Long.parseLong(s[1]);
		
		out.println(min(N%K, abs(K-(N%K))));
			
		
		
	}
	
	
}


