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();
		if (n == 0) {
			System.out.println(0);
			return;
		}

		long bn = Long.MAX_VALUE;
		while (true) {
			bn = n;
			n = m(n, k);
			if (bn >= n) {
				n = Math.min(k - n, n);
				System.out.println(n);
				break;
			}
		}

	}

	public static long m(long n, long k) {
		return n - ((long) (n / k)) * k;
	}
} 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();
		if (n == 0) {
			System.out.println(0);
			return;
		}

		long bn = Long.MAX_VALUE;
		while (true) {
			bn = n;
			n = m(n, k);
			if (bn >= n) {
				n = Math.min(k - n, n);
				System.out.println(n);
				break;
			}
		}

	}

	public static long m(long n, long k) {
		return n - ((long) (n / k)) * k;
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		sc.close();
		if (n == 0) {
			System.out.println(0);
			return;
		}

		long bn = Long.MAX_VALUE;
		while (true) {
			bn = n;
			n = m(n, k);
			if (bn >= n) {
				n = Math.min(k - n, n);
				System.out.println(n);
				break;
			}
		}

	} main {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		sc.close();
		if (n == 0) {
			System.out.println(0);
			return;
		}

		long bn = Long.MAX_VALUE;
		while (true) {
			bn = n;
			n = m(n, k);
			if (bn >= n) {
				n = Math.min(k - n, n);
				System.out.println(n);
				break;
			}
		}

	} 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 if (n == 0) {
			System.out.println(0);
			return;
		} n == 0 n 0 {
			System.out.println(0);
			return;
		} System.out.println(0) System.out.println System.out System System.out 0 return; long bn = Long.MAX_VALUE; bn Long.MAX_VALUE Long Long.MAX_VALUE while (true) {
			bn = n;
			n = m(n, k);
			if (bn >= n) {
				n = Math.min(k - n, n);
				System.out.println(n);
				break;
			}
		} true {
			bn = n;
			n = m(n, k);
			if (bn >= n) {
				n = Math.min(k - n, n);
				System.out.println(n);
				break;
			}
		} bn = n bn n n = m(n, k) n m(n, k) m n k if (bn >= n) {
				n = Math.min(k - n, n);
				System.out.println(n);
				break;
			} bn >= n bn n {
				n = Math.min(k - n, n);
				System.out.println(n);
				break;
			} n = Math.min(k - n, n) n Math.min(k - n, n) Math.min Math k - n k n n System.out.println(n) System.out.println System.out System System.out n break; String[] args args public static long m(long n, long k) {
		return n - ((long) (n / k)) * k;
	} m {
		return n - ((long) (n / k)) * k;
	} return n - ((long) (n / k)) * k; n - ((long) (n / k)) * k n ((long) (n / k)) * k ((long) (n / k)) n k k long n n long k k 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();
		if (n == 0) {
			System.out.println(0);
			return;
		}

		long bn = Long.MAX_VALUE;
		while (true) {
			bn = n;
			n = m(n, k);
			if (bn >= n) {
				n = Math.min(k - n, n);
				System.out.println(n);
				break;
			}
		}

	}

	public static long m(long n, long k) {
		return n - ((long) (n / k)) * k;
	}
} 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();
		if (n == 0) {
			System.out.println(0);
			return;
		}

		long bn = Long.MAX_VALUE;
		while (true) {
			bn = n;
			n = m(n, k);
			if (bn >= n) {
				n = Math.min(k - n, n);
				System.out.println(n);
				break;
			}
		}

	}

	public static long m(long n, long k) {
		return n - ((long) (n / k)) * k;
	}
} 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();
		if (n == 0) {
			System.out.println(0);
			return;
		}

		long bn = Long.MAX_VALUE;
		while (true) {
			bn = n;
			n = m(n, k);
			if (bn >= n) {
				n = Math.min(k - n, n);
				System.out.println(n);
				break;
			}
		}

	}

	public static long m(long n, long k) {
		return n - ((long) (n / k)) * k;
	}
}
