import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = 0, k = 0 ,ans = 0, a = 0;
		n = sc.nextLong();
		k = sc.nextLong();
		a = n % k;
		System.out.print(ans = Math.min(a, k - a));
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = 0, k = 0 ,ans = 0, a = 0;
		n = sc.nextLong();
		k = sc.nextLong();
		a = n % k;
		System.out.print(ans = Math.min(a, k - a));
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = 0, k = 0 ,ans = 0, a = 0;
		n = sc.nextLong();
		k = sc.nextLong();
		a = n % k;
		System.out.print(ans = Math.min(a, k - a));
	} main {
		Scanner sc = new Scanner(System.in);
		long n = 0, k = 0 ,ans = 0, a = 0;
		n = sc.nextLong();
		k = sc.nextLong();
		a = n % k;
		System.out.print(ans = Math.min(a, k - a));
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n = 0 n 0 k = 0 k 0 ans = 0 ans 0 a = 0; a 0 n = sc.nextLong() n sc.nextLong() sc.nextLong sc k = sc.nextLong() k sc.nextLong() sc.nextLong sc a = n % k a n % k n k System.out.print(ans = Math.min(a, k - a)) System.out.print System.out System System.out ans = Math.min(a, k - a) ans Math.min(a, k - a) Math.min Math a k - a k a String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = 0, k = 0 ,ans = 0, a = 0;
		n = sc.nextLong();
		k = sc.nextLong();
		a = n % k;
		System.out.print(ans = Math.min(a, k - a));
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = 0, k = 0 ,ans = 0, a = 0;
		n = sc.nextLong();
		k = sc.nextLong();
		a = n % k;
		System.out.print(ans = Math.min(a, k - a));
	}
} Main
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = 0, k = 0 ,ans = 0, a = 0;
		n = sc.nextLong();
		k = sc.nextLong();
		a = n % k;
		System.out.print(ans = Math.min(a, k - a));
	}
}
