import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		Long N = scan.nextLong();
		Long K = scan.nextLong();

		Long  m = new Long(N % K);
		Long x = new Long(K - m);

		if(m.compareTo(x) == 1) {
			System.out.println(x);
		}else {
			System.out.println(m);
		}


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

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		Long N = scan.nextLong();
		Long K = scan.nextLong();

		Long  m = new Long(N % K);
		Long x = new Long(K - m);

		if(m.compareTo(x) == 1) {
			System.out.println(x);
		}else {
			System.out.println(m);
		}


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

		Scanner scan = new Scanner(System.in);

		Long N = scan.nextLong();
		Long K = scan.nextLong();

		Long  m = new Long(N % K);
		Long x = new Long(K - m);

		if(m.compareTo(x) == 1) {
			System.out.println(x);
		}else {
			System.out.println(m);
		}


	} main {

		Scanner scan = new Scanner(System.in);

		Long N = scan.nextLong();
		Long K = scan.nextLong();

		Long  m = new Long(N % K);
		Long x = new Long(K - m);

		if(m.compareTo(x) == 1) {
			System.out.println(x);
		}else {
			System.out.println(m);
		}


	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) Long N = scan.nextLong(); N scan.nextLong() scan.nextLong scan Long K = scan.nextLong(); K scan.nextLong() scan.nextLong scan Long  m = new Long(N % K); m new Long(N % K) Long x = new Long(K - m); x new Long(K - m) if(m.compareTo(x) == 1) {
			System.out.println(x);
		}else {
			System.out.println(m);
		} m.compareTo(x) == 1 m.compareTo(x) m.compareTo m x 1 {
			System.out.println(x);
		} System.out.println(x) System.out.println System.out System System.out x {
			System.out.println(m);
		} System.out.println(m) System.out.println System.out System System.out m String[] args args public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		Long N = scan.nextLong();
		Long K = scan.nextLong();

		Long  m = new Long(N % K);
		Long x = new Long(K - m);

		if(m.compareTo(x) == 1) {
			System.out.println(x);
		}else {
			System.out.println(m);
		}


	}
} public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		Long N = scan.nextLong();
		Long K = scan.nextLong();

		Long  m = new Long(N % K);
		Long x = new Long(K - m);

		if(m.compareTo(x) == 1) {
			System.out.println(x);
		}else {
			System.out.println(m);
		}


	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		Long N = scan.nextLong();
		Long K = scan.nextLong();

		Long  m = new Long(N % K);
		Long x = new Long(K - m);

		if(m.compareTo(x) == 1) {
			System.out.println(x);
		}else {
			System.out.println(m);
		}


	}
}