import java.util.*;

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

 
class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();
    
    n = n % k;
    
    if(n == 0){
      System.out.println(0);
      return;
    }
    
    long ans = Math.min(k-n,n);
    System.out.println(ans);
    
  }
}