import 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 a = n % k;
    long b = Math.abs(a - k);

    if(a>b){
      System.out.println(b);
    }else{
      System.out.println(a);
    }


  }
} import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();

    long a = n % k;
    long b = Math.abs(a - k);

    if(a>b){
      System.out.println(b);
    }else{
      System.out.println(a);
    }


  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();

    long a = n % k;
    long b = Math.abs(a - k);

    if(a>b){
      System.out.println(b);
    }else{
      System.out.println(a);
    }


  } main {
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();

    long a = n % k;
    long b = Math.abs(a - k);

    if(a>b){
      System.out.println(b);
    }else{
      System.out.println(a);
    }


  } 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 a = n % k; a n % k n k long b = Math.abs(a - k); b Math.abs(a - k) Math.abs Math a - k a k if(a>b){
      System.out.println(b);
    }else{
      System.out.println(a);
    } a>b a b {
      System.out.println(b);
    } System.out.println(b) System.out.println System.out System System.out b {
      System.out.println(a);
    } System.out.println(a) System.out.println System.out System System.out a 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 a = n % k;
    long b = Math.abs(a - k);

    if(a>b){
      System.out.println(b);
    }else{
      System.out.println(a);
    }


  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();

    long a = n % k;
    long b = Math.abs(a - k);

    if(a>b){
      System.out.println(b);
    }else{
      System.out.println(a);
    }


  }
} Main
import 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 a = n % k;
    long b = Math.abs(a - k);

    if(a>b){
      System.out.println(b);
    }else{
      System.out.println(a);
    }


  }
}