
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);

        long N = scanner.nextLong();
        long K = scanner.nextLong();
        long min = N;

        N %= K;
        while (true) {
            long next = Math.abs(N - K);
            if (min > next) {
                min = next;
            } else {
                break;
            }
            N = next;
        }
        System.out.println(N);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);

        long N = scanner.nextLong();
        long K = scanner.nextLong();
        long min = N;

        N %= K;
        while (true) {
            long next = Math.abs(N - K);
            if (min > next) {
                min = next;
            } else {
                break;
            }
            N = next;
        }
        System.out.println(N);
    }
} Main public static void main(String[] args) {
        new Main().run();
    } main {
        new Main().run();
    } new Main().run() new Main().run new Main() String[] args args private void run() {
        Scanner scanner = new Scanner(System.in);

        long N = scanner.nextLong();
        long K = scanner.nextLong();
        long min = N;

        N %= K;
        while (true) {
            long next = Math.abs(N - K);
            if (min > next) {
                min = next;
            } else {
                break;
            }
            N = next;
        }
        System.out.println(N);
    } run {
        Scanner scanner = new Scanner(System.in);

        long N = scanner.nextLong();
        long K = scanner.nextLong();
        long min = N;

        N %= K;
        while (true) {
            long next = Math.abs(N - K);
            if (min > next) {
                min = next;
            } else {
                break;
            }
            N = next;
        }
        System.out.println(N);
    } 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 long min = N; min N N %= K N K while (true) {
            long next = Math.abs(N - K);
            if (min > next) {
                min = next;
            } else {
                break;
            }
            N = next;
        } true {
            long next = Math.abs(N - K);
            if (min > next) {
                min = next;
            } else {
                break;
            }
            N = next;
        } long next = Math.abs(N - K); next Math.abs(N - K) Math.abs Math N - K N K if (min > next) {
                min = next;
            } else {
                break;
            } min > next min next {
                min = next;
            } min = next min next {
                break;
            } break; N = next N next System.out.println(N) System.out.println System.out System System.out N public class Main {
    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);

        long N = scanner.nextLong();
        long K = scanner.nextLong();
        long min = N;

        N %= K;
        while (true) {
            long next = Math.abs(N - K);
            if (min > next) {
                min = next;
            } else {
                break;
            }
            N = next;
        }
        System.out.println(N);
    }
} public class Main {
    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);

        long N = scanner.nextLong();
        long K = scanner.nextLong();
        long min = N;

        N %= K;
        while (true) {
            long next = Math.abs(N - K);
            if (min > next) {
                min = next;
            } else {
                break;
            }
            N = next;
        }
        System.out.println(N);
    }
} Main

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);

        long N = scanner.nextLong();
        long K = scanner.nextLong();
        long min = N;

        N %= K;
        while (true) {
            long next = Math.abs(N - K);
            if (min > next) {
                min = next;
            } else {
                break;
            }
            N = next;
        }
        System.out.println(N);
    }
}
