import java.util.Scanner;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    Long n = sc.nextLong();
    Long k = sc.nextLong();
    
    long min = n % k;
    if(min < Math.abs(min - k)){
      System.out.println(min);
    }else{
      System.out.println(Math.abs(min - k));
    }
  }
} import java.util.Scanner; Scanner java.util public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    Long n = sc.nextLong();
    Long k = sc.nextLong();
    
    long min = n % k;
    if(min < Math.abs(min - k)){
      System.out.println(min);
    }else{
      System.out.println(Math.abs(min - k));
    }
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    Long n = sc.nextLong();
    Long k = sc.nextLong();
    
    long min = n % k;
    if(min < Math.abs(min - k)){
      System.out.println(min);
    }else{
      System.out.println(Math.abs(min - k));
    }
  } main {
    Scanner sc = new Scanner(System.in);
    Long n = sc.nextLong();
    Long k = sc.nextLong();
    
    long min = n % k;
    if(min < Math.abs(min - k)){
      System.out.println(min);
    }else{
      System.out.println(Math.abs(min - k));
    }
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) Long n = sc.nextLong(); n sc.nextLong() sc.nextLong sc Long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc long min = n % k; min n % k n k if(min < Math.abs(min - k)){
      System.out.println(min);
    }else{
      System.out.println(Math.abs(min - k));
    } min < Math.abs(min - k) min Math.abs(min - k) Math.abs Math min - k min k {
      System.out.println(min);
    } System.out.println(min) System.out.println System.out System System.out min {
      System.out.println(Math.abs(min - k));
    } System.out.println(Math.abs(min - k)) System.out.println System.out System System.out Math.abs(min - k) Math.abs Math min - k min k String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    Long n = sc.nextLong();
    Long k = sc.nextLong();
    
    long min = n % k;
    if(min < Math.abs(min - k)){
      System.out.println(min);
    }else{
      System.out.println(Math.abs(min - k));
    }
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    Long n = sc.nextLong();
    Long k = sc.nextLong();
    
    long min = n % k;
    if(min < Math.abs(min - k)){
      System.out.println(min);
    }else{
      System.out.println(Math.abs(min - k));
    }
  }
} Main
import java.util.Scanner;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    Long n = sc.nextLong();
    Long k = sc.nextLong();
    
    long min = n % k;
    if(min < Math.abs(min - k)){
      System.out.println(min);
    }else{
      System.out.println(Math.abs(min - k));
    }
  }
}