import java.util.*;


		       public class Main {

		           public static void main(String args[])  {
		        	   Scanner sc =new Scanner(System.in);
		        	   long start =sc.nextLong();
		        	   long parm = sc.nextLong();
		        	   long remainder = start%parm;
		        	   System.out.println(Math.min(remainder,Math.abs(remainder -parm)));
		        	   
		        	   
		           }
		       } import java.util.*; util.* java public class Main {

		           public static void main(String args[])  {
		        	   Scanner sc =new Scanner(System.in);
		        	   long start =sc.nextLong();
		        	   long parm = sc.nextLong();
		        	   long remainder = start%parm;
		        	   System.out.println(Math.min(remainder,Math.abs(remainder -parm)));
		        	   
		        	   
		           }
		       } Main public static void main(String args[])  {
		        	   Scanner sc =new Scanner(System.in);
		        	   long start =sc.nextLong();
		        	   long parm = sc.nextLong();
		        	   long remainder = start%parm;
		        	   System.out.println(Math.min(remainder,Math.abs(remainder -parm)));
		        	   
		        	   
		           } main {
		        	   Scanner sc =new Scanner(System.in);
		        	   long start =sc.nextLong();
		        	   long parm = sc.nextLong();
		        	   long remainder = start%parm;
		        	   System.out.println(Math.min(remainder,Math.abs(remainder -parm)));
		        	   
		        	   
		           } Scanner sc =new Scanner(System.in); sc new Scanner(System.in) long start =sc.nextLong(); start sc.nextLong() sc.nextLong sc long parm = sc.nextLong(); parm sc.nextLong() sc.nextLong sc long remainder = start%parm; remainder start%parm start parm System.out.println(Math.min(remainder,Math.abs(remainder -parm))) System.out.println System.out System System.out Math.min(remainder,Math.abs(remainder -parm)) Math.min Math remainder Math.abs(remainder -parm) Math.abs Math remainder -parm remainder parm String args[] args public class Main {

		           public static void main(String args[])  {
		        	   Scanner sc =new Scanner(System.in);
		        	   long start =sc.nextLong();
		        	   long parm = sc.nextLong();
		        	   long remainder = start%parm;
		        	   System.out.println(Math.min(remainder,Math.abs(remainder -parm)));
		        	   
		        	   
		           }
		       } public class Main {

		           public static void main(String args[])  {
		        	   Scanner sc =new Scanner(System.in);
		        	   long start =sc.nextLong();
		        	   long parm = sc.nextLong();
		        	   long remainder = start%parm;
		        	   System.out.println(Math.min(remainder,Math.abs(remainder -parm)));
		        	   
		        	   
		           }
		       } Main
import java.util.*;


		       public class Main {

		           public static void main(String args[])  {
		        	   Scanner sc =new Scanner(System.in);
		        	   long start =sc.nextLong();
		        	   long parm = sc.nextLong();
		        	   long remainder = start%parm;
		        	   System.out.println(Math.min(remainder,Math.abs(remainder -parm)));
		        	   
		        	   
		           }
		       }
