import java.util.*;
import java.lang.Math;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner cin = new Scanner(System.in);
		long N = cin.nextLong();
		long K = cin.nextLong();
		
		long min = N;
		
		if(N<K) {
			if(min>Math.abs(min-K)) min = Math.abs(min-K);
		}
		else {
			if(N%K==0) {
				min = 0;
			}else{
				min = N%K;
				if(min>Math.abs(min-K)) min = Math.abs(min-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) {
		// TODO Auto-generated method stub
		
		Scanner cin = new Scanner(System.in);
		long N = cin.nextLong();
		long K = cin.nextLong();
		
		long min = N;
		
		if(N<K) {
			if(min>Math.abs(min-K)) min = Math.abs(min-K);
		}
		else {
			if(N%K==0) {
				min = 0;
			}else{
				min = N%K;
				if(min>Math.abs(min-K)) min = Math.abs(min-K);
			}
		}
		
		System.out.println(min);

	}

} Main public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner cin = new Scanner(System.in);
		long N = cin.nextLong();
		long K = cin.nextLong();
		
		long min = N;
		
		if(N<K) {
			if(min>Math.abs(min-K)) min = Math.abs(min-K);
		}
		else {
			if(N%K==0) {
				min = 0;
			}else{
				min = N%K;
				if(min>Math.abs(min-K)) min = Math.abs(min-K);
			}
		}
		
		System.out.println(min);

	} main {
		// TODO Auto-generated method stub
		
		Scanner cin = new Scanner(System.in);
		long N = cin.nextLong();
		long K = cin.nextLong();
		
		long min = N;
		
		if(N<K) {
			if(min>Math.abs(min-K)) min = Math.abs(min-K);
		}
		else {
			if(N%K==0) {
				min = 0;
			}else{
				min = N%K;
				if(min>Math.abs(min-K)) min = Math.abs(min-K);
			}
		}
		
		System.out.println(min);

	} Scanner cin = new Scanner(System.in); cin new Scanner(System.in) long N = cin.nextLong(); N cin.nextLong() cin.nextLong cin long K = cin.nextLong(); K cin.nextLong() cin.nextLong cin long min = N; min N if(N<K) {
			if(min>Math.abs(min-K)) min = Math.abs(min-K);
		}
		else {
			if(N%K==0) {
				min = 0;
			}else{
				min = N%K;
				if(min>Math.abs(min-K)) min = Math.abs(min-K);
			}
		} N<K N K {
			if(min>Math.abs(min-K)) min = Math.abs(min-K);
		} if(min>Math.abs(min-K)) min = Math.abs(min-K); min>Math.abs(min-K) min Math.abs(min-K) Math.abs Math min-K min K min = Math.abs(min-K) min Math.abs(min-K) Math.abs Math min-K min K {
			if(N%K==0) {
				min = 0;
			}else{
				min = N%K;
				if(min>Math.abs(min-K)) min = Math.abs(min-K);
			}
		} if(N%K==0) {
				min = 0;
			}else{
				min = N%K;
				if(min>Math.abs(min-K)) min = Math.abs(min-K);
			} N%K==0 N%K N K 0 {
				min = 0;
			} min = 0 min 0 {
				min = N%K;
				if(min>Math.abs(min-K)) min = Math.abs(min-K);
			} min = N%K min N%K N K if(min>Math.abs(min-K)) min = Math.abs(min-K); min>Math.abs(min-K) min Math.abs(min-K) Math.abs Math min-K min K min = Math.abs(min-K) min Math.abs(min-K) Math.abs Math min-K min 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) {
		// TODO Auto-generated method stub
		
		Scanner cin = new Scanner(System.in);
		long N = cin.nextLong();
		long K = cin.nextLong();
		
		long min = N;
		
		if(N<K) {
			if(min>Math.abs(min-K)) min = Math.abs(min-K);
		}
		else {
			if(N%K==0) {
				min = 0;
			}else{
				min = N%K;
				if(min>Math.abs(min-K)) min = Math.abs(min-K);
			}
		}
		
		System.out.println(min);

	}

} public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner cin = new Scanner(System.in);
		long N = cin.nextLong();
		long K = cin.nextLong();
		
		long min = N;
		
		if(N<K) {
			if(min>Math.abs(min-K)) min = Math.abs(min-K);
		}
		else {
			if(N%K==0) {
				min = 0;
			}else{
				min = N%K;
				if(min>Math.abs(min-K)) min = Math.abs(min-K);
			}
		}
		
		System.out.println(min);

	}

} Main
import java.util.*;
import java.lang.Math;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner cin = new Scanner(System.in);
		long N = cin.nextLong();
		long K = cin.nextLong();
		
		long min = N;
		
		if(N<K) {
			if(min>Math.abs(min-K)) min = Math.abs(min-K);
		}
		else {
			if(N%K==0) {
				min = 0;
			}else{
				min = N%K;
				if(min>Math.abs(min-K)) min = Math.abs(min-K);
			}
		}
		
		System.out.println(min);

	}

}
