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

public class Main {

	public static void main(String [] args) {
		Scanner in =new Scanner(System.in);
		String N=in.next();
		String K=in.next();
		BigInteger b =new BigInteger(N);
		BigInteger jian =new BigInteger(K);
		b=b.mod(jian);
		  b=b.min(b.subtract(jian).abs());
		System.out.print(b.toString());

		
	}
} import java.util.*; util.* java import java.math.BigInteger; BigInteger java.math public class Main {

	public static void main(String [] args) {
		Scanner in =new Scanner(System.in);
		String N=in.next();
		String K=in.next();
		BigInteger b =new BigInteger(N);
		BigInteger jian =new BigInteger(K);
		b=b.mod(jian);
		  b=b.min(b.subtract(jian).abs());
		System.out.print(b.toString());

		
	}
} Main public static void main(String [] args) {
		Scanner in =new Scanner(System.in);
		String N=in.next();
		String K=in.next();
		BigInteger b =new BigInteger(N);
		BigInteger jian =new BigInteger(K);
		b=b.mod(jian);
		  b=b.min(b.subtract(jian).abs());
		System.out.print(b.toString());

		
	} main {
		Scanner in =new Scanner(System.in);
		String N=in.next();
		String K=in.next();
		BigInteger b =new BigInteger(N);
		BigInteger jian =new BigInteger(K);
		b=b.mod(jian);
		  b=b.min(b.subtract(jian).abs());
		System.out.print(b.toString());

		
	} Scanner in =new Scanner(System.in); in new Scanner(System.in) String N=in.next(); N in.next() in.next in String K=in.next(); K in.next() in.next in BigInteger b =new BigInteger(N); b new BigInteger(N) BigInteger jian =new BigInteger(K); jian new BigInteger(K) b=b.mod(jian) b b.mod(jian) b.mod b jian b=b.min(b.subtract(jian).abs()) b b.min(b.subtract(jian).abs()) b.min b b.subtract(jian).abs() b.subtract(jian).abs b.subtract(jian) b.subtract b jian System.out.print(b.toString()) System.out.print System.out System System.out b.toString() b.toString b String [] args args public class Main {

	public static void main(String [] args) {
		Scanner in =new Scanner(System.in);
		String N=in.next();
		String K=in.next();
		BigInteger b =new BigInteger(N);
		BigInteger jian =new BigInteger(K);
		b=b.mod(jian);
		  b=b.min(b.subtract(jian).abs());
		System.out.print(b.toString());

		
	}
} public class Main {

	public static void main(String [] args) {
		Scanner in =new Scanner(System.in);
		String N=in.next();
		String K=in.next();
		BigInteger b =new BigInteger(N);
		BigInteger jian =new BigInteger(K);
		b=b.mod(jian);
		  b=b.min(b.subtract(jian).abs());
		System.out.print(b.toString());

		
	}
} Main
import java.util.*;
import java.math.BigInteger;

public class Main {

	public static void main(String [] args) {
		Scanner in =new Scanner(System.in);
		String N=in.next();
		String K=in.next();
		BigInteger b =new BigInteger(N);
		BigInteger jian =new BigInteger(K);
		b=b.mod(jian);
		  b=b.min(b.subtract(jian).abs());
		System.out.print(b.toString());

		
	}
}