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();
        if(k == 1 || n == k){
            System.out.println("0");
        }else if(n > k){
            long t = n % k;
            long t1 = k - t;
            System.out.println(Long.min(t, t1));
        }else{
            long t = k - n;
            System.out.println(Long.min(t, n));
        }
    }
} 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();
        if(k == 1 || n == k){
            System.out.println("0");
        }else if(n > k){
            long t = n % k;
            long t1 = k - t;
            System.out.println(Long.min(t, t1));
        }else{
            long t = k - n;
            System.out.println(Long.min(t, n));
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        if(k == 1 || n == k){
            System.out.println("0");
        }else if(n > k){
            long t = n % k;
            long t1 = k - t;
            System.out.println(Long.min(t, t1));
        }else{
            long t = k - n;
            System.out.println(Long.min(t, n));
        }
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        if(k == 1 || n == k){
            System.out.println("0");
        }else if(n > k){
            long t = n % k;
            long t1 = k - t;
            System.out.println(Long.min(t, t1));
        }else{
            long t = k - n;
            System.out.println(Long.min(t, n));
        }
    } 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(k == 1 || n == k){
            System.out.println("0");
        }else if(n > k){
            long t = n % k;
            long t1 = k - t;
            System.out.println(Long.min(t, t1));
        }else{
            long t = k - n;
            System.out.println(Long.min(t, n));
        } k == 1 || n == k k == 1 k 1 n == k n k {
            System.out.println("0");
        } System.out.println("0") System.out.println System.out System System.out "0" if(n > k){
            long t = n % k;
            long t1 = k - t;
            System.out.println(Long.min(t, t1));
        }else{
            long t = k - n;
            System.out.println(Long.min(t, n));
        } n > k n k {
            long t = n % k;
            long t1 = k - t;
            System.out.println(Long.min(t, t1));
        } long t = n % k; t n % k n k long t1 = k - t; t1 k - t k t System.out.println(Long.min(t, t1)) System.out.println System.out System System.out Long.min(t, t1) Long.min Long t t1 {
            long t = k - n;
            System.out.println(Long.min(t, n));
        } long t = k - n; t k - n k n System.out.println(Long.min(t, n)) System.out.println System.out System System.out Long.min(t, n) Long.min Long t n 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();
        if(k == 1 || n == k){
            System.out.println("0");
        }else if(n > k){
            long t = n % k;
            long t1 = k - t;
            System.out.println(Long.min(t, t1));
        }else{
            long t = k - n;
            System.out.println(Long.min(t, n));
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        if(k == 1 || n == k){
            System.out.println("0");
        }else if(n > k){
            long t = n % k;
            long t1 = k - t;
            System.out.println(Long.min(t, t1));
        }else{
            long t = k - n;
            System.out.println(Long.min(t, n));
        }
    }
} 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();
        if(k == 1 || n == k){
            System.out.println("0");
        }else if(n > k){
            long t = n % k;
            long t1 = k - t;
            System.out.println(Long.min(t, t1));
        }else{
            long t = k - n;
            System.out.println(Long.min(t, n));
        }
    }
}