import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C solver = new C();
        solver.solve(1, in, out);
        out.close();
    }

    static class C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            long n = Long.parseLong(in.next());
            long k = Long.parseLong(in.next());
            long ans = 0;
            long x = 0;

            x = n;
            long tmp = Long.MAX_VALUE;
            if (n == 0) {
                out.println(0);
                return;
            } else if (k == 1) {
                out.println(0);
                return;
            } else if (n < k) {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } else if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            }

        }

    }
}
 import java.io.OutputStream; OutputStream java.io import java.io.IOException; IOException java.io import java.io.InputStream; InputStream java.io import java.io.PrintWriter; PrintWriter java.io import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C solver = new C();
        solver.solve(1, in, out);
        out.close();
    }

    static class C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            long n = Long.parseLong(in.next());
            long k = Long.parseLong(in.next());
            long ans = 0;
            long x = 0;

            x = n;
            long tmp = Long.MAX_VALUE;
            if (n == 0) {
                out.println(0);
                return;
            } else if (k == 1) {
                out.println(0);
                return;
            } else if (n < k) {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } else if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            }

        }

    }
} Main public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C solver = new C();
        solver.solve(1, in, out);
        out.close();
    } main {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C solver = new C();
        solver.solve(1, in, out);
        out.close();
    } InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out Scanner in = new Scanner(inputStream); in new Scanner(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) C solver = new C(); solver new C() solver.solve(1, in, out) solver.solve solver 1 in out out.close() out.close out String[] args args static class C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            long n = Long.parseLong(in.next());
            long k = Long.parseLong(in.next());
            long ans = 0;
            long x = 0;

            x = n;
            long tmp = Long.MAX_VALUE;
            if (n == 0) {
                out.println(0);
                return;
            } else if (k == 1) {
                out.println(0);
                return;
            } else if (n < k) {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } else if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            }

        }

    } C public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            long n = Long.parseLong(in.next());
            long k = Long.parseLong(in.next());
            long ans = 0;
            long x = 0;

            x = n;
            long tmp = Long.MAX_VALUE;
            if (n == 0) {
                out.println(0);
                return;
            } else if (k == 1) {
                out.println(0);
                return;
            } else if (n < k) {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } else if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            }

        } solve {
            // 入力
            long n = Long.parseLong(in.next());
            long k = Long.parseLong(in.next());
            long ans = 0;
            long x = 0;

            x = n;
            long tmp = Long.MAX_VALUE;
            if (n == 0) {
                out.println(0);
                return;
            } else if (k == 1) {
                out.println(0);
                return;
            } else if (n < k) {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } else if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            }

        } long n = Long.parseLong(in.next()); n Long.parseLong(in.next()) Long.parseLong Long in.next() in.next in long k = Long.parseLong(in.next()); k Long.parseLong(in.next()) Long.parseLong Long in.next() in.next in long ans = 0; ans 0 long x = 0; x 0 x = n x n long tmp = Long.MAX_VALUE; tmp Long.MAX_VALUE Long Long.MAX_VALUE if (n == 0) {
                out.println(0);
                return;
            } else if (k == 1) {
                out.println(0);
                return;
            } else if (n < k) {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } else if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            } n == 0 n 0 {
                out.println(0);
                return;
            } out.println(0) out.println out 0 return; if (k == 1) {
                out.println(0);
                return;
            } else if (n < k) {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } else if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            } k == 1 k 1 {
                out.println(0);
                return;
            } out.println(0) out.println out 0 return; if (n < k) {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } else if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            } n < k n k {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } x = Math.abs(n - k) x Math.abs(n - k) Math.abs Math n - k n k if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                } x < n x n {
                    out.println(x);
                } out.println(x) out.println out x {
                    out.println(n);
                } out.println(n) out.println out n return; if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            } n > k n k {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                } n % k == 0 n % k n k 0 {
                    out.println(0);
                    return;

                } out.println(0) out.println out 0 return; if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                } n % k == 1 n % k n k 1 {
                    out.println(1);
                    return;

                } out.println(1) out.println out 1 return; {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                } x = Math.abs(n % k - k) x Math.abs(n % k - k) Math.abs Math n % k - k n % k n k k if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    } n % k > x n % k n k x {
                        out.println(x);
                        return;

                    } out.println(x) out.println out x return; {
                        out.println(n % k);
                        return;

                    } out.println(n % k) out.println out n % k n k return; {
                out.println(0);
                return;
            } out.println(0) out.println out 0 return; int testNumber testNumber Scanner in in PrintWriter out out public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C solver = new C();
        solver.solve(1, in, out);
        out.close();
    }

    static class C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            long n = Long.parseLong(in.next());
            long k = Long.parseLong(in.next());
            long ans = 0;
            long x = 0;

            x = n;
            long tmp = Long.MAX_VALUE;
            if (n == 0) {
                out.println(0);
                return;
            } else if (k == 1) {
                out.println(0);
                return;
            } else if (n < k) {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } else if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            }

        }

    }
} public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C solver = new C();
        solver.solve(1, in, out);
        out.close();
    }

    static class C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            long n = Long.parseLong(in.next());
            long k = Long.parseLong(in.next());
            long ans = 0;
            long x = 0;

            x = n;
            long tmp = Long.MAX_VALUE;
            if (n == 0) {
                out.println(0);
                return;
            } else if (k == 1) {
                out.println(0);
                return;
            } else if (n < k) {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } else if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            }

        }

    }
} Main
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C solver = new C();
        solver.solve(1, in, out);
        out.close();
    }

    static class C {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            long n = Long.parseLong(in.next());
            long k = Long.parseLong(in.next());
            long ans = 0;
            long x = 0;

            x = n;
            long tmp = Long.MAX_VALUE;
            if (n == 0) {
                out.println(0);
                return;
            } else if (k == 1) {
                out.println(0);
                return;
            } else if (n < k) {
                x = Math.abs(n - k);
                if (x < n) {
                    out.println(x);
                } else {
                    out.println(n);
                }
                return;
            } else if (n > k) {
                if (n % k == 0) {
                    out.println(0);
                    return;

                } else if (n % k == 1) {
                    out.println(1);
                    return;

                } else {
                    x = Math.abs(n % k - k);
                    if (n % k > x) {
                        out.println(x);
                        return;

                    } else {
                        out.println(n % k);
                        return;

                    }

                }
            } else {
                out.println(0);
                return;
            }

        }

    }
}

