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();
    
    if(N>=K) {
      N %= K;
    }
    if(K-N<N) {
      N = K-N;
    }
    System.out.println(N);
  }
} 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();
    
    if(N>=K) {
      N %= K;
    }
    if(K-N<N) {
      N = K-N;
    }
    System.out.println(N);
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long K = sc.nextLong();
    
    if(N>=K) {
      N %= K;
    }
    if(K-N<N) {
      N = K-N;
    }
    System.out.println(N);
  } main {
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long K = sc.nextLong();
    
    if(N>=K) {
      N %= K;
    }
    if(K-N<N) {
      N = K-N;
    }
    System.out.println(N);
  } 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(N>=K) {
      N %= K;
    } N>=K N K {
      N %= K;
    } N %= K N K if(K-N<N) {
      N = K-N;
    } K-N<N K-N K N N {
      N = K-N;
    } N = K-N N K-N K N System.out.println(N) System.out.println System.out System System.out N 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();
    
    if(N>=K) {
      N %= K;
    }
    if(K-N<N) {
      N = K-N;
    }
    System.out.println(N);
  }
} public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    long K = sc.nextLong();
    
    if(N>=K) {
      N %= K;
    }
    if(K-N<N) {
      N = K-N;
    }
    System.out.println(N);
  }
} 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();
    
    if(N>=K) {
      N %= K;
    }
    if(K-N<N) {
      N = K-N;
    }
    System.out.println(N);
  }
}