import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

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

		long x = N%K;
		long y = Math.abs(x-K);

		if (x > y) {
			System.out.println(y);
		} else {
			System.out.println(x);
		}
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

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

		long x = N%K;
		long y = Math.abs(x-K);

		if (x > y) {
			System.out.println(y);
		} else {
			System.out.println(x);
		}
	}

} Main public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

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

		long x = N%K;
		long y = Math.abs(x-K);

		if (x > y) {
			System.out.println(y);
		} else {
			System.out.println(x);
		}
	} main {

		Scanner scan = new Scanner(System.in);

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

		long x = N%K;
		long y = Math.abs(x-K);

		if (x > y) {
			System.out.println(y);
		} else {
			System.out.println(x);
		}
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long N = scan.nextLong(); N scan.nextLong() scan.nextLong scan long K = scan.nextLong(); K scan.nextLong() scan.nextLong scan long x = N%K; x N%K N K long y = Math.abs(x-K); y Math.abs(x-K) Math.abs Math x-K x K if (x > y) {
			System.out.println(y);
		} else {
			System.out.println(x);
		} x > y x y {
			System.out.println(y);
		} System.out.println(y) System.out.println System.out System System.out y {
			System.out.println(x);
		} System.out.println(x) System.out.println System.out System System.out x String[] args args public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

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

		long x = N%K;
		long y = Math.abs(x-K);

		if (x > y) {
			System.out.println(y);
		} else {
			System.out.println(x);
		}
	}

} public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

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

		long x = N%K;
		long y = Math.abs(x-K);

		if (x > y) {
			System.out.println(y);
		} else {
			System.out.println(x);
		}
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

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

		long x = N%K;
		long y = Math.abs(x-K);

		if (x > y) {
			System.out.println(y);
		} else {
			System.out.println(x);
		}
	}

}