import java.util.*;
import java.io.*;

public class Main{
  public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      String[] S = br.readLine().split(" ");
      long N = Long.parseLong(S[0]);
      long M = Long.parseLong(S[1]);
      
      long mod = N%M;
      System.out.println(Math.min(mod, Math.abs(M-mod))); 
    }catch(Exception e){
    }
  }
} import java.util.*; util.* java import java.io.*; io.* java public class Main{
  public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      String[] S = br.readLine().split(" ");
      long N = Long.parseLong(S[0]);
      long M = Long.parseLong(S[1]);
      
      long mod = N%M;
      System.out.println(Math.min(mod, Math.abs(M-mod))); 
    }catch(Exception e){
    }
  }
} Main public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      String[] S = br.readLine().split(" ");
      long N = Long.parseLong(S[0]);
      long M = Long.parseLong(S[1]);
      
      long mod = N%M;
      System.out.println(Math.min(mod, Math.abs(M-mod))); 
    }catch(Exception e){
    }
  } main {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      String[] S = br.readLine().split(" ");
      long N = Long.parseLong(S[0]);
      long M = Long.parseLong(S[1]);
      
      long mod = N%M;
      System.out.println(Math.min(mod, Math.abs(M-mod))); 
    }catch(Exception e){
    }
  } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) try{
      String[] S = br.readLine().split(" ");
      long N = Long.parseLong(S[0]);
      long M = Long.parseLong(S[1]);
      
      long mod = N%M;
      System.out.println(Math.min(mod, Math.abs(M-mod))); 
    }catch(Exception e){
    } catch(Exception e){
    } Exception e {
    } {
      String[] S = br.readLine().split(" ");
      long N = Long.parseLong(S[0]);
      long M = Long.parseLong(S[1]);
      
      long mod = N%M;
      System.out.println(Math.min(mod, Math.abs(M-mod))); 
    } String[] S = br.readLine().split(" "); S br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long N = Long.parseLong(S[0]); N Long.parseLong(S[0]) Long.parseLong Long S[0] S 0 long M = Long.parseLong(S[1]); M Long.parseLong(S[1]) Long.parseLong Long S[1] S 1 long mod = N%M; mod N%M N M System.out.println(Math.min(mod, Math.abs(M-mod))) System.out.println System.out System System.out Math.min(mod, Math.abs(M-mod)) Math.min Math mod Math.abs(M-mod) Math.abs Math M-mod M mod String[] args args public class Main{
  public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      String[] S = br.readLine().split(" ");
      long N = Long.parseLong(S[0]);
      long M = Long.parseLong(S[1]);
      
      long mod = N%M;
      System.out.println(Math.min(mod, Math.abs(M-mod))); 
    }catch(Exception e){
    }
  }
} public class Main{
  public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      String[] S = br.readLine().split(" ");
      long N = Long.parseLong(S[0]);
      long M = Long.parseLong(S[1]);
      
      long mod = N%M;
      System.out.println(Math.min(mod, Math.abs(M-mod))); 
    }catch(Exception e){
    }
  }
} Main
import java.util.*;
import java.io.*;

public class Main{
  public static void main(String[] args)throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try{
      String[] S = br.readLine().split(" ");
      long N = Long.parseLong(S[0]);
      long M = Long.parseLong(S[1]);
      
      long mod = N%M;
      System.out.println(Math.min(mod, Math.abs(M-mod))); 
    }catch(Exception e){
    }
  }
}
