import java.io.IOException;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        long k = in.nextLong();
        if (n > k) {
            n = n % k;
        }
        System.out.println(k - n > n ? n : k - n);
    }
} import java.io.IOException; IOException java.io import java.util.*; util.* java public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        long k = in.nextLong();
        if (n > k) {
            n = n % k;
        }
        System.out.println(k - n > n ? n : k - n);
    }
} Main public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        long k = in.nextLong();
        if (n > k) {
            n = n % k;
        }
        System.out.println(k - n > n ? n : k - n);
    } main {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        long k = in.nextLong();
        if (n > k) {
            n = n % k;
        }
        System.out.println(k - n > n ? n : k - n);
    } Scanner in = new Scanner(System.in); in new Scanner(System.in) long n = in.nextLong(); n in.nextLong() in.nextLong in long k = in.nextLong(); k in.nextLong() in.nextLong in if (n > k) {
            n = n % k;
        } n > k n k {
            n = n % k;
        } n = n % k n n % k n k System.out.println(k - n > n ? n : k - n) System.out.println System.out System System.out k - n > n ? n : k - n k - n > n k - n k n n n k - n k n String[] args args public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        long k = in.nextLong();
        if (n > k) {
            n = n % k;
        }
        System.out.println(k - n > n ? n : k - n);
    }
} public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        long k = in.nextLong();
        if (n > k) {
            n = n % k;
        }
        System.out.println(k - n > n ? n : k - n);
    }
} Main
import java.io.IOException;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        long k = in.nextLong();
        if (n > k) {
            n = n % k;
        }
        System.out.println(k - n > n ? n : k - n);
    }
}