import java.util.Scanner;
import java.lang.Math;
public class Main{
  public static void main(String[] args){
	Scanner a = new Scanner(System.in);
    long x = a.nextLong();
    long y = a.nextLong();
    long z = x%y;
    System.out.println(Math.min(z,Math.abs(y-z)));
  }
} import java.util.Scanner; Scanner java.util import java.lang.Math; Math java.lang public class Main{
  public static void main(String[] args){
	Scanner a = new Scanner(System.in);
    long x = a.nextLong();
    long y = a.nextLong();
    long z = x%y;
    System.out.println(Math.min(z,Math.abs(y-z)));
  }
} Main public static void main(String[] args){
	Scanner a = new Scanner(System.in);
    long x = a.nextLong();
    long y = a.nextLong();
    long z = x%y;
    System.out.println(Math.min(z,Math.abs(y-z)));
  } main {
	Scanner a = new Scanner(System.in);
    long x = a.nextLong();
    long y = a.nextLong();
    long z = x%y;
    System.out.println(Math.min(z,Math.abs(y-z)));
  } Scanner a = new Scanner(System.in); a new Scanner(System.in) long x = a.nextLong(); x a.nextLong() a.nextLong a long y = a.nextLong(); y a.nextLong() a.nextLong a long z = x%y; z x%y x y System.out.println(Math.min(z,Math.abs(y-z))) System.out.println System.out System System.out Math.min(z,Math.abs(y-z)) Math.min Math z Math.abs(y-z) Math.abs Math y-z y z String[] args args public class Main{
  public static void main(String[] args){
	Scanner a = new Scanner(System.in);
    long x = a.nextLong();
    long y = a.nextLong();
    long z = x%y;
    System.out.println(Math.min(z,Math.abs(y-z)));
  }
} public class Main{
  public static void main(String[] args){
	Scanner a = new Scanner(System.in);
    long x = a.nextLong();
    long y = a.nextLong();
    long z = x%y;
    System.out.println(Math.min(z,Math.abs(y-z)));
  }
} Main
import java.util.Scanner;
import java.lang.Math;
public class Main{
  public static void main(String[] args){
	Scanner a = new Scanner(System.in);
    long x = a.nextLong();
    long y = a.nextLong();
    long z = x%y;
    System.out.println(Math.min(z,Math.abs(y-z)));
  }
}