import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong(), k = scan.nextLong();
		
		if(n == 0 || k == 0) {
			System.out.println(n);
			
		}else if(n < k) {
			System.out.println(Math.min(n, k - n));
			
		}else {
			System.out.println(Math.min(n % k, k - ( n % k )));
			
		}
		
		
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong(), k = scan.nextLong();
		
		if(n == 0 || k == 0) {
			System.out.println(n);
			
		}else if(n < k) {
			System.out.println(Math.min(n, k - n));
			
		}else {
			System.out.println(Math.min(n % k, k - ( n % k )));
			
		}
		
		
	}
} Main public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong(), k = scan.nextLong();
		
		if(n == 0 || k == 0) {
			System.out.println(n);
			
		}else if(n < k) {
			System.out.println(Math.min(n, k - n));
			
		}else {
			System.out.println(Math.min(n % k, k - ( n % k )));
			
		}
		
		
	} main {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong(), k = scan.nextLong();
		
		if(n == 0 || k == 0) {
			System.out.println(n);
			
		}else if(n < k) {
			System.out.println(Math.min(n, k - n));
			
		}else {
			System.out.println(Math.min(n % k, k - ( n % k )));
			
		}
		
		
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long n = scan.nextLong() n scan.nextLong() scan.nextLong scan k = scan.nextLong(); k scan.nextLong() scan.nextLong scan if(n == 0 || k == 0) {
			System.out.println(n);
			
		}else if(n < k) {
			System.out.println(Math.min(n, k - n));
			
		}else {
			System.out.println(Math.min(n % k, k - ( n % k )));
			
		} n == 0 || k == 0 n == 0 n 0 k == 0 k 0 {
			System.out.println(n);
			
		} System.out.println(n) System.out.println System.out System System.out n if(n < k) {
			System.out.println(Math.min(n, k - n));
			
		}else {
			System.out.println(Math.min(n % k, k - ( n % k )));
			
		} n < k n k {
			System.out.println(Math.min(n, k - n));
			
		} System.out.println(Math.min(n, k - n)) System.out.println System.out System System.out Math.min(n, k - n) Math.min Math n k - n k n {
			System.out.println(Math.min(n % k, k - ( n % k )));
			
		} System.out.println(Math.min(n % k, k - ( n % k ))) System.out.println System.out System System.out Math.min(n % k, k - ( n % k )) Math.min Math n % k n k k - ( n % k ) k ( n % k ) n k String args[] args public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong(), k = scan.nextLong();
		
		if(n == 0 || k == 0) {
			System.out.println(n);
			
		}else if(n < k) {
			System.out.println(Math.min(n, k - n));
			
		}else {
			System.out.println(Math.min(n % k, k - ( n % k )));
			
		}
		
		
	}
} public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong(), k = scan.nextLong();
		
		if(n == 0 || k == 0) {
			System.out.println(n);
			
		}else if(n < k) {
			System.out.println(Math.min(n, k - n));
			
		}else {
			System.out.println(Math.min(n % k, k - ( n % k )));
			
		}
		
		
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong(), k = scan.nextLong();
		
		if(n == 0 || k == 0) {
			System.out.println(n);
			
		}else if(n < k) {
			System.out.println(Math.min(n, k - n));
			
		}else {
			System.out.println(Math.min(n % k, k - ( n % k )));
			
		}
		
		
	}
}
