import java.util.*;

class Main {
  public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
    
    long x = sc.nextLong(); //next(文字列), nextDouble, nextLine(1行)とか
    long y = sc.nextLong();
    long z = x / y;
    x = x - y*z;
    if (Math.abs(x-y) < x){
      x = Math.abs(x-y);
    }
    
    System.out.println(x);
    
  }
} import java.util.*; util.* java class Main {
  public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
    
    long x = sc.nextLong(); //next(文字列), nextDouble, nextLine(1行)とか
    long y = sc.nextLong();
    long z = x / y;
    x = x - y*z;
    if (Math.abs(x-y) < x){
      x = Math.abs(x-y);
    }
    
    System.out.println(x);
    
  }
} Main public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
    
    long x = sc.nextLong(); //next(文字列), nextDouble, nextLine(1行)とか
    long y = sc.nextLong();
    long z = x / y;
    x = x - y*z;
    if (Math.abs(x-y) < x){
      x = Math.abs(x-y);
    }
    
    System.out.println(x);
    
  } main {
	Scanner sc = new Scanner(System.in);
    
    long x = sc.nextLong(); //next(文字列), nextDouble, nextLine(1行)とか
    long y = sc.nextLong();
    long z = x / y;
    x = x - y*z;
    if (Math.abs(x-y) < x){
      x = Math.abs(x-y);
    }
    
    System.out.println(x);
    
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long x = sc.nextLong(); x sc.nextLong() sc.nextLong sc long y = sc.nextLong(); y sc.nextLong() sc.nextLong sc long z = x / y; z x / y x y x = x - y*z x x - y*z x y*z y z if (Math.abs(x-y) < x){
      x = Math.abs(x-y);
    } Math.abs(x-y) < x Math.abs(x-y) Math.abs Math x-y x y x {
      x = Math.abs(x-y);
    } x = Math.abs(x-y) x Math.abs(x-y) Math.abs Math x-y x y System.out.println(x) System.out.println System.out System System.out x String[] args args
import java.util.*;

class Main {
  public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
    
    long x = sc.nextLong(); //next(文字列), nextDouble, nextLine(1行)とか
    long y = sc.nextLong();
    long z = x / y;
    x = x - y*z;
    if (Math.abs(x-y) < x){
      x = Math.abs(x-y);
    }
    
    System.out.println(x);
    
  }
}
