import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        System.out.println(Math.min(Math.abs(n-n/k*k), Math.abs(n-(n/k+1)*k)));
        return;
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        System.out.println(Math.min(Math.abs(n-n/k*k), Math.abs(n-(n/k+1)*k)));
        return;
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        System.out.println(Math.min(Math.abs(n-n/k*k), Math.abs(n-(n/k+1)*k)));
        return;
    } main {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        System.out.println(Math.min(Math.abs(n-n/k*k), Math.abs(n-(n/k+1)*k)));
        return;
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) long n = scanner.nextLong(); n scanner.nextLong() scanner.nextLong scanner long k = scanner.nextLong(); k scanner.nextLong() scanner.nextLong scanner System.out.println(Math.min(Math.abs(n-n/k*k), Math.abs(n-(n/k+1)*k))) System.out.println System.out System System.out Math.min(Math.abs(n-n/k*k), Math.abs(n-(n/k+1)*k)) Math.min Math Math.abs(n-n/k*k) Math.abs Math n-n/k*k n n/k*k n/k n k k Math.abs(n-(n/k+1)*k) Math.abs Math n-(n/k+1)*k n (n/k+1)*k (n/k+1) n/k n k 1 k return; String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        System.out.println(Math.min(Math.abs(n-n/k*k), Math.abs(n-(n/k+1)*k)));
        return;
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        System.out.println(Math.min(Math.abs(n-n/k*k), Math.abs(n-(n/k+1)*k)));
        return;
    }
} Main
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        System.out.println(Math.min(Math.abs(n-n/k*k), Math.abs(n-(n/k+1)*k)));
        return;
    }
}