import java.util.*;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		//n-kiが最小になればいい
		
		long x = n%k;
		if(Math.abs(x-k) < x) {
			System.out.println(Math.abs(x-k));
		}else {
			System.out.println(x);
		}
		
	}	
} import java.util.*; util.* java 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();
		long k = scan.nextLong();
		
		//n-kiが最小になればいい
		
		long x = n%k;
		if(Math.abs(x-k) < x) {
			System.out.println(Math.abs(x-k));
		}else {
			System.out.println(x);
		}
		
	}	
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		//n-kiが最小になればいい
		
		long x = n%k;
		if(Math.abs(x-k) < x) {
			System.out.println(Math.abs(x-k));
		}else {
			System.out.println(x);
		}
		
	} main {
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		//n-kiが最小になればいい
		
		long x = n%k;
		if(Math.abs(x-k) < x) {
			System.out.println(Math.abs(x-k));
		}else {
			System.out.println(x);
		}
		
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long n = scan.nextLong(); n scan.nextLong() scan.nextLong scan long k = scan.nextLong(); k scan.nextLong() scan.nextLong scan long x = n%k; x n%k n k if(Math.abs(x-k) < x) {
			System.out.println(Math.abs(x-k));
		}else {
			System.out.println(x);
		} Math.abs(x-k) < x Math.abs(x-k) Math.abs Math x-k x k x {
			System.out.println(Math.abs(x-k));
		} System.out.println(Math.abs(x-k)) System.out.println System.out System System.out Math.abs(x-k) Math.abs Math x-k x k {
			System.out.println(x);
		} System.out.println(x) System.out.println System.out System System.out x String[] args args public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		//n-kiが最小になればいい
		
		long x = n%k;
		if(Math.abs(x-k) < x) {
			System.out.println(Math.abs(x-k));
		}else {
			System.out.println(x);
		}
		
	}	
} public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		//n-kiが最小になればいい
		
		long x = n%k;
		if(Math.abs(x-k) < x) {
			System.out.println(Math.abs(x-k));
		}else {
			System.out.println(x);
		}
		
	}	
} Main
import java.util.*;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		//n-kiが最小になればいい
		
		long x = n%k;
		if(Math.abs(x-k) < x) {
			System.out.println(Math.abs(x-k));
		}else {
			System.out.println(x);
		}
		
	}	
}