import java.util.*;
public class Main {
        static Scanner sc = new Scanner(System.in);
        static void myout(Object t){System.out.println(t);}//standard output
        static void myerr(Object t){System.err.println(t);}//standard error
        static String getStr(){return sc.next();}
        static int getInt(){return Integer.parseInt(getStr());}
        static long getLong(){return Long.parseLong(getStr());}
        static boolean hasNext(){return sc.hasNext();}
        static char[] mySplit(String str){return str.toCharArray();}
        public static void main(String[] args){
          long N = getLong();
          long K = getLong();
          
          N %= K;
          if(N > K / 2){
            N = K % N;
          }
          myout(N);
        }
        //Method addition frame start

        //Method addition frame end
} import java.util.*; util.* java public class Main {
        static Scanner sc = new Scanner(System.in);
        static void myout(Object t){System.out.println(t);}//standard output
        static void myerr(Object t){System.err.println(t);}//standard error
        static String getStr(){return sc.next();}
        static int getInt(){return Integer.parseInt(getStr());}
        static long getLong(){return Long.parseLong(getStr());}
        static boolean hasNext(){return sc.hasNext();}
        static char[] mySplit(String str){return str.toCharArray();}
        public static void main(String[] args){
          long N = getLong();
          long K = getLong();
          
          N %= K;
          if(N > K / 2){
            N = K % N;
          }
          myout(N);
        }
        //Method addition frame start

        //Method addition frame end
} Main static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) static void myout(Object t){System.out.println(t);}//standard output myout {System.out.println(t);} System.out.println(t) System.out.println System.out System System.out t Object t t static void myerr(Object t){System.err.println(t);}//standard error myerr {System.err.println(t);} System.err.println(t) System.err.println System.err System System.err t Object t t static String getStr(){return sc.next();} getStr {return sc.next();} return sc.next(); sc.next() sc.next sc static int getInt(){return Integer.parseInt(getStr());} getInt {return Integer.parseInt(getStr());} return Integer.parseInt(getStr()); Integer.parseInt(getStr()) Integer.parseInt Integer getStr() getStr static long getLong(){return Long.parseLong(getStr());} getLong {return Long.parseLong(getStr());} return Long.parseLong(getStr()); Long.parseLong(getStr()) Long.parseLong Long getStr() getStr static boolean hasNext(){return sc.hasNext();} hasNext {return sc.hasNext();} return sc.hasNext(); sc.hasNext() sc.hasNext sc static char[] mySplit(String str){return str.toCharArray();} mySplit {return str.toCharArray();} return str.toCharArray(); str.toCharArray() str.toCharArray str String str str public static void main(String[] args){
          long N = getLong();
          long K = getLong();
          
          N %= K;
          if(N > K / 2){
            N = K % N;
          }
          myout(N);
        } main {
          long N = getLong();
          long K = getLong();
          
          N %= K;
          if(N > K / 2){
            N = K % N;
          }
          myout(N);
        } long N = getLong(); N getLong() getLong long K = getLong(); K getLong() getLong N %= K N K if(N > K / 2){
            N = K % N;
          } N > K / 2 N K / 2 K 2 {
            N = K % N;
          } N = K % N N K % N K N myout(N) myout N String[] args args public class Main {
        static Scanner sc = new Scanner(System.in);
        static void myout(Object t){System.out.println(t);}//standard output
        static void myerr(Object t){System.err.println(t);}//standard error
        static String getStr(){return sc.next();}
        static int getInt(){return Integer.parseInt(getStr());}
        static long getLong(){return Long.parseLong(getStr());}
        static boolean hasNext(){return sc.hasNext();}
        static char[] mySplit(String str){return str.toCharArray();}
        public static void main(String[] args){
          long N = getLong();
          long K = getLong();
          
          N %= K;
          if(N > K / 2){
            N = K % N;
          }
          myout(N);
        }
        //Method addition frame start

        //Method addition frame end
} public class Main {
        static Scanner sc = new Scanner(System.in);
        static void myout(Object t){System.out.println(t);}//standard output
        static void myerr(Object t){System.err.println(t);}//standard error
        static String getStr(){return sc.next();}
        static int getInt(){return Integer.parseInt(getStr());}
        static long getLong(){return Long.parseLong(getStr());}
        static boolean hasNext(){return sc.hasNext();}
        static char[] mySplit(String str){return str.toCharArray();}
        public static void main(String[] args){
          long N = getLong();
          long K = getLong();
          
          N %= K;
          if(N > K / 2){
            N = K % N;
          }
          myout(N);
        }
        //Method addition frame start

        //Method addition frame end
} Main
import java.util.*;
public class Main {
        static Scanner sc = new Scanner(System.in);
        static void myout(Object t){System.out.println(t);}//standard output
        static void myerr(Object t){System.err.println(t);}//standard error
        static String getStr(){return sc.next();}
        static int getInt(){return Integer.parseInt(getStr());}
        static long getLong(){return Long.parseLong(getStr());}
        static boolean hasNext(){return sc.hasNext();}
        static char[] mySplit(String str){return str.toCharArray();}
        public static void main(String[] args){
          long N = getLong();
          long K = getLong();
          
          N %= K;
          if(N > K / 2){
            N = K % N;
          }
          myout(N);
        }
        //Method addition frame start

        //Method addition frame end
}
