import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        long remainder = N % K;

        if (remainder == 0) {
            System.out.println(0);
        } else {
            if (remainder > (K - remainder)) {
                System.out.println(K - remainder);
            } else {
                System.out.println(remainder);
            }
        }
    }
} import java.util.*; util.* java class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        long remainder = N % K;

        if (remainder == 0) {
            System.out.println(0);
        } else {
            if (remainder > (K - remainder)) {
                System.out.println(K - remainder);
            } else {
                System.out.println(remainder);
            }
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        long remainder = N % K;

        if (remainder == 0) {
            System.out.println(0);
        } else {
            if (remainder > (K - remainder)) {
                System.out.println(K - remainder);
            } else {
                System.out.println(remainder);
            }
        }
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        long remainder = N % K;

        if (remainder == 0) {
            System.out.println(0);
        } else {
            if (remainder > (K - remainder)) {
                System.out.println(K - remainder);
            } else {
                System.out.println(remainder);
            }
        }
    } 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 remainder = N % K; remainder N % K N K if (remainder == 0) {
            System.out.println(0);
        } else {
            if (remainder > (K - remainder)) {
                System.out.println(K - remainder);
            } else {
                System.out.println(remainder);
            }
        } remainder == 0 remainder 0 {
            System.out.println(0);
        } System.out.println(0) System.out.println System.out System System.out 0 {
            if (remainder > (K - remainder)) {
                System.out.println(K - remainder);
            } else {
                System.out.println(remainder);
            }
        } if (remainder > (K - remainder)) {
                System.out.println(K - remainder);
            } else {
                System.out.println(remainder);
            } remainder > (K - remainder) remainder (K - remainder) K remainder {
                System.out.println(K - remainder);
            } System.out.println(K - remainder) System.out.println System.out System System.out K - remainder K remainder {
                System.out.println(remainder);
            } System.out.println(remainder) System.out.println System.out System System.out remainder String[] args args
import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        long remainder = N % K;

        if (remainder == 0) {
            System.out.println(0);
        } else {
            if (remainder > (K - remainder)) {
                System.out.println(K - remainder);
            } else {
                System.out.println(remainder);
            }
        }
    }
}