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