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();

      System.out.println(Math.min(n % k, k - n % k));
  }
} 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();

      System.out.println(Math.min(n % k, k - n % k));
  }
} Main public static void main(String[] args) { 
      Scanner sc = new Scanner(System.in);
      long n = sc.nextLong();
      long k = sc.nextLong();

      System.out.println(Math.min(n % k, k - n % k));
  } main { 
      Scanner sc = new Scanner(System.in);
      long n = sc.nextLong();
      long k = sc.nextLong();

      System.out.println(Math.min(n % k, k - n % k));
  } 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 System.out.println(Math.min(n % k, k - n % k)) System.out.println System.out System System.out Math.min(n % k, k - n % k) Math.min Math n % k n k k - n % k k n % k n k 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();

      System.out.println(Math.min(n % k, k - n % k));
  }
} public class Main {

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

      System.out.println(Math.min(n % k, k - n % k));
  }
} 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();

      System.out.println(Math.min(n % k, k - n % k));
  }
}