import java.util.*;

public class Main{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    long n=sc.nextLong();//初期値
    long k=sc.nextLong();
    long count=0;
    long m=n;

    if (n>=k) {
      count=n/k;
    }


    long ans1=Math.abs(n-(k*count));
    long ans2=Math.abs(n-(k*(count+1)));

    long min=Math.min(ans1,ans2);

    System.out.println(min);
  }
} import java.util.*; util.* java public class Main{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    long n=sc.nextLong();//初期値
    long k=sc.nextLong();
    long count=0;
    long m=n;

    if (n>=k) {
      count=n/k;
    }


    long ans1=Math.abs(n-(k*count));
    long ans2=Math.abs(n-(k*(count+1)));

    long min=Math.min(ans1,ans2);

    System.out.println(min);
  }
} Main public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    long n=sc.nextLong();//初期値
    long k=sc.nextLong();
    long count=0;
    long m=n;

    if (n>=k) {
      count=n/k;
    }


    long ans1=Math.abs(n-(k*count));
    long ans2=Math.abs(n-(k*(count+1)));

    long min=Math.min(ans1,ans2);

    System.out.println(min);
  } main {
    Scanner sc=new Scanner(System.in);
    long n=sc.nextLong();//初期値
    long k=sc.nextLong();
    long count=0;
    long m=n;

    if (n>=k) {
      count=n/k;
    }


    long ans1=Math.abs(n-(k*count));
    long ans2=Math.abs(n-(k*(count+1)));

    long min=Math.min(ans1,ans2);

    System.out.println(min);
  } Scanner sc=new Scanner(System.in); sc new Scanner(System.in) long n=sc.nextLong(); n sc.nextLong() sc.nextLong sc long k=sc.nextLong(); k sc.nextLong() sc.nextLong sc long count=0; count 0 long m=n; m n if (n>=k) {
      count=n/k;
    } n>=k n k {
      count=n/k;
    } count=n/k count n/k n k long ans1=Math.abs(n-(k*count)); ans1 Math.abs(n-(k*count)) Math.abs Math n-(k*count) n (k*count) k count long ans2=Math.abs(n-(k*(count+1))); ans2 Math.abs(n-(k*(count+1))) Math.abs Math n-(k*(count+1)) n (k*(count+1)) k (count+1) count 1 long min=Math.min(ans1,ans2); min Math.min(ans1,ans2) Math.min Math ans1 ans2 System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    long n=sc.nextLong();//初期値
    long k=sc.nextLong();
    long count=0;
    long m=n;

    if (n>=k) {
      count=n/k;
    }


    long ans1=Math.abs(n-(k*count));
    long ans2=Math.abs(n-(k*(count+1)));

    long min=Math.min(ans1,ans2);

    System.out.println(min);
  }
} public class Main{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    long n=sc.nextLong();//初期値
    long k=sc.nextLong();
    long count=0;
    long m=n;

    if (n>=k) {
      count=n/k;
    }


    long ans1=Math.abs(n-(k*count));
    long ans2=Math.abs(n-(k*(count+1)));

    long min=Math.min(ans1,ans2);

    System.out.println(min);
  }
} Main
import java.util.*;

public class Main{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    long n=sc.nextLong();//初期値
    long k=sc.nextLong();
    long count=0;
    long m=n;

    if (n>=k) {
      count=n/k;
    }


    long ans1=Math.abs(n-(k*count));
    long ans2=Math.abs(n-(k*(count+1)));

    long min=Math.min(ans1,ans2);

    System.out.println(min);
  }
}
