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 min = 0;
    
    if (n > k){
      long a = n%k;
      if (a > 0){
        min = Math.abs(a-k);
      }
    } else if (n < k){
      long a = k-n;
      if (n < a){
        min = n;
      } else {
        min = a;
      }
    }
    
    System.out.println(min);
  }
} 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 min = 0;
    
    if (n > k){
      long a = n%k;
      if (a > 0){
        min = Math.abs(a-k);
      }
    } else if (n < k){
      long a = k-n;
      if (n < a){
        min = n;
      } else {
        min = a;
      }
    }
    
    System.out.println(min);
  }
} Main public static void main (String args[]){
    Scanner sc = new Scanner(System.in);
    
    long n = sc.nextLong();
    long k = sc.nextLong();
    
    long min = 0;
    
    if (n > k){
      long a = n%k;
      if (a > 0){
        min = Math.abs(a-k);
      }
    } else if (n < k){
      long a = k-n;
      if (n < a){
        min = n;
      } else {
        min = a;
      }
    }
    
    System.out.println(min);
  } main {
    Scanner sc = new Scanner(System.in);
    
    long n = sc.nextLong();
    long k = sc.nextLong();
    
    long min = 0;
    
    if (n > k){
      long a = n%k;
      if (a > 0){
        min = Math.abs(a-k);
      }
    } else if (n < k){
      long a = k-n;
      if (n < a){
        min = n;
      } else {
        min = a;
      }
    }
    
    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 min = 0; min 0 if (n > k){
      long a = n%k;
      if (a > 0){
        min = Math.abs(a-k);
      }
    } else if (n < k){
      long a = k-n;
      if (n < a){
        min = n;
      } else {
        min = a;
      }
    } n > k n k {
      long a = n%k;
      if (a > 0){
        min = Math.abs(a-k);
      }
    } long a = n%k; a n%k n k if (a > 0){
        min = Math.abs(a-k);
      } a > 0 a 0 {
        min = Math.abs(a-k);
      } min = Math.abs(a-k) min Math.abs(a-k) Math.abs Math a-k a k if (n < k){
      long a = k-n;
      if (n < a){
        min = n;
      } else {
        min = a;
      }
    } n < k n k {
      long a = k-n;
      if (n < a){
        min = n;
      } else {
        min = a;
      }
    } long a = k-n; a k-n k n if (n < a){
        min = n;
      } else {
        min = a;
      } n < a n a {
        min = n;
      } min = n min n {
        min = a;
      } min = a min a System.out.println(min) System.out.println System.out System System.out min 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();
    
    long min = 0;
    
    if (n > k){
      long a = n%k;
      if (a > 0){
        min = Math.abs(a-k);
      }
    } else if (n < k){
      long a = k-n;
      if (n < a){
        min = n;
      } else {
        min = a;
      }
    }
    
    System.out.println(min);
  }
} public class Main{
  public static void main (String args[]){
    Scanner sc = new Scanner(System.in);
    
    long n = sc.nextLong();
    long k = sc.nextLong();
    
    long min = 0;
    
    if (n > k){
      long a = n%k;
      if (a > 0){
        min = Math.abs(a-k);
      }
    } else if (n < k){
      long a = k-n;
      if (n < a){
        min = n;
      } else {
        min = a;
      }
    }
    
    System.out.println(min);
  }
} 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 min = 0;
    
    if (n > k){
      long a = n%k;
      if (a > 0){
        min = Math.abs(a-k);
      }
    } else if (n < k){
      long a = k-n;
      if (n < a){
        min = n;
      } else {
        min = a;
      }
    }
    
    System.out.println(min);
  }
}