import java.util.Scanner;
class Main {
  public static void main(String args[]){
    Scanner  sc = new Scanner(System.in);
    long n , k ,i,t;
    n = sc.nextLong();
    k = sc.nextLong();
    i = n%k;
    t = Math.abs(k-i);
    if(i > t){
      i = t;
    }
    System.out.println(i);
  }
} import java.util.Scanner; Scanner java.util class Main {
  public static void main(String args[]){
    Scanner  sc = new Scanner(System.in);
    long n , k ,i,t;
    n = sc.nextLong();
    k = sc.nextLong();
    i = n%k;
    t = Math.abs(k-i);
    if(i > t){
      i = t;
    }
    System.out.println(i);
  }
} Main public static void main(String args[]){
    Scanner  sc = new Scanner(System.in);
    long n , k ,i,t;
    n = sc.nextLong();
    k = sc.nextLong();
    i = n%k;
    t = Math.abs(k-i);
    if(i > t){
      i = t;
    }
    System.out.println(i);
  } main {
    Scanner  sc = new Scanner(System.in);
    long n , k ,i,t;
    n = sc.nextLong();
    k = sc.nextLong();
    i = n%k;
    t = Math.abs(k-i);
    if(i > t){
      i = t;
    }
    System.out.println(i);
  } Scanner  sc = new Scanner(System.in); sc new Scanner(System.in) long n n k k i i t; t n = sc.nextLong() n sc.nextLong() sc.nextLong sc k = sc.nextLong() k sc.nextLong() sc.nextLong sc i = n%k i n%k n k t = Math.abs(k-i) t Math.abs(k-i) Math.abs Math k-i k i if(i > t){
      i = t;
    } i > t i t {
      i = t;
    } i = t i t System.out.println(i) System.out.println System.out System System.out i String args[] args
import java.util.Scanner;
class Main {
  public static void main(String args[]){
    Scanner  sc = new Scanner(System.in);
    long n , k ,i,t;
    n = sc.nextLong();
    k = sc.nextLong();
    i = n%k;
    t = Math.abs(k-i);
    if(i > t){
      i = t;
    }
    System.out.println(i);
  }
}
