import java.util.Scanner;
import java.util.Arrays;
class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long N=sc.nextLong();
    long K=sc.nextLong();
        if (K>N) {
          if(K-N<N){
            System.out.println(K-N);
          }else{
            System.out.println(N);
          }

        }else if(K==N){
          System.out.println(0);
        }else if(N>K){
          if(N%K>K-N%K){
            System.out.println(K-N%K);
          }else{
            System.out.println(N%K);
          }
        }

        }
      } import java.util.Scanner; Scanner java.util import java.util.Arrays; Arrays java.util class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long N=sc.nextLong();
    long K=sc.nextLong();
        if (K>N) {
          if(K-N<N){
            System.out.println(K-N);
          }else{
            System.out.println(N);
          }

        }else if(K==N){
          System.out.println(0);
        }else if(N>K){
          if(N%K>K-N%K){
            System.out.println(K-N%K);
          }else{
            System.out.println(N%K);
          }
        }

        }
      } Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long N=sc.nextLong();
    long K=sc.nextLong();
        if (K>N) {
          if(K-N<N){
            System.out.println(K-N);
          }else{
            System.out.println(N);
          }

        }else if(K==N){
          System.out.println(0);
        }else if(N>K){
          if(N%K>K-N%K){
            System.out.println(K-N%K);
          }else{
            System.out.println(N%K);
          }
        }

        } main {
    Scanner sc = new Scanner(System.in);
    long N=sc.nextLong();
    long K=sc.nextLong();
        if (K>N) {
          if(K-N<N){
            System.out.println(K-N);
          }else{
            System.out.println(N);
          }

        }else if(K==N){
          System.out.println(0);
        }else if(N>K){
          if(N%K>K-N%K){
            System.out.println(K-N%K);
          }else{
            System.out.println(N%K);
          }
        }

        } 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 if (K>N) {
          if(K-N<N){
            System.out.println(K-N);
          }else{
            System.out.println(N);
          }

        }else if(K==N){
          System.out.println(0);
        }else if(N>K){
          if(N%K>K-N%K){
            System.out.println(K-N%K);
          }else{
            System.out.println(N%K);
          }
        } K>N K N {
          if(K-N<N){
            System.out.println(K-N);
          }else{
            System.out.println(N);
          }

        } if(K-N<N){
            System.out.println(K-N);
          }else{
            System.out.println(N);
          } K-N<N K-N K N N {
            System.out.println(K-N);
          } System.out.println(K-N) System.out.println System.out System System.out K-N K N {
            System.out.println(N);
          } System.out.println(N) System.out.println System.out System System.out N if(K==N){
          System.out.println(0);
        }else if(N>K){
          if(N%K>K-N%K){
            System.out.println(K-N%K);
          }else{
            System.out.println(N%K);
          }
        } K==N K N {
          System.out.println(0);
        } System.out.println(0) System.out.println System.out System System.out 0 if(N>K){
          if(N%K>K-N%K){
            System.out.println(K-N%K);
          }else{
            System.out.println(N%K);
          }
        } N>K N K {
          if(N%K>K-N%K){
            System.out.println(K-N%K);
          }else{
            System.out.println(N%K);
          }
        } if(N%K>K-N%K){
            System.out.println(K-N%K);
          }else{
            System.out.println(N%K);
          } N%K>K-N%K N%K N K K-N%K K N%K N K {
            System.out.println(K-N%K);
          } System.out.println(K-N%K) System.out.println System.out System System.out K-N%K K N%K N K {
            System.out.println(N%K);
          } System.out.println(N%K) System.out.println System.out System System.out N%K N K String[] args args
import java.util.Scanner;
import java.util.Arrays;
class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long N=sc.nextLong();
    long K=sc.nextLong();
        if (K>N) {
          if(K-N<N){
            System.out.println(K-N);
          }else{
            System.out.println(N);
          }

        }else if(K==N){
          System.out.println(0);
        }else if(N>K){
          if(N%K>K-N%K){
            System.out.println(K-N%K);
          }else{
            System.out.println(N%K);
          }
        }

        }
      }
