import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final long n = sc.nextLong();
        final long k = sc.nextLong();

        final long t = n % k;
        System.out.println(Math.min(t, k-t));
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final long n = sc.nextLong();
        final long k = sc.nextLong();

        final long t = n % k;
        System.out.println(Math.min(t, k-t));
    }
} Main public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final long n = sc.nextLong();
        final long k = sc.nextLong();

        final long t = n % k;
        System.out.println(Math.min(t, k-t));
    } main {
        final Scanner sc = new Scanner(System.in);

        final long n = sc.nextLong();
        final long k = sc.nextLong();

        final long t = n % k;
        System.out.println(Math.min(t, k-t));
    } final Scanner sc = new Scanner(System.in); sc new Scanner(System.in) final long n = sc.nextLong(); n sc.nextLong() sc.nextLong sc final long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc final long t = n % k; t n % k n k System.out.println(Math.min(t, k-t)) System.out.println System.out System System.out Math.min(t, k-t) Math.min Math t k-t k t String[] args args public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final long n = sc.nextLong();
        final long k = sc.nextLong();

        final long t = n % k;
        System.out.println(Math.min(t, k-t));
    }
} public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final long n = sc.nextLong();
        final long k = sc.nextLong();

        final long t = n % k;
        System.out.println(Math.min(t, k-t));
    }
} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        final Scanner sc = new Scanner(System.in);

        final long n = sc.nextLong();
        final long k = sc.nextLong();

        final long t = n % k;
        System.out.println(Math.min(t, k-t));
    }
}
