import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		long x, y = 0;
		
		x = n % k;	//x < k
		y = k - x;
		
		if(x < y) {
			System.out.println(x);
		}
		else if(x > y) {
			System.out.println(y);
		}
		
	}

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

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		long x, y = 0;
		
		x = n % k;	//x < k
		y = k - x;
		
		if(x < y) {
			System.out.println(x);
		}
		else if(x > y) {
			System.out.println(y);
		}
		
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		long x, y = 0;
		
		x = n % k;	//x < k
		y = k - x;
		
		if(x < y) {
			System.out.println(x);
		}
		else if(x > y) {
			System.out.println(y);
		}
		
	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		long x, y = 0;
		
		x = n % k;	//x < k
		y = k - x;
		
		if(x < y) {
			System.out.println(x);
		}
		else if(x > y) {
			System.out.println(y);
		}
		
	} 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 x y = 0; y 0 x = n % k x n % k n k y = k - x y k - x k x if(x < y) {
			System.out.println(x);
		}
		else if(x > y) {
			System.out.println(y);
		} x < y x y {
			System.out.println(x);
		} System.out.println(x) System.out.println System.out System System.out x if(x > y) {
			System.out.println(y);
		} x > y x y {
			System.out.println(y);
		} System.out.println(y) System.out.println System.out System System.out y String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		long x, y = 0;
		
		x = n % k;	//x < k
		y = k - x;
		
		if(x < y) {
			System.out.println(x);
		}
		else if(x > y) {
			System.out.println(y);
		}
		
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		long x, y = 0;
		
		x = n % k;	//x < k
		y = k - x;
		
		if(x < y) {
			System.out.println(x);
		}
		else if(x > y) {
			System.out.println(y);
		}
		
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		
		long n = scan.nextLong();
		long k = scan.nextLong();
		
		long x, y = 0;
		
		x = n % k;	//x < k
		y = k - x;
		
		if(x < y) {
			System.out.println(x);
		}
		else if(x > y) {
			System.out.println(y);
		}
		
	}

}
