import java.util.*;

class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long K = sc.nextLong();
    System.out.print(Math.min(N % K, K - N % K));
  }
} import java.util.*; util.* java class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long K = sc.nextLong();
    System.out.print(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.print(Math.min(N % K, K - N % K));
  } main {
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long K = sc.nextLong();
    System.out.print(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.print(Math.min(N % K, K - N % K)) System.out.print 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
import java.util.*;

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