import java.util.Scanner;
 
public class Main{
 
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		
		Scanner sc=new Scanner(System.in);
		
		long N_absolute=Math.abs(sc.nextLong());
		long K_absolute=Math.abs(sc.nextLong());
		long result=N_absolute;
		long hiki=0;

		result=N_absolute%K_absolute;
	
		if(result==0) {
			System.out.println(result);
		}else {
			hiki=result-K_absolute;
			hiki=Math.abs(hiki);
			if(result>hiki) {
				System.out.println(hiki);
			}else {
				System.out.println(result);
			}
		}
	
	}
} import java.util.Scanner; Scanner java.util public class Main{
 
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		
		Scanner sc=new Scanner(System.in);
		
		long N_absolute=Math.abs(sc.nextLong());
		long K_absolute=Math.abs(sc.nextLong());
		long result=N_absolute;
		long hiki=0;

		result=N_absolute%K_absolute;
	
		if(result==0) {
			System.out.println(result);
		}else {
			hiki=result-K_absolute;
			hiki=Math.abs(hiki);
			if(result>hiki) {
				System.out.println(hiki);
			}else {
				System.out.println(result);
			}
		}
	
	}
} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		
		Scanner sc=new Scanner(System.in);
		
		long N_absolute=Math.abs(sc.nextLong());
		long K_absolute=Math.abs(sc.nextLong());
		long result=N_absolute;
		long hiki=0;

		result=N_absolute%K_absolute;
	
		if(result==0) {
			System.out.println(result);
		}else {
			hiki=result-K_absolute;
			hiki=Math.abs(hiki);
			if(result>hiki) {
				System.out.println(hiki);
			}else {
				System.out.println(result);
			}
		}
	
	} main {
		// TODO 自動生成されたメソッド・スタブ
		
		Scanner sc=new Scanner(System.in);
		
		long N_absolute=Math.abs(sc.nextLong());
		long K_absolute=Math.abs(sc.nextLong());
		long result=N_absolute;
		long hiki=0;

		result=N_absolute%K_absolute;
	
		if(result==0) {
			System.out.println(result);
		}else {
			hiki=result-K_absolute;
			hiki=Math.abs(hiki);
			if(result>hiki) {
				System.out.println(hiki);
			}else {
				System.out.println(result);
			}
		}
	
	} Scanner sc=new Scanner(System.in); sc new Scanner(System.in) long N_absolute=Math.abs(sc.nextLong()); N_absolute Math.abs(sc.nextLong()) Math.abs Math sc.nextLong() sc.nextLong sc long K_absolute=Math.abs(sc.nextLong()); K_absolute Math.abs(sc.nextLong()) Math.abs Math sc.nextLong() sc.nextLong sc long result=N_absolute; result N_absolute long hiki=0; hiki 0 result=N_absolute%K_absolute result N_absolute%K_absolute N_absolute K_absolute if(result==0) {
			System.out.println(result);
		}else {
			hiki=result-K_absolute;
			hiki=Math.abs(hiki);
			if(result>hiki) {
				System.out.println(hiki);
			}else {
				System.out.println(result);
			}
		} result==0 result 0 {
			System.out.println(result);
		} System.out.println(result) System.out.println System.out System System.out result {
			hiki=result-K_absolute;
			hiki=Math.abs(hiki);
			if(result>hiki) {
				System.out.println(hiki);
			}else {
				System.out.println(result);
			}
		} hiki=result-K_absolute hiki result-K_absolute result K_absolute hiki=Math.abs(hiki) hiki Math.abs(hiki) Math.abs Math hiki if(result>hiki) {
				System.out.println(hiki);
			}else {
				System.out.println(result);
			} result>hiki result hiki {
				System.out.println(hiki);
			} System.out.println(hiki) System.out.println System.out System System.out hiki {
				System.out.println(result);
			} System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main{
 
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		
		Scanner sc=new Scanner(System.in);
		
		long N_absolute=Math.abs(sc.nextLong());
		long K_absolute=Math.abs(sc.nextLong());
		long result=N_absolute;
		long hiki=0;

		result=N_absolute%K_absolute;
	
		if(result==0) {
			System.out.println(result);
		}else {
			hiki=result-K_absolute;
			hiki=Math.abs(hiki);
			if(result>hiki) {
				System.out.println(hiki);
			}else {
				System.out.println(result);
			}
		}
	
	}
} public class Main{
 
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		
		Scanner sc=new Scanner(System.in);
		
		long N_absolute=Math.abs(sc.nextLong());
		long K_absolute=Math.abs(sc.nextLong());
		long result=N_absolute;
		long hiki=0;

		result=N_absolute%K_absolute;
	
		if(result==0) {
			System.out.println(result);
		}else {
			hiki=result-K_absolute;
			hiki=Math.abs(hiki);
			if(result>hiki) {
				System.out.println(hiki);
			}else {
				System.out.println(result);
			}
		}
	
	}
} Main
import java.util.Scanner;
 
public class Main{
 
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		
		Scanner sc=new Scanner(System.in);
		
		long N_absolute=Math.abs(sc.nextLong());
		long K_absolute=Math.abs(sc.nextLong());
		long result=N_absolute;
		long hiki=0;

		result=N_absolute%K_absolute;
	
		if(result==0) {
			System.out.println(result);
		}else {
			hiki=result-K_absolute;
			hiki=Math.abs(hiki);
			if(result>hiki) {
				System.out.println(hiki);
			}else {
				System.out.println(result);
			}
		}
	
	}
}