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 ans = n;
		long num = n % k;
		long num2 = 0;

		while(true) {
			num2 = Math.abs(num);
			ans = Math.min(ans, num2);
			if(num < 0) {
				break;
			}
			num = num -k;
		}
		
		System.out.println(ans);

	}
} 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 ans = n;
		long num = n % k;
		long num2 = 0;

		while(true) {
			num2 = Math.abs(num);
			ans = Math.min(ans, num2);
			if(num < 0) {
				break;
			}
			num = num -k;
		}
		
		System.out.println(ans);

	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		
		long n = sc.nextLong();
		long k = sc.nextLong();
		long ans = n;
		long num = n % k;
		long num2 = 0;

		while(true) {
			num2 = Math.abs(num);
			ans = Math.min(ans, num2);
			if(num < 0) {
				break;
			}
			num = num -k;
		}
		
		System.out.println(ans);

	} main {

		Scanner sc = new Scanner(System.in);
		
		long n = sc.nextLong();
		long k = sc.nextLong();
		long ans = n;
		long num = n % k;
		long num2 = 0;

		while(true) {
			num2 = Math.abs(num);
			ans = Math.min(ans, num2);
			if(num < 0) {
				break;
			}
			num = num -k;
		}
		
		System.out.println(ans);

	} 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 ans = n; ans n long num = n % k; num n % k n k long num2 = 0; num2 0 while(true) {
			num2 = Math.abs(num);
			ans = Math.min(ans, num2);
			if(num < 0) {
				break;
			}
			num = num -k;
		} true {
			num2 = Math.abs(num);
			ans = Math.min(ans, num2);
			if(num < 0) {
				break;
			}
			num = num -k;
		} num2 = Math.abs(num) num2 Math.abs(num) Math.abs Math num ans = Math.min(ans, num2) ans Math.min(ans, num2) Math.min Math ans num2 if(num < 0) {
				break;
			} num < 0 num 0 {
				break;
			} break; num = num -k num num -k num k System.out.println(ans) System.out.println System.out System System.out ans 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 ans = n;
		long num = n % k;
		long num2 = 0;

		while(true) {
			num2 = Math.abs(num);
			ans = Math.min(ans, num2);
			if(num < 0) {
				break;
			}
			num = num -k;
		}
		
		System.out.println(ans);

	}
} public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		
		long n = sc.nextLong();
		long k = sc.nextLong();
		long ans = n;
		long num = n % k;
		long num2 = 0;

		while(true) {
			num2 = Math.abs(num);
			ans = Math.min(ans, num2);
			if(num < 0) {
				break;
			}
			num = num -k;
		}
		
		System.out.println(ans);

	}
} 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 ans = n;
		long num = n % k;
		long num2 = 0;

		while(true) {
			num2 = Math.abs(num);
			ans = Math.min(ans, num2);
			if(num < 0) {
				break;
			}
			num = num -k;
		}
		
		System.out.println(ans);

	}
}
