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