import 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();
		long ans = -1;
		long[] tmp = new long[2];
		long i = -1;
		ans = Math.abs(n-k);
		long j = ans;
		if(n > k){
			long z = n/k;
			long w = z+1;
			z = Math.abs(n-z*k);
			w = Math.abs(n-w*k);
			System.out.println(Math.min(z, w));
		} else {
			while(ans != 0 || i != ans){
				ans = Math.abs(ans-k);
				if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				}
				i = j;
				j = ans;
			}
		}
	}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long ans = -1;
		long[] tmp = new long[2];
		long i = -1;
		ans = Math.abs(n-k);
		long j = ans;
		if(n > k){
			long z = n/k;
			long w = z+1;
			z = Math.abs(n-z*k);
			w = Math.abs(n-w*k);
			System.out.println(Math.min(z, w));
		} else {
			while(ans != 0 || i != ans){
				ans = Math.abs(ans-k);
				if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				}
				i = j;
				j = ans;
			}
		}
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long ans = -1;
		long[] tmp = new long[2];
		long i = -1;
		ans = Math.abs(n-k);
		long j = ans;
		if(n > k){
			long z = n/k;
			long w = z+1;
			z = Math.abs(n-z*k);
			w = Math.abs(n-w*k);
			System.out.println(Math.min(z, w));
		} else {
			while(ans != 0 || i != ans){
				ans = Math.abs(ans-k);
				if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				}
				i = j;
				j = ans;
			}
		}
	} main {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long ans = -1;
		long[] tmp = new long[2];
		long i = -1;
		ans = Math.abs(n-k);
		long j = ans;
		if(n > k){
			long z = n/k;
			long w = z+1;
			z = Math.abs(n-z*k);
			w = Math.abs(n-w*k);
			System.out.println(Math.min(z, w));
		} else {
			while(ans != 0 || i != ans){
				ans = Math.abs(ans-k);
				if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				}
				i = j;
				j = 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 long ans = -1; ans -1 1 long[] tmp = new long[2]; tmp new long[2] 2 long i = -1; i -1 1 ans = Math.abs(n-k) ans Math.abs(n-k) Math.abs Math n-k n k long j = ans; j ans if(n > k){
			long z = n/k;
			long w = z+1;
			z = Math.abs(n-z*k);
			w = Math.abs(n-w*k);
			System.out.println(Math.min(z, w));
		} else {
			while(ans != 0 || i != ans){
				ans = Math.abs(ans-k);
				if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				}
				i = j;
				j = ans;
			}
		} n > k n k {
			long z = n/k;
			long w = z+1;
			z = Math.abs(n-z*k);
			w = Math.abs(n-w*k);
			System.out.println(Math.min(z, w));
		} long z = n/k; z n/k n k long w = z+1; w z+1 z 1 z = Math.abs(n-z*k) z Math.abs(n-z*k) Math.abs Math n-z*k n z*k z k w = Math.abs(n-w*k) w Math.abs(n-w*k) Math.abs Math n-w*k n w*k w k System.out.println(Math.min(z, w)) System.out.println System.out System System.out Math.min(z, w) Math.min Math z w {
			while(ans != 0 || i != ans){
				ans = Math.abs(ans-k);
				if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				}
				i = j;
				j = ans;
			}
		} while(ans != 0 || i != ans){
				ans = Math.abs(ans-k);
				if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				}
				i = j;
				j = ans;
			} ans != 0 || i != ans ans != 0 ans 0 i != ans i ans {
				ans = Math.abs(ans-k);
				if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				}
				i = j;
				j = ans;
			} ans = Math.abs(ans-k) ans Math.abs(ans-k) Math.abs Math ans-k ans k if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				} ans == 0 || i == ans ans == 0 ans 0 i == ans i ans {
					System.out.println(Math.min(ans, j));
					break;
				} System.out.println(Math.min(ans, j)) System.out.println System.out System System.out Math.min(ans, j) Math.min Math ans j break; i = j i j j = ans j 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();
		long ans = -1;
		long[] tmp = new long[2];
		long i = -1;
		ans = Math.abs(n-k);
		long j = ans;
		if(n > k){
			long z = n/k;
			long w = z+1;
			z = Math.abs(n-z*k);
			w = Math.abs(n-w*k);
			System.out.println(Math.min(z, w));
		} else {
			while(ans != 0 || i != ans){
				ans = Math.abs(ans-k);
				if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				}
				i = j;
				j = ans;
			}
		}
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long ans = -1;
		long[] tmp = new long[2];
		long i = -1;
		ans = Math.abs(n-k);
		long j = ans;
		if(n > k){
			long z = n/k;
			long w = z+1;
			z = Math.abs(n-z*k);
			w = Math.abs(n-w*k);
			System.out.println(Math.min(z, w));
		} else {
			while(ans != 0 || i != ans){
				ans = Math.abs(ans-k);
				if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				}
				i = j;
				j = ans;
			}
		}
	}
} Main
import 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();
		long ans = -1;
		long[] tmp = new long[2];
		long i = -1;
		ans = Math.abs(n-k);
		long j = ans;
		if(n > k){
			long z = n/k;
			long w = z+1;
			z = Math.abs(n-z*k);
			w = Math.abs(n-w*k);
			System.out.println(Math.min(z, w));
		} else {
			while(ans != 0 || i != ans){
				ans = Math.abs(ans-k);
				if(ans == 0 || i == ans) {
					System.out.println(Math.min(ans, j));
					break;
				}
				i = j;
				j = ans;
			}
		}
	}
}
