import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long mod = N % K;
		System.out.println(mod <= K / 2 ? mod : K - mod);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long mod = N % K;
		System.out.println(mod <= K / 2 ? mod : K - mod);
	}
} Main public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long mod = N % K;
		System.out.println(mod <= K / 2 ? mod : K - mod);
	} main {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long mod = N % K;
		System.out.println(mod <= K / 2 ? mod : K - mod);
	} 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 mod = N % K; mod N % K N K System.out.println(mod <= K / 2 ? mod : K - mod) System.out.println System.out System System.out mod <= K / 2 ? mod : K - mod mod <= K / 2 mod K / 2 K 2 mod K - mod K mod String[] args args public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long mod = N % K;
		System.out.println(mod <= K / 2 ? mod : K - mod);
	}
} public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long mod = N % K;
		System.out.println(mod <= K / 2 ? mod : K - mod);
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long mod = N % K;
		System.out.println(mod <= K / 2 ? mod : K - mod);
	}
}