import java.util.Scanner;

// 14
class Main{

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

        long tmp;
        n -= (n / k) * k;
        while(true) {
            tmp = n;
            n = Math.abs(n - k);
            if(tmp < n) break;
        }

        System.out.println(tmp);
    }

} import java.util.Scanner; Scanner java.util class Main{

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

        long tmp;
        n -= (n / k) * k;
        while(true) {
            tmp = n;
            n = Math.abs(n - k);
            if(tmp < n) break;
        }

        System.out.println(tmp);
    }

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

        long tmp;
        n -= (n / k) * k;
        while(true) {
            tmp = n;
            n = Math.abs(n - k);
            if(tmp < n) break;
        }

        System.out.println(tmp);
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();

        long tmp;
        n -= (n / k) * k;
        while(true) {
            tmp = n;
            n = Math.abs(n - k);
            if(tmp < n) break;
        }

        System.out.println(tmp);
    } 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 long tmp; tmp n -= (n / k) * k n (n / k) * k (n / k) n k k while(true) {
            tmp = n;
            n = Math.abs(n - k);
            if(tmp < n) break;
        } true {
            tmp = n;
            n = Math.abs(n - k);
            if(tmp < n) break;
        } tmp = n tmp n n = Math.abs(n - k) n Math.abs(n - k) Math.abs Math n - k n k if(tmp < n) break; tmp < n tmp n break; System.out.println(tmp) System.out.println System.out System System.out tmp String[] args args
import java.util.Scanner;

// 14
class Main{

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

        long tmp;
        n -= (n / k) * k;
        while(true) {
            tmp = n;
            n = Math.abs(n - k);
            if(tmp < n) break;
        }

        System.out.println(tmp);
    }

}
