import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		
		long n = stdIn.nextLong();
		long k = stdIn.nextLong();
		
		long min = Math.min(n%k,k-(n%k));
		System.out.println(min);
		
		
		
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		
		long n = stdIn.nextLong();
		long k = stdIn.nextLong();
		
		long min = Math.min(n%k,k-(n%k));
		System.out.println(min);
		
		
		
	}

} Main public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		
		long n = stdIn.nextLong();
		long k = stdIn.nextLong();
		
		long min = Math.min(n%k,k-(n%k));
		System.out.println(min);
		
		
		
	} main {
		Scanner stdIn = new Scanner(System.in);
		
		
		long n = stdIn.nextLong();
		long k = stdIn.nextLong();
		
		long min = Math.min(n%k,k-(n%k));
		System.out.println(min);
		
		
		
	} Scanner stdIn = new Scanner(System.in); stdIn new Scanner(System.in) long n = stdIn.nextLong(); n stdIn.nextLong() stdIn.nextLong stdIn long k = stdIn.nextLong(); k stdIn.nextLong() stdIn.nextLong stdIn long min = Math.min(n%k,k-(n%k)); min Math.min(n%k,k-(n%k)) Math.min Math n%k n k k-(n%k) k (n%k) n 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 stdIn = new Scanner(System.in);
		
		
		long n = stdIn.nextLong();
		long k = stdIn.nextLong();
		
		long min = Math.min(n%k,k-(n%k));
		System.out.println(min);
		
		
		
	}

} public class Main {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		
		long n = stdIn.nextLong();
		long k = stdIn.nextLong();
		
		long min = Math.min(n%k,k-(n%k));
		System.out.println(min);
		
		
		
	}

} Main
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		
		long n = stdIn.nextLong();
		long k = stdIn.nextLong();
		
		long min = Math.min(n%k,k-(n%k));
		System.out.println(min);
		
		
		
	}

}
