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

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		if(N>=K){
		if(N%K==0){
			System.out.println(0);
		}
		else{
			if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			}
		}
		}
		
		else{
			if(K>=N*2){
				System.out.println(N);
			}
			
			else{
				System.out.println(K-N);
			}
		}
	}
} import java.util.*; util.* java import java.lang.*; lang.* java public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		if(N>=K){
		if(N%K==0){
			System.out.println(0);
		}
		else{
			if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			}
		}
		}
		
		else{
			if(K>=N*2){
				System.out.println(N);
			}
			
			else{
				System.out.println(K-N);
			}
		}
	}
} Main public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		if(N>=K){
		if(N%K==0){
			System.out.println(0);
		}
		else{
			if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			}
		}
		}
		
		else{
			if(K>=N*2){
				System.out.println(N);
			}
			
			else{
				System.out.println(K-N);
			}
		}
	} main {
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		if(N>=K){
		if(N%K==0){
			System.out.println(0);
		}
		else{
			if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			}
		}
		}
		
		else{
			if(K>=N*2){
				System.out.println(N);
			}
			
			else{
				System.out.println(K-N);
			}
		}
	} 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 if(N>=K){
		if(N%K==0){
			System.out.println(0);
		}
		else{
			if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			}
		}
		}
		
		else{
			if(K>=N*2){
				System.out.println(N);
			}
			
			else{
				System.out.println(K-N);
			}
		} N>=K N K {
		if(N%K==0){
			System.out.println(0);
		}
		else{
			if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			}
		}
		} if(N%K==0){
			System.out.println(0);
		}
		else{
			if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			}
		} N%K==0 N%K N K 0 {
			System.out.println(0);
		} System.out.println(0) System.out.println System.out System System.out 0 {
			if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			}
		} if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			} (K/2.0) >= N%K (K/2.0) K 2.0 N%K N K {
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			} N = N-K*(N/K) N N-K*(N/K) N K*(N/K) K (N/K) N K System.out.println(N) System.out.println System.out System System.out N if(N<=0){
				N=N*(-1);
			} N<=0 N 0 {
				N=N*(-1);
			} N=N*(-1) N N*(-1) N (-1) 1 {
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			} N = N-(K*((N/K)+1)) N N-(K*((N/K)+1)) N (K*((N/K)+1)) K ((N/K)+1) (N/K) N K 1 if(N<=0){
				N=N*(-1);
			} N<=0 N 0 {
				N=N*(-1);
			} N=N*(-1) N N*(-1) N (-1) 1 System.out.println(N) System.out.println System.out System System.out N {
			if(K>=N*2){
				System.out.println(N);
			}
			
			else{
				System.out.println(K-N);
			}
		} if(K>=N*2){
				System.out.println(N);
			}
			
			else{
				System.out.println(K-N);
			} K>=N*2 K N*2 N 2 {
				System.out.println(N);
			} System.out.println(N) System.out.println System.out System System.out N {
				System.out.println(K-N);
			} System.out.println(K-N) System.out.println System.out System System.out K-N K N 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();
		
		if(N>=K){
		if(N%K==0){
			System.out.println(0);
		}
		else{
			if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			}
		}
		}
		
		else{
			if(K>=N*2){
				System.out.println(N);
			}
			
			else{
				System.out.println(K-N);
			}
		}
	}
} public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		if(N>=K){
		if(N%K==0){
			System.out.println(0);
		}
		else{
			if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			}
		}
		}
		
		else{
			if(K>=N*2){
				System.out.println(N);
			}
			
			else{
				System.out.println(K-N);
			}
		}
	}
} Main
import java.util.*; 
import java.lang.*; 

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		if(N>=K){
		if(N%K==0){
			System.out.println(0);
		}
		else{
			if((K/2.0) >= N%K){
			N = N-K*(N/K);
			System.out.println(N);
			if(N<=0){
				N=N*(-1);
			}
			}
			
			else{
			N = N-(K*((N/K)+1));
			
			if(N<=0){
				N=N*(-1);
			}
			System.out.println(N);
			}
		}
		}
		
		else{
			if(K>=N*2){
				System.out.println(N);
			}
			
			else{
				System.out.println(K-N);
			}
		}
	}
}