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.close();

        if (k == 0) {
            System.out.print(n);
            return;
        }

        if (n < k)
        {
            long ans = Math.min(n, Math.abs(n - k));
            System.out.print(ans);
            return;
        }

        long count = n / k;//n > k
        long biggerK = k * (count + 1);
        long smallerK = k * (count);
        long ans = Math.min(Math.min(n, Math.abs(n - biggerK)), Math.abs(n - smallerK));
        System.out.print(ans);
    }
} 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.close();

        if (k == 0) {
            System.out.print(n);
            return;
        }

        if (n < k)
        {
            long ans = Math.min(n, Math.abs(n - k));
            System.out.print(ans);
            return;
        }

        long count = n / k;//n > k
        long biggerK = k * (count + 1);
        long smallerK = k * (count);
        long ans = Math.min(Math.min(n, Math.abs(n - biggerK)), Math.abs(n - smallerK));
        System.out.print(ans);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();

        if (k == 0) {
            System.out.print(n);
            return;
        }

        if (n < k)
        {
            long ans = Math.min(n, Math.abs(n - k));
            System.out.print(ans);
            return;
        }

        long count = n / k;//n > k
        long biggerK = k * (count + 1);
        long smallerK = k * (count);
        long ans = Math.min(Math.min(n, Math.abs(n - biggerK)), Math.abs(n - smallerK));
        System.out.print(ans);
    } main {
        Scanner sc = new Scanner(System.in);

        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();

        if (k == 0) {
            System.out.print(n);
            return;
        }

        if (n < k)
        {
            long ans = Math.min(n, Math.abs(n - k));
            System.out.print(ans);
            return;
        }

        long count = n / k;//n > k
        long biggerK = k * (count + 1);
        long smallerK = k * (count);
        long ans = Math.min(Math.min(n, Math.abs(n - biggerK)), Math.abs(n - smallerK));
        System.out.print(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 sc.close() sc.close sc if (k == 0) {
            System.out.print(n);
            return;
        } k == 0 k 0 {
            System.out.print(n);
            return;
        } System.out.print(n) System.out.print System.out System System.out n return; if (n < k)
        {
            long ans = Math.min(n, Math.abs(n - k));
            System.out.print(ans);
            return;
        } n < k n k {
            long ans = Math.min(n, Math.abs(n - k));
            System.out.print(ans);
            return;
        } long ans = Math.min(n, Math.abs(n - k)); ans Math.min(n, Math.abs(n - k)) Math.min Math n Math.abs(n - k) Math.abs Math n - k n k System.out.print(ans) System.out.print System.out System System.out ans return; long count = n / k; count n / k n k long biggerK = k * (count + 1); biggerK k * (count + 1) k (count + 1) count 1 long smallerK = k * (count); smallerK k * (count) k (count) long ans = Math.min(Math.min(n, Math.abs(n - biggerK)), Math.abs(n - smallerK)); ans Math.min(Math.min(n, Math.abs(n - biggerK)), Math.abs(n - smallerK)) Math.min Math Math.min(n, Math.abs(n - biggerK)) Math.min Math n Math.abs(n - biggerK) Math.abs Math n - biggerK n biggerK Math.abs(n - smallerK) Math.abs Math n - smallerK n smallerK System.out.print(ans) System.out.print 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();
        sc.close();

        if (k == 0) {
            System.out.print(n);
            return;
        }

        if (n < k)
        {
            long ans = Math.min(n, Math.abs(n - k));
            System.out.print(ans);
            return;
        }

        long count = n / k;//n > k
        long biggerK = k * (count + 1);
        long smallerK = k * (count);
        long ans = Math.min(Math.min(n, Math.abs(n - biggerK)), Math.abs(n - smallerK));
        System.out.print(ans);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();

        if (k == 0) {
            System.out.print(n);
            return;
        }

        if (n < k)
        {
            long ans = Math.min(n, Math.abs(n - k));
            System.out.print(ans);
            return;
        }

        long count = n / k;//n > k
        long biggerK = k * (count + 1);
        long smallerK = k * (count);
        long ans = Math.min(Math.min(n, Math.abs(n - biggerK)), Math.abs(n - smallerK));
        System.out.print(ans);
    }
} 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.close();

        if (k == 0) {
            System.out.print(n);
            return;
        }

        if (n < k)
        {
            long ans = Math.min(n, Math.abs(n - k));
            System.out.print(ans);
            return;
        }

        long count = n / k;//n > k
        long biggerK = k * (count + 1);
        long smallerK = k * (count);
        long ans = Math.min(Math.min(n, Math.abs(n - biggerK)), Math.abs(n - smallerK));
        System.out.print(ans);
    }
}