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();
        long mod = N % K;
        long sub = Math.abs(mod - K);

        System.out.println(Math.min(sub, mod));
    }
} 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();
        long mod = N % K;
        long sub = Math.abs(mod - K);

        System.out.println(Math.min(sub, mod));
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();
        long mod = N % K;
        long sub = Math.abs(mod - K);

        System.out.println(Math.min(sub, mod));
    } main {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();
        long mod = N % K;
        long sub = Math.abs(mod - K);

        System.out.println(Math.min(sub, mod));
    } 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 long mod = N % K; mod N % K N K long sub = Math.abs(mod - K); sub Math.abs(mod - K) Math.abs Math mod - K mod K System.out.println(Math.min(sub, mod)) System.out.println System.out System System.out Math.min(sub, mod) Math.min Math sub mod 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();
        long mod = N % K;
        long sub = Math.abs(mod - K);

        System.out.println(Math.min(sub, mod));
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();
        long mod = N % K;
        long sub = Math.abs(mod - K);

        System.out.println(Math.min(sub, mod));
    }
} 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();
        long mod = N % K;
        long sub = Math.abs(mod - K);

        System.out.println(Math.min(sub, mod));
    }
}
