/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
https://www.a2oj.com/Ladder16.html
*/
import java.util.*;
import java.io.*;
import java.math.*;

   public class Main
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         long N = Long.parseLong(st.nextToken());
         long K = Long.parseLong(st.nextToken());
         long res = N%K;
         if(res > 0)
            res = Math.min(res, K-res);
         System.out.println(res);
      }
   } import java.util.*; util.* java import java.io.*; io.* java import java.math.*; math.* java public class Main
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         long N = Long.parseLong(st.nextToken());
         long K = Long.parseLong(st.nextToken());
         long res = N%K;
         if(res > 0)
            res = Math.min(res, K-res);
         System.out.println(res);
      }
   } Main public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         long N = Long.parseLong(st.nextToken());
         long K = Long.parseLong(st.nextToken());
         long res = N%K;
         if(res > 0)
            res = Math.min(res, K-res);
         System.out.println(res);
      } main {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         long N = Long.parseLong(st.nextToken());
         long K = Long.parseLong(st.nextToken());
         long res = N%K;
         if(res > 0)
            res = Math.min(res, K-res);
         System.out.println(res);
      } BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); infile new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st = new StringTokenizer(infile.readLine()); st new StringTokenizer(infile.readLine()) long N = Long.parseLong(st.nextToken()); N Long.parseLong(st.nextToken()) Long.parseLong Long st.nextToken() st.nextToken st long K = Long.parseLong(st.nextToken()); K Long.parseLong(st.nextToken()) Long.parseLong Long st.nextToken() st.nextToken st long res = N%K; res N%K N K if(res > 0)
            res = Math.min(res, K-res); res > 0 res 0 res = Math.min(res, K-res) res Math.min(res, K-res) Math.min Math res K-res K res System.out.println(res) System.out.println System.out System System.out res String omkar[] omkar public class Main
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         long N = Long.parseLong(st.nextToken());
         long K = Long.parseLong(st.nextToken());
         long res = N%K;
         if(res > 0)
            res = Math.min(res, K-res);
         System.out.println(res);
      }
   } public class Main
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         long N = Long.parseLong(st.nextToken());
         long K = Long.parseLong(st.nextToken());
         long res = N%K;
         if(res > 0)
            res = Math.min(res, K-res);
         System.out.println(res);
      }
   } Main
/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
https://www.a2oj.com/Ladder16.html
*/
import java.util.*;
import java.io.*;
import java.math.*;

   public class Main
   {
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         long N = Long.parseLong(st.nextToken());
         long K = Long.parseLong(st.nextToken());
         long res = N%K;
         if(res > 0)
            res = Math.min(res, K-res);
         System.out.println(res);
      }
   }