import java.util.*;
class Main{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong(),k = sc.nextLong();
    n = n%k;
    if(n<(double)k/2){
      pl(n+"");
    }else{
      pl((k-n)+"");
    }
  }

  public static void pr(String str){
    System.out.print(str);
  }
  public static void pl(String str){
    System.out.println(str);
  }
} import java.util.*; util.* java class Main{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong(),k = sc.nextLong();
    n = n%k;
    if(n<(double)k/2){
      pl(n+"");
    }else{
      pl((k-n)+"");
    }
  }

  public static void pr(String str){
    System.out.print(str);
  }
  public static void pl(String str){
    System.out.println(str);
  }
} Main public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong(),k = sc.nextLong();
    n = n%k;
    if(n<(double)k/2){
      pl(n+"");
    }else{
      pl((k-n)+"");
    }
  } main {
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong(),k = sc.nextLong();
    n = n%k;
    if(n<(double)k/2){
      pl(n+"");
    }else{
      pl((k-n)+"");
    }
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n = sc.nextLong() n sc.nextLong() sc.nextLong sc k = sc.nextLong(); k sc.nextLong() sc.nextLong sc n = n%k n n%k n k if(n<(double)k/2){
      pl(n+"");
    }else{
      pl((k-n)+"");
    } n<(double)k/2 n (double)k/2 (double)k 2 {
      pl(n+"");
    } pl(n+"") pl n+"" n "" {
      pl((k-n)+"");
    } pl((k-n)+"") pl (k-n)+"" (k-n) k n "" String args[] args public static void pr(String str){
    System.out.print(str);
  } pr {
    System.out.print(str);
  } System.out.print(str) System.out.print System.out System System.out str String str str public static void pl(String str){
    System.out.println(str);
  } pl {
    System.out.println(str);
  } System.out.println(str) System.out.println System.out System System.out str String str str
import java.util.*;
class Main{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong(),k = sc.nextLong();
    n = n%k;
    if(n<(double)k/2){
      pl(n+"");
    }else{
      pl((k-n)+"");
    }
  }

  public static void pr(String str){
    System.out.print(str);
  }
  public static void pl(String str){
    System.out.println(str);
  }
}
