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