import java.util.Scanner;
public class Main
{
	public static void main(String[] args)
    {
     	 Scanner sc = new Scanner(System.in);
      	
     	String[] sep = sc.nextLine().split(" ");
      	long n = Long.parseLong(sep[0]);
      	long k = Long.parseLong(sep[1]);
      	
      	Long min = n % k;
      	if(min > (k - min))
        {
         	System.out.println(k - min); 
        }else{
         	System.out.println(min); 
        }
    }
} import java.util.Scanner; Scanner java.util public class Main
{
	public static void main(String[] args)
    {
     	 Scanner sc = new Scanner(System.in);
      	
     	String[] sep = sc.nextLine().split(" ");
      	long n = Long.parseLong(sep[0]);
      	long k = Long.parseLong(sep[1]);
      	
      	Long min = n % k;
      	if(min > (k - min))
        {
         	System.out.println(k - min); 
        }else{
         	System.out.println(min); 
        }
    }
} Main public static void main(String[] args)
    {
     	 Scanner sc = new Scanner(System.in);
      	
     	String[] sep = sc.nextLine().split(" ");
      	long n = Long.parseLong(sep[0]);
      	long k = Long.parseLong(sep[1]);
      	
      	Long min = n % k;
      	if(min > (k - min))
        {
         	System.out.println(k - min); 
        }else{
         	System.out.println(min); 
        }
    } main {
     	 Scanner sc = new Scanner(System.in);
      	
     	String[] sep = sc.nextLine().split(" ");
      	long n = Long.parseLong(sep[0]);
      	long k = Long.parseLong(sep[1]);
      	
      	Long min = n % k;
      	if(min > (k - min))
        {
         	System.out.println(k - min); 
        }else{
         	System.out.println(min); 
        }
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) String[] sep = sc.nextLine().split(" "); sep sc.nextLine().split(" ") sc.nextLine().split sc.nextLine() sc.nextLine sc " " long n = Long.parseLong(sep[0]); n Long.parseLong(sep[0]) Long.parseLong Long sep[0] sep 0 long k = Long.parseLong(sep[1]); k Long.parseLong(sep[1]) Long.parseLong Long sep[1] sep 1 Long min = n % k; min n % k n k if(min > (k - min))
        {
         	System.out.println(k - min); 
        }else{
         	System.out.println(min); 
        } min > (k - min) min (k - min) k min {
         	System.out.println(k - min); 
        } System.out.println(k - min) System.out.println System.out System System.out k - min k min {
         	System.out.println(min); 
        } System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main
{
	public static void main(String[] args)
    {
     	 Scanner sc = new Scanner(System.in);
      	
     	String[] sep = sc.nextLine().split(" ");
      	long n = Long.parseLong(sep[0]);
      	long k = Long.parseLong(sep[1]);
      	
      	Long min = n % k;
      	if(min > (k - min))
        {
         	System.out.println(k - min); 
        }else{
         	System.out.println(min); 
        }
    }
} public class Main
{
	public static void main(String[] args)
    {
     	 Scanner sc = new Scanner(System.in);
      	
     	String[] sep = sc.nextLine().split(" ");
      	long n = Long.parseLong(sep[0]);
      	long k = Long.parseLong(sep[1]);
      	
      	Long min = n % k;
      	if(min > (k - min))
        {
         	System.out.println(k - min); 
        }else{
         	System.out.println(min); 
        }
    }
} Main
import java.util.Scanner;
public class Main
{
	public static void main(String[] args)
    {
     	 Scanner sc = new Scanner(System.in);
      	
     	String[] sep = sc.nextLine().split(" ");
      	long n = Long.parseLong(sep[0]);
      	long k = Long.parseLong(sep[1]);
      	
      	Long min = n % k;
      	if(min > (k - min))
        {
         	System.out.println(k - min); 
        }else{
         	System.out.println(min); 
        }
    }
}