//https://atcoder.jp/contests/abc161/tasks/abc161_c
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        long n = in.nextLong(), k = in.nextLong();

        long remainder = n % k;
        out.println(Math.min(remainder,k - remainder));
        out.close();

    }
} import java.io.PrintWriter; PrintWriter java.io import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        long n = in.nextLong(), k = in.nextLong();

        long remainder = n % k;
        out.println(Math.min(remainder,k - remainder));
        out.close();

    }
} Main public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        long n = in.nextLong(), k = in.nextLong();

        long remainder = n % k;
        out.println(Math.min(remainder,k - remainder));
        out.close();

    } main {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        long n = in.nextLong(), k = in.nextLong();

        long remainder = n % k;
        out.println(Math.min(remainder,k - remainder));
        out.close();

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

    public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        long n = in.nextLong(), k = in.nextLong();

        long remainder = n % k;
        out.println(Math.min(remainder,k - remainder));
        out.close();

    }
} public class Main {

    public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        long n = in.nextLong(), k = in.nextLong();

        long remainder = n % k;
        out.println(Math.min(remainder,k - remainder));
        out.close();

    }
} Main
//https://atcoder.jp/contests/abc161/tasks/abc161_c
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        long n = in.nextLong(), k = in.nextLong();

        long remainder = n % k;
        out.println(Math.min(remainder,k - remainder));
        out.close();

    }
}
