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 res = 0;
//		String s = sc.next();
//		String res = "";
		long semiFinal1 = n % k;
		long semiFinal2 = k - semiFinal1;
//		System.out.printf("%d %d\n", semiFinal1, semiFinal2);

		if(semiFinal1 < semiFinal2) {
			res = semiFinal1;
		} else {
			res = semiFinal2;
		}
		System.out.printf("%d", res);
		//System.out.println(res);
	}
} 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 res = 0;
//		String s = sc.next();
//		String res = "";
		long semiFinal1 = n % k;
		long semiFinal2 = k - semiFinal1;
//		System.out.printf("%d %d\n", semiFinal1, semiFinal2);

		if(semiFinal1 < semiFinal2) {
			res = semiFinal1;
		} else {
			res = semiFinal2;
		}
		System.out.printf("%d", res);
		//System.out.println(res);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long k = sc.nextLong();
		long res = 0;
//		String s = sc.next();
//		String res = "";
		long semiFinal1 = n % k;
		long semiFinal2 = k - semiFinal1;
//		System.out.printf("%d %d\n", semiFinal1, semiFinal2);

		if(semiFinal1 < semiFinal2) {
			res = semiFinal1;
		} else {
			res = semiFinal2;
		}
		System.out.printf("%d", res);
		//System.out.println(res);
	} main {
		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long k = sc.nextLong();
		long res = 0;
//		String s = sc.next();
//		String res = "";
		long semiFinal1 = n % k;
		long semiFinal2 = k - semiFinal1;
//		System.out.printf("%d %d\n", semiFinal1, semiFinal2);

		if(semiFinal1 < semiFinal2) {
			res = semiFinal1;
		} else {
			res = semiFinal2;
		}
		System.out.printf("%d", res);
		//System.out.println(res);
	} 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 res = 0; res 0 long semiFinal1 = n % k; semiFinal1 n % k n k long semiFinal2 = k - semiFinal1; semiFinal2 k - semiFinal1 k semiFinal1 if(semiFinal1 < semiFinal2) {
			res = semiFinal1;
		} else {
			res = semiFinal2;
		} semiFinal1 < semiFinal2 semiFinal1 semiFinal2 {
			res = semiFinal1;
		} res = semiFinal1 res semiFinal1 {
			res = semiFinal2;
		} res = semiFinal2 res semiFinal2 System.out.printf("%d", res) System.out.printf System.out System System.out "%d" res 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 res = 0;
//		String s = sc.next();
//		String res = "";
		long semiFinal1 = n % k;
		long semiFinal2 = k - semiFinal1;
//		System.out.printf("%d %d\n", semiFinal1, semiFinal2);

		if(semiFinal1 < semiFinal2) {
			res = semiFinal1;
		} else {
			res = semiFinal2;
		}
		System.out.printf("%d", res);
		//System.out.println(res);
	}
} public class Main {

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

		long n = sc.nextLong();
		long k = sc.nextLong();
		long res = 0;
//		String s = sc.next();
//		String res = "";
		long semiFinal1 = n % k;
		long semiFinal2 = k - semiFinal1;
//		System.out.printf("%d %d\n", semiFinal1, semiFinal2);

		if(semiFinal1 < semiFinal2) {
			res = semiFinal1;
		} else {
			res = semiFinal2;
		}
		System.out.printf("%d", res);
		//System.out.println(res);
	}
} 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 res = 0;
//		String s = sc.next();
//		String res = "";
		long semiFinal1 = n % k;
		long semiFinal2 = k - semiFinal1;
//		System.out.printf("%d %d\n", semiFinal1, semiFinal2);

		if(semiFinal1 < semiFinal2) {
			res = semiFinal1;
		} else {
			res = semiFinal2;
		}
		System.out.printf("%d", res);
		//System.out.println(res);
	}
}
