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();
		long result = 0;

			if(N%K==0) {
			result = 0;
		}else {
			if(N%K<K-N%K) {
				result = N%K;
			}else {
				result = K-N%K;
			}
		}
		System.out.println(result);
	}
} 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();
		long result = 0;

			if(N%K==0) {
			result = 0;
		}else {
			if(N%K<K-N%K) {
				result = N%K;
			}else {
				result = K-N%K;
			}
		}
		System.out.println(result);
	}
} Main public static void main(String args[]){
		Scanner scan = new Scanner(System.in);
		long N = scan.nextLong();
		long K = scan.nextLong();
		long result = 0;

			if(N%K==0) {
			result = 0;
		}else {
			if(N%K<K-N%K) {
				result = N%K;
			}else {
				result = K-N%K;
			}
		}
		System.out.println(result);
	} main {
		Scanner scan = new Scanner(System.in);
		long N = scan.nextLong();
		long K = scan.nextLong();
		long result = 0;

			if(N%K==0) {
			result = 0;
		}else {
			if(N%K<K-N%K) {
				result = N%K;
			}else {
				result = K-N%K;
			}
		}
		System.out.println(result);
	} 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 result = 0; result 0 if(N%K==0) {
			result = 0;
		}else {
			if(N%K<K-N%K) {
				result = N%K;
			}else {
				result = K-N%K;
			}
		} N%K==0 N%K N K 0 {
			result = 0;
		} result = 0 result 0 {
			if(N%K<K-N%K) {
				result = N%K;
			}else {
				result = K-N%K;
			}
		} if(N%K<K-N%K) {
				result = N%K;
			}else {
				result = K-N%K;
			} N%K<K-N%K N%K N K K-N%K K N%K N K {
				result = N%K;
			} result = N%K result N%K N K {
				result = K-N%K;
			} result = K-N%K result K-N%K K N%K N K System.out.println(result) System.out.println System.out System System.out result 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();
		long result = 0;

			if(N%K==0) {
			result = 0;
		}else {
			if(N%K<K-N%K) {
				result = N%K;
			}else {
				result = K-N%K;
			}
		}
		System.out.println(result);
	}
} public class Main{
	public static void main(String args[]){
		Scanner scan = new Scanner(System.in);
		long N = scan.nextLong();
		long K = scan.nextLong();
		long result = 0;

			if(N%K==0) {
			result = 0;
		}else {
			if(N%K<K-N%K) {
				result = N%K;
			}else {
				result = K-N%K;
			}
		}
		System.out.println(result);
	}
} Main
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();
		long result = 0;

			if(N%K==0) {
			result = 0;
		}else {
			if(N%K<K-N%K) {
				result = N%K;
			}else {
				result = K-N%K;
			}
		}
		System.out.println(result);
	}
}