import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        Scanner stdIn=new Scanner(System.in);

        long x=stdIn.nextLong();
        long y=stdIn.nextLong();
        
        long t=x%y;
    
        if(t>=-(t-y)){
            System.out.println(Math.abs(t-y));
            
        }
        else{
            System.out.println(Math.abs(t));
            
        }
    }
} import java.util.Scanner; Scanner java.util class Main {

    public static void main(String[] args) {
        Scanner stdIn=new Scanner(System.in);

        long x=stdIn.nextLong();
        long y=stdIn.nextLong();
        
        long t=x%y;
    
        if(t>=-(t-y)){
            System.out.println(Math.abs(t-y));
            
        }
        else{
            System.out.println(Math.abs(t));
            
        }
    }
} Main public static void main(String[] args) {
        Scanner stdIn=new Scanner(System.in);

        long x=stdIn.nextLong();
        long y=stdIn.nextLong();
        
        long t=x%y;
    
        if(t>=-(t-y)){
            System.out.println(Math.abs(t-y));
            
        }
        else{
            System.out.println(Math.abs(t));
            
        }
    } main {
        Scanner stdIn=new Scanner(System.in);

        long x=stdIn.nextLong();
        long y=stdIn.nextLong();
        
        long t=x%y;
    
        if(t>=-(t-y)){
            System.out.println(Math.abs(t-y));
            
        }
        else{
            System.out.println(Math.abs(t));
            
        }
    } Scanner stdIn=new Scanner(System.in); stdIn new Scanner(System.in) long x=stdIn.nextLong(); x stdIn.nextLong() stdIn.nextLong stdIn long y=stdIn.nextLong(); y stdIn.nextLong() stdIn.nextLong stdIn long t=x%y; t x%y x y if(t>=-(t-y)){
            System.out.println(Math.abs(t-y));
            
        }
        else{
            System.out.println(Math.abs(t));
            
        } t>=-(t-y) t -(t-y) (t-y) t y {
            System.out.println(Math.abs(t-y));
            
        } System.out.println(Math.abs(t-y)) System.out.println System.out System System.out Math.abs(t-y) Math.abs Math t-y t y {
            System.out.println(Math.abs(t));
            
        } System.out.println(Math.abs(t)) System.out.println System.out System System.out Math.abs(t) Math.abs Math t String[] args args
import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        Scanner stdIn=new Scanner(System.in);

        long x=stdIn.nextLong();
        long y=stdIn.nextLong();
        
        long t=x%y;
    
        if(t>=-(t-y)){
            System.out.println(Math.abs(t-y));
            
        }
        else{
            System.out.println(Math.abs(t));
            
        }
    }
}