import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        if (n % k == 0) {
            System.out.println(0);
            return;
        }
        long mod = Math.abs(n % k - k);
        System.out.println(Math.min(n % k, mod));
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        if (n % k == 0) {
            System.out.println(0);
            return;
        }
        long mod = Math.abs(n % k - k);
        System.out.println(Math.min(n % k, mod));
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        if (n % k == 0) {
            System.out.println(0);
            return;
        }
        long mod = Math.abs(n % k - k);
        System.out.println(Math.min(n % k, mod));
    } main {
        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        if (n % k == 0) {
            System.out.println(0);
            return;
        }
        long mod = Math.abs(n % k - k);
        System.out.println(Math.min(n % k, mod));
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n = Long.parseLong(sc.next()); n Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long k = Long.parseLong(sc.next()); k Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc if (n % k == 0) {
            System.out.println(0);
            return;
        } n % k == 0 n % k n k 0 {
            System.out.println(0);
            return;
        } System.out.println(0) System.out.println System.out System System.out 0 return; long mod = Math.abs(n % k - k); mod Math.abs(n % k - k) Math.abs Math n % k - k n % k n k k System.out.println(Math.min(n % k, mod)) System.out.println System.out System System.out Math.min(n % k, mod) Math.min Math n % k n k mod String[] args args public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        if (n % k == 0) {
            System.out.println(0);
            return;
        }
        long mod = Math.abs(n % k - k);
        System.out.println(Math.min(n % k, mod));
    }
} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        if (n % k == 0) {
            System.out.println(0);
            return;
        }
        long mod = Math.abs(n % k - k);
        System.out.println(Math.min(n % k, mod));
    }
} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        if (n % k == 0) {
            System.out.println(0);
            return;
        }
        long mod = Math.abs(n % k - k);
        System.out.println(Math.min(n % k, mod));
    }
}