import java.util.*;

class Main{
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        if(n==0 || n==k){
            System.out.println(0);
            return;
        }

        long min = n;
        if(n > k){
            n = Math.abs(n - (n/k - 1)*k);
        }
        else if(n<k){
            n = Math.abs(n - (k/n - 1)*n);
        }
        n = Math.min(n, min);
        min = n;
        while(true){
            long tmp = Math.abs(n-k);
            if(tmp < min){
                min = tmp;
                n = tmp;
            }
            else{
                break;
            }
        }

        System.out.println(min);
    }
} import java.util.*; util.* java class Main{
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        if(n==0 || n==k){
            System.out.println(0);
            return;
        }

        long min = n;
        if(n > k){
            n = Math.abs(n - (n/k - 1)*k);
        }
        else if(n<k){
            n = Math.abs(n - (k/n - 1)*n);
        }
        n = Math.min(n, min);
        min = n;
        while(true){
            long tmp = Math.abs(n-k);
            if(tmp < min){
                min = tmp;
                n = tmp;
            }
            else{
                break;
            }
        }

        System.out.println(min);
    }
} Main public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        if(n==0 || n==k){
            System.out.println(0);
            return;
        }

        long min = n;
        if(n > k){
            n = Math.abs(n - (n/k - 1)*k);
        }
        else if(n<k){
            n = Math.abs(n - (k/n - 1)*n);
        }
        n = Math.min(n, min);
        min = n;
        while(true){
            long tmp = Math.abs(n-k);
            if(tmp < min){
                min = tmp;
                n = tmp;
            }
            else{
                break;
            }
        }

        System.out.println(min);
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        if(n==0 || n==k){
            System.out.println(0);
            return;
        }

        long min = n;
        if(n > k){
            n = Math.abs(n - (n/k - 1)*k);
        }
        else if(n<k){
            n = Math.abs(n - (k/n - 1)*n);
        }
        n = Math.min(n, min);
        min = n;
        while(true){
            long tmp = Math.abs(n-k);
            if(tmp < min){
                min = tmp;
                n = tmp;
            }
            else{
                break;
            }
        }

        System.out.println(min);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n = sc.nextLong(); n sc.nextLong() sc.nextLong sc long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc if(n==0 || n==k){
            System.out.println(0);
            return;
        } n==0 || n==k n==0 n 0 n==k n k {
            System.out.println(0);
            return;
        } System.out.println(0) System.out.println System.out System System.out 0 return; long min = n; min n if(n > k){
            n = Math.abs(n - (n/k - 1)*k);
        }
        else if(n<k){
            n = Math.abs(n - (k/n - 1)*n);
        } n > k n k {
            n = Math.abs(n - (n/k - 1)*k);
        } n = Math.abs(n - (n/k - 1)*k) n Math.abs(n - (n/k - 1)*k) Math.abs Math n - (n/k - 1)*k n (n/k - 1)*k (n/k - 1) n/k n k 1 k if(n<k){
            n = Math.abs(n - (k/n - 1)*n);
        } n<k n k {
            n = Math.abs(n - (k/n - 1)*n);
        } n = Math.abs(n - (k/n - 1)*n) n Math.abs(n - (k/n - 1)*n) Math.abs Math n - (k/n - 1)*n n (k/n - 1)*n (k/n - 1) k/n k n 1 n n = Math.min(n, min) n Math.min(n, min) Math.min Math n min min = n min n while(true){
            long tmp = Math.abs(n-k);
            if(tmp < min){
                min = tmp;
                n = tmp;
            }
            else{
                break;
            }
        } true {
            long tmp = Math.abs(n-k);
            if(tmp < min){
                min = tmp;
                n = tmp;
            }
            else{
                break;
            }
        } long tmp = Math.abs(n-k); tmp Math.abs(n-k) Math.abs Math n-k n k if(tmp < min){
                min = tmp;
                n = tmp;
            }
            else{
                break;
            } tmp < min tmp min {
                min = tmp;
                n = tmp;
            } min = tmp min tmp n = tmp n tmp {
                break;
            } break; System.out.println(min) System.out.println System.out System System.out min String args[] args
import java.util.*;

class Main{
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        if(n==0 || n==k){
            System.out.println(0);
            return;
        }

        long min = n;
        if(n > k){
            n = Math.abs(n - (n/k - 1)*k);
        }
        else if(n<k){
            n = Math.abs(n - (k/n - 1)*n);
        }
        n = Math.min(n, min);
        min = n;
        while(true){
            long tmp = Math.abs(n-k);
            if(tmp < min){
                min = tmp;
                n = tmp;
            }
            else{
                break;
            }
        }

        System.out.println(min);
    }
}
