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