import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    long a = scan.nextLong();
    long b = scan.nextLong();

    long c = a%b;
    if (b-c > c){
      System.out.println(c);
    }
    else{
      System.out.println(b-c);
    }
  }
} import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    long a = scan.nextLong();
    long b = scan.nextLong();

    long c = a%b;
    if (b-c > c){
      System.out.println(c);
    }
    else{
      System.out.println(b-c);
    }
  }
} Main public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    long a = scan.nextLong();
    long b = scan.nextLong();

    long c = a%b;
    if (b-c > c){
      System.out.println(c);
    }
    else{
      System.out.println(b-c);
    }
  } main {
    Scanner scan = new Scanner(System.in);
    long a = scan.nextLong();
    long b = scan.nextLong();

    long c = a%b;
    if (b-c > c){
      System.out.println(c);
    }
    else{
      System.out.println(b-c);
    }
  } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long a = scan.nextLong(); a scan.nextLong() scan.nextLong scan long b = scan.nextLong(); b scan.nextLong() scan.nextLong scan long c = a%b; c a%b a b if (b-c > c){
      System.out.println(c);
    }
    else{
      System.out.println(b-c);
    } b-c > c b-c b c c {
      System.out.println(c);
    } System.out.println(c) System.out.println System.out System System.out c {
      System.out.println(b-c);
    } System.out.println(b-c) System.out.println System.out System System.out b-c b c String[] args args public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    long a = scan.nextLong();
    long b = scan.nextLong();

    long c = a%b;
    if (b-c > c){
      System.out.println(c);
    }
    else{
      System.out.println(b-c);
    }
  }
} public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    long a = scan.nextLong();
    long b = scan.nextLong();

    long c = a%b;
    if (b-c > c){
      System.out.println(c);
    }
    else{
      System.out.println(b-c);
    }
  }
} Main
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    long a = scan.nextLong();
    long b = scan.nextLong();

    long c = a%b;
    if (b-c > c){
      System.out.println(c);
    }
    else{
      System.out.println(b-c);
    }
  }
}
