import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		long A = input.nextLong();
		long B = input.nextLong();
		long answer = A%B;
		System.out.println(Math.min(B-answer, answer));
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		long A = input.nextLong();
		long B = input.nextLong();
		long answer = A%B;
		System.out.println(Math.min(B-answer, answer));
	}
} Main public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		long A = input.nextLong();
		long B = input.nextLong();
		long answer = A%B;
		System.out.println(Math.min(B-answer, answer));
	} main {
		Scanner input = new Scanner(System.in);
		long A = input.nextLong();
		long B = input.nextLong();
		long answer = A%B;
		System.out.println(Math.min(B-answer, answer));
	} Scanner input = new Scanner(System.in); input new Scanner(System.in) long A = input.nextLong(); A input.nextLong() input.nextLong input long B = input.nextLong(); B input.nextLong() input.nextLong input long answer = A%B; answer A%B A B System.out.println(Math.min(B-answer, answer)) System.out.println System.out System System.out Math.min(B-answer, answer) Math.min Math B-answer B answer answer String[] args args public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		long A = input.nextLong();
		long B = input.nextLong();
		long answer = A%B;
		System.out.println(Math.min(B-answer, answer));
	}
} public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		long A = input.nextLong();
		long B = input.nextLong();
		long answer = A%B;
		System.out.println(Math.min(B-answer, answer));
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		long A = input.nextLong();
		long B = input.nextLong();
		long answer = A%B;
		System.out.println(Math.min(B-answer, answer));
	}
}