import java.util.Scanner;
import java.lang.Math;

public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    long N = scan.nextLong();
    long K = scan.nextLong();
    scan.close();

    long x = N % K;
    System.out.println(Math.min(x, K-x));
  }
} import java.util.Scanner; Scanner java.util import java.lang.Math; Math java.lang public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    long N = scan.nextLong();
    long K = scan.nextLong();
    scan.close();

    long x = N % K;
    System.out.println(Math.min(x, K-x));
  }
} Main public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    long N = scan.nextLong();
    long K = scan.nextLong();
    scan.close();

    long x = N % K;
    System.out.println(Math.min(x, K-x));
  } main {
    Scanner scan = new Scanner(System.in);
    long N = scan.nextLong();
    long K = scan.nextLong();
    scan.close();

    long x = N % K;
    System.out.println(Math.min(x, K-x));
  } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long N = scan.nextLong(); N scan.nextLong() scan.nextLong scan long K = scan.nextLong(); K scan.nextLong() scan.nextLong scan scan.close() scan.close scan long x = N % K; x N % K N K System.out.println(Math.min(x, K-x)) System.out.println System.out System System.out Math.min(x, K-x) Math.min Math x K-x K x String[] args args public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    long N = scan.nextLong();
    long K = scan.nextLong();
    scan.close();

    long x = N % K;
    System.out.println(Math.min(x, K-x));
  }
} public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    long N = scan.nextLong();
    long K = scan.nextLong();
    scan.close();

    long x = N % K;
    System.out.println(Math.min(x, K-x));
  }
} Main
import java.util.Scanner;
import java.lang.Math;

public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    long N = scan.nextLong();
    long K = scan.nextLong();
    scan.close();

    long x = N % K;
    System.out.println(Math.min(x, K-x));
  }
}