import java.util.*;
import java.lang.Math;
class Main{
    static Scanner sc=new Scanner(System.in);
    public static void main(String[] args){
        long N = sc.nextLong();
        long K = sc.nextLong();
        long t = N/K;
        long temp1=Math.abs(N-K*t);
        long temp2=Math.abs(temp1-K);
        System.out.println((temp1<=temp2) ? temp1 : temp2);
    }
} import java.util.*; util.* java import java.lang.Math; Math java.lang class Main{
    static Scanner sc=new Scanner(System.in);
    public static void main(String[] args){
        long N = sc.nextLong();
        long K = sc.nextLong();
        long t = N/K;
        long temp1=Math.abs(N-K*t);
        long temp2=Math.abs(temp1-K);
        System.out.println((temp1<=temp2) ? temp1 : temp2);
    }
} Main static Scanner sc=new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args){
        long N = sc.nextLong();
        long K = sc.nextLong();
        long t = N/K;
        long temp1=Math.abs(N-K*t);
        long temp2=Math.abs(temp1-K);
        System.out.println((temp1<=temp2) ? temp1 : temp2);
    } main {
        long N = sc.nextLong();
        long K = sc.nextLong();
        long t = N/K;
        long temp1=Math.abs(N-K*t);
        long temp2=Math.abs(temp1-K);
        System.out.println((temp1<=temp2) ? temp1 : temp2);
    } long N = sc.nextLong(); N sc.nextLong() sc.nextLong sc long K = sc.nextLong(); K sc.nextLong() sc.nextLong sc long t = N/K; t N/K N K long temp1=Math.abs(N-K*t); temp1 Math.abs(N-K*t) Math.abs Math N-K*t N K*t K t long temp2=Math.abs(temp1-K); temp2 Math.abs(temp1-K) Math.abs Math temp1-K temp1 K System.out.println((temp1<=temp2) ? temp1 : temp2) System.out.println System.out System System.out (temp1<=temp2) ? temp1 : temp2 (temp1<=temp2) temp1 temp2 temp1 temp2 String[] args args
import java.util.*;
import java.lang.Math;
class Main{
    static Scanner sc=new Scanner(System.in);
    public static void main(String[] args){
        long N = sc.nextLong();
        long K = sc.nextLong();
        long t = N/K;
        long temp1=Math.abs(N-K*t);
        long temp2=Math.abs(temp1-K);
        System.out.println((temp1<=temp2) ? temp1 : temp2);
    }
}