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();
		if(n > k) {
			n -= k * (n/k);
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}else {
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}
	}
} 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();
		if(n > k) {
			n -= k * (n/k);
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}else {
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}
	}
} Main public static void main(String[] args) {


		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		if(n > k) {
			n -= k * (n/k);
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}else {
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}
	} main {


		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		if(n > k) {
			n -= k * (n/k);
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}else {
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}
	} 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 if(n > k) {
			n -= k * (n/k);
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}else {
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		} n > k n k {
			n -= k * (n/k);
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		} n -= k * (n/k) n k * (n/k) k (n/k) n k if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			} k -n < n k -n k n n {
				System.out.println(k-n);
			} System.out.println(k-n) System.out.println System.out System System.out k-n k n {
				System.out.println(n);
			} System.out.println(n) System.out.println System.out System System.out n {
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		} if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			} k -n < n k -n k n n {
				System.out.println(k-n);
			} System.out.println(k-n) System.out.println System.out System System.out k-n k n {
				System.out.println(n);
			} System.out.println(n) System.out.println System.out System System.out n 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();
		if(n > k) {
			n -= k * (n/k);
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}else {
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}
	}
} public class Main {

	public static void main(String[] args) {


		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		if(n > k) {
			n -= k * (n/k);
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}else {
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}
	}
} 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();
		if(n > k) {
			n -= k * (n/k);
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}else {
			if(k -n < n) {
				System.out.println(k-n);
			}else {
				System.out.println(n);
			}
		}
	}
}