import java.util.Scanner;

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

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		
		long N = sc.nextLong();
		long K = sc.nextLong();
		
		long min = N;
		if (N >= K) {
			min = N % K;
		}
		
		if (min > K / 2.0) {
			min = Math.abs(min - K);
		}
		
		System.out.println(min);
		
	}
}