import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans = Long.MAX_VALUE;
        if(n > k){
            n = n - (k * (n / k));
            ans = Long.min(n, Math.abs(n - k));
        } else if(n == k){
            ans = 0;
        } else {
            // (n < k)
            ans = Long.min(n, Math.abs(n - k));
        }
        System.out.println(ans);
    }
} import java.util.Scanner; Scanner 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();
        long ans = Long.MAX_VALUE;
        if(n > k){
            n = n - (k * (n / k));
            ans = Long.min(n, Math.abs(n - k));
        } else if(n == k){
            ans = 0;
        } else {
            // (n < k)
            ans = Long.min(n, Math.abs(n - k));
        }
        System.out.println(ans);
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans = Long.MAX_VALUE;
        if(n > k){
            n = n - (k * (n / k));
            ans = Long.min(n, Math.abs(n - k));
        } else if(n == k){
            ans = 0;
        } else {
            // (n < k)
            ans = Long.min(n, Math.abs(n - k));
        }
        System.out.println(ans);
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans = Long.MAX_VALUE;
        if(n > k){
            n = n - (k * (n / k));
            ans = Long.min(n, Math.abs(n - k));
        } else if(n == k){
            ans = 0;
        } else {
            // (n < k)
            ans = Long.min(n, Math.abs(n - k));
        }
        System.out.println(ans);
    } 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 ans = Long.MAX_VALUE; ans Long.MAX_VALUE Long Long.MAX_VALUE if(n > k){
            n = n - (k * (n / k));
            ans = Long.min(n, Math.abs(n - k));
        } else if(n == k){
            ans = 0;
        } else {
            // (n < k)
            ans = Long.min(n, Math.abs(n - k));
        } n > k n k {
            n = n - (k * (n / k));
            ans = Long.min(n, Math.abs(n - k));
        } n = n - (k * (n / k)) n n - (k * (n / k)) n (k * (n / k)) k (n / k) n k ans = Long.min(n, Math.abs(n - k)) ans Long.min(n, Math.abs(n - k)) Long.min Long n Math.abs(n - k) Math.abs Math n - k n k if(n == k){
            ans = 0;
        } else {
            // (n < k)
            ans = Long.min(n, Math.abs(n - k));
        } n == k n k {
            ans = 0;
        } ans = 0 ans 0 {
            // (n < k)
            ans = Long.min(n, Math.abs(n - k));
        } ans = Long.min(n, Math.abs(n - k)) ans Long.min(n, Math.abs(n - k)) Long.min Long n Math.abs(n - k) Math.abs Math n - k n k System.out.println(ans) System.out.println System.out System System.out ans 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();
        long ans = Long.MAX_VALUE;
        if(n > k){
            n = n - (k * (n / k));
            ans = Long.min(n, Math.abs(n - k));
        } else if(n == k){
            ans = 0;
        } else {
            // (n < k)
            ans = Long.min(n, Math.abs(n - k));
        }
        System.out.println(ans);
    }
} public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans = Long.MAX_VALUE;
        if(n > k){
            n = n - (k * (n / k));
            ans = Long.min(n, Math.abs(n - k));
        } else if(n == k){
            ans = 0;
        } else {
            // (n < k)
            ans = Long.min(n, Math.abs(n - k));
        }
        System.out.println(ans);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans = Long.MAX_VALUE;
        if(n > k){
            n = n - (k * (n / k));
            ans = Long.min(n, Math.abs(n - k));
        } else if(n == k){
            ans = 0;
        } else {
            // (n < k)
            ans = Long.min(n, Math.abs(n - k));
        }
        System.out.println(ans);
    }
}