import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		sc.close();

		long quotiment = N/K;

		long answer = 0;

		if(N%K == 0) {
			System.out.println(answer);
		}else {
			long innerQuot = N - (K * quotiment);
			long outerQuot = K * (quotiment + 1) - N;
			if(innerQuot <= outerQuot) {
				System.out.println(innerQuot);
			}else {
				System.out.println(outerQuot);
			}
		}
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		sc.close();

		long quotiment = N/K;

		long answer = 0;

		if(N%K == 0) {
			System.out.println(answer);
		}else {
			long innerQuot = N - (K * quotiment);
			long outerQuot = K * (quotiment + 1) - N;
			if(innerQuot <= outerQuot) {
				System.out.println(innerQuot);
			}else {
				System.out.println(outerQuot);
			}
		}
	}
} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		sc.close();

		long quotiment = N/K;

		long answer = 0;

		if(N%K == 0) {
			System.out.println(answer);
		}else {
			long innerQuot = N - (K * quotiment);
			long outerQuot = K * (quotiment + 1) - N;
			if(innerQuot <= outerQuot) {
				System.out.println(innerQuot);
			}else {
				System.out.println(outerQuot);
			}
		}
	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		sc.close();

		long quotiment = N/K;

		long answer = 0;

		if(N%K == 0) {
			System.out.println(answer);
		}else {
			long innerQuot = N - (K * quotiment);
			long outerQuot = K * (quotiment + 1) - N;
			if(innerQuot <= outerQuot) {
				System.out.println(innerQuot);
			}else {
				System.out.println(outerQuot);
			}
		}
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long N = sc.nextLong(); N sc.nextLong() sc.nextLong sc long K = sc.nextLong(); K sc.nextLong() sc.nextLong sc sc.close() sc.close sc long quotiment = N/K; quotiment N/K N K long answer = 0; answer 0 if(N%K == 0) {
			System.out.println(answer);
		}else {
			long innerQuot = N - (K * quotiment);
			long outerQuot = K * (quotiment + 1) - N;
			if(innerQuot <= outerQuot) {
				System.out.println(innerQuot);
			}else {
				System.out.println(outerQuot);
			}
		} N%K == 0 N%K N K 0 {
			System.out.println(answer);
		} System.out.println(answer) System.out.println System.out System System.out answer {
			long innerQuot = N - (K * quotiment);
			long outerQuot = K * (quotiment + 1) - N;
			if(innerQuot <= outerQuot) {
				System.out.println(innerQuot);
			}else {
				System.out.println(outerQuot);
			}
		} long innerQuot = N - (K * quotiment); innerQuot N - (K * quotiment) N (K * quotiment) K quotiment long outerQuot = K * (quotiment + 1) - N; outerQuot K * (quotiment + 1) - N K * (quotiment + 1) K (quotiment + 1) quotiment 1 N if(innerQuot <= outerQuot) {
				System.out.println(innerQuot);
			}else {
				System.out.println(outerQuot);
			} innerQuot <= outerQuot innerQuot outerQuot {
				System.out.println(innerQuot);
			} System.out.println(innerQuot) System.out.println System.out System System.out innerQuot {
				System.out.println(outerQuot);
			} System.out.println(outerQuot) System.out.println System.out System System.out outerQuot String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		sc.close();

		long quotiment = N/K;

		long answer = 0;

		if(N%K == 0) {
			System.out.println(answer);
		}else {
			long innerQuot = N - (K * quotiment);
			long outerQuot = K * (quotiment + 1) - N;
			if(innerQuot <= outerQuot) {
				System.out.println(innerQuot);
			}else {
				System.out.println(outerQuot);
			}
		}
	}
} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		sc.close();

		long quotiment = N/K;

		long answer = 0;

		if(N%K == 0) {
			System.out.println(answer);
		}else {
			long innerQuot = N - (K * quotiment);
			long outerQuot = K * (quotiment + 1) - N;
			if(innerQuot <= outerQuot) {
				System.out.println(innerQuot);
			}else {
				System.out.println(outerQuot);
			}
		}
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		sc.close();

		long quotiment = N/K;

		long answer = 0;

		if(N%K == 0) {
			System.out.println(answer);
		}else {
			long innerQuot = N - (K * quotiment);
			long outerQuot = K * (quotiment + 1) - N;
			if(innerQuot <= outerQuot) {
				System.out.println(innerQuot);
			}else {
				System.out.println(outerQuot);
			}
		}
	}
}
