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();
		sc.close();
		if (N < K) {
			System.out.println(Math.min(N, Math.abs(N - K)));
		} else {
			System.out.println(Math.min(N % K, Math.abs((N % K) - K)));
		}
	}

} 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();
		sc.close();
		if (N < K) {
			System.out.println(Math.min(N, Math.abs(N - K)));
		} else {
			System.out.println(Math.min(N % K, Math.abs((N % K) - K)));
		}
	}

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

} public class Main {

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

} 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();
		sc.close();
		if (N < K) {
			System.out.println(Math.min(N, Math.abs(N - K)));
		} else {
			System.out.println(Math.min(N % K, Math.abs((N % K) - K)));
		}
	}

}