import java.util.Scanner;

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

            long min = N;
            min = Math.min(min, N % K);
            min = Math.min(min, (N % K) + K);
            min = Math.min(min, Math.abs((N % K) - K));
            System.out.println(min);
        }
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            long N = sc.nextLong();
            long K = sc.nextLong();

            long min = N;
            min = Math.min(min, N % K);
            min = Math.min(min, (N % K) + K);
            min = Math.min(min, Math.abs((N % K) - K));
            System.out.println(min);
        }
    }
} Main public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            long N = sc.nextLong();
            long K = sc.nextLong();

            long min = N;
            min = Math.min(min, N % K);
            min = Math.min(min, (N % K) + K);
            min = Math.min(min, Math.abs((N % K) - K));
            System.out.println(min);
        }
    } main {
        try (Scanner sc = new Scanner(System.in)) {
            long N = sc.nextLong();
            long K = sc.nextLong();

            long min = N;
            min = Math.min(min, N % K);
            min = Math.min(min, (N % K) + K);
            min = Math.min(min, Math.abs((N % K) - K));
            System.out.println(min);
        }
    } try (Scanner sc = new Scanner(System.in)) {
            long N = sc.nextLong();
            long K = sc.nextLong();

            long min = N;
            min = Math.min(min, N % K);
            min = Math.min(min, (N % K) + K);
            min = Math.min(min, Math.abs((N % K) - K));
            System.out.println(min);
        } {
            long N = sc.nextLong();
            long K = sc.nextLong();

            long min = N;
            min = Math.min(min, N % K);
            min = Math.min(min, (N % K) + K);
            min = Math.min(min, Math.abs((N % K) - K));
            System.out.println(min);
        } Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) 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 min = N; min N min = Math.min(min, N % K) min Math.min(min, N % K) Math.min Math min N % K N K min = Math.min(min, (N % K) + K) min Math.min(min, (N % K) + K) Math.min Math min (N % K) + K (N % K) N K K min = Math.min(min, Math.abs((N % K) - K)) min Math.min(min, Math.abs((N % K) - K)) Math.min Math min Math.abs((N % K) - K) Math.abs Math (N % K) - K (N % K) N K K System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            long N = sc.nextLong();
            long K = sc.nextLong();

            long min = N;
            min = Math.min(min, N % K);
            min = Math.min(min, (N % K) + K);
            min = Math.min(min, Math.abs((N % K) - K));
            System.out.println(min);
        }
    }
} public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            long N = sc.nextLong();
            long K = sc.nextLong();

            long min = N;
            min = Math.min(min, N % K);
            min = Math.min(min, (N % K) + K);
            min = Math.min(min, Math.abs((N % K) - K));
            System.out.println(min);
        }
    }
} Main
import java.util.Scanner;

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

            long min = N;
            min = Math.min(min, N % K);
            min = Math.min(min, (N % K) + K);
            min = Math.min(min, Math.abs((N % K) - K));
            System.out.println(min);
        }
    }
}
