import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long a = n / k;
        
        System.out.println(Math.min(n, Math.min(Math.abs(n-k*(a+1)), Math.abs(n-k*a))));
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long a = n / k;
        
        System.out.println(Math.min(n, Math.min(Math.abs(n-k*(a+1)), Math.abs(n-k*a))));
    }
} Main public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long a = n / k;
        
        System.out.println(Math.min(n, Math.min(Math.abs(n-k*(a+1)), Math.abs(n-k*a))));
    } main {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long a = n / k;
        
        System.out.println(Math.min(n, Math.min(Math.abs(n-k*(a+1)), Math.abs(n-k*a))));
    } 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 a = n / k; a n / k n k System.out.println(Math.min(n, Math.min(Math.abs(n-k*(a+1)), Math.abs(n-k*a)))) System.out.println System.out System System.out Math.min(n, Math.min(Math.abs(n-k*(a+1)), Math.abs(n-k*a))) Math.min Math n Math.min(Math.abs(n-k*(a+1)), Math.abs(n-k*a)) Math.min Math Math.abs(n-k*(a+1)) Math.abs Math n-k*(a+1) n k*(a+1) k (a+1) a 1 Math.abs(n-k*a) Math.abs Math n-k*a n k*a k a String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long a = n / k;
        
        System.out.println(Math.min(n, Math.min(Math.abs(n-k*(a+1)), Math.abs(n-k*a))));
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long a = n / k;
        
        System.out.println(Math.min(n, Math.min(Math.abs(n-k*(a+1)), Math.abs(n-k*a))));
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long a = n / k;
        
        System.out.println(Math.min(n, Math.min(Math.abs(n-k*(a+1)), Math.abs(n-k*a))));
    }
}
