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