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 min = n%k;
		long a = k-min;

		if(min > a) {
			min =a;
		}

		System.out.println(min);
		scan.close();
	}

} 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 min = n%k;
		long a = k-min;

		if(min > a) {
			min =a;
		}

		System.out.println(min);
		scan.close();
	}

} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		long n = scan.nextLong();
		long k = scan.nextLong();
		long min = n%k;
		long a = k-min;

		if(min > a) {
			min =a;
		}

		System.out.println(min);
		scan.close();
	} main {
		Scanner scan = new Scanner(System.in);

		long n = scan.nextLong();
		long k = scan.nextLong();
		long min = n%k;
		long a = k-min;

		if(min > a) {
			min =a;
		}

		System.out.println(min);
		scan.close();
	} 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 min = n%k; min n%k n k long a = k-min; a k-min k min if(min > a) {
			min =a;
		} min > a min a {
			min =a;
		} min =a min a System.out.println(min) System.out.println System.out System System.out min scan.close() scan.close scan 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 min = n%k;
		long a = k-min;

		if(min > a) {
			min =a;
		}

		System.out.println(min);
		scan.close();
	}

} public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		long n = scan.nextLong();
		long k = scan.nextLong();
		long min = n%k;
		long a = k-min;

		if(min > a) {
			min =a;
		}

		System.out.println(min);
		scan.close();
	}

} 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 min = n%k;
		long a = k-min;

		if(min > a) {
			min =a;
		}

		System.out.println(min);
		scan.close();
	}

}
