import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

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

		BigInteger n = sc.nextBigInteger();
		BigInteger k = sc.nextBigInteger();
		// NとKの余り
		BigInteger ans = n.remainder(k);

		// NとKの余りと余りとKの差を大小比較
		System.out.println(ans.min(ans.subtract(k).abs()));
	}
} import java.math.BigDecimal; BigDecimal java.math import java.math.BigInteger; BigInteger java.math import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		BigInteger n = sc.nextBigInteger();
		BigInteger k = sc.nextBigInteger();
		// NとKの余り
		BigInteger ans = n.remainder(k);

		// NとKの余りと余りとKの差を大小比較
		System.out.println(ans.min(ans.subtract(k).abs()));
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		BigInteger n = sc.nextBigInteger();
		BigInteger k = sc.nextBigInteger();
		// NとKの余り
		BigInteger ans = n.remainder(k);

		// NとKの余りと余りとKの差を大小比較
		System.out.println(ans.min(ans.subtract(k).abs()));
	} main {
		Scanner sc = new Scanner(System.in);

		BigInteger n = sc.nextBigInteger();
		BigInteger k = sc.nextBigInteger();
		// NとKの余り
		BigInteger ans = n.remainder(k);

		// NとKの余りと余りとKの差を大小比較
		System.out.println(ans.min(ans.subtract(k).abs()));
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) BigInteger n = sc.nextBigInteger(); n sc.nextBigInteger() sc.nextBigInteger sc BigInteger k = sc.nextBigInteger(); k sc.nextBigInteger() sc.nextBigInteger sc BigInteger ans = n.remainder(k); ans n.remainder(k) n.remainder n k System.out.println(ans.min(ans.subtract(k).abs())) System.out.println System.out System System.out ans.min(ans.subtract(k).abs()) ans.min ans ans.subtract(k).abs() ans.subtract(k).abs ans.subtract(k) ans.subtract ans k String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		BigInteger n = sc.nextBigInteger();
		BigInteger k = sc.nextBigInteger();
		// NとKの余り
		BigInteger ans = n.remainder(k);

		// NとKの余りと余りとKの差を大小比較
		System.out.println(ans.min(ans.subtract(k).abs()));
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		BigInteger n = sc.nextBigInteger();
		BigInteger k = sc.nextBigInteger();
		// NとKの余り
		BigInteger ans = n.remainder(k);

		// NとKの余りと余りとKの差を大小比較
		System.out.println(ans.min(ans.subtract(k).abs()));
	}
} Main
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

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

		BigInteger n = sc.nextBigInteger();
		BigInteger k = sc.nextBigInteger();
		// NとKの余り
		BigInteger ans = n.remainder(k);

		// NとKの余りと余りとKの差を大小比較
		System.out.println(ans.min(ans.subtract(k).abs()));
	}
}