import java.util.*;

public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long min = n % k;
    long a = k - min;  
      if(min > a)
        min = a;	
	System.out.println(min);
	}
} import java.util.*; util.* java public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long min = n % k;
    long a = k - min;  
      if(min > a)
        min = a;	
	System.out.println(min);
	}
} Main public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long min = n % k;
    long a = k - min;  
      if(min > a)
        min = a;	
	System.out.println(min);
	} main {
	Scanner sc = new Scanner(System.in);
	long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long min = n % k;
    long a = k - min;  
      if(min > a)
        min = a;	
	System.out.println(min);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n = Long.parseLong(sc.next()); n Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long k = Long.parseLong(sc.next()); k Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long min = n % k; min n % k n k long a = k - min; a k - min k min if(min > a)
        min = a; min > a min a min = a min a System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long min = n % k;
    long a = k - min;  
      if(min > a)
        min = a;	
	System.out.println(min);
	}
} public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long min = n % k;
    long a = k - min;  
      if(min > a)
        min = a;	
	System.out.println(min);
	}
} Main
import java.util.*;

public class Main{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long min = n % k;
    long a = k - min;  
      if(min > a)
        min = a;	
	System.out.println(min);
	}
}