import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		long n=scanner.nextLong();
		long k=scanner.nextLong();
		long a=n%k;
		long b=k-a;
		if(a>b)a=b;
		System.out.println(a);
	}

} import java.util.Scanner; Scanner java.util class Main {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		long n=scanner.nextLong();
		long k=scanner.nextLong();
		long a=n%k;
		long b=k-a;
		if(a>b)a=b;
		System.out.println(a);
	}

} Main public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		long n=scanner.nextLong();
		long k=scanner.nextLong();
		long a=n%k;
		long b=k-a;
		if(a>b)a=b;
		System.out.println(a);
	} main {
		Scanner scanner=new Scanner(System.in);
		long n=scanner.nextLong();
		long k=scanner.nextLong();
		long a=n%k;
		long b=k-a;
		if(a>b)a=b;
		System.out.println(a);
	} Scanner scanner=new Scanner(System.in); scanner new Scanner(System.in) long n=scanner.nextLong(); n scanner.nextLong() scanner.nextLong scanner long k=scanner.nextLong(); k scanner.nextLong() scanner.nextLong scanner long a=n%k; a n%k n k long b=k-a; b k-a k a if(a>b)a=b; a>b a b a=b a b System.out.println(a) System.out.println System.out System System.out a String[] args args
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		long n=scanner.nextLong();
		long k=scanner.nextLong();
		long a=n%k;
		long b=k-a;
		if(a>b)a=b;
		System.out.println(a);
	}

}