import 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();
            if(N%K < K - N%K){
                System.out.println(N%K);
            }else{
                System.out.println(K-N%K);
            }
    }
}  import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
            if(N%K < K - N%K){
                System.out.println(N%K);
            }else{
                System.out.println(K-N%K);
            }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
            if(N%K < K - N%K){
                System.out.println(N%K);
            }else{
                System.out.println(K-N%K);
            }
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
            if(N%K < K - N%K){
                System.out.println(N%K);
            }else{
                System.out.println(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 if(N%K < K - N%K){
                System.out.println(N%K);
            }else{
                System.out.println(K-N%K);
            } N%K < K - N%K N%K N K K - N%K K N%K N K {
                System.out.println(N%K);
            } System.out.println(N%K) System.out.println System.out System System.out N%K N K {
                System.out.println(K-N%K);
            } System.out.println(K-N%K) System.out.println System.out System System.out 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();
            if(N%K < K - N%K){
                System.out.println(N%K);
            }else{
                System.out.println(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();
            if(N%K < K - N%K){
                System.out.println(N%K);
            }else{
                System.out.println(K-N%K);
            }
    }
} Main
import 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();
            if(N%K < K - N%K){
                System.out.println(N%K);
            }else{
                System.out.println(K-N%K);
            }
    }
} 