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();

		long ans = n % k;
		if (ans == 0) {
			System.out.println(ans);
		} else {
			long a2 = Math.abs(ans - k);

			if (a2 > n) {
				System.out.println(n);
			} else {
				System.out.println(a2);
			}
		}

		sc.close();
	}

} 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();

		long ans = n % k;
		if (ans == 0) {
			System.out.println(ans);
		} else {
			long a2 = Math.abs(ans - k);

			if (a2 > n) {
				System.out.println(n);
			} else {
				System.out.println(a2);
			}
		}

		sc.close();
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();

		long ans = n % k;
		if (ans == 0) {
			System.out.println(ans);
		} else {
			long a2 = Math.abs(ans - k);

			if (a2 > n) {
				System.out.println(n);
			} else {
				System.out.println(a2);
			}
		}

		sc.close();
	} main {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();

		long ans = n % k;
		if (ans == 0) {
			System.out.println(ans);
		} else {
			long a2 = Math.abs(ans - k);

			if (a2 > n) {
				System.out.println(n);
			} else {
				System.out.println(a2);
			}
		}

		sc.close();
	} 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 long ans = n % k; ans n % k n k if (ans == 0) {
			System.out.println(ans);
		} else {
			long a2 = Math.abs(ans - k);

			if (a2 > n) {
				System.out.println(n);
			} else {
				System.out.println(a2);
			}
		} ans == 0 ans 0 {
			System.out.println(ans);
		} System.out.println(ans) System.out.println System.out System System.out ans {
			long a2 = Math.abs(ans - k);

			if (a2 > n) {
				System.out.println(n);
			} else {
				System.out.println(a2);
			}
		} long a2 = Math.abs(ans - k); a2 Math.abs(ans - k) Math.abs Math ans - k ans k if (a2 > n) {
				System.out.println(n);
			} else {
				System.out.println(a2);
			} a2 > n a2 n {
				System.out.println(n);
			} System.out.println(n) System.out.println System.out System System.out n {
				System.out.println(a2);
			} System.out.println(a2) System.out.println System.out System System.out a2 sc.close() sc.close sc 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();

		long ans = n % k;
		if (ans == 0) {
			System.out.println(ans);
		} else {
			long a2 = Math.abs(ans - k);

			if (a2 > n) {
				System.out.println(n);
			} else {
				System.out.println(a2);
			}
		}

		sc.close();
	}

} public class Main {

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

		long ans = n % k;
		if (ans == 0) {
			System.out.println(ans);
		} else {
			long a2 = Math.abs(ans - k);

			if (a2 > n) {
				System.out.println(n);
			} else {
				System.out.println(a2);
			}
		}

		sc.close();
	}

} 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();

		long ans = n % k;
		if (ans == 0) {
			System.out.println(ans);
		} else {
			long a2 = Math.abs(ans - k);

			if (a2 > n) {
				System.out.println(n);
			} else {
				System.out.println(a2);
			}
		}

		sc.close();
	}

}