import java.util.*;
public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long ans = N;
		if(N > K){
			N = N - (K * (N / K));
		}
		for(int i = 0;i < 2;i++){
			N = Math.abs(N - K);
			if(N < ans){
				ans = N;
			}
		}
		System.out.println(ans);
	}
} import java.util.*; util.* java public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long ans = N;
		if(N > K){
			N = N - (K * (N / K));
		}
		for(int i = 0;i < 2;i++){
			N = Math.abs(N - K);
			if(N < ans){
				ans = N;
			}
		}
		System.out.println(ans);
	}
} Main public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long ans = N;
		if(N > K){
			N = N - (K * (N / K));
		}
		for(int i = 0;i < 2;i++){
			N = Math.abs(N - K);
			if(N < ans){
				ans = N;
			}
		}
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long ans = N;
		if(N > K){
			N = N - (K * (N / K));
		}
		for(int i = 0;i < 2;i++){
			N = Math.abs(N - K);
			if(N < ans){
				ans = N;
			}
		}
		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long N = sc.nextLong(); N sc.nextLong() sc.nextLong sc long K = sc.nextLong(); K sc.nextLong() sc.nextLong sc long ans = N; ans N if(N > K){
			N = N - (K * (N / K));
		} N > K N K {
			N = N - (K * (N / K));
		} N = N - (K * (N / K)) N N - (K * (N / K)) N (K * (N / K)) K (N / K) N K for(int i = 0;i < 2;i++){
			N = Math.abs(N - K);
			if(N < ans){
				ans = N;
			}
		} int i = 0; int i = 0; i 0 i < 2 i 2 i++ i++ i {
			N = Math.abs(N - K);
			if(N < ans){
				ans = N;
			}
		} {
			N = Math.abs(N - K);
			if(N < ans){
				ans = N;
			}
		} N = Math.abs(N - K) N Math.abs(N - K) Math.abs Math N - K N K if(N < ans){
				ans = N;
			} N < ans N ans {
				ans = N;
			} ans = N ans N System.out.println(ans) System.out.println System.out System System.out ans String args[] args public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long ans = N;
		if(N > K){
			N = N - (K * (N / K));
		}
		for(int i = 0;i < 2;i++){
			N = Math.abs(N - K);
			if(N < ans){
				ans = N;
			}
		}
		System.out.println(ans);
	}
} public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long ans = N;
		if(N > K){
			N = N - (K * (N / K));
		}
		for(int i = 0;i < 2;i++){
			N = Math.abs(N - K);
			if(N < ans){
				ans = N;
			}
		}
		System.out.println(ans);
	}
} Main
import java.util.*;
public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		long ans = N;
		if(N > K){
			N = N - (K * (N / K));
		}
		for(int i = 0;i < 2;i++){
			N = Math.abs(N - K);
			if(N < ans){
				ans = N;
			}
		}
		System.out.println(ans);
	}
}