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();
                System.out.println(Math.min(n % k , -1 * (n % k - k)));
                sc.close();
        }
}
//おやつ食べました
//昔解けなかった問題が解けるようになっていて感動しました
//こんなこと言ってWAが出たら鳴くのでACを出せ 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();
                System.out.println(Math.min(n % k , -1 * (n % k - k)));
                sc.close();
        }
} Main public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                long n = sc.nextLong();
                long k = sc.nextLong();
                System.out.println(Math.min(n % k , -1 * (n % k - k)));
                sc.close();
        } main {
                Scanner sc = new Scanner(System.in);
                long n = sc.nextLong();
                long k = sc.nextLong();
                System.out.println(Math.min(n % k , -1 * (n % k - k)));
                sc.close();
        } 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 System.out.println(Math.min(n % k , -1 * (n % k - k))) System.out.println System.out System System.out Math.min(n % k , -1 * (n % k - k)) Math.min Math n % k n k -1 * (n % k - k) -1 1 (n % k - k) n % k n k k sc.close() sc.close sc 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();
                System.out.println(Math.min(n % k , -1 * (n % k - k)));
                sc.close();
        }
} public class Main{
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                long n = sc.nextLong();
                long k = sc.nextLong();
                System.out.println(Math.min(n % k , -1 * (n % k - k)));
                sc.close();
        }
} 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();
                System.out.println(Math.min(n % k , -1 * (n % k - k)));
                sc.close();
        }
}
//おやつ食べました
//昔解けなかった問題が解けるようになっていて感動しました
//こんなこと言ってWAが出たら鳴くのでACを出せ