import java.util.*;
import java.lang.*;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

		long N = sc.nextLong();
		long K = sc.nextLong();
		long A = N%K;
		long ans = 0;
		if( N<K ){
			long a = K-N;
			ans = Math.min( a,N );
		}
		if( N%K==0 ){
			ans = 0;
		}
		if( !(N<K) && !(N%K==0) ){
			long a = A;
			long b = K-A;
			if( a<b ){
				ans = a;
			}else{
				ans = b;
			}
		}
		System.out.println(ans);
	}
} import java.util.*; util.* java import java.lang.*; lang.* 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 A = N%K;
		long ans = 0;
		if( N<K ){
			long a = K-N;
			ans = Math.min( a,N );
		}
		if( N%K==0 ){
			ans = 0;
		}
		if( !(N<K) && !(N%K==0) ){
			long a = A;
			long b = K-A;
			if( a<b ){
				ans = a;
			}else{
				ans = b;
			}
		}
		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 A = N%K;
		long ans = 0;
		if( N<K ){
			long a = K-N;
			ans = Math.min( a,N );
		}
		if( N%K==0 ){
			ans = 0;
		}
		if( !(N<K) && !(N%K==0) ){
			long a = A;
			long b = K-A;
			if( a<b ){
				ans = a;
			}else{
				ans = b;
			}
		}
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);

		long N = sc.nextLong();
		long K = sc.nextLong();
		long A = N%K;
		long ans = 0;
		if( N<K ){
			long a = K-N;
			ans = Math.min( a,N );
		}
		if( N%K==0 ){
			ans = 0;
		}
		if( !(N<K) && !(N%K==0) ){
			long a = A;
			long b = K-A;
			if( a<b ){
				ans = a;
			}else{
				ans = b;
			}
		}
		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 A = N%K; A N%K N K long ans = 0; ans 0 if( N<K ){
			long a = K-N;
			ans = Math.min( a,N );
		} N<K N K {
			long a = K-N;
			ans = Math.min( a,N );
		} long a = K-N; a K-N K N ans = Math.min( a,N ) ans Math.min( a,N ) Math.min Math a N if( N%K==0 ){
			ans = 0;
		} N%K==0 N%K N K 0 {
			ans = 0;
		} ans = 0 ans 0 if( !(N<K) && !(N%K==0) ){
			long a = A;
			long b = K-A;
			if( a<b ){
				ans = a;
			}else{
				ans = b;
			}
		} !(N<K) && !(N%K==0) !(N<K) (N<K) N K !(N%K==0) (N%K==0) N%K N K 0 {
			long a = A;
			long b = K-A;
			if( a<b ){
				ans = a;
			}else{
				ans = b;
			}
		} long a = A; a A long b = K-A; b K-A K A if( a<b ){
				ans = a;
			}else{
				ans = b;
			} a<b a b {
				ans = a;
			} ans = a ans a {
				ans = b;
			} ans = b ans b 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 A = N%K;
		long ans = 0;
		if( N<K ){
			long a = K-N;
			ans = Math.min( a,N );
		}
		if( N%K==0 ){
			ans = 0;
		}
		if( !(N<K) && !(N%K==0) ){
			long a = A;
			long b = K-A;
			if( a<b ){
				ans = a;
			}else{
				ans = b;
			}
		}
		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 A = N%K;
		long ans = 0;
		if( N<K ){
			long a = K-N;
			ans = Math.min( a,N );
		}
		if( N%K==0 ){
			ans = 0;
		}
		if( !(N<K) && !(N%K==0) ){
			long a = A;
			long b = K-A;
			if( a<b ){
				ans = a;
			}else{
				ans = b;
			}
		}
		System.out.println(ans);
	}
} Main
import java.util.*;
import java.lang.*;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

		long N = sc.nextLong();
		long K = sc.nextLong();
		long A = N%K;
		long ans = 0;
		if( N<K ){
			long a = K-N;
			ans = Math.min( a,N );
		}
		if( N%K==0 ){
			ans = 0;
		}
		if( !(N<K) && !(N%K==0) ){
			long a = A;
			long b = K-A;
			if( a<b ){
				ans = a;
			}else{
				ans = b;
			}
		}
		System.out.println(ans);
	}
}
