import java.math.BigInteger;
import java.util.Scanner;

public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String[] substr = str.split(" ", 0);

		String ns = substr[0];
		String ks = substr[1];
		sc.close();

		BigInteger ans = new BigInteger(ns);
		BigInteger buf = new BigInteger(ns);

		BigInteger n = new BigInteger(ns);
		BigInteger k = new BigInteger(ks);

		ans = n.mod(k);
		buf = ans.subtract(k).abs();
		if (ans.compareTo(buf)>0) {
			ans = buf;
		}
		System.out.println(ans);
	}
} import java.math.BigInteger; BigInteger java.math import java.util.Scanner; Scanner java.util public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String[] substr = str.split(" ", 0);

		String ns = substr[0];
		String ks = substr[1];
		sc.close();

		BigInteger ans = new BigInteger(ns);
		BigInteger buf = new BigInteger(ns);

		BigInteger n = new BigInteger(ns);
		BigInteger k = new BigInteger(ks);

		ans = n.mod(k);
		buf = ans.subtract(k).abs();
		if (ans.compareTo(buf)>0) {
			ans = buf;
		}
		System.out.println(ans);
	}
} Main public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String[] substr = str.split(" ", 0);

		String ns = substr[0];
		String ks = substr[1];
		sc.close();

		BigInteger ans = new BigInteger(ns);
		BigInteger buf = new BigInteger(ns);

		BigInteger n = new BigInteger(ns);
		BigInteger k = new BigInteger(ks);

		ans = n.mod(k);
		buf = ans.subtract(k).abs();
		if (ans.compareTo(buf)>0) {
			ans = buf;
		}
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String[] substr = str.split(" ", 0);

		String ns = substr[0];
		String ks = substr[1];
		sc.close();

		BigInteger ans = new BigInteger(ns);
		BigInteger buf = new BigInteger(ns);

		BigInteger n = new BigInteger(ns);
		BigInteger k = new BigInteger(ks);

		ans = n.mod(k);
		buf = ans.subtract(k).abs();
		if (ans.compareTo(buf)>0) {
			ans = buf;
		}
		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) String str = sc.nextLine(); str sc.nextLine() sc.nextLine sc String[] substr = str.split(" ", 0); substr str.split(" ", 0) str.split str " " 0 String ns = substr[0]; ns substr[0] substr 0 String ks = substr[1]; ks substr[1] substr 1 sc.close() sc.close sc BigInteger ans = new BigInteger(ns); ans new BigInteger(ns) BigInteger buf = new BigInteger(ns); buf new BigInteger(ns) BigInteger n = new BigInteger(ns); n new BigInteger(ns) BigInteger k = new BigInteger(ks); k new BigInteger(ks) ans = n.mod(k) ans n.mod(k) n.mod n k buf = ans.subtract(k).abs() buf ans.subtract(k).abs() ans.subtract(k).abs ans.subtract(k) ans.subtract ans k if (ans.compareTo(buf)>0) {
			ans = buf;
		} ans.compareTo(buf)>0 ans.compareTo(buf) ans.compareTo ans buf 0 {
			ans = buf;
		} ans = buf ans buf System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String[] substr = str.split(" ", 0);

		String ns = substr[0];
		String ks = substr[1];
		sc.close();

		BigInteger ans = new BigInteger(ns);
		BigInteger buf = new BigInteger(ns);

		BigInteger n = new BigInteger(ns);
		BigInteger k = new BigInteger(ks);

		ans = n.mod(k);
		buf = ans.subtract(k).abs();
		if (ans.compareTo(buf)>0) {
			ans = buf;
		}
		System.out.println(ans);
	}
} public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String[] substr = str.split(" ", 0);

		String ns = substr[0];
		String ks = substr[1];
		sc.close();

		BigInteger ans = new BigInteger(ns);
		BigInteger buf = new BigInteger(ns);

		BigInteger n = new BigInteger(ns);
		BigInteger k = new BigInteger(ks);

		ans = n.mod(k);
		buf = ans.subtract(k).abs();
		if (ans.compareTo(buf)>0) {
			ans = buf;
		}
		System.out.println(ans);
	}
} Main
import java.math.BigInteger;
import java.util.Scanner;

public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String[] substr = str.split(" ", 0);

		String ns = substr[0];
		String ks = substr[1];
		sc.close();

		BigInteger ans = new BigInteger(ns);
		BigInteger buf = new BigInteger(ns);

		BigInteger n = new BigInteger(ns);
		BigInteger k = new BigInteger(ks);

		ans = n.mod(k);
		buf = ans.subtract(k).abs();
		if (ans.compareTo(buf)>0) {
			ans = buf;
		}
		System.out.println(ans);
	}
}
