import 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();
        sc.close();

        if(N % K == 0){
            System.out.println("0");
        }else{
            long tmp1 = N / K;
            long tmp2 = tmp1 + 1;
            long a = Math.abs(N - K * tmp1);
            long b = Math.abs(N - K * tmp2);
            System.out.println(Math.min(a, b));
        }
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        if(N % K == 0){
            System.out.println("0");
        }else{
            long tmp1 = N / K;
            long tmp2 = tmp1 + 1;
            long a = Math.abs(N - K * tmp1);
            long b = Math.abs(N - K * tmp2);
            System.out.println(Math.min(a, b));
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        if(N % K == 0){
            System.out.println("0");
        }else{
            long tmp1 = N / K;
            long tmp2 = tmp1 + 1;
            long a = Math.abs(N - K * tmp1);
            long b = Math.abs(N - K * tmp2);
            System.out.println(Math.min(a, b));
        }
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        if(N % K == 0){
            System.out.println("0");
        }else{
            long tmp1 = N / K;
            long tmp2 = tmp1 + 1;
            long a = Math.abs(N - K * tmp1);
            long b = Math.abs(N - K * tmp2);
            System.out.println(Math.min(a, b));
        }
    } 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 sc.close() sc.close sc if(N % K == 0){
            System.out.println("0");
        }else{
            long tmp1 = N / K;
            long tmp2 = tmp1 + 1;
            long a = Math.abs(N - K * tmp1);
            long b = Math.abs(N - K * tmp2);
            System.out.println(Math.min(a, b));
        } N % K == 0 N % K N K 0 {
            System.out.println("0");
        } System.out.println("0") System.out.println System.out System System.out "0" {
            long tmp1 = N / K;
            long tmp2 = tmp1 + 1;
            long a = Math.abs(N - K * tmp1);
            long b = Math.abs(N - K * tmp2);
            System.out.println(Math.min(a, b));
        } long tmp1 = N / K; tmp1 N / K N K long tmp2 = tmp1 + 1; tmp2 tmp1 + 1 tmp1 1 long a = Math.abs(N - K * tmp1); a Math.abs(N - K * tmp1) Math.abs Math N - K * tmp1 N K * tmp1 K tmp1 long b = Math.abs(N - K * tmp2); b Math.abs(N - K * tmp2) Math.abs Math N - K * tmp2 N K * tmp2 K tmp2 System.out.println(Math.min(a, b)) System.out.println System.out System System.out Math.min(a, b) Math.min Math a b 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();
        sc.close();

        if(N % K == 0){
            System.out.println("0");
        }else{
            long tmp1 = N / K;
            long tmp2 = tmp1 + 1;
            long a = Math.abs(N - K * tmp1);
            long b = Math.abs(N - K * tmp2);
            System.out.println(Math.min(a, b));
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        if(N % K == 0){
            System.out.println("0");
        }else{
            long tmp1 = N / K;
            long tmp2 = tmp1 + 1;
            long a = Math.abs(N - K * tmp1);
            long b = Math.abs(N - K * tmp2);
            System.out.println(Math.min(a, b));
        }
    }
} Main
import 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();
        sc.close();

        if(N % K == 0){
            System.out.println("0");
        }else{
            long tmp1 = N / K;
            long tmp2 = tmp1 + 1;
            long a = Math.abs(N - K * tmp1);
            long b = Math.abs(N - K * tmp2);
            System.out.println(Math.min(a, b));
        }
    }
}
