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 a = n % k;
        System.out.println(Math.min(a, k-a));
    }
} 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 a = n % k;
        System.out.println(Math.min(a, k-a));
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();

        long a = n % k;
        System.out.println(Math.min(a, k-a));
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();

        long a = n % k;
        System.out.println(Math.min(a, k-a));
    } 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 a = n % k; a n % k n k System.out.println(Math.min(a, k-a)) System.out.println System.out System System.out Math.min(a, k-a) Math.min Math a k-a k a 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 a = n % k;
        System.out.println(Math.min(a, k-a));
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();

        long a = n % k;
        System.out.println(Math.min(a, k-a));
    }
} 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 a = n % k;
        System.out.println(Math.min(a, k-a));
    }
}