import java.util.*;
import java.math.RoundingMode;
import java.math.BigDecimal;

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

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&&k!=1) {
			long mod = n%k;
			long ans = Math.min(mod, Math.abs(k-mod));
			System.out.println(ans);
		}
		else if(n<k&&k!=1) {
			System.out.println(Math.min(n,k-n));
		}
		else {
			System.out.println(0);
		}
		
	}
}