import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		String[] t = sc.nextLine().split(" ");
		long n = Long.parseLong(t[0]);
		long k = Long.parseLong(t[1]);

		long min = Math.min(n, n % k);
		min = Math.min(min, (n % k) + k);
		min = Math.min(min, Math.abs((n % k) - k));

		System.out.println(min);
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		String[] t = sc.nextLine().split(" ");
		long n = Long.parseLong(t[0]);
		long k = Long.parseLong(t[1]);

		long min = Math.min(n, n % k);
		min = Math.min(min, (n % k) + k);
		min = Math.min(min, Math.abs((n % k) - k));

		System.out.println(min);
	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		String[] t = sc.nextLine().split(" ");
		long n = Long.parseLong(t[0]);
		long k = Long.parseLong(t[1]);

		long min = Math.min(n, n % k);
		min = Math.min(min, (n % k) + k);
		min = Math.min(min, Math.abs((n % k) - k));

		System.out.println(min);
	} main {

		Scanner sc = new Scanner(System.in);

		String[] t = sc.nextLine().split(" ");
		long n = Long.parseLong(t[0]);
		long k = Long.parseLong(t[1]);

		long min = Math.min(n, n % k);
		min = Math.min(min, (n % k) + k);
		min = Math.min(min, Math.abs((n % k) - k));

		System.out.println(min);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) String[] t = sc.nextLine().split(" "); t sc.nextLine().split(" ") sc.nextLine().split sc.nextLine() sc.nextLine sc " " long n = Long.parseLong(t[0]); n Long.parseLong(t[0]) Long.parseLong Long t[0] t 0 long k = Long.parseLong(t[1]); k Long.parseLong(t[1]) Long.parseLong Long t[1] t 1 long min = Math.min(n, n % k); min Math.min(n, n % k) Math.min Math n n % k n k min = Math.min(min, (n % k) + k) min Math.min(min, (n % k) + k) Math.min Math min (n % k) + k (n % k) n k k min = Math.min(min, Math.abs((n % k) - k)) min Math.min(min, Math.abs((n % k) - k)) Math.min Math min Math.abs((n % k) - k) Math.abs Math (n % k) - k (n % k) n k k System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		String[] t = sc.nextLine().split(" ");
		long n = Long.parseLong(t[0]);
		long k = Long.parseLong(t[1]);

		long min = Math.min(n, n % k);
		min = Math.min(min, (n % k) + k);
		min = Math.min(min, Math.abs((n % k) - k));

		System.out.println(min);
	}
} public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		String[] t = sc.nextLine().split(" ");
		long n = Long.parseLong(t[0]);
		long k = Long.parseLong(t[1]);

		long min = Math.min(n, n % k);
		min = Math.min(min, (n % k) + k);
		min = Math.min(min, Math.abs((n % k) - k));

		System.out.println(min);
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		String[] t = sc.nextLine().split(" ");
		long n = Long.parseLong(t[0]);
		long k = Long.parseLong(t[1]);

		long min = Math.min(n, n % k);
		min = Math.min(min, (n % k) + k);
		min = Math.min(min, Math.abs((n % k) - k));

		System.out.println(min);
	}
}
