import java.util.*;

public class Main {

	public static void main(String[] args) {

		// 入力
		Scanner sc = new Scanner(System.in);
		long n = Long.parseLong(sc.next());
		long k = Long.parseLong(sc.next());

		System.out.println(Math.min(n % k, k - (n % k)));
	}
} import java.util.*; util.* java public class Main {

	public static void main(String[] args) {

		// 入力
		Scanner sc = new Scanner(System.in);
		long n = Long.parseLong(sc.next());
		long k = Long.parseLong(sc.next());

		System.out.println(Math.min(n % k, k - (n % k)));
	}
} Main public static void main(String[] args) {

		// 入力
		Scanner sc = new Scanner(System.in);
		long n = Long.parseLong(sc.next());
		long k = Long.parseLong(sc.next());

		System.out.println(Math.min(n % k, k - (n % k)));
	} main {

		// 入力
		Scanner sc = new Scanner(System.in);
		long n = Long.parseLong(sc.next());
		long k = Long.parseLong(sc.next());

		System.out.println(Math.min(n % k, k - (n % k)));
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n = Long.parseLong(sc.next()); n Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long k = Long.parseLong(sc.next()); k Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc System.out.println(Math.min(n % k, k - (n % k))) System.out.println System.out System System.out Math.min(n % k, k - (n % k)) Math.min Math n % k n k k - (n % k) k (n % k) n k String[] args args public class Main {

	public static void main(String[] args) {

		// 入力
		Scanner sc = new Scanner(System.in);
		long n = Long.parseLong(sc.next());
		long k = Long.parseLong(sc.next());

		System.out.println(Math.min(n % k, k - (n % k)));
	}
} public class Main {

	public static void main(String[] args) {

		// 入力
		Scanner sc = new Scanner(System.in);
		long n = Long.parseLong(sc.next());
		long k = Long.parseLong(sc.next());

		System.out.println(Math.min(n % k, k - (n % k)));
	}
} Main
import java.util.*;

public class Main {

	public static void main(String[] args) {

		// 入力
		Scanner sc = new Scanner(System.in);
		long n = Long.parseLong(sc.next());
		long k = Long.parseLong(sc.next());

		System.out.println(Math.min(n % k, k - (n % k)));
	}
}
