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

		n %= k;

		if(n <= Math.abs(n - k)) {
		}else {
			n = Math.abs(n - k);
		}

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

		n %= k;

		if(n <= Math.abs(n - k)) {
		}else {
			n = Math.abs(n - k);
		}

		System.out.println(n);
	}
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

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

		n %= k;

		if(n <= Math.abs(n - k)) {
		}else {
			n = Math.abs(n - k);
		}

		System.out.println(n);
	} main {
		Scanner scan = new Scanner(System.in);

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

		n %= k;

		if(n <= Math.abs(n - k)) {
		}else {
			n = Math.abs(n - k);
		}

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

		n %= k;

		if(n <= Math.abs(n - k)) {
		}else {
			n = Math.abs(n - k);
		}

		System.out.println(n);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

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

		n %= k;

		if(n <= Math.abs(n - k)) {
		}else {
			n = Math.abs(n - k);
		}

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

		n %= k;

		if(n <= Math.abs(n - k)) {
		}else {
			n = Math.abs(n - k);
		}

		System.out.println(n);
	}
}
