import java.util.*;

class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        scan.close();

        if(k == 1 || n == k) System.out.println("0");

        else if(k == 2){
            if(n % k == 0) System.out.println("0");
            else System.out.println("1");
        }

        else{
            while(true){
                if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k);
            }
            System.out.println(n);
        }
    }
} import java.util.*; util.* java class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        scan.close();

        if(k == 1 || n == k) System.out.println("0");

        else if(k == 2){
            if(n % k == 0) System.out.println("0");
            else System.out.println("1");
        }

        else{
            while(true){
                if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k);
            }
            System.out.println(n);
        }
    }
} Main public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        scan.close();

        if(k == 1 || n == k) System.out.println("0");

        else if(k == 2){
            if(n % k == 0) System.out.println("0");
            else System.out.println("1");
        }

        else{
            while(true){
                if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k);
            }
            System.out.println(n);
        }
    } main {
        Scanner scan = new Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        scan.close();

        if(k == 1 || n == k) System.out.println("0");

        else if(k == 2){
            if(n % k == 0) System.out.println("0");
            else System.out.println("1");
        }

        else{
            while(true){
                if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k);
            }
            System.out.println(n);
        }
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long n = scan.nextLong(); n scan.nextLong() scan.nextLong scan long k = scan.nextLong(); k scan.nextLong() scan.nextLong scan scan.close() scan.close scan if(k == 1 || n == k) System.out.println("0");

        else if(k == 2){
            if(n % k == 0) System.out.println("0");
            else System.out.println("1");
        }

        else{
            while(true){
                if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k);
            }
            System.out.println(n);
        } k == 1 || n == k k == 1 k 1 n == k n k System.out.println("0") System.out.println System.out System System.out "0" if(k == 2){
            if(n % k == 0) System.out.println("0");
            else System.out.println("1");
        }

        else{
            while(true){
                if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k);
            }
            System.out.println(n);
        } k == 2 k 2 {
            if(n % k == 0) System.out.println("0");
            else System.out.println("1");
        } if(n % k == 0) System.out.println("0");
            else System.out.println("1"); n % k == 0 n % k n k 0 System.out.println("0") System.out.println System.out System System.out "0" System.out.println("1") System.out.println System.out System System.out "1" {
            while(true){
                if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k);
            }
            System.out.println(n);
        } while(true){
                if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k);
            } true {
                if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k);
            } if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k); n * 2 <= k n * 2 n 2 k break; if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k); Math.abs(n - k) > n Math.abs(n - k) Math.abs Math n - k n k n break; if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k); n >= (long)Math.pow(k,2) n (long)Math.pow(k,2) Math.pow Math k 2 n = n % k n n % k n k n = Math.abs(n - k) n Math.abs(n - k) Math.abs Math n - k n k System.out.println(n) System.out.println System.out System System.out n String[] args args
import java.util.*;

class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        scan.close();

        if(k == 1 || n == k) System.out.println("0");

        else if(k == 2){
            if(n % k == 0) System.out.println("0");
            else System.out.println("1");
        }

        else{
            while(true){
                if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k);
            }
            System.out.println(n);
        }
    }
}