import java.util.*;

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();

		boolean flag = true;
		if ( n > k){
			long w = n / k;
			n = n - (k*w);
		}
		while(flag){
			long min = 0;
			if( n < k){
				min = k - n;
			}else{
				min = n -k;
			}
			if(n <= min){
				flag = false;
			}else{
				n =min;
			}
		}
		System.out.println(n);

	}
} import java.util.*; util.* java public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();

		boolean flag = true;
		if ( n > k){
			long w = n / k;
			n = n - (k*w);
		}
		while(flag){
			long min = 0;
			if( n < k){
				min = k - n;
			}else{
				min = n -k;
			}
			if(n <= min){
				flag = false;
			}else{
				n =min;
			}
		}
		System.out.println(n);

	}
} Main public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();

		boolean flag = true;
		if ( n > k){
			long w = n / k;
			n = n - (k*w);
		}
		while(flag){
			long min = 0;
			if( n < k){
				min = k - n;
			}else{
				min = n -k;
			}
			if(n <= min){
				flag = false;
			}else{
				n =min;
			}
		}
		System.out.println(n);

	} main {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();

		boolean flag = true;
		if ( n > k){
			long w = n / k;
			n = n - (k*w);
		}
		while(flag){
			long min = 0;
			if( n < k){
				min = k - n;
			}else{
				min = n -k;
			}
			if(n <= min){
				flag = false;
			}else{
				n =min;
			}
		}
		System.out.println(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 boolean flag = true; flag true if ( n > k){
			long w = n / k;
			n = n - (k*w);
		} n > k n k {
			long w = n / k;
			n = n - (k*w);
		} long w = n / k; w n / k n k n = n - (k*w) n n - (k*w) n (k*w) k w while(flag){
			long min = 0;
			if( n < k){
				min = k - n;
			}else{
				min = n -k;
			}
			if(n <= min){
				flag = false;
			}else{
				n =min;
			}
		} flag {
			long min = 0;
			if( n < k){
				min = k - n;
			}else{
				min = n -k;
			}
			if(n <= min){
				flag = false;
			}else{
				n =min;
			}
		} long min = 0; min 0 if( n < k){
				min = k - n;
			}else{
				min = n -k;
			} n < k n k {
				min = k - n;
			} min = k - n min k - n k n {
				min = n -k;
			} min = n -k min n -k n k if(n <= min){
				flag = false;
			}else{
				n =min;
			} n <= min n min {
				flag = false;
			} flag = false flag false {
				n =min;
			} n =min n min System.out.println(n) System.out.println System.out System System.out 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();

		boolean flag = true;
		if ( n > k){
			long w = n / k;
			n = n - (k*w);
		}
		while(flag){
			long min = 0;
			if( n < k){
				min = k - n;
			}else{
				min = n -k;
			}
			if(n <= min){
				flag = false;
			}else{
				n =min;
			}
		}
		System.out.println(n);

	}
} public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();

		boolean flag = true;
		if ( n > k){
			long w = n / k;
			n = n - (k*w);
		}
		while(flag){
			long min = 0;
			if( n < k){
				min = k - n;
			}else{
				min = n -k;
			}
			if(n <= min){
				flag = false;
			}else{
				n =min;
			}
		}
		System.out.println(n);

	}
} Main
import java.util.*;

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();

		boolean flag = true;
		if ( n > k){
			long w = n / k;
			n = n - (k*w);
		}
		while(flag){
			long min = 0;
			if( n < k){
				min = k - n;
			}else{
				min = n -k;
			}
			if(n <= min){
				flag = false;
			}else{
				n =min;
			}
		}
		System.out.println(n);

	}
}