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