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

