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