import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    long n;
    long k;
    long res;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        a(n%k);
        System.out.println(res);
    }
    
    private void a(long a) {
        if (Math.abs(a - k) <= a) {
            a = Math.abs(a - k);
            a(a);
        } else {
            res = a;
            return;
        }
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    long n;
    long k;
    long res;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        a(n%k);
        System.out.println(res);
    }
    
    private void a(long a) {
        if (Math.abs(a - k) <= a) {
            a = Math.abs(a - k);
            a(a);
        } else {
            res = a;
            return;
        }
    }
} Main // 変数一覧
    long n; n long k; k long res; res public static void main(String[] args) {
        Main main = new Main();
        main.run();
    } main {
        Main main = new Main();
        main.run();
    } Main main = new Main(); main new Main() main.run() main.run main String[] args args public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    } run {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) n = sc.nextLong() n sc.nextLong() sc.nextLong sc k = sc.nextLong() k sc.nextLong() sc.nextLong sc sc.close() sc.close sc solve() solve private void solve() {
        a(n%k);
        System.out.println(res);
    } solve {
        a(n%k);
        System.out.println(res);
    } a(n%k) a n%k n k System.out.println(res) System.out.println System.out System System.out res private void a(long a) {
        if (Math.abs(a - k) <= a) {
            a = Math.abs(a - k);
            a(a);
        } else {
            res = a;
            return;
        }
    } a {
        if (Math.abs(a - k) <= a) {
            a = Math.abs(a - k);
            a(a);
        } else {
            res = a;
            return;
        }
    } if (Math.abs(a - k) <= a) {
            a = Math.abs(a - k);
            a(a);
        } else {
            res = a;
            return;
        } Math.abs(a - k) <= a Math.abs(a - k) Math.abs Math a - k a k a {
            a = Math.abs(a - k);
            a(a);
        } a = Math.abs(a - k) a Math.abs(a - k) Math.abs Math a - k a k a(a) a a {
            res = a;
            return;
        } res = a res a return; long a a public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    long n;
    long k;
    long res;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        a(n%k);
        System.out.println(res);
    }
    
    private void a(long a) {
        if (Math.abs(a - k) <= a) {
            a = Math.abs(a - k);
            a(a);
        } else {
            res = a;
            return;
        }
    }
} public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    long n;
    long k;
    long res;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        a(n%k);
        System.out.println(res);
    }
    
    private void a(long a) {
        if (Math.abs(a - k) <= a) {
            a = Math.abs(a - k);
            a(a);
        } else {
            res = a;
            return;
        }
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    long n;
    long k;
    long res;

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextLong();
        k = sc.nextLong();
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        a(n%k);
        System.out.println(res);
    }
    
    private void a(long a) {
        if (Math.abs(a - k) <= a) {
            a = Math.abs(a - k);
            a(a);
        } else {
            res = a;
            return;
        }
    }
}