import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
//        sc.useDelimiter("\n");
//        DecimalFormat df = new DecimalFormat("0.00000000");
        
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long ans = n % k;
        long temp;
        while (true) {
            n = Math.abs(ans - k);
            if (ans < n) {
                break;
            } else {
                temp = ans;     ans = n;    n = temp;
            }
        }
        pw.println(ans);
        pw.flush();
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
//        sc.useDelimiter("\n");
//        DecimalFormat df = new DecimalFormat("0.00000000");
        
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long ans = n % k;
        long temp;
        while (true) {
            n = Math.abs(ans - k);
            if (ans < n) {
                break;
            } else {
                temp = ans;     ans = n;    n = temp;
            }
        }
        pw.println(ans);
        pw.flush();
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
//        sc.useDelimiter("\n");
//        DecimalFormat df = new DecimalFormat("0.00000000");
        
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long ans = n % k;
        long temp;
        while (true) {
            n = Math.abs(ans - k);
            if (ans < n) {
                break;
            } else {
                temp = ans;     ans = n;    n = temp;
            }
        }
        pw.println(ans);
        pw.flush();
    } main {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
//        sc.useDelimiter("\n");
//        DecimalFormat df = new DecimalFormat("0.00000000");
        
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long ans = n % k;
        long temp;
        while (true) {
            n = Math.abs(ans - k);
            if (ans < n) {
                break;
            } else {
                temp = ans;     ans = n;    n = temp;
            }
        }
        pw.println(ans);
        pw.flush();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) PrintWriter pw = new PrintWriter(System.out); pw new PrintWriter(System.out) long n = Long.parseLong(sc.next()); n Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long k = Long.parseLong(sc.next()); k Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long ans = n % k; ans n % k n k long temp; temp while (true) {
            n = Math.abs(ans - k);
            if (ans < n) {
                break;
            } else {
                temp = ans;     ans = n;    n = temp;
            }
        } true {
            n = Math.abs(ans - k);
            if (ans < n) {
                break;
            } else {
                temp = ans;     ans = n;    n = temp;
            }
        } n = Math.abs(ans - k) n Math.abs(ans - k) Math.abs Math ans - k ans k if (ans < n) {
                break;
            } else {
                temp = ans;     ans = n;    n = temp;
            } ans < n ans n {
                break;
            } break; {
                temp = ans;     ans = n;    n = temp;
            } temp = ans temp ans ans = n ans n n = temp n temp pw.println(ans) pw.println pw ans pw.flush() pw.flush pw String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
//        sc.useDelimiter("\n");
//        DecimalFormat df = new DecimalFormat("0.00000000");
        
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long ans = n % k;
        long temp;
        while (true) {
            n = Math.abs(ans - k);
            if (ans < n) {
                break;
            } else {
                temp = ans;     ans = n;    n = temp;
            }
        }
        pw.println(ans);
        pw.flush();
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
//        sc.useDelimiter("\n");
//        DecimalFormat df = new DecimalFormat("0.00000000");
        
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long ans = n % k;
        long temp;
        while (true) {
            n = Math.abs(ans - k);
            if (ans < n) {
                break;
            } else {
                temp = ans;     ans = n;    n = temp;
            }
        }
        pw.println(ans);
        pw.flush();
    }
} Main
import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
//        sc.useDelimiter("\n");
//        DecimalFormat df = new DecimalFormat("0.00000000");
        
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long ans = n % k;
        long temp;
        while (true) {
            n = Math.abs(ans - k);
            if (ans < n) {
                break;
            } else {
                temp = ans;     ans = n;    n = temp;
            }
        }
        pw.println(ans);
        pw.flush();
    }
}