import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    
    long n = scan.nextLong();
    long k = scan.nextLong();
    
    long res = n % k;
    
    System.out.println(Math.min(Math.abs(res-k),res));
  } 
} import java.util.Scanner; Scanner java.util public class Main {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    
    long n = scan.nextLong();
    long k = scan.nextLong();
    
    long res = n % k;
    
    System.out.println(Math.min(Math.abs(res-k),res));
  } 
} Main public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    
    long n = scan.nextLong();
    long k = scan.nextLong();
    
    long res = n % k;
    
    System.out.println(Math.min(Math.abs(res-k),res));
  } main {

    Scanner scan = new Scanner(System.in);
    
    long n = scan.nextLong();
    long k = scan.nextLong();
    
    long res = n % k;
    
    System.out.println(Math.min(Math.abs(res-k),res));
  } 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 res = n % k; res n % k n k System.out.println(Math.min(Math.abs(res-k),res)) System.out.println System.out System System.out Math.min(Math.abs(res-k),res) Math.min Math Math.abs(res-k) Math.abs Math res-k res k res 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 res = n % k;
    
    System.out.println(Math.min(Math.abs(res-k),res));
  } 
} public class Main {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    
    long n = scan.nextLong();
    long k = scan.nextLong();
    
    long res = n % k;
    
    System.out.println(Math.min(Math.abs(res-k),res));
  } 
} Main
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    
    long n = scan.nextLong();
    long k = scan.nextLong();
    
    long res = n % k;
    
    System.out.println(Math.min(Math.abs(res-k),res));
  } 
}