import java.util.Scanner;

class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long a = n - (n/k)*k;
    long b = k-a;
    System.out.print(Math.min(a,b));
    System.out.flush();
  }
} import java.util.Scanner; Scanner java.util class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long a = n - (n/k)*k;
    long b = k-a;
    System.out.print(Math.min(a,b));
    System.out.flush();
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long a = n - (n/k)*k;
    long b = k-a;
    System.out.print(Math.min(a,b));
    System.out.flush();
  } main {
    Scanner sc = new Scanner(System.in);
    long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long a = n - (n/k)*k;
    long b = k-a;
    System.out.print(Math.min(a,b));
    System.out.flush();
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n = Long.parseLong(sc.next()); n Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long k = Long.parseLong(sc.next()); k Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long a = n - (n/k)*k; a n - (n/k)*k n (n/k)*k (n/k) n k k long b = k-a; b k-a k a System.out.print(Math.min(a,b)) System.out.print System.out System System.out Math.min(a,b) Math.min Math a b System.out.flush() System.out.flush System.out System System.out String[] args args
import java.util.Scanner;

class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long n = Long.parseLong(sc.next());
    long k = Long.parseLong(sc.next());
    long a = n - (n/k)*k;
    long b = k-a;
    System.out.print(Math.min(a,b));
    System.out.flush();
  }
}