import java.util.Scanner;

public class Main {

	public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		sc.close();
		long p = N % K;
		// System.out.println("p = " + p);
		// System.out.println("abs = " + Math.abs(p - K));
		// System.out.println(1000000000000000000L - 1);
		// 1000000000000000000 999999999999999999
		System.out.println(N % K == 0 ? 0 : Math.min(Math.min(p, Math.abs(p - K)), N));
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		sc.close();
		long p = N % K;
		// System.out.println("p = " + p);
		// System.out.println("abs = " + Math.abs(p - K));
		// System.out.println(1000000000000000000L - 1);
		// 1000000000000000000 999999999999999999
		System.out.println(N % K == 0 ? 0 : Math.min(Math.min(p, Math.abs(p - K)), N));
	}
} Main public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		sc.close();
		long p = N % K;
		// System.out.println("p = " + p);
		// System.out.println("abs = " + Math.abs(p - K));
		// System.out.println(1000000000000000000L - 1);
		// 1000000000000000000 999999999999999999
		System.out.println(N % K == 0 ? 0 : Math.min(Math.min(p, Math.abs(p - K)), N));
	} main {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		sc.close();
		long p = N % K;
		// System.out.println("p = " + p);
		// System.out.println("abs = " + Math.abs(p - K));
		// System.out.println(1000000000000000000L - 1);
		// 1000000000000000000 999999999999999999
		System.out.println(N % K == 0 ? 0 : Math.min(Math.min(p, Math.abs(p - K)), N));
	} 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 sc.close() sc.close sc long p = N % K; p N % K N K System.out.println(N % K == 0 ? 0 : Math.min(Math.min(p, Math.abs(p - K)), N)) System.out.println System.out System System.out N % K == 0 ? 0 : Math.min(Math.min(p, Math.abs(p - K)), N) N % K == 0 N % K N K 0 0 Math.min(Math.min(p, Math.abs(p - K)), N) Math.min Math Math.min(p, Math.abs(p - K)) Math.min Math p Math.abs(p - K) Math.abs Math p - K p K N final String[] args args public class Main {

	public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		sc.close();
		long p = N % K;
		// System.out.println("p = " + p);
		// System.out.println("abs = " + Math.abs(p - K));
		// System.out.println(1000000000000000000L - 1);
		// 1000000000000000000 999999999999999999
		System.out.println(N % K == 0 ? 0 : Math.min(Math.min(p, Math.abs(p - K)), N));
	}
} public class Main {

	public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		sc.close();
		long p = N % K;
		// System.out.println("p = " + p);
		// System.out.println("abs = " + Math.abs(p - K));
		// System.out.println(1000000000000000000L - 1);
		// 1000000000000000000 999999999999999999
		System.out.println(N % K == 0 ? 0 : Math.min(Math.min(p, Math.abs(p - K)), N));
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		sc.close();
		long p = N % K;
		// System.out.println("p = " + p);
		// System.out.println("abs = " + Math.abs(p - K));
		// System.out.println(1000000000000000000L - 1);
		// 1000000000000000000 999999999999999999
		System.out.println(N % K == 0 ? 0 : Math.min(Math.min(p, Math.abs(p - K)), N));
	}
}