import java.util.*;

import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Main {
	
	
	
	
    public static void main(String[] args) {
    	Scanner sc=new Scanner(System.in);
    	long  n=sc.nextLong();
    	long k=sc.nextLong();
    	long min=Integer.MIN_VALUE;
    	System.out.println(Math.min(n%k, k-(n%k)));
    
    	
    	
    	
       
        
       
    	

     }
    	
    	
    	
    	
    
}
 import java.util.*; util.* java import java.math.*; math.* java import java.security.*; security.* java import java.text.*; text.* java import java.util.*; util.* java import java.util.concurrent.*; concurrent.* java.util import java.util.regex.*; regex.* 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();
    	long min=Integer.MIN_VALUE;
    	System.out.println(Math.min(n%k, k-(n%k)));
    
    	
    	
    	
       
        
       
    	

     }
    	
    	
    	
    	
    
} Main public static void main(String[] args) {
    	Scanner sc=new Scanner(System.in);
    	long  n=sc.nextLong();
    	long k=sc.nextLong();
    	long min=Integer.MIN_VALUE;
    	System.out.println(Math.min(n%k, k-(n%k)));
    
    	
    	
    	
       
        
       
    	

     } main {
    	Scanner sc=new Scanner(System.in);
    	long  n=sc.nextLong();
    	long k=sc.nextLong();
    	long min=Integer.MIN_VALUE;
    	System.out.println(Math.min(n%k, k-(n%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 long min=Integer.MIN_VALUE; min Integer.MIN_VALUE Integer Integer.MIN_VALUE System.out.println(Math.min(n%k, k-(n%k))) System.out.println System.out System System.out Math.min(n%k, k-(n%k)) Math.min Math n%k n k k-(n%k) k (n%k) n 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();
    	long min=Integer.MIN_VALUE;
    	System.out.println(Math.min(n%k, k-(n%k)));
    
    	
    	
    	
       
        
       
    	

     }
    	
    	
    	
    	
    
} public class Main {
	
	
	
	
    public static void main(String[] args) {
    	Scanner sc=new Scanner(System.in);
    	long  n=sc.nextLong();
    	long k=sc.nextLong();
    	long min=Integer.MIN_VALUE;
    	System.out.println(Math.min(n%k, k-(n%k)));
    
    	
    	
    	
       
        
       
    	

     }
    	
    	
    	
    	
    
} Main
import java.util.*;

import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Main {
	
	
	
	
    public static void main(String[] args) {
    	Scanner sc=new Scanner(System.in);
    	long  n=sc.nextLong();
    	long k=sc.nextLong();
    	long min=Integer.MIN_VALUE;
    	System.out.println(Math.min(n%k, k-(n%k)));
    
    	
    	
    	
       
        
       
    	

     }
    	
    	
    	
    	
    
}

