import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        long N;
        long K;
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextLong();
            K = sc.nextLong();
        }
        if (N > K) {
            N = N % K;
        }
        if (K - N > N) {
            System.out.println(N);
        } else {
            System.out.println(K - N);
        }
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        long N;
        long K;
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextLong();
            K = sc.nextLong();
        }
        if (N > K) {
            N = N % K;
        }
        if (K - N > N) {
            System.out.println(N);
        } else {
            System.out.println(K - N);
        }
    }
} Main public static void main(String[] args) {
        long N;
        long K;
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextLong();
            K = sc.nextLong();
        }
        if (N > K) {
            N = N % K;
        }
        if (K - N > N) {
            System.out.println(N);
        } else {
            System.out.println(K - N);
        }
    } main {
        long N;
        long K;
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextLong();
            K = sc.nextLong();
        }
        if (N > K) {
            N = N % K;
        }
        if (K - N > N) {
            System.out.println(N);
        } else {
            System.out.println(K - N);
        }
    } long N; N long K; K try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextLong();
            K = sc.nextLong();
        } {
            N = sc.nextLong();
            K = sc.nextLong();
        } Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) new Scanner(System.in) N = sc.nextLong() N sc.nextLong() sc.nextLong sc K = sc.nextLong() K sc.nextLong() sc.nextLong sc if (N > K) {
            N = N % K;
        } N > K N K {
            N = N % K;
        } N = N % K N N % K N K if (K - N > N) {
            System.out.println(N);
        } else {
            System.out.println(K - N);
        } K - N > N K - N K N N {
            System.out.println(N);
        } System.out.println(N) System.out.println System.out System System.out N {
            System.out.println(K - N);
        } System.out.println(K - N) System.out.println System.out System System.out K - N K N String[] args args public class Main {
    public static void main(String[] args) {
        long N;
        long K;
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextLong();
            K = sc.nextLong();
        }
        if (N > K) {
            N = N % K;
        }
        if (K - N > N) {
            System.out.println(N);
        } else {
            System.out.println(K - N);
        }
    }
} public class Main {
    public static void main(String[] args) {
        long N;
        long K;
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextLong();
            K = sc.nextLong();
        }
        if (N > K) {
            N = N % K;
        }
        if (K - N > N) {
            System.out.println(N);
        } else {
            System.out.println(K - N);
        }
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        long N;
        long K;
        try (Scanner sc = new Scanner(System.in)) {
            N = sc.nextLong();
            K = sc.nextLong();
        }
        if (N > K) {
            N = N % K;
        }
        if (K - N > N) {
            System.out.println(N);
        } else {
            System.out.println(K - N);
        }
    }
}
