import java.util.*;
public class Main{
  
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    Long N=scanner.nextLong();
    Long K=scanner.nextLong();
    

    if (K==1){
        System.out.println(0);
    }else if(K>N*2){
        System.out.println(N);
    }else{
        N=(N>K)?N%K:K%N;
        do{
            N=Math.abs(N-K);
        }while(N>Math.abs(N-K));
        /*while(N>Math.abs(N-K)){
            N=Math.abs(N-K);
            
        }*/
        System.out.println(N);
    }
  }

} import java.util.*; util.* java public class Main{
  
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    Long N=scanner.nextLong();
    Long K=scanner.nextLong();
    

    if (K==1){
        System.out.println(0);
    }else if(K>N*2){
        System.out.println(N);
    }else{
        N=(N>K)?N%K:K%N;
        do{
            N=Math.abs(N-K);
        }while(N>Math.abs(N-K));
        /*while(N>Math.abs(N-K)){
            N=Math.abs(N-K);
            
        }*/
        System.out.println(N);
    }
  }

} Main public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    Long N=scanner.nextLong();
    Long K=scanner.nextLong();
    

    if (K==1){
        System.out.println(0);
    }else if(K>N*2){
        System.out.println(N);
    }else{
        N=(N>K)?N%K:K%N;
        do{
            N=Math.abs(N-K);
        }while(N>Math.abs(N-K));
        /*while(N>Math.abs(N-K)){
            N=Math.abs(N-K);
            
        }*/
        System.out.println(N);
    }
  } main {
    Scanner scanner = new Scanner(System.in);
    Long N=scanner.nextLong();
    Long K=scanner.nextLong();
    

    if (K==1){
        System.out.println(0);
    }else if(K>N*2){
        System.out.println(N);
    }else{
        N=(N>K)?N%K:K%N;
        do{
            N=Math.abs(N-K);
        }while(N>Math.abs(N-K));
        /*while(N>Math.abs(N-K)){
            N=Math.abs(N-K);
            
        }*/
        System.out.println(N);
    }
  } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) Long N=scanner.nextLong(); N scanner.nextLong() scanner.nextLong scanner Long K=scanner.nextLong(); K scanner.nextLong() scanner.nextLong scanner if (K==1){
        System.out.println(0);
    }else if(K>N*2){
        System.out.println(N);
    }else{
        N=(N>K)?N%K:K%N;
        do{
            N=Math.abs(N-K);
        }while(N>Math.abs(N-K));
        /*while(N>Math.abs(N-K)){
            N=Math.abs(N-K);
            
        }*/
        System.out.println(N);
    } K==1 K 1 {
        System.out.println(0);
    } System.out.println(0) System.out.println System.out System System.out 0 if(K>N*2){
        System.out.println(N);
    }else{
        N=(N>K)?N%K:K%N;
        do{
            N=Math.abs(N-K);
        }while(N>Math.abs(N-K));
        /*while(N>Math.abs(N-K)){
            N=Math.abs(N-K);
            
        }*/
        System.out.println(N);
    } K>N*2 K N*2 N 2 {
        System.out.println(N);
    } System.out.println(N) System.out.println System.out System System.out N {
        N=(N>K)?N%K:K%N;
        do{
            N=Math.abs(N-K);
        }while(N>Math.abs(N-K));
        /*while(N>Math.abs(N-K)){
            N=Math.abs(N-K);
            
        }*/
        System.out.println(N);
    } N=(N>K)?N%K:K%N N (N>K)?N%K:K%N (N>K) N K N%K N K K%N K N do{
            N=Math.abs(N-K);
        }while(N>Math.abs(N-K)); N>Math.abs(N-K) N Math.abs(N-K) Math.abs Math N-K N K {
            N=Math.abs(N-K);
        } N=Math.abs(N-K) N Math.abs(N-K) Math.abs Math N-K N K 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 scanner = new Scanner(System.in);
    Long N=scanner.nextLong();
    Long K=scanner.nextLong();
    

    if (K==1){
        System.out.println(0);
    }else if(K>N*2){
        System.out.println(N);
    }else{
        N=(N>K)?N%K:K%N;
        do{
            N=Math.abs(N-K);
        }while(N>Math.abs(N-K));
        /*while(N>Math.abs(N-K)){
            N=Math.abs(N-K);
            
        }*/
        System.out.println(N);
    }
  }

} public class Main{
  
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    Long N=scanner.nextLong();
    Long K=scanner.nextLong();
    

    if (K==1){
        System.out.println(0);
    }else if(K>N*2){
        System.out.println(N);
    }else{
        N=(N>K)?N%K:K%N;
        do{
            N=Math.abs(N-K);
        }while(N>Math.abs(N-K));
        /*while(N>Math.abs(N-K)){
            N=Math.abs(N-K);
            
        }*/
        System.out.println(N);
    }
  }

} Main
import java.util.*;
public class Main{
  
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    Long N=scanner.nextLong();
    Long K=scanner.nextLong();
    

    if (K==1){
        System.out.println(0);
    }else if(K>N*2){
        System.out.println(N);
    }else{
        N=(N>K)?N%K:K%N;
        do{
            N=Math.abs(N-K);
        }while(N>Math.abs(N-K));
        /*while(N>Math.abs(N-K)){
            N=Math.abs(N-K);
            
        }*/
        System.out.println(N);
    }
  }

}