import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		long n, k;
		long ans = 0;
		Scanner scan = new Scanner(System.in);

		n = scan.nextLong();
		k = scan.nextLong();

		long a = n % k;

		if(a == 0) {
			System.out.println(0);
		} else {
			System.out.println(Math.min(a, Math.abs(a-k)));
		}
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		long n, k;
		long ans = 0;
		Scanner scan = new Scanner(System.in);

		n = scan.nextLong();
		k = scan.nextLong();

		long a = n % k;

		if(a == 0) {
			System.out.println(0);
		} else {
			System.out.println(Math.min(a, Math.abs(a-k)));
		}
	}
} Main public static void main(String[] args) {
		long n, k;
		long ans = 0;
		Scanner scan = new Scanner(System.in);

		n = scan.nextLong();
		k = scan.nextLong();

		long a = n % k;

		if(a == 0) {
			System.out.println(0);
		} else {
			System.out.println(Math.min(a, Math.abs(a-k)));
		}
	} main {
		long n, k;
		long ans = 0;
		Scanner scan = new Scanner(System.in);

		n = scan.nextLong();
		k = scan.nextLong();

		long a = n % k;

		if(a == 0) {
			System.out.println(0);
		} else {
			System.out.println(Math.min(a, Math.abs(a-k)));
		}
	} long n n k; k long ans = 0; ans 0 Scanner scan = new Scanner(System.in); scan new Scanner(System.in) n = scan.nextLong() n scan.nextLong() scan.nextLong scan k = scan.nextLong() k scan.nextLong() scan.nextLong scan long a = n % k; a n % k n k if(a == 0) {
			System.out.println(0);
		} else {
			System.out.println(Math.min(a, Math.abs(a-k)));
		} a == 0 a 0 {
			System.out.println(0);
		} System.out.println(0) System.out.println System.out System System.out 0 {
			System.out.println(Math.min(a, Math.abs(a-k)));
		} System.out.println(Math.min(a, Math.abs(a-k))) System.out.println System.out System System.out Math.min(a, Math.abs(a-k)) Math.min Math a Math.abs(a-k) Math.abs Math a-k a k String[] args args public class Main {
	public static void main(String[] args) {
		long n, k;
		long ans = 0;
		Scanner scan = new Scanner(System.in);

		n = scan.nextLong();
		k = scan.nextLong();

		long a = n % k;

		if(a == 0) {
			System.out.println(0);
		} else {
			System.out.println(Math.min(a, Math.abs(a-k)));
		}
	}
} public class Main {
	public static void main(String[] args) {
		long n, k;
		long ans = 0;
		Scanner scan = new Scanner(System.in);

		n = scan.nextLong();
		k = scan.nextLong();

		long a = n % k;

		if(a == 0) {
			System.out.println(0);
		} else {
			System.out.println(Math.min(a, Math.abs(a-k)));
		}
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		long n, k;
		long ans = 0;
		Scanner scan = new Scanner(System.in);

		n = scan.nextLong();
		k = scan.nextLong();

		long a = n % k;

		if(a == 0) {
			System.out.println(0);
		} else {
			System.out.println(Math.min(a, Math.abs(a-k)));
		}
	}
}
