import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();

        long ans1 = N % K;
        long ans2 = K - ans1;

        System.out.println(Math.min(ans1, ans2));
    }
} import java.util.Scanner; Scanner 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 ans1 = N % K;
        long ans2 = K - ans1;

        System.out.println(Math.min(ans1, ans2));
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();

        long ans1 = N % K;
        long ans2 = K - ans1;

        System.out.println(Math.min(ans1, ans2));
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();

        long ans1 = N % K;
        long ans2 = K - ans1;

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

        System.out.println(Math.min(ans1, ans2));
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();

        long ans1 = N % K;
        long ans2 = K - ans1;

        System.out.println(Math.min(ans1, ans2));
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();

        long ans1 = N % K;
        long ans2 = K - ans1;

        System.out.println(Math.min(ans1, ans2));
    }
}
