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();
    sc.close();

    long q = N / K; // 0方向に丸め込む
    long ans = Math.min(Math.abs(N - (K * (q + 1))), Math.abs(N - (K * q)));
    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();
    sc.close();

    long q = N / K; // 0方向に丸め込む
    long ans = Math.min(Math.abs(N - (K * (q + 1))), Math.abs(N - (K * q)));
    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();
    sc.close();

    long q = N / K; // 0方向に丸め込む
    long ans = Math.min(Math.abs(N - (K * (q + 1))), Math.abs(N - (K * q)));
    System.out.println(ans);
  } main {
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long K = sc.nextLong();
    sc.close();

    long q = N / K; // 0方向に丸め込む
    long ans = Math.min(Math.abs(N - (K * (q + 1))), Math.abs(N - (K * q)));
    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 sc.close() sc.close sc long q = N / K; q N / K N K long ans = Math.min(Math.abs(N - (K * (q + 1))), Math.abs(N - (K * q))); ans Math.min(Math.abs(N - (K * (q + 1))), Math.abs(N - (K * q))) Math.min Math Math.abs(N - (K * (q + 1))) Math.abs Math N - (K * (q + 1)) N (K * (q + 1)) K (q + 1) q 1 Math.abs(N - (K * q)) Math.abs Math N - (K * q) N (K * q) K q 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();
    sc.close();

    long q = N / K; // 0方向に丸め込む
    long ans = Math.min(Math.abs(N - (K * (q + 1))), Math.abs(N - (K * q)));
    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();
    sc.close();

    long q = N / K; // 0方向に丸め込む
    long ans = Math.min(Math.abs(N - (K * (q + 1))), Math.abs(N - (K * q)));
    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();
    sc.close();

    long q = N / K; // 0方向に丸め込む
    long ans = Math.min(Math.abs(N - (K * (q + 1))), Math.abs(N - (K * q)));
    System.out.println(ans);
  }
}
