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();
        Set<Long> s = new HashSet<>();

        long min = n;
        while (!s.contains(n)) {
            if (n % k == 0) {
                min = 0;
                break;
            }

            s.add(n);
            if (n > k) n = n % k;
            else n = Math.abs(n-k);
            min = Math.min(min, n);
        }

        System.out.println(min);
    }
} 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();
        Set<Long> s = new HashSet<>();

        long min = n;
        while (!s.contains(n)) {
            if (n % k == 0) {
                min = 0;
                break;
            }

            s.add(n);
            if (n > k) n = n % k;
            else n = Math.abs(n-k);
            min = Math.min(min, n);
        }

        System.out.println(min);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        Set<Long> s = new HashSet<>();

        long min = n;
        while (!s.contains(n)) {
            if (n % k == 0) {
                min = 0;
                break;
            }

            s.add(n);
            if (n > k) n = n % k;
            else n = Math.abs(n-k);
            min = Math.min(min, n);
        }

        System.out.println(min);
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        Set<Long> s = new HashSet<>();

        long min = n;
        while (!s.contains(n)) {
            if (n % k == 0) {
                min = 0;
                break;
            }

            s.add(n);
            if (n > k) n = n % k;
            else n = Math.abs(n-k);
            min = Math.min(min, n);
        }

        System.out.println(min);
    } 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 Set<Long> s = new HashSet<>(); s new HashSet<>() long min = n; min n while (!s.contains(n)) {
            if (n % k == 0) {
                min = 0;
                break;
            }

            s.add(n);
            if (n > k) n = n % k;
            else n = Math.abs(n-k);
            min = Math.min(min, n);
        } !s.contains(n) s.contains(n) s.contains s n {
            if (n % k == 0) {
                min = 0;
                break;
            }

            s.add(n);
            if (n > k) n = n % k;
            else n = Math.abs(n-k);
            min = Math.min(min, n);
        } if (n % k == 0) {
                min = 0;
                break;
            } n % k == 0 n % k n k 0 {
                min = 0;
                break;
            } min = 0 min 0 break; s.add(n) s.add s n if (n > k) n = n % k;
            else n = Math.abs(n-k); n > k n k n = n % k n n % k n k n = Math.abs(n-k) n Math.abs(n-k) Math.abs Math n-k n k min = Math.min(min, n) min Math.min(min, n) Math.min Math min n System.out.println(min) System.out.println System.out System System.out min 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();
        Set<Long> s = new HashSet<>();

        long min = n;
        while (!s.contains(n)) {
            if (n % k == 0) {
                min = 0;
                break;
            }

            s.add(n);
            if (n > k) n = n % k;
            else n = Math.abs(n-k);
            min = Math.min(min, n);
        }

        System.out.println(min);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        Set<Long> s = new HashSet<>();

        long min = n;
        while (!s.contains(n)) {
            if (n % k == 0) {
                min = 0;
                break;
            }

            s.add(n);
            if (n > k) n = n % k;
            else n = Math.abs(n-k);
            min = Math.min(min, n);
        }

        System.out.println(min);
    }
} 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();
        Set<Long> s = new HashSet<>();

        long min = n;
        while (!s.contains(n)) {
            if (n % k == 0) {
                min = 0;
                break;
            }

            s.add(n);
            if (n > k) n = n % k;
            else n = Math.abs(n-k);
            min = Math.min(min, n);
        }

        System.out.println(min);
    }
}