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 min=N;
		N=N%K;
		min=N;
		while(min>Math.abs(N-K)){
			N=Math.abs(N-K);
			min=Math.min(min,N);
		}
		System.out.println(min);
	}

} 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 min=N;
		N=N%K;
		min=N;
		while(min>Math.abs(N-K)){
			N=Math.abs(N-K);
			min=Math.min(min,N);
		}
		System.out.println(min);
	}

} Main public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		long N=scan.nextLong();
		long K=scan.nextLong();
		long min=N;
		N=N%K;
		min=N;
		while(min>Math.abs(N-K)){
			N=Math.abs(N-K);
			min=Math.min(min,N);
		}
		System.out.println(min);
	} main {
		Scanner scan=new Scanner(System.in);
		long N=scan.nextLong();
		long K=scan.nextLong();
		long min=N;
		N=N%K;
		min=N;
		while(min>Math.abs(N-K)){
			N=Math.abs(N-K);
			min=Math.min(min,N);
		}
		System.out.println(min);
	} 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 min=N; min N N=N%K N N%K N K min=N min N while(min>Math.abs(N-K)){
			N=Math.abs(N-K);
			min=Math.min(min,N);
		} min>Math.abs(N-K) min Math.abs(N-K) Math.abs Math N-K N K {
			N=Math.abs(N-K);
			min=Math.min(min,N);
		} N=Math.abs(N-K) N Math.abs(N-K) Math.abs Math N-K N K min=Math.min(min,N) min Math.min(min,N) Math.min Math min N 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 scan=new Scanner(System.in);
		long N=scan.nextLong();
		long K=scan.nextLong();
		long min=N;
		N=N%K;
		min=N;
		while(min>Math.abs(N-K)){
			N=Math.abs(N-K);
			min=Math.min(min,N);
		}
		System.out.println(min);
	}

} public class Main {

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		long N=scan.nextLong();
		long K=scan.nextLong();
		long min=N;
		N=N%K;
		min=N;
		while(min>Math.abs(N-K)){
			N=Math.abs(N-K);
			min=Math.min(min,N);
		}
		System.out.println(min);
	}

} 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 min=N;
		N=N%K;
		min=N;
		while(min>Math.abs(N-K)){
			N=Math.abs(N-K);
			min=Math.min(min,N);
		}
		System.out.println(min);
	}

}
