import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long n,k,subtraction;

        n = sc.nextLong();
        k = sc.nextLong();
        
        subtraction = n - ((n / k) * k);
        
        
        if(subtraction >= Math.abs(subtraction - k)){
            subtraction = Math.abs(subtraction - k);
        }
        
        System.out.println(subtraction);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long n,k,subtraction;

        n = sc.nextLong();
        k = sc.nextLong();
        
        subtraction = n - ((n / k) * k);
        
        
        if(subtraction >= Math.abs(subtraction - k)){
            subtraction = Math.abs(subtraction - k);
        }
        
        System.out.println(subtraction);
    }
} Main public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long n,k,subtraction;

        n = sc.nextLong();
        k = sc.nextLong();
        
        subtraction = n - ((n / k) * k);
        
        
        if(subtraction >= Math.abs(subtraction - k)){
            subtraction = Math.abs(subtraction - k);
        }
        
        System.out.println(subtraction);
    } main {
        Scanner sc = new Scanner(System.in);
        long n,k,subtraction;

        n = sc.nextLong();
        k = sc.nextLong();
        
        subtraction = n - ((n / k) * k);
        
        
        if(subtraction >= Math.abs(subtraction - k)){
            subtraction = Math.abs(subtraction - k);
        }
        
        System.out.println(subtraction);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n n k k subtraction; subtraction n = sc.nextLong() n sc.nextLong() sc.nextLong sc k = sc.nextLong() k sc.nextLong() sc.nextLong sc subtraction = n - ((n / k) * k) subtraction n - ((n / k) * k) n ((n / k) * k) (n / k) n k k if(subtraction >= Math.abs(subtraction - k)){
            subtraction = Math.abs(subtraction - k);
        } subtraction >= Math.abs(subtraction - k) subtraction Math.abs(subtraction - k) Math.abs Math subtraction - k subtraction k {
            subtraction = Math.abs(subtraction - k);
        } subtraction = Math.abs(subtraction - k) subtraction Math.abs(subtraction - k) Math.abs Math subtraction - k subtraction k System.out.println(subtraction) System.out.println System.out System System.out subtraction String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long n,k,subtraction;

        n = sc.nextLong();
        k = sc.nextLong();
        
        subtraction = n - ((n / k) * k);
        
        
        if(subtraction >= Math.abs(subtraction - k)){
            subtraction = Math.abs(subtraction - k);
        }
        
        System.out.println(subtraction);
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long n,k,subtraction;

        n = sc.nextLong();
        k = sc.nextLong();
        
        subtraction = n - ((n / k) * k);
        
        
        if(subtraction >= Math.abs(subtraction - k)){
            subtraction = Math.abs(subtraction - k);
        }
        
        System.out.println(subtraction);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long n,k,subtraction;

        n = sc.nextLong();
        k = sc.nextLong();
        
        subtraction = n - ((n / k) * k);
        
        
        if(subtraction >= Math.abs(subtraction - k)){
            subtraction = Math.abs(subtraction - k);
        }
        
        System.out.println(subtraction);
    }
}
