import 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();
        sc.close();
        long min = n % k;
        long res = (min > Math.abs(min-k)) ? Math.abs(min-k) : min;
        
        System.out.println(res);
    }
} import java.util.*; util.* java public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();
        long min = n % k;
        long res = (min > Math.abs(min-k)) ? Math.abs(min-k) : min;
        
        System.out.println(res);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();
        long min = n % k;
        long res = (min > Math.abs(min-k)) ? Math.abs(min-k) : min;
        
        System.out.println(res);
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();
        long min = n % k;
        long res = (min > Math.abs(min-k)) ? Math.abs(min-k) : min;
        
        System.out.println(res);
    } 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 sc.close() sc.close sc long min = n % k; min n % k n k long res = (min > Math.abs(min-k)) ? Math.abs(min-k) : min; res (min > Math.abs(min-k)) ? Math.abs(min-k) : min (min > Math.abs(min-k)) min Math.abs(min-k) Math.abs Math min-k min k Math.abs(min-k) Math.abs Math min-k min k min System.out.println(res) System.out.println System.out System System.out res 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();
        sc.close();
        long min = n % k;
        long res = (min > Math.abs(min-k)) ? Math.abs(min-k) : min;
        
        System.out.println(res);
    }
} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();
        long min = n % k;
        long res = (min > Math.abs(min-k)) ? Math.abs(min-k) : min;
        
        System.out.println(res);
    }
} Main
import 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();
        sc.close();
        long min = n % k;
        long res = (min > Math.abs(min-k)) ? Math.abs(min-k) : min;
        
        System.out.println(res);
    }
}