import java.util.Scanner;
class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    long n = scan.nextLong();
    long k = scan.nextLong();
    
    long digit = n % k;
    System.out.println(digit < k - digit ? digit : k - digit);
  }
} import java.util.Scanner; Scanner java.util class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    long n = scan.nextLong();
    long k = scan.nextLong();
    
    long digit = n % k;
    System.out.println(digit < k - digit ? digit : k - digit);
  }
} Main public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    long n = scan.nextLong();
    long k = scan.nextLong();
    
    long digit = n % k;
    System.out.println(digit < k - digit ? digit : k - digit);
  } main {
    Scanner scan = new Scanner(System.in);
    long n = scan.nextLong();
    long k = scan.nextLong();
    
    long digit = n % k;
    System.out.println(digit < k - digit ? digit : k - digit);
  } 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 long digit = n % k; digit n % k n k System.out.println(digit < k - digit ? digit : k - digit) System.out.println System.out System System.out digit < k - digit ? digit : k - digit digit < k - digit digit k - digit k digit digit k - digit k digit String[] args args
import java.util.Scanner;
class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    long n = scan.nextLong();
    long k = scan.nextLong();
    
    long digit = n % k;
    System.out.println(digit < k - digit ? digit : k - digit);
  }
}