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 res1 = n % k;
		long res2 = k - n % k;
		if(res1 > res2){
			res1 = res2;
		}
		System.out.println(res1);
	}
} 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 res1 = n % k;
		long res2 = k - n % k;
		if(res1 > res2){
			res1 = res2;
		}
		System.out.println(res1);
	}
} Main public static void main(String[]args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long res1 = n % k;
		long res2 = k - n % k;
		if(res1 > res2){
			res1 = res2;
		}
		System.out.println(res1);
	} main {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long res1 = n % k;
		long res2 = k - n % k;
		if(res1 > res2){
			res1 = res2;
		}
		System.out.println(res1);
	} 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 res1 = n % k; res1 n % k n k long res2 = k - n % k; res2 k - n % k k n % k n k if(res1 > res2){
			res1 = res2;
		} res1 > res2 res1 res2 {
			res1 = res2;
		} res1 = res2 res1 res2 System.out.println(res1) System.out.println System.out System System.out res1 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 res1 = n % k;
		long res2 = k - n % k;
		if(res1 > res2){
			res1 = res2;
		}
		System.out.println(res1);
	}
} public class Main {
	public static void main(String[]args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long res1 = n % k;
		long res2 = k - n % k;
		if(res1 > res2){
			res1 = res2;
		}
		System.out.println(res1);
	}
} 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 res1 = n % k;
		long res2 = k - n % k;
		if(res1 > res2){
			res1 = res2;
		}
		System.out.println(res1);
	}
}