import java.util.Scanner;

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

        long answer = N - ((N / K) * K);
        if (answer != 0) {
            long check = answer - K;
            if (check < 0) {
                check *= -1;
            }
            if (check < answer) {
                answer = check;
            }
        }
        System.out.println(answer);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();

        long answer = N - ((N / K) * K);
        if (answer != 0) {
            long check = answer - K;
            if (check < 0) {
                check *= -1;
            }
            if (check < answer) {
                answer = check;
            }
        }
        System.out.println(answer);
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();

        long answer = N - ((N / K) * K);
        if (answer != 0) {
            long check = answer - K;
            if (check < 0) {
                check *= -1;
            }
            if (check < answer) {
                answer = check;
            }
        }
        System.out.println(answer);
    } main {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();

        long answer = N - ((N / K) * K);
        if (answer != 0) {
            long check = answer - K;
            if (check < 0) {
                check *= -1;
            }
            if (check < answer) {
                answer = check;
            }
        }
        System.out.println(answer);
    } 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 answer = N - ((N / K) * K); answer N - ((N / K) * K) N ((N / K) * K) (N / K) N K K if (answer != 0) {
            long check = answer - K;
            if (check < 0) {
                check *= -1;
            }
            if (check < answer) {
                answer = check;
            }
        } answer != 0 answer 0 {
            long check = answer - K;
            if (check < 0) {
                check *= -1;
            }
            if (check < answer) {
                answer = check;
            }
        } long check = answer - K; check answer - K answer K if (check < 0) {
                check *= -1;
            } check < 0 check 0 {
                check *= -1;
            } check *= -1 check -1 1 if (check < answer) {
                answer = check;
            } check < answer check answer {
                answer = check;
            } answer = check answer check System.out.println(answer) System.out.println System.out System System.out answer String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();

        long answer = N - ((N / K) * K);
        if (answer != 0) {
            long check = answer - K;
            if (check < 0) {
                check *= -1;
            }
            if (check < answer) {
                answer = check;
            }
        }
        System.out.println(answer);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();

        long answer = N - ((N / K) * K);
        if (answer != 0) {
            long check = answer - K;
            if (check < 0) {
                check *= -1;
            }
            if (check < answer) {
                answer = check;
            }
        }
        System.out.println(answer);
    }
} Main
import java.util.Scanner;

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

        long answer = N - ((N / K) * K);
        if (answer != 0) {
            long check = answer - K;
            if (check < 0) {
                check *= -1;
            }
            if (check < answer) {
                answer = check;
            }
        }
        System.out.println(answer);
    }
}
