import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String[] command = br.readLine().split(" ");
		long N = Long.parseLong(command[0]);
		long K = Long.parseLong(command[1]);
		
		long a = N / K ;
		long b = N % K;
		
		long result;
		if(b == 0) result = 0;
		
		
		if(a == 0) {
			if(Math.abs(N) > Math.abs(N - K)) {
				result = Math.abs(N - K);
			}else {
				result = Math.abs(N);
			}
		}else{
			result = (Math.min(b, Math.abs(b - K)));
		}
		
		System.out.println(result);
	}

} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String[] command = br.readLine().split(" ");
		long N = Long.parseLong(command[0]);
		long K = Long.parseLong(command[1]);
		
		long a = N / K ;
		long b = N % K;
		
		long result;
		if(b == 0) result = 0;
		
		
		if(a == 0) {
			if(Math.abs(N) > Math.abs(N - K)) {
				result = Math.abs(N - K);
			}else {
				result = Math.abs(N);
			}
		}else{
			result = (Math.min(b, Math.abs(b - K)));
		}
		
		System.out.println(result);
	}

} Main public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String[] command = br.readLine().split(" ");
		long N = Long.parseLong(command[0]);
		long K = Long.parseLong(command[1]);
		
		long a = N / K ;
		long b = N % K;
		
		long result;
		if(b == 0) result = 0;
		
		
		if(a == 0) {
			if(Math.abs(N) > Math.abs(N - K)) {
				result = Math.abs(N - K);
			}else {
				result = Math.abs(N);
			}
		}else{
			result = (Math.min(b, Math.abs(b - K)));
		}
		
		System.out.println(result);
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String[] command = br.readLine().split(" ");
		long N = Long.parseLong(command[0]);
		long K = Long.parseLong(command[1]);
		
		long a = N / K ;
		long b = N % K;
		
		long result;
		if(b == 0) result = 0;
		
		
		if(a == 0) {
			if(Math.abs(N) > Math.abs(N - K)) {
				result = Math.abs(N - K);
			}else {
				result = Math.abs(N);
			}
		}else{
			result = (Math.min(b, Math.abs(b - K)));
		}
		
		System.out.println(result);
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] command = br.readLine().split(" "); command br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long N = Long.parseLong(command[0]); N Long.parseLong(command[0]) Long.parseLong Long command[0] command 0 long K = Long.parseLong(command[1]); K Long.parseLong(command[1]) Long.parseLong Long command[1] command 1 long a = N / K ; a N / K N K long b = N % K; b N % K N K long result; result if(b == 0) result = 0; b == 0 b 0 result = 0 result 0 if(a == 0) {
			if(Math.abs(N) > Math.abs(N - K)) {
				result = Math.abs(N - K);
			}else {
				result = Math.abs(N);
			}
		}else{
			result = (Math.min(b, Math.abs(b - K)));
		} a == 0 a 0 {
			if(Math.abs(N) > Math.abs(N - K)) {
				result = Math.abs(N - K);
			}else {
				result = Math.abs(N);
			}
		} if(Math.abs(N) > Math.abs(N - K)) {
				result = Math.abs(N - K);
			}else {
				result = Math.abs(N);
			} Math.abs(N) > Math.abs(N - K) Math.abs(N) Math.abs Math N Math.abs(N - K) Math.abs Math N - K N K {
				result = Math.abs(N - K);
			} result = Math.abs(N - K) result Math.abs(N - K) Math.abs Math N - K N K {
				result = Math.abs(N);
			} result = Math.abs(N) result Math.abs(N) Math.abs Math N {
			result = (Math.min(b, Math.abs(b - K)));
		} result = (Math.min(b, Math.abs(b - K))) result (Math.min(b, Math.abs(b - K))) Math.min Math b Math.abs(b - K) Math.abs Math b - K b K System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String[] command = br.readLine().split(" ");
		long N = Long.parseLong(command[0]);
		long K = Long.parseLong(command[1]);
		
		long a = N / K ;
		long b = N % K;
		
		long result;
		if(b == 0) result = 0;
		
		
		if(a == 0) {
			if(Math.abs(N) > Math.abs(N - K)) {
				result = Math.abs(N - K);
			}else {
				result = Math.abs(N);
			}
		}else{
			result = (Math.min(b, Math.abs(b - K)));
		}
		
		System.out.println(result);
	}

} public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String[] command = br.readLine().split(" ");
		long N = Long.parseLong(command[0]);
		long K = Long.parseLong(command[1]);
		
		long a = N / K ;
		long b = N % K;
		
		long result;
		if(b == 0) result = 0;
		
		
		if(a == 0) {
			if(Math.abs(N) > Math.abs(N - K)) {
				result = Math.abs(N - K);
			}else {
				result = Math.abs(N);
			}
		}else{
			result = (Math.min(b, Math.abs(b - K)));
		}
		
		System.out.println(result);
	}

} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String[] command = br.readLine().split(" ");
		long N = Long.parseLong(command[0]);
		long K = Long.parseLong(command[1]);
		
		long a = N / K ;
		long b = N % K;
		
		long result;
		if(b == 0) result = 0;
		
		
		if(a == 0) {
			if(Math.abs(N) > Math.abs(N - K)) {
				result = Math.abs(N - K);
			}else {
				result = Math.abs(N);
			}
		}else{
			result = (Math.min(b, Math.abs(b - K)));
		}
		
		System.out.println(result);
	}

}
