import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		sc.close();

		long ans = Math.min(n % k, Math.abs(n % k - k));
		
		System.out.println(ans);
	}

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

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		sc.close();

		long ans = Math.min(n % k, Math.abs(n % k - k));
		
		System.out.println(ans);
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		sc.close();

		long ans = Math.min(n % k, Math.abs(n % k - k));
		
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		sc.close();

		long ans = Math.min(n % k, Math.abs(n % k - k));
		
		System.out.println(ans);
	} 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 ans = Math.min(n % k, Math.abs(n % k - k)); ans Math.min(n % k, Math.abs(n % k - k)) Math.min Math n % k n k Math.abs(n % k - k) Math.abs Math n % k - k n % k n k k System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		sc.close();

		long ans = Math.min(n % k, Math.abs(n % k - k));
		
		System.out.println(ans);
	}

} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		sc.close();

		long ans = Math.min(n % k, Math.abs(n % k - k));
		
		System.out.println(ans);
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		sc.close();

		long ans = Math.min(n % k, Math.abs(n % k - k));
		
		System.out.println(ans);
	}

}