import java.util.*; 
import java.lang.*; 

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		long r = N%K;
		System.out.println(Math.min(r,K-r));
	}
} import java.util.*; util.* java import java.lang.*; lang.* java public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		long r = N%K;
		System.out.println(Math.min(r,K-r));
	}
} Main public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		long r = N%K;
		System.out.println(Math.min(r,K-r));
	} main {
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		long r = N%K;
		System.out.println(Math.min(r,K-r));
	} 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 r = N%K; r N%K N K System.out.println(Math.min(r,K-r)) System.out.println System.out System System.out Math.min(r,K-r) Math.min Math r K-r K r 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 r = N%K;
		System.out.println(Math.min(r,K-r));
	}
} public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		long r = N%K;
		System.out.println(Math.min(r,K-r));
	}
} Main
import java.util.*; 
import java.lang.*; 

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		
		long N = scan.nextLong();
		long K = scan.nextLong();
		
		long r = N%K;
		System.out.println(Math.min(r,K-r));
	}
}