import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    long n = scanner.nextLong();
    long k = scanner.nextLong();
    long r = n%k;
    
    long ans = Math.min(r, (k-r));
    System.out.println(ans);
    scanner.close();
  }

	
} import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    long n = scanner.nextLong();
    long k = scanner.nextLong();
    long r = n%k;
    
    long ans = Math.min(r, (k-r));
    System.out.println(ans);
    scanner.close();
  }

	
} Main public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    long n = scanner.nextLong();
    long k = scanner.nextLong();
    long r = n%k;
    
    long ans = Math.min(r, (k-r));
    System.out.println(ans);
    scanner.close();
  } main {
    Scanner scanner = new Scanner(System.in);
    long n = scanner.nextLong();
    long k = scanner.nextLong();
    long r = n%k;
    
    long ans = Math.min(r, (k-r));
    System.out.println(ans);
    scanner.close();
  } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) long n = scanner.nextLong(); n scanner.nextLong() scanner.nextLong scanner long k = scanner.nextLong(); k scanner.nextLong() scanner.nextLong scanner long r = n%k; r n%k n k long ans = Math.min(r, (k-r)); ans Math.min(r, (k-r)) Math.min Math r (k-r) k r System.out.println(ans) System.out.println System.out System System.out ans scanner.close() scanner.close scanner String[] args args public class Main{
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    long n = scanner.nextLong();
    long k = scanner.nextLong();
    long r = n%k;
    
    long ans = Math.min(r, (k-r));
    System.out.println(ans);
    scanner.close();
  }

	
} public class Main{
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    long n = scanner.nextLong();
    long k = scanner.nextLong();
    long r = n%k;
    
    long ans = Math.min(r, (k-r));
    System.out.println(ans);
    scanner.close();
  }

	
} Main
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    long n = scanner.nextLong();
    long k = scanner.nextLong();
    long r = n%k;
    
    long ans = Math.min(r, (k-r));
    System.out.println(ans);
    scanner.close();
  }

	
}
