import java.util.Arrays;
import java.util.Collections;
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*(n/k);
        System.out.println(Math.min(n, Math.abs(n-k)));
    }

} import java.util.Arrays; Arrays java.util import java.util.Collections; Collections java.util 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*(n/k);
        System.out.println(Math.min(n, Math.abs(n-k)));
    }

} Main public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        long n = sc.nextLong();
        long k = sc.nextLong();
        n -= k*(n/k);
        System.out.println(Math.min(n, Math.abs(n-k)));
    } main {

        Scanner sc = new Scanner(System.in);

        long n = sc.nextLong();
        long k = sc.nextLong();
        n -= k*(n/k);
        System.out.println(Math.min(n, Math.abs(n-k)));
    } 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) n k*(n/k) k (n/k) n k System.out.println(Math.min(n, Math.abs(n-k))) System.out.println System.out System System.out Math.min(n, Math.abs(n-k)) Math.min Math n Math.abs(n-k) Math.abs Math n-k n k 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*(n/k);
        System.out.println(Math.min(n, Math.abs(n-k)));
    }

} 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*(n/k);
        System.out.println(Math.min(n, Math.abs(n-k)));
    }

} Main
import java.util.Arrays;
import java.util.Collections;
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*(n/k);
        System.out.println(Math.min(n, Math.abs(n-k)));
    }

}
