import java.util.*;
public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		long N= sc.nextLong();
		long M = sc.nextLong();
		long t = N % M;
		long mae = t;
		while(true) {
		    if(mae < Math.abs(mae-M)) {
				break;
			 }else {
				mae = Math.abs(mae-M);
		     }
		}	
		
		System.out.println(mae);
		
	}
	

} import java.util.*; util.* java public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		long N= sc.nextLong();
		long M = sc.nextLong();
		long t = N % M;
		long mae = t;
		while(true) {
		    if(mae < Math.abs(mae-M)) {
				break;
			 }else {
				mae = Math.abs(mae-M);
		     }
		}	
		
		System.out.println(mae);
		
	}
	

} Main public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		long N= sc.nextLong();
		long M = sc.nextLong();
		long t = N % M;
		long mae = t;
		while(true) {
		    if(mae < Math.abs(mae-M)) {
				break;
			 }else {
				mae = Math.abs(mae-M);
		     }
		}	
		
		System.out.println(mae);
		
	} main {
		Scanner sc = new Scanner(System.in);
		long N= sc.nextLong();
		long M = sc.nextLong();
		long t = N % M;
		long mae = t;
		while(true) {
		    if(mae < Math.abs(mae-M)) {
				break;
			 }else {
				mae = Math.abs(mae-M);
		     }
		}	
		
		System.out.println(mae);
		
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long N= sc.nextLong(); N sc.nextLong() sc.nextLong sc long M = sc.nextLong(); M sc.nextLong() sc.nextLong sc long t = N % M; t N % M N M long mae = t; mae t while(true) {
		    if(mae < Math.abs(mae-M)) {
				break;
			 }else {
				mae = Math.abs(mae-M);
		     }
		} true {
		    if(mae < Math.abs(mae-M)) {
				break;
			 }else {
				mae = Math.abs(mae-M);
		     }
		} if(mae < Math.abs(mae-M)) {
				break;
			 }else {
				mae = Math.abs(mae-M);
		     } mae < Math.abs(mae-M) mae Math.abs(mae-M) Math.abs Math mae-M mae M {
				break;
			 } break; {
				mae = Math.abs(mae-M);
		     } mae = Math.abs(mae-M) mae Math.abs(mae-M) Math.abs Math mae-M mae M System.out.println(mae) System.out.println System.out System System.out mae String args[] args public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		long N= sc.nextLong();
		long M = sc.nextLong();
		long t = N % M;
		long mae = t;
		while(true) {
		    if(mae < Math.abs(mae-M)) {
				break;
			 }else {
				mae = Math.abs(mae-M);
		     }
		}	
		
		System.out.println(mae);
		
	}
	

} public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		long N= sc.nextLong();
		long M = sc.nextLong();
		long t = N % M;
		long mae = t;
		while(true) {
		    if(mae < Math.abs(mae-M)) {
				break;
			 }else {
				mae = Math.abs(mae-M);
		     }
		}	
		
		System.out.println(mae);
		
	}
	

} Main
import java.util.*;
public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		long N= sc.nextLong();
		long M = sc.nextLong();
		long t = N % M;
		long mae = t;
		while(true) {
		    if(mae < Math.abs(mae-M)) {
				break;
			 }else {
				mae = Math.abs(mae-M);
		     }
		}	
		
		System.out.println(mae);
		
	}
	

}
