import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

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

		if(N%K == 0) {
			System.out.println(0);
		} else {
			long remainder = N%K;
			long diff = Math.abs(remainder-K);
			System.out.println(Math.min(remainder, diff));
		}
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		if(N%K == 0) {
			System.out.println(0);
		} else {
			long remainder = N%K;
			long diff = Math.abs(remainder-K);
			System.out.println(Math.min(remainder, diff));
		}
	} main {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		if(N%K == 0) {
			System.out.println(0);
		} else {
			long remainder = N%K;
			long diff = Math.abs(remainder-K);
			System.out.println(Math.min(remainder, diff));
		}
	} 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 if(N%K == 0) {
			System.out.println(0);
		} else {
			long remainder = N%K;
			long diff = Math.abs(remainder-K);
			System.out.println(Math.min(remainder, diff));
		} N%K == 0 N%K N K 0 {
			System.out.println(0);
		} System.out.println(0) System.out.println System.out System System.out 0 {
			long remainder = N%K;
			long diff = Math.abs(remainder-K);
			System.out.println(Math.min(remainder, diff));
		} long remainder = N%K; remainder N%K N K long diff = Math.abs(remainder-K); diff Math.abs(remainder-K) Math.abs Math remainder-K remainder K System.out.println(Math.min(remainder, diff)) System.out.println System.out System System.out Math.min(remainder, diff) Math.min Math remainder diff String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		if(N%K == 0) {
			System.out.println(0);
		} else {
			long remainder = N%K;
			long diff = Math.abs(remainder-K);
			System.out.println(Math.min(remainder, diff));
		}
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		if(N%K == 0) {
			System.out.println(0);
		} else {
			long remainder = N%K;
			long diff = Math.abs(remainder-K);
			System.out.println(Math.min(remainder, diff));
		}
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		if(N%K == 0) {
			System.out.println(0);
		} else {
			long remainder = N%K;
			long diff = Math.abs(remainder-K);
			System.out.println(Math.min(remainder, diff));
		}
	}
}