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();
        if(n < k) {
            long min = Math.min(n, k - n);
            System.out.println(min);
        } else if(k == 1) {
            System.out.println(0);
        } else if(n % k == 0){
            System.out.println(0);
        } else {
            long tmp = n % k;
            long min = Math.min(tmp, Math.abs(tmp - k));
            System.out.println(min);
        }
    }
} 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();
        if(n < k) {
            long min = Math.min(n, k - n);
            System.out.println(min);
        } else if(k == 1) {
            System.out.println(0);
        } else if(n % k == 0){
            System.out.println(0);
        } else {
            long tmp = n % k;
            long min = Math.min(tmp, Math.abs(tmp - k));
            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();
        if(n < k) {
            long min = Math.min(n, k - n);
            System.out.println(min);
        } else if(k == 1) {
            System.out.println(0);
        } else if(n % k == 0){
            System.out.println(0);
        } else {
            long tmp = n % k;
            long min = Math.min(tmp, Math.abs(tmp - k));
            System.out.println(min);
        }
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        if(n < k) {
            long min = Math.min(n, k - n);
            System.out.println(min);
        } else if(k == 1) {
            System.out.println(0);
        } else if(n % k == 0){
            System.out.println(0);
        } else {
            long tmp = n % k;
            long min = Math.min(tmp, Math.abs(tmp - k));
            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 if(n < k) {
            long min = Math.min(n, k - n);
            System.out.println(min);
        } else if(k == 1) {
            System.out.println(0);
        } else if(n % k == 0){
            System.out.println(0);
        } else {
            long tmp = n % k;
            long min = Math.min(tmp, Math.abs(tmp - k));
            System.out.println(min);
        } n < k n k {
            long min = Math.min(n, k - n);
            System.out.println(min);
        } long min = Math.min(n, k - n); min Math.min(n, k - n) Math.min Math n k - n k n System.out.println(min) System.out.println System.out System System.out min if(k == 1) {
            System.out.println(0);
        } else if(n % k == 0){
            System.out.println(0);
        } else {
            long tmp = n % k;
            long min = Math.min(tmp, Math.abs(tmp - k));
            System.out.println(min);
        } k == 1 k 1 {
            System.out.println(0);
        } System.out.println(0) System.out.println System.out System System.out 0 if(n % k == 0){
            System.out.println(0);
        } else {
            long tmp = n % k;
            long min = Math.min(tmp, Math.abs(tmp - k));
            System.out.println(min);
        } n % k == 0 n % k n k 0 {
            System.out.println(0);
        } System.out.println(0) System.out.println System.out System System.out 0 {
            long tmp = n % k;
            long min = Math.min(tmp, Math.abs(tmp - k));
            System.out.println(min);
        } long tmp = n % k; tmp n % k n k long min = Math.min(tmp, Math.abs(tmp - k)); min Math.min(tmp, Math.abs(tmp - k)) Math.min Math tmp Math.abs(tmp - k) Math.abs Math tmp - k tmp k 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();
        if(n < k) {
            long min = Math.min(n, k - n);
            System.out.println(min);
        } else if(k == 1) {
            System.out.println(0);
        } else if(n % k == 0){
            System.out.println(0);
        } else {
            long tmp = n % k;
            long min = Math.min(tmp, Math.abs(tmp - k));
            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();
        if(n < k) {
            long min = Math.min(n, k - n);
            System.out.println(min);
        } else if(k == 1) {
            System.out.println(0);
        } else if(n % k == 0){
            System.out.println(0);
        } else {
            long tmp = n % k;
            long min = Math.min(tmp, Math.abs(tmp - k));
            System.out.println(min);
        }
    }
} 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();
        if(n < k) {
            long min = Math.min(n, k - n);
            System.out.println(min);
        } else if(k == 1) {
            System.out.println(0);
        } else if(n % k == 0){
            System.out.println(0);
        } else {
            long tmp = n % k;
            long min = Math.min(tmp, Math.abs(tmp - k));
            System.out.println(min);
        }
    }
}