import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();


        if (n % k == 0) {
            System.out.println(0);
        } else {
            long a = n % k;
            long b = a - k;


            if (b < 0) {
                b = -b;
            }

            if (a > b) {
                System.out.println(b);
            } else {
                System.out.println(a);
            }
        }
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();


        if (n % k == 0) {
            System.out.println(0);
        } else {
            long a = n % k;
            long b = a - k;


            if (b < 0) {
                b = -b;
            }

            if (a > b) {
                System.out.println(b);
            } else {
                System.out.println(a);
            }
        }
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();


        if (n % k == 0) {
            System.out.println(0);
        } else {
            long a = n % k;
            long b = a - k;


            if (b < 0) {
                b = -b;
            }

            if (a > b) {
                System.out.println(b);
            } else {
                System.out.println(a);
            }
        }
    } main {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();


        if (n % k == 0) {
            System.out.println(0);
        } else {
            long a = n % k;
            long b = a - k;


            if (b < 0) {
                b = -b;
            }

            if (a > b) {
                System.out.println(b);
            } else {
                System.out.println(a);
            }
        }
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) long n = scanner.nextLong(); n scanner.nextLong() scanner.nextLong scanner long k = scanner.nextLong(); k scanner.nextLong() scanner.nextLong scanner if (n % k == 0) {
            System.out.println(0);
        } else {
            long a = n % k;
            long b = a - k;


            if (b < 0) {
                b = -b;
            }

            if (a > b) {
                System.out.println(b);
            } else {
                System.out.println(a);
            }
        } n % k == 0 n % k n k 0 {
            System.out.println(0);
        } System.out.println(0) System.out.println System.out System System.out 0 {
            long a = n % k;
            long b = a - k;


            if (b < 0) {
                b = -b;
            }

            if (a > b) {
                System.out.println(b);
            } else {
                System.out.println(a);
            }
        } long a = n % k; a n % k n k long b = a - k; b a - k a k if (b < 0) {
                b = -b;
            } b < 0 b 0 {
                b = -b;
            } b = -b b -b b if (a > b) {
                System.out.println(b);
            } else {
                System.out.println(a);
            } a > b a b {
                System.out.println(b);
            } System.out.println(b) System.out.println System.out System System.out b {
                System.out.println(a);
            } System.out.println(a) System.out.println System.out System System.out a String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();


        if (n % k == 0) {
            System.out.println(0);
        } else {
            long a = n % k;
            long b = a - k;


            if (b < 0) {
                b = -b;
            }

            if (a > b) {
                System.out.println(b);
            } else {
                System.out.println(a);
            }
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();


        if (n % k == 0) {
            System.out.println(0);
        } else {
            long a = n % k;
            long b = a - k;


            if (b < 0) {
                b = -b;
            }

            if (a > b) {
                System.out.println(b);
            } else {
                System.out.println(a);
            }
        }
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();


        if (n % k == 0) {
            System.out.println(0);
        } else {
            long a = n % k;
            long b = a - k;


            if (b < 0) {
                b = -b;
            }

            if (a > b) {
                System.out.println(b);
            } else {
                System.out.println(a);
            }
        }
    }
}