import java.util.Scanner;

public class Main {
	public static void main (String args[]) {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		boolean loop = true;
		
		if(n == 0) {
			loop = false;
		}
      
      	if(k == 1) {
			loop = false;
		}
      	long x = n;
		if(x > k) {
			long a = x / k;
			x = x - (k * a);
			if(x == 0) {
				loop = false;
			}
		} else if(x == k){
			loop = false;
		}
      	
		long pre = x;
		while(loop) {			
			x = x - k;
			if( x == 0) {
				loop = false;
			}
			if(x < 0) {
				loop = false;
                x = Math.abs(x);
				if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				}
			}
			
			pre = Math.abs(x);
		}
				
		System.out.println(ans);
	}
} import java.util.Scanner; Scanner 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();
		long ans = 0;
		boolean loop = true;
		
		if(n == 0) {
			loop = false;
		}
      
      	if(k == 1) {
			loop = false;
		}
      	long x = n;
		if(x > k) {
			long a = x / k;
			x = x - (k * a);
			if(x == 0) {
				loop = false;
			}
		} else if(x == k){
			loop = false;
		}
      	
		long pre = x;
		while(loop) {			
			x = x - k;
			if( x == 0) {
				loop = false;
			}
			if(x < 0) {
				loop = false;
                x = Math.abs(x);
				if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				}
			}
			
			pre = Math.abs(x);
		}
				
		System.out.println(ans);
	}
} Main public static void main (String args[]) {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		boolean loop = true;
		
		if(n == 0) {
			loop = false;
		}
      
      	if(k == 1) {
			loop = false;
		}
      	long x = n;
		if(x > k) {
			long a = x / k;
			x = x - (k * a);
			if(x == 0) {
				loop = false;
			}
		} else if(x == k){
			loop = false;
		}
      	
		long pre = x;
		while(loop) {			
			x = x - k;
			if( x == 0) {
				loop = false;
			}
			if(x < 0) {
				loop = false;
                x = Math.abs(x);
				if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				}
			}
			
			pre = Math.abs(x);
		}
				
		System.out.println(ans);
	} main {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		boolean loop = true;
		
		if(n == 0) {
			loop = false;
		}
      
      	if(k == 1) {
			loop = false;
		}
      	long x = n;
		if(x > k) {
			long a = x / k;
			x = x - (k * a);
			if(x == 0) {
				loop = false;
			}
		} else if(x == k){
			loop = false;
		}
      	
		long pre = x;
		while(loop) {			
			x = x - k;
			if( x == 0) {
				loop = false;
			}
			if(x < 0) {
				loop = false;
                x = Math.abs(x);
				if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				}
			}
			
			pre = Math.abs(x);
		}
				
		System.out.println(ans);
	} 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 long ans = 0; ans 0 boolean loop = true; loop true if(n == 0) {
			loop = false;
		} n == 0 n 0 {
			loop = false;
		} loop = false loop false if(k == 1) {
			loop = false;
		} k == 1 k 1 {
			loop = false;
		} loop = false loop false long x = n; x n if(x > k) {
			long a = x / k;
			x = x - (k * a);
			if(x == 0) {
				loop = false;
			}
		} else if(x == k){
			loop = false;
		} x > k x k {
			long a = x / k;
			x = x - (k * a);
			if(x == 0) {
				loop = false;
			}
		} long a = x / k; a x / k x k x = x - (k * a) x x - (k * a) x (k * a) k a if(x == 0) {
				loop = false;
			} x == 0 x 0 {
				loop = false;
			} loop = false loop false if(x == k){
			loop = false;
		} x == k x k {
			loop = false;
		} loop = false loop false long pre = x; pre x while(loop) {			
			x = x - k;
			if( x == 0) {
				loop = false;
			}
			if(x < 0) {
				loop = false;
                x = Math.abs(x);
				if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				}
			}
			
			pre = Math.abs(x);
		} loop {			
			x = x - k;
			if( x == 0) {
				loop = false;
			}
			if(x < 0) {
				loop = false;
                x = Math.abs(x);
				if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				}
			}
			
			pre = Math.abs(x);
		} x = x - k x x - k x k if( x == 0) {
				loop = false;
			} x == 0 x 0 {
				loop = false;
			} loop = false loop false if(x < 0) {
				loop = false;
                x = Math.abs(x);
				if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				}
			} x < 0 x 0 {
				loop = false;
                x = Math.abs(x);
				if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				}
			} loop = false loop false x = Math.abs(x) x Math.abs(x) Math.abs Math x if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				} x < pre x pre {
					ans = x;
				} ans = x ans x {
					ans = pre;
				} ans = pre ans pre pre = Math.abs(x) pre Math.abs(x) Math.abs Math x 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 scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		boolean loop = true;
		
		if(n == 0) {
			loop = false;
		}
      
      	if(k == 1) {
			loop = false;
		}
      	long x = n;
		if(x > k) {
			long a = x / k;
			x = x - (k * a);
			if(x == 0) {
				loop = false;
			}
		} else if(x == k){
			loop = false;
		}
      	
		long pre = x;
		while(loop) {			
			x = x - k;
			if( x == 0) {
				loop = false;
			}
			if(x < 0) {
				loop = false;
                x = Math.abs(x);
				if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				}
			}
			
			pre = Math.abs(x);
		}
				
		System.out.println(ans);
	}
} public class Main {
	public static void main (String args[]) {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		boolean loop = true;
		
		if(n == 0) {
			loop = false;
		}
      
      	if(k == 1) {
			loop = false;
		}
      	long x = n;
		if(x > k) {
			long a = x / k;
			x = x - (k * a);
			if(x == 0) {
				loop = false;
			}
		} else if(x == k){
			loop = false;
		}
      	
		long pre = x;
		while(loop) {			
			x = x - k;
			if( x == 0) {
				loop = false;
			}
			if(x < 0) {
				loop = false;
                x = Math.abs(x);
				if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				}
			}
			
			pre = Math.abs(x);
		}
				
		System.out.println(ans);
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main (String args[]) {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();
		long ans = 0;
		boolean loop = true;
		
		if(n == 0) {
			loop = false;
		}
      
      	if(k == 1) {
			loop = false;
		}
      	long x = n;
		if(x > k) {
			long a = x / k;
			x = x - (k * a);
			if(x == 0) {
				loop = false;
			}
		} else if(x == k){
			loop = false;
		}
      	
		long pre = x;
		while(loop) {			
			x = x - k;
			if( x == 0) {
				loop = false;
			}
			if(x < 0) {
				loop = false;
                x = Math.abs(x);
				if(x < pre) {
					ans = x;
				} else {
					ans = pre;
				}
			}
			
			pre = Math.abs(x);
		}
				
		System.out.println(ans);
	}
}