import 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();
		long N_min= 0;
		
			if (N<=K){
			N_min=Math.min(Math.abs(N-K),Math.abs(N));
			System.out.println(N_min);
			}
			else{
			N_min=Math.min(N%K,Math.abs((N%K)-K));
			System.out.println(N_min);
			}
		}
    } import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long N_min= 0;
		
			if (N<=K){
			N_min=Math.min(Math.abs(N-K),Math.abs(N));
			System.out.println(N_min);
			}
			else{
			N_min=Math.min(N%K,Math.abs((N%K)-K));
			System.out.println(N_min);
			}
		}
    } Main public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long N_min= 0;
		
			if (N<=K){
			N_min=Math.min(Math.abs(N-K),Math.abs(N));
			System.out.println(N_min);
			}
			else{
			N_min=Math.min(N%K,Math.abs((N%K)-K));
			System.out.println(N_min);
			}
		} main {
    	Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long N_min= 0;
		
			if (N<=K){
			N_min=Math.min(Math.abs(N-K),Math.abs(N));
			System.out.println(N_min);
			}
			else{
			N_min=Math.min(N%K,Math.abs((N%K)-K));
			System.out.println(N_min);
			}
		} 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 N_min= 0; N_min 0 if (N<=K){
			N_min=Math.min(Math.abs(N-K),Math.abs(N));
			System.out.println(N_min);
			}
			else{
			N_min=Math.min(N%K,Math.abs((N%K)-K));
			System.out.println(N_min);
			} N<=K N K {
			N_min=Math.min(Math.abs(N-K),Math.abs(N));
			System.out.println(N_min);
			} N_min=Math.min(Math.abs(N-K),Math.abs(N)) N_min Math.min(Math.abs(N-K),Math.abs(N)) Math.min Math Math.abs(N-K) Math.abs Math N-K N K Math.abs(N) Math.abs Math N System.out.println(N_min) System.out.println System.out System System.out N_min {
			N_min=Math.min(N%K,Math.abs((N%K)-K));
			System.out.println(N_min);
			} N_min=Math.min(N%K,Math.abs((N%K)-K)) N_min Math.min(N%K,Math.abs((N%K)-K)) Math.min Math N%K N K Math.abs((N%K)-K) Math.abs Math (N%K)-K (N%K) N K K System.out.println(N_min) System.out.println System.out System System.out N_min String[] args args public class Main {
	public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long N_min= 0;
		
			if (N<=K){
			N_min=Math.min(Math.abs(N-K),Math.abs(N));
			System.out.println(N_min);
			}
			else{
			N_min=Math.min(N%K,Math.abs((N%K)-K));
			System.out.println(N_min);
			}
		}
    } public class Main {
	public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long N_min= 0;
		
			if (N<=K){
			N_min=Math.min(Math.abs(N-K),Math.abs(N));
			System.out.println(N_min);
			}
			else{
			N_min=Math.min(N%K,Math.abs((N%K)-K));
			System.out.println(N_min);
			}
		}
    } Main
import 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();
		long N_min= 0;
		
			if (N<=K){
			N_min=Math.min(Math.abs(N-K),Math.abs(N));
			System.out.println(N_min);
			}
			else{
			N_min=Math.min(N%K,Math.abs((N%K)-K));
			System.out.println(N_min);
			}
		}
    }