import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long h = sc.nextLong();
        long k = sc.nextLong();
        long num = h / k;
        h -= k * num;
        while (true) {
            if (k / 2 < h) {
                h = Math.abs(h - k);
            } else {
                System.out.println(h);
                System.exit(0);
            }
        }
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long h = sc.nextLong();
        long k = sc.nextLong();
        long num = h / k;
        h -= k * num;
        while (true) {
            if (k / 2 < h) {
                h = Math.abs(h - k);
            } else {
                System.out.println(h);
                System.exit(0);
            }
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long h = sc.nextLong();
        long k = sc.nextLong();
        long num = h / k;
        h -= k * num;
        while (true) {
            if (k / 2 < h) {
                h = Math.abs(h - k);
            } else {
                System.out.println(h);
                System.exit(0);
            }
        }
    } main {
        Scanner sc = new Scanner(System.in);
        long h = sc.nextLong();
        long k = sc.nextLong();
        long num = h / k;
        h -= k * num;
        while (true) {
            if (k / 2 < h) {
                h = Math.abs(h - k);
            } else {
                System.out.println(h);
                System.exit(0);
            }
        }
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long h = sc.nextLong(); h sc.nextLong() sc.nextLong sc long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc long num = h / k; num h / k h k h -= k * num h k * num k num while (true) {
            if (k / 2 < h) {
                h = Math.abs(h - k);
            } else {
                System.out.println(h);
                System.exit(0);
            }
        } true {
            if (k / 2 < h) {
                h = Math.abs(h - k);
            } else {
                System.out.println(h);
                System.exit(0);
            }
        } if (k / 2 < h) {
                h = Math.abs(h - k);
            } else {
                System.out.println(h);
                System.exit(0);
            } k / 2 < h k / 2 k 2 h {
                h = Math.abs(h - k);
            } h = Math.abs(h - k) h Math.abs(h - k) Math.abs Math h - k h k {
                System.out.println(h);
                System.exit(0);
            } System.out.println(h) System.out.println System.out System System.out h System.exit(0) System.exit System 0 String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long h = sc.nextLong();
        long k = sc.nextLong();
        long num = h / k;
        h -= k * num;
        while (true) {
            if (k / 2 < h) {
                h = Math.abs(h - k);
            } else {
                System.out.println(h);
                System.exit(0);
            }
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long h = sc.nextLong();
        long k = sc.nextLong();
        long num = h / k;
        h -= k * num;
        while (true) {
            if (k / 2 < h) {
                h = Math.abs(h - k);
            } else {
                System.out.println(h);
                System.exit(0);
            }
        }
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long h = sc.nextLong();
        long k = sc.nextLong();
        long num = h / k;
        h -= k * num;
        while (true) {
            if (k / 2 < h) {
                h = Math.abs(h - k);
            } else {
                System.out.println(h);
                System.exit(0);
            }
        }
    }
}