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 buf = n / k;
    long ans = Math.min(n - k * buf, k * (buf + 1) - n);

    System.out.println(ans);
  }
} 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 buf = n / k;
    long ans = Math.min(n - k * buf, k * (buf + 1) - n);

    System.out.println(ans);
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    long n = sc.nextLong();
    long k = sc.nextLong();
    
    long buf = n / k;
    long ans = Math.min(n - k * buf, k * (buf + 1) - n);

    System.out.println(ans);
  } main {
    Scanner sc = new Scanner(System.in);

    long n = sc.nextLong();
    long k = sc.nextLong();
    
    long buf = n / k;
    long ans = Math.min(n - k * buf, k * (buf + 1) - n);

    System.out.println(ans);
  } 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 buf = n / k; buf n / k n k long ans = Math.min(n - k * buf, k * (buf + 1) - n); ans Math.min(n - k * buf, k * (buf + 1) - n) Math.min Math n - k * buf n k * buf k buf k * (buf + 1) - n k * (buf + 1) k (buf + 1) buf 1 n System.out.println(ans) System.out.println System.out System System.out ans 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 buf = n / k;
    long ans = Math.min(n - k * buf, k * (buf + 1) - n);

    System.out.println(ans);
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);

    long n = sc.nextLong();
    long k = sc.nextLong();
    
    long buf = n / k;
    long ans = Math.min(n - k * buf, k * (buf + 1) - n);

    System.out.println(ans);
  }
} 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 buf = n / k;
    long ans = Math.min(n - k * buf, k * (buf + 1) - n);

    System.out.println(ans);
  }
}
