import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		final long N = in.nextLong();
		final long K = in.nextLong();
		if(N < K) System.out.println(Math.min(N, K - N));
		else {
			long mod = N % K;
			System.out.println((mod == 0) ? 0 : (Math.min(mod, Math.abs(K - mod))));
		}
	}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		final long N = in.nextLong();
		final long K = in.nextLong();
		if(N < K) System.out.println(Math.min(N, K - N));
		else {
			long mod = N % K;
			System.out.println((mod == 0) ? 0 : (Math.min(mod, Math.abs(K - mod))));
		}
	}
} Main public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		final long N = in.nextLong();
		final long K = in.nextLong();
		if(N < K) System.out.println(Math.min(N, K - N));
		else {
			long mod = N % K;
			System.out.println((mod == 0) ? 0 : (Math.min(mod, Math.abs(K - mod))));
		}
	} main {
		Scanner in = new Scanner(System.in);
		final long N = in.nextLong();
		final long K = in.nextLong();
		if(N < K) System.out.println(Math.min(N, K - N));
		else {
			long mod = N % K;
			System.out.println((mod == 0) ? 0 : (Math.min(mod, Math.abs(K - mod))));
		}
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) final long N = in.nextLong(); N in.nextLong() in.nextLong in final long K = in.nextLong(); K in.nextLong() in.nextLong in if(N < K) System.out.println(Math.min(N, K - N));
		else {
			long mod = N % K;
			System.out.println((mod == 0) ? 0 : (Math.min(mod, Math.abs(K - mod))));
		} N < K N K System.out.println(Math.min(N, K - N)) System.out.println System.out System System.out Math.min(N, K - N) Math.min Math N K - N K N {
			long mod = N % K;
			System.out.println((mod == 0) ? 0 : (Math.min(mod, Math.abs(K - mod))));
		} long mod = N % K; mod N % K N K System.out.println((mod == 0) ? 0 : (Math.min(mod, Math.abs(K - mod)))) System.out.println System.out System System.out (mod == 0) ? 0 : (Math.min(mod, Math.abs(K - mod))) (mod == 0) mod 0 0 (Math.min(mod, Math.abs(K - mod))) Math.min Math mod Math.abs(K - mod) Math.abs Math K - mod K mod String[] args args public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		final long N = in.nextLong();
		final long K = in.nextLong();
		if(N < K) System.out.println(Math.min(N, K - N));
		else {
			long mod = N % K;
			System.out.println((mod == 0) ? 0 : (Math.min(mod, Math.abs(K - mod))));
		}
	}
} public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		final long N = in.nextLong();
		final long K = in.nextLong();
		if(N < K) System.out.println(Math.min(N, K - N));
		else {
			long mod = N % K;
			System.out.println((mod == 0) ? 0 : (Math.min(mod, Math.abs(K - mod))));
		}
	}
} Main
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		final long N = in.nextLong();
		final long K = in.nextLong();
		if(N < K) System.out.println(Math.min(N, K - N));
		else {
			long mod = N % K;
			System.out.println((mod == 0) ? 0 : (Math.min(mod, Math.abs(K - mod))));
		}
	}
}