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%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%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%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%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%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%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%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%K;
		
		if(min>Math.abs(min-K)) min = Math.abs(min-K);
		
		System.out.println(min);

	}

}
