import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        long N = Long.parseLong(scan.next());
        long K = Long.parseLong(scan.next());
        long temp = 0;

        System.out.println(Math.min(N%K, K-N%K));
        scan.close();
    }
} import java.util.*; util.* java public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        long N = Long.parseLong(scan.next());
        long K = Long.parseLong(scan.next());
        long temp = 0;

        System.out.println(Math.min(N%K, K-N%K));
        scan.close();
    }
} Main public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        long N = Long.parseLong(scan.next());
        long K = Long.parseLong(scan.next());
        long temp = 0;

        System.out.println(Math.min(N%K, K-N%K));
        scan.close();
    } main {
        Scanner scan = new Scanner(System.in);

        long N = Long.parseLong(scan.next());
        long K = Long.parseLong(scan.next());
        long temp = 0;

        System.out.println(Math.min(N%K, K-N%K));
        scan.close();
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long N = Long.parseLong(scan.next()); N Long.parseLong(scan.next()) Long.parseLong Long scan.next() scan.next scan long K = Long.parseLong(scan.next()); K Long.parseLong(scan.next()) Long.parseLong Long scan.next() scan.next scan long temp = 0; temp 0 System.out.println(Math.min(N%K, K-N%K)) System.out.println System.out System System.out Math.min(N%K, K-N%K) Math.min Math N%K N K K-N%K K N%K N K scan.close() scan.close scan String[] args args public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        long N = Long.parseLong(scan.next());
        long K = Long.parseLong(scan.next());
        long temp = 0;

        System.out.println(Math.min(N%K, K-N%K));
        scan.close();
    }
} public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        long N = Long.parseLong(scan.next());
        long K = Long.parseLong(scan.next());
        long temp = 0;

        System.out.println(Math.min(N%K, K-N%K));
        scan.close();
    }
} Main
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        long N = Long.parseLong(scan.next());
        long K = Long.parseLong(scan.next());
        long temp = 0;

        System.out.println(Math.min(N%K, K-N%K));
        scan.close();
    }
}