import 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();
		if(n<k) {
			System.out.println((long)Math.min(n, k-n));
		}else if(n%k==0)System.out.println(0);
		else {
			System.out.println(k-(n%k));
		}
		
		sc.close();
	}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		long n=sc.nextLong();
		long k=sc.nextLong();
		if(n<k) {
			System.out.println((long)Math.min(n, k-n));
		}else if(n%k==0)System.out.println(0);
		else {
			System.out.println(k-(n%k));
		}
		
		sc.close();
	}
} Main public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		long n=sc.nextLong();
		long k=sc.nextLong();
		if(n<k) {
			System.out.println((long)Math.min(n, k-n));
		}else if(n%k==0)System.out.println(0);
		else {
			System.out.println(k-(n%k));
		}
		
		sc.close();
	} main {
		Scanner sc =new Scanner(System.in);
		long n=sc.nextLong();
		long k=sc.nextLong();
		if(n<k) {
			System.out.println((long)Math.min(n, k-n));
		}else if(n%k==0)System.out.println(0);
		else {
			System.out.println(k-(n%k));
		}
		
		sc.close();
	} 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 if(n<k) {
			System.out.println((long)Math.min(n, k-n));
		}else if(n%k==0)System.out.println(0);
		else {
			System.out.println(k-(n%k));
		} n<k n k {
			System.out.println((long)Math.min(n, k-n));
		} System.out.println((long)Math.min(n, k-n)) System.out.println System.out System System.out (long)Math.min(n, k-n) Math.min Math n k-n k n if(n%k==0)System.out.println(0);
		else {
			System.out.println(k-(n%k));
		} n%k==0 n%k n k 0 System.out.println(0) System.out.println System.out System System.out 0 {
			System.out.println(k-(n%k));
		} System.out.println(k-(n%k)) System.out.println System.out System System.out k-(n%k) k (n%k) n k sc.close() sc.close sc 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();
		if(n<k) {
			System.out.println((long)Math.min(n, k-n));
		}else if(n%k==0)System.out.println(0);
		else {
			System.out.println(k-(n%k));
		}
		
		sc.close();
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		long n=sc.nextLong();
		long k=sc.nextLong();
		if(n<k) {
			System.out.println((long)Math.min(n, k-n));
		}else if(n%k==0)System.out.println(0);
		else {
			System.out.println(k-(n%k));
		}
		
		sc.close();
	}
} Main
import 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();
		if(n<k) {
			System.out.println((long)Math.min(n, k-n));
		}else if(n%k==0)System.out.println(0);
		else {
			System.out.println(k-(n%k));
		}
		
		sc.close();
	}
}
