import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		long n = Long.parseLong(str[0]);
		long k = Long.parseLong(str[1]);

		long m = n % k;
		if (m == 0) {
			System.out.println("0");
		} else {
			long min = Math.min(m, k - m);
			System.out.println(min);
		}
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		long n = Long.parseLong(str[0]);
		long k = Long.parseLong(str[1]);

		long m = n % k;
		if (m == 0) {
			System.out.println("0");
		} else {
			long min = Math.min(m, k - m);
			System.out.println(min);
		}
	}
} Main public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		long n = Long.parseLong(str[0]);
		long k = Long.parseLong(str[1]);

		long m = n % k;
		if (m == 0) {
			System.out.println("0");
		} else {
			long min = Math.min(m, k - m);
			System.out.println(min);
		}
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		long n = Long.parseLong(str[0]);
		long k = Long.parseLong(str[1]);

		long m = n % k;
		if (m == 0) {
			System.out.println("0");
		} else {
			long min = Math.min(m, k - m);
			System.out.println(min);
		}
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String str[] = br.readLine().split(" "); str br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long n = Long.parseLong(str[0]); n Long.parseLong(str[0]) Long.parseLong Long str[0] str 0 long k = Long.parseLong(str[1]); k Long.parseLong(str[1]) Long.parseLong Long str[1] str 1 long m = n % k; m n % k n k if (m == 0) {
			System.out.println("0");
		} else {
			long min = Math.min(m, k - m);
			System.out.println(min);
		} m == 0 m 0 {
			System.out.println("0");
		} System.out.println("0") System.out.println System.out System System.out "0" {
			long min = Math.min(m, k - m);
			System.out.println(min);
		} long min = Math.min(m, k - m); min Math.min(m, k - m) Math.min Math m k - m k m System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		long n = Long.parseLong(str[0]);
		long k = Long.parseLong(str[1]);

		long m = n % k;
		if (m == 0) {
			System.out.println("0");
		} else {
			long min = Math.min(m, k - m);
			System.out.println(min);
		}
	}
} public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		long n = Long.parseLong(str[0]);
		long k = Long.parseLong(str[1]);

		long m = n % k;
		if (m == 0) {
			System.out.println("0");
		} else {
			long min = Math.min(m, k - m);
			System.out.println(min);
		}
	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String str[] = br.readLine().split(" ");

		long n = Long.parseLong(str[0]);
		long k = Long.parseLong(str[1]);

		long m = n % k;
		if (m == 0) {
			System.out.println("0");
		} else {
			long min = Math.min(m, k - m);
			System.out.println(min);
		}
	}
}