import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long x = sc.nextLong();
    long k = sc.nextLong();
    boolean flag=true;
    long d=0;
    x=x%k;

    d=Math.min(x,Math.abs(x-k));

    System.out.println(d);
  }}
     import java.util.Scanner; Scanner java.util public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long x = sc.nextLong();
    long k = sc.nextLong();
    boolean flag=true;
    long d=0;
    x=x%k;

    d=Math.min(x,Math.abs(x-k));

    System.out.println(d);
  }} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long x = sc.nextLong();
    long k = sc.nextLong();
    boolean flag=true;
    long d=0;
    x=x%k;

    d=Math.min(x,Math.abs(x-k));

    System.out.println(d);
  } main {
    Scanner sc = new Scanner(System.in);
    long x = sc.nextLong();
    long k = sc.nextLong();
    boolean flag=true;
    long d=0;
    x=x%k;

    d=Math.min(x,Math.abs(x-k));

    System.out.println(d);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long x = sc.nextLong(); x sc.nextLong() sc.nextLong sc long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc boolean flag=true; flag true long d=0; d 0 x=x%k x x%k x k d=Math.min(x,Math.abs(x-k)) d Math.min(x,Math.abs(x-k)) Math.min Math x Math.abs(x-k) Math.abs Math x-k x k System.out.println(d) System.out.println System.out System System.out d String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long x = sc.nextLong();
    long k = sc.nextLong();
    boolean flag=true;
    long d=0;
    x=x%k;

    d=Math.min(x,Math.abs(x-k));

    System.out.println(d);
  }} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long x = sc.nextLong();
    long k = sc.nextLong();
    boolean flag=true;
    long d=0;
    x=x%k;

    d=Math.min(x,Math.abs(x-k));

    System.out.println(d);
  }} Main
import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long x = sc.nextLong();
    long k = sc.nextLong();
    boolean flag=true;
    long d=0;
    x=x%k;

    d=Math.min(x,Math.abs(x-k));

    System.out.println(d);
  }}
    