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