import java.util.Scanner;

public class Main {
    private static final int MOD = 1_000_000_007;
    private static final String YES = "Yes";
    private static final String NO = "No";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();

        System.out.println(solve(N, K));
    }

    private static long solve(long N, long K) {
        return Math.min(N%K, Math.abs(K - N%K));
    }
} import java.util.Scanner; Scanner java.util public class Main {
    private static final int MOD = 1_000_000_007;
    private static final String YES = "Yes";
    private static final String NO = "No";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();

        System.out.println(solve(N, K));
    }

    private static long solve(long N, long K) {
        return Math.min(N%K, Math.abs(K - N%K));
    }
} Main private static final int MOD = 1_000_000_007; MOD 1_000_000_007 private static final String YES = "Yes"; YES "Yes" private static final String NO = "No"; NO "No" public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();

        System.out.println(solve(N, K));
    } main {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();

        System.out.println(solve(N, K));
    } 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 System.out.println(solve(N, K)) System.out.println System.out System System.out solve(N, K) solve N K String[] args args private static long solve(long N, long K) {
        return Math.min(N%K, Math.abs(K - N%K));
    } solve {
        return Math.min(N%K, Math.abs(K - N%K));
    } return Math.min(N%K, Math.abs(K - N%K)); Math.min(N%K, Math.abs(K - N%K)) Math.min Math N%K N K Math.abs(K - N%K) Math.abs Math K - N%K K N%K N K long N N long K K public class Main {
    private static final int MOD = 1_000_000_007;
    private static final String YES = "Yes";
    private static final String NO = "No";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();

        System.out.println(solve(N, K));
    }

    private static long solve(long N, long K) {
        return Math.min(N%K, Math.abs(K - N%K));
    }
} public class Main {
    private static final int MOD = 1_000_000_007;
    private static final String YES = "Yes";
    private static final String NO = "No";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();

        System.out.println(solve(N, K));
    }

    private static long solve(long N, long K) {
        return Math.min(N%K, Math.abs(K - N%K));
    }
} Main
import java.util.Scanner;

public class Main {
    private static final int MOD = 1_000_000_007;
    private static final String YES = "Yes";
    private static final String NO = "No";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();

        System.out.println(solve(N, K));
    }

    private static long solve(long N, long K) {
        return Math.min(N%K, Math.abs(K - N%K));
    }
}