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 quot = N / K;
      Long candidate1 = abs(N - K * quot);
      Long candidate2 = abs(N - K * (quot+1));
      Long min;
      if(candidate1 < candidate2){
        min = candidate1;
      }else{
        min = candidate2;
      }
      
      // 出力
      System.out.println(min);
    }
    
    // 絶対値を求める関数
    public static Long abs(Long num){
      if(num < 0){
        return num * (-1);
      }else{
        return num;
      }
    }

} 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 quot = N / K;
      Long candidate1 = abs(N - K * quot);
      Long candidate2 = abs(N - K * (quot+1));
      Long min;
      if(candidate1 < candidate2){
        min = candidate1;
      }else{
        min = candidate2;
      }
      
      // 出力
      System.out.println(min);
    }
    
    // 絶対値を求める関数
    public static Long abs(Long num){
      if(num < 0){
        return num * (-1);
      }else{
        return num;
      }
    }

} Main public static void main(String[] args){
      // 入力
      Scanner sc = new Scanner(System.in);
      Long N = sc.nextLong();
      Long K = sc.nextLong();
      
      // 操作
      Long quot = N / K;
      Long candidate1 = abs(N - K * quot);
      Long candidate2 = abs(N - K * (quot+1));
      Long min;
      if(candidate1 < candidate2){
        min = candidate1;
      }else{
        min = candidate2;
      }
      
      // 出力
      System.out.println(min);
    } main {
      // 入力
      Scanner sc = new Scanner(System.in);
      Long N = sc.nextLong();
      Long K = sc.nextLong();
      
      // 操作
      Long quot = N / K;
      Long candidate1 = abs(N - K * quot);
      Long candidate2 = abs(N - K * (quot+1));
      Long min;
      if(candidate1 < candidate2){
        min = candidate1;
      }else{
        min = candidate2;
      }
      
      // 出力
      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 quot = N / K; quot N / K N K Long candidate1 = abs(N - K * quot); candidate1 abs(N - K * quot) abs N - K * quot N K * quot K quot Long candidate2 = abs(N - K * (quot+1)); candidate2 abs(N - K * (quot+1)) abs N - K * (quot+1) N K * (quot+1) K (quot+1) quot 1 Long min; min if(candidate1 < candidate2){
        min = candidate1;
      }else{
        min = candidate2;
      } candidate1 < candidate2 candidate1 candidate2 {
        min = candidate1;
      } min = candidate1 min candidate1 {
        min = candidate2;
      } min = candidate2 min candidate2 System.out.println(min) System.out.println System.out System System.out min String[] args args // 絶対値を求める関数
    public static Long abs(Long num){
      if(num < 0){
        return num * (-1);
      }else{
        return num;
      }
    } abs {
      if(num < 0){
        return num * (-1);
      }else{
        return num;
      }
    } if(num < 0){
        return num * (-1);
      }else{
        return num;
      } num < 0 num 0 {
        return num * (-1);
      } return num * (-1); num * (-1) num (-1) 1 {
        return num;
      } return num; num Long num num public class Main {
    public static void main(String[] args){
      // 入力
      Scanner sc = new Scanner(System.in);
      Long N = sc.nextLong();
      Long K = sc.nextLong();
      
      // 操作
      Long quot = N / K;
      Long candidate1 = abs(N - K * quot);
      Long candidate2 = abs(N - K * (quot+1));
      Long min;
      if(candidate1 < candidate2){
        min = candidate1;
      }else{
        min = candidate2;
      }
      
      // 出力
      System.out.println(min);
    }
    
    // 絶対値を求める関数
    public static Long abs(Long num){
      if(num < 0){
        return num * (-1);
      }else{
        return num;
      }
    }

} public class Main {
    public static void main(String[] args){
      // 入力
      Scanner sc = new Scanner(System.in);
      Long N = sc.nextLong();
      Long K = sc.nextLong();
      
      // 操作
      Long quot = N / K;
      Long candidate1 = abs(N - K * quot);
      Long candidate2 = abs(N - K * (quot+1));
      Long min;
      if(candidate1 < candidate2){
        min = candidate1;
      }else{
        min = candidate2;
      }
      
      // 出力
      System.out.println(min);
    }
    
    // 絶対値を求める関数
    public static Long abs(Long num){
      if(num < 0){
        return num * (-1);
      }else{
        return num;
      }
    }

} 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 quot = N / K;
      Long candidate1 = abs(N - K * quot);
      Long candidate2 = abs(N - K * (quot+1));
      Long min;
      if(candidate1 < candidate2){
        min = candidate1;
      }else{
        min = candidate2;
      }
      
      // 出力
      System.out.println(min);
    }
    
    // 絶対値を求める関数
    public static Long abs(Long num){
      if(num < 0){
        return num * (-1);
      }else{
        return num;
      }
    }

}