import java.util.*;
import java.lang.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long n = scanner.nextLong();
        Long k = scanner.nextLong();

        Long x = n % k;
        System.out.println(Math.min(x, Math.abs(k-x)));

    }
} import java.util.*; util.* java import java.lang.*; lang.* java import java.util.stream.*; stream.* java.util public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long n = scanner.nextLong();
        Long k = scanner.nextLong();

        Long x = n % k;
        System.out.println(Math.min(x, Math.abs(k-x)));

    }
} Main public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long n = scanner.nextLong();
        Long k = scanner.nextLong();

        Long x = n % k;
        System.out.println(Math.min(x, Math.abs(k-x)));

    } main {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long n = scanner.nextLong();
        Long k = scanner.nextLong();

        Long x = n % k;
        System.out.println(Math.min(x, Math.abs(k-x)));

    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) Long n = scanner.nextLong(); n scanner.nextLong() scanner.nextLong scanner Long k = scanner.nextLong(); k scanner.nextLong() scanner.nextLong scanner Long x = n % k; x n % k n k System.out.println(Math.min(x, Math.abs(k-x))) System.out.println System.out System System.out Math.min(x, Math.abs(k-x)) Math.min Math x Math.abs(k-x) Math.abs Math k-x k x String[] args args public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long n = scanner.nextLong();
        Long k = scanner.nextLong();

        Long x = n % k;
        System.out.println(Math.min(x, Math.abs(k-x)));

    }
} public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long n = scanner.nextLong();
        Long k = scanner.nextLong();

        Long x = n % k;
        System.out.println(Math.min(x, Math.abs(k-x)));

    }
} Main
import java.util.*;
import java.lang.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long n = scanner.nextLong();
        Long k = scanner.nextLong();

        Long x = n % k;
        System.out.println(Math.min(x, Math.abs(k-x)));

    }
}
