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 = Math.abs(ans1 - k);
        if (ans1 > ans2) System.out.println(ans2);
        else System.out.println(ans1);
    }
} 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 = Math.abs(ans1 - k);
        if (ans1 > ans2) System.out.println(ans2);
        else System.out.println(ans1);
    }
} 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 = Math.abs(ans1 - k);
        if (ans1 > ans2) System.out.println(ans2);
        else System.out.println(ans1);
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans1 = n%k;
        long ans2 = Math.abs(ans1 - k);
        if (ans1 > ans2) System.out.println(ans2);
        else System.out.println(ans1);
    } 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 = Math.abs(ans1 - k); ans2 Math.abs(ans1 - k) Math.abs Math ans1 - k ans1 k if (ans1 > ans2) System.out.println(ans2);
        else System.out.println(ans1); ans1 > ans2 ans1 ans2 System.out.println(ans2) System.out.println System.out System System.out ans2 System.out.println(ans1) System.out.println System.out System System.out ans1 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 = Math.abs(ans1 - k);
        if (ans1 > ans2) System.out.println(ans2);
        else System.out.println(ans1);
    }
} 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 = Math.abs(ans1 - k);
        if (ans1 > ans2) System.out.println(ans2);
        else System.out.println(ans1);
    }
} 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 = Math.abs(ans1 - k);
        if (ans1 > ans2) System.out.println(ans2);
        else System.out.println(ans1);
    }
}