import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
	long N, K;
	long min;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		N = Long.parseLong(tokens[0]);
		K = Long.parseLong(tokens[1]);
	}

	void calc() {
		min = N;
		min = Math.min(min, N % K);
		min = Math.min(min, K - N % K);

	}

	void showResult() {
		System.out.println(min);
	}

} import java.util.*; util.* java import java.util.stream.Collectors; Collectors java.util.stream import java.io.*; io.* java import java.nio.charset.StandardCharsets; StandardCharsets java.nio.charset public class Main {
	long N, K;
	long min;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		N = Long.parseLong(tokens[0]);
		K = Long.parseLong(tokens[1]);
	}

	void calc() {
		min = N;
		min = Math.min(min, N % K);
		min = Math.min(min, K - N % K);

	}

	void showResult() {
		System.out.println(min);
	}

} Main long N N K; K long min; min public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	} main {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	} InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8); reader new InputStreamReader(System.in, StandardCharsets.UTF_8) BufferedReader in = new BufferedReader(reader); in new BufferedReader(reader) Main ins = new Main(in); ins new Main(in) ins.calc() ins.calc ins ins.showResult() ins.showResult ins String[] args args Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		N = Long.parseLong(tokens[0]);
		K = Long.parseLong(tokens[1]);
	} Main {
		String[] tokens = in.readLine().split(" ");
		N = Long.parseLong(tokens[0]);
		K = Long.parseLong(tokens[1]);
	} String[] tokens = in.readLine().split(" "); tokens in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " N = Long.parseLong(tokens[0]) N Long.parseLong(tokens[0]) Long.parseLong Long tokens[0] tokens 0 K = Long.parseLong(tokens[1]) K Long.parseLong(tokens[1]) Long.parseLong Long tokens[1] tokens 1 BufferedReader in in void calc() {
		min = N;
		min = Math.min(min, N % K);
		min = Math.min(min, K - N % K);

	} calc {
		min = N;
		min = Math.min(min, N % K);
		min = Math.min(min, K - N % K);

	} min = N min N min = Math.min(min, N % K) min Math.min(min, N % K) Math.min Math min N % K N K min = Math.min(min, K - N % K) min Math.min(min, K - N % K) Math.min Math min K - N % K K N % K N K void showResult() {
		System.out.println(min);
	} showResult {
		System.out.println(min);
	} System.out.println(min) System.out.println System.out System System.out min public class Main {
	long N, K;
	long min;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		N = Long.parseLong(tokens[0]);
		K = Long.parseLong(tokens[1]);
	}

	void calc() {
		min = N;
		min = Math.min(min, N % K);
		min = Math.min(min, K - N % K);

	}

	void showResult() {
		System.out.println(min);
	}

} public class Main {
	long N, K;
	long min;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		N = Long.parseLong(tokens[0]);
		K = Long.parseLong(tokens[1]);
	}

	void calc() {
		min = N;
		min = Math.min(min, N % K);
		min = Math.min(min, K - N % K);

	}

	void showResult() {
		System.out.println(min);
	}

} Main
import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
	long N, K;
	long min;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		N = Long.parseLong(tokens[0]);
		K = Long.parseLong(tokens[1]);
	}

	void calc() {
		min = N;
		min = Math.min(min, N % K);
		min = Math.min(min, K - N % K);

	}

	void showResult() {
		System.out.println(min);
	}

}
