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) {
			System.out.println(0);
			return;
		}


		if(n>k) {
			long mod = n%k;
			long abs = Math.abs(mod-k);

			if(mod < abs) {
				System.out.println(mod);
			}else {
				System.out.println(abs);
			}
		}else{
			if(n < Math.abs(n-k)) {;
				System.out.println(n);
			}else{
				System.out.println(Math.abs(n-k));
			}
		}

	}
} 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) {
			System.out.println(0);
			return;
		}


		if(n>k) {
			long mod = n%k;
			long abs = Math.abs(mod-k);

			if(mod < abs) {
				System.out.println(mod);
			}else {
				System.out.println(abs);
			}
		}else{
			if(n < Math.abs(n-k)) {;
				System.out.println(n);
			}else{
				System.out.println(Math.abs(n-k));
			}
		}

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

		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long k = sc.nextLong();

		if(n == k) {
			System.out.println(0);
			return;
		}


		if(n>k) {
			long mod = n%k;
			long abs = Math.abs(mod-k);

			if(mod < abs) {
				System.out.println(mod);
			}else {
				System.out.println(abs);
			}
		}else{
			if(n < Math.abs(n-k)) {;
				System.out.println(n);
			}else{
				System.out.println(Math.abs(n-k));
			}
		}

	} main {

		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long k = sc.nextLong();

		if(n == k) {
			System.out.println(0);
			return;
		}


		if(n>k) {
			long mod = n%k;
			long abs = Math.abs(mod-k);

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

			if(mod < abs) {
				System.out.println(mod);
			}else {
				System.out.println(abs);
			}
		}else{
			if(n < Math.abs(n-k)) {;
				System.out.println(n);
			}else{
				System.out.println(Math.abs(n-k));
			}
		} n>k n k {
			long mod = n%k;
			long abs = Math.abs(mod-k);

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


		if(n>k) {
			long mod = n%k;
			long abs = Math.abs(mod-k);

			if(mod < abs) {
				System.out.println(mod);
			}else {
				System.out.println(abs);
			}
		}else{
			if(n < Math.abs(n-k)) {;
				System.out.println(n);
			}else{
				System.out.println(Math.abs(n-k));
			}
		}

	}
} 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) {
			System.out.println(0);
			return;
		}


		if(n>k) {
			long mod = n%k;
			long abs = Math.abs(mod-k);

			if(mod < abs) {
				System.out.println(mod);
			}else {
				System.out.println(abs);
			}
		}else{
			if(n < Math.abs(n-k)) {;
				System.out.println(n);
			}else{
				System.out.println(Math.abs(n-k));
			}
		}

	}
} 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) {
			System.out.println(0);
			return;
		}


		if(n>k) {
			long mod = n%k;
			long abs = Math.abs(mod-k);

			if(mod < abs) {
				System.out.println(mod);
			}else {
				System.out.println(abs);
			}
		}else{
			if(n < Math.abs(n-k)) {;
				System.out.println(n);
			}else{
				System.out.println(Math.abs(n-k));
			}
		}

	}
}
