import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner scan = new java.util.Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        n -= k * (n / k - 1);
        System.out.println(Math.min(Math.abs(n - k), Math.abs(n - k * 2)));
    }
} import java.util.*; util.* java public class Main{
    public static void main(String[] args){
        Scanner scan = new java.util.Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        n -= k * (n / k - 1);
        System.out.println(Math.min(Math.abs(n - k), Math.abs(n - k * 2)));
    }
} Main public static void main(String[] args){
        Scanner scan = new java.util.Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        n -= k * (n / k - 1);
        System.out.println(Math.min(Math.abs(n - k), Math.abs(n - k * 2)));
    } main {
        Scanner scan = new java.util.Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        n -= k * (n / k - 1);
        System.out.println(Math.min(Math.abs(n - k), Math.abs(n - k * 2)));
    } Scanner scan = new java.util.Scanner(System.in); scan new java.util.Scanner(System.in) long n = scan.nextLong(); n scan.nextLong() scan.nextLong scan long k = scan.nextLong(); k scan.nextLong() scan.nextLong scan n -= k * (n / k - 1) n k * (n / k - 1) k (n / k - 1) n / k n k 1 System.out.println(Math.min(Math.abs(n - k), Math.abs(n - k * 2))) System.out.println System.out System System.out Math.min(Math.abs(n - k), Math.abs(n - k * 2)) Math.min Math Math.abs(n - k) Math.abs Math n - k n k Math.abs(n - k * 2) Math.abs Math n - k * 2 n k * 2 k 2 String[] args args public class Main{
    public static void main(String[] args){
        Scanner scan = new java.util.Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        n -= k * (n / k - 1);
        System.out.println(Math.min(Math.abs(n - k), Math.abs(n - k * 2)));
    }
} public class Main{
    public static void main(String[] args){
        Scanner scan = new java.util.Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        n -= k * (n / k - 1);
        System.out.println(Math.min(Math.abs(n - k), Math.abs(n - k * 2)));
    }
} Main
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner scan = new java.util.Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        n -= k * (n / k - 1);
        System.out.println(Math.min(Math.abs(n - k), Math.abs(n - k * 2)));
    }
}