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 x = N % K;
 long y = Math.abs(K - x);
    if(x > y){
      x = y;
    }
  System.out.println(x);
 }
} 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 x = N % K;
 long y = Math.abs(K - x);
    if(x > y){
      x = y;
    }
  System.out.println(x);
 }
} Main public static void main(String[] args){
 Scanner scan = new Scanner(System.in);
 
 long N = scan.nextLong();
 long K = scan.nextLong();
 long x = N % K;
 long y = Math.abs(K - x);
    if(x > y){
      x = y;
    }
  System.out.println(x);
 } main {
 Scanner scan = new Scanner(System.in);
 
 long N = scan.nextLong();
 long K = scan.nextLong();
 long x = N % K;
 long y = Math.abs(K - x);
    if(x > y){
      x = y;
    }
  System.out.println(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 long x = N % K; x N % K N K long y = Math.abs(K - x); y Math.abs(K - x) Math.abs Math K - x K x if(x > y){
      x = y;
    } x > y x y {
      x = y;
    } x = y x y System.out.println(x) System.out.println System.out System System.out x 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 x = N % K;
 long y = Math.abs(K - x);
    if(x > y){
      x = y;
    }
  System.out.println(x);
 }
}