import java.math.BigDecimal;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		BigDecimal n = new BigDecimal(sc.next());
		BigDecimal k = new BigDecimal(sc.next());

		sc.close();

		BigDecimal a = n.divideToIntegralValue(k);
		BigDecimal b = a.multiply(k);
		BigDecimal c = n.subtract(b);

		BigDecimal d = c.subtract(k).abs();

		if(c.compareTo(d) < 0) {
			System.out.println(c);
		} else {
			System.out.println(d);
		}

	}

} import java.math.BigDecimal; BigDecimal java.math import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		BigDecimal n = new BigDecimal(sc.next());
		BigDecimal k = new BigDecimal(sc.next());

		sc.close();

		BigDecimal a = n.divideToIntegralValue(k);
		BigDecimal b = a.multiply(k);
		BigDecimal c = n.subtract(b);

		BigDecimal d = c.subtract(k).abs();

		if(c.compareTo(d) < 0) {
			System.out.println(c);
		} else {
			System.out.println(d);
		}

	}

} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		BigDecimal n = new BigDecimal(sc.next());
		BigDecimal k = new BigDecimal(sc.next());

		sc.close();

		BigDecimal a = n.divideToIntegralValue(k);
		BigDecimal b = a.multiply(k);
		BigDecimal c = n.subtract(b);

		BigDecimal d = c.subtract(k).abs();

		if(c.compareTo(d) < 0) {
			System.out.println(c);
		} else {
			System.out.println(d);
		}

	} main {

		Scanner sc = new Scanner(System.in);

		BigDecimal n = new BigDecimal(sc.next());
		BigDecimal k = new BigDecimal(sc.next());

		sc.close();

		BigDecimal a = n.divideToIntegralValue(k);
		BigDecimal b = a.multiply(k);
		BigDecimal c = n.subtract(b);

		BigDecimal d = c.subtract(k).abs();

		if(c.compareTo(d) < 0) {
			System.out.println(c);
		} else {
			System.out.println(d);
		}

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) BigDecimal n = new BigDecimal(sc.next()); n new BigDecimal(sc.next()) BigDecimal k = new BigDecimal(sc.next()); k new BigDecimal(sc.next()) sc.close() sc.close sc BigDecimal a = n.divideToIntegralValue(k); a n.divideToIntegralValue(k) n.divideToIntegralValue n k BigDecimal b = a.multiply(k); b a.multiply(k) a.multiply a k BigDecimal c = n.subtract(b); c n.subtract(b) n.subtract n b BigDecimal d = c.subtract(k).abs(); d c.subtract(k).abs() c.subtract(k).abs c.subtract(k) c.subtract c k if(c.compareTo(d) < 0) {
			System.out.println(c);
		} else {
			System.out.println(d);
		} c.compareTo(d) < 0 c.compareTo(d) c.compareTo c d 0 {
			System.out.println(c);
		} System.out.println(c) System.out.println System.out System System.out c {
			System.out.println(d);
		} System.out.println(d) System.out.println System.out System System.out d String[] args args public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		BigDecimal n = new BigDecimal(sc.next());
		BigDecimal k = new BigDecimal(sc.next());

		sc.close();

		BigDecimal a = n.divideToIntegralValue(k);
		BigDecimal b = a.multiply(k);
		BigDecimal c = n.subtract(b);

		BigDecimal d = c.subtract(k).abs();

		if(c.compareTo(d) < 0) {
			System.out.println(c);
		} else {
			System.out.println(d);
		}

	}

} public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		BigDecimal n = new BigDecimal(sc.next());
		BigDecimal k = new BigDecimal(sc.next());

		sc.close();

		BigDecimal a = n.divideToIntegralValue(k);
		BigDecimal b = a.multiply(k);
		BigDecimal c = n.subtract(b);

		BigDecimal d = c.subtract(k).abs();

		if(c.compareTo(d) < 0) {
			System.out.println(c);
		} else {
			System.out.println(d);
		}

	}

} Main
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		BigDecimal n = new BigDecimal(sc.next());
		BigDecimal k = new BigDecimal(sc.next());

		sc.close();

		BigDecimal a = n.divideToIntegralValue(k);
		BigDecimal b = a.multiply(k);
		BigDecimal c = n.subtract(b);

		BigDecimal d = c.subtract(k).abs();

		if(c.compareTo(d) < 0) {
			System.out.println(c);
		} else {
			System.out.println(d);
		}

	}

}
