import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        n %= k;
        if (2 * n < k) {
            System.out.println(n);
        } else {
            n = k % n;
            System.out.println(n);
        }
        sc.close();
    }
} import java.util.Scanner; Scanner 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 %= k;
        if (2 * n < k) {
            System.out.println(n);
        } else {
            n = k % n;
            System.out.println(n);
        }
        sc.close();
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        n %= k;
        if (2 * n < k) {
            System.out.println(n);
        } else {
            n = k % n;
            System.out.println(n);
        }
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        n %= k;
        if (2 * n < k) {
            System.out.println(n);
        } else {
            n = k % n;
            System.out.println(n);
        }
        sc.close();
    } 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 %= k n k if (2 * n < k) {
            System.out.println(n);
        } else {
            n = k % n;
            System.out.println(n);
        } 2 * n < k 2 * n 2 n k {
            System.out.println(n);
        } System.out.println(n) System.out.println System.out System System.out n {
            n = k % n;
            System.out.println(n);
        } n = k % n n k % n k n System.out.println(n) System.out.println System.out System System.out n sc.close() sc.close sc 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 %= k;
        if (2 * n < k) {
            System.out.println(n);
        } else {
            n = k % n;
            System.out.println(n);
        }
        sc.close();
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        n %= k;
        if (2 * n < k) {
            System.out.println(n);
        } else {
            n = k % n;
            System.out.println(n);
        }
        sc.close();
    }
} Main
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        n %= k;
        if (2 * n < k) {
            System.out.println(n);
        } else {
            n = k % n;
            System.out.println(n);
        }
        sc.close();
    }
}