import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
   
        long diff = Math.abs(n - k);
        if(diff == 0) {
            System.out.println("0");
            return;
        }
        long a = n % k;
        long b = k - a;

        System.out.println(Math.min(a, b));
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
   
        long diff = Math.abs(n - k);
        if(diff == 0) {
            System.out.println("0");
            return;
        }
        long a = n % k;
        long b = k - a;

        System.out.println(Math.min(a, b));
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
   
        long diff = Math.abs(n - k);
        if(diff == 0) {
            System.out.println("0");
            return;
        }
        long a = n % k;
        long b = k - a;

        System.out.println(Math.min(a, b));
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
   
        long diff = Math.abs(n - k);
        if(diff == 0) {
            System.out.println("0");
            return;
        }
        long a = n % k;
        long b = k - a;

        System.out.println(Math.min(a, b));
    } 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 long diff = Math.abs(n - k); diff Math.abs(n - k) Math.abs Math n - k n k if(diff == 0) {
            System.out.println("0");
            return;
        } diff == 0 diff 0 {
            System.out.println("0");
            return;
        } System.out.println("0") System.out.println System.out System System.out "0" return; long a = n % k; a n % k n k long b = k - a; b k - a k a System.out.println(Math.min(a, b)) System.out.println System.out System System.out Math.min(a, b) Math.min Math a b String[] args args public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
   
        long diff = Math.abs(n - k);
        if(diff == 0) {
            System.out.println("0");
            return;
        }
        long a = n % k;
        long b = k - a;

        System.out.println(Math.min(a, b));
    }
} public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
   
        long diff = Math.abs(n - k);
        if(diff == 0) {
            System.out.println("0");
            return;
        }
        long a = n % k;
        long b = k - a;

        System.out.println(Math.min(a, b));
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
   
        long diff = Math.abs(n - k);
        if(diff == 0) {
            System.out.println("0");
            return;
        }
        long a = n % k;
        long b = k - a;

        System.out.println(Math.min(a, b));
    }
}
