import java.io.*;
import java.util.*;

public class Main extends PrintWriter {
	Main() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	}

	void main() {
		long n = sc.nextLong();
		long k = sc.nextLong();
		n %= k;
		println(Math.min(n, k - n));
	}
} import java.io.*; io.* java import java.util.*; util.* java public class Main extends PrintWriter {
	Main() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	}

	void main() {
		long n = sc.nextLong();
		long k = sc.nextLong();
		n %= k;
		println(Math.min(n, k - n));
	}
} Main Scanner sc = new Scanner(System.in); sc new Scanner(System.in) Main() { super(System.out, true); } Main { super(System.out, true); } super(System.out, true) super System.out System System.out true public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	} main {
		Main o = new Main(); o.main(); o.flush();
	} Main o = new Main(); o new Main() o.main() o.main o o.flush() o.flush o String[] $ $ void main() {
		long n = sc.nextLong();
		long k = sc.nextLong();
		n %= k;
		println(Math.min(n, k - n));
	} main {
		long n = sc.nextLong();
		long k = sc.nextLong();
		n %= k;
		println(Math.min(n, k - n));
	} long n = sc.nextLong(); n sc.nextLong() sc.nextLong sc long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc n %= k n k println(Math.min(n, k - n)) println Math.min(n, k - n) Math.min Math n k - n k n PrintWriter public class Main extends PrintWriter {
	Main() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	}

	void main() {
		long n = sc.nextLong();
		long k = sc.nextLong();
		n %= k;
		println(Math.min(n, k - n));
	}
} public class Main extends PrintWriter {
	Main() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	}

	void main() {
		long n = sc.nextLong();
		long k = sc.nextLong();
		n %= k;
		println(Math.min(n, k - n));
	}
} Main
import java.io.*;
import java.util.*;

public class Main extends PrintWriter {
	Main() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		Main o = new Main(); o.main(); o.flush();
	}

	void main() {
		long n = sc.nextLong();
		long k = sc.nextLong();
		n %= k;
		println(Math.min(n, k - n));
	}
}
