import java.util.*;
 
public class Main{
    public static  void main(String[]args){
        Scanner sc = new Scanner(System.in);
        long N=0;
        long K=0;
        N=sc.nextLong();
        K=sc.nextLong();
        long m=0;
        m=N/K;
        long min=0;
        min=Math.abs(N-m*K);
        if(min>Math.abs(N-(m-1)*K))
        min=Math.abs(N-(m-1)*K);
        else if(min>Math.abs(N-(m+1)*K))
        min=Math.abs(N-(m+1)*K);
        System.out.println(min);
    }
} import java.util.*; util.* java public class Main{
    public static  void main(String[]args){
        Scanner sc = new Scanner(System.in);
        long N=0;
        long K=0;
        N=sc.nextLong();
        K=sc.nextLong();
        long m=0;
        m=N/K;
        long min=0;
        min=Math.abs(N-m*K);
        if(min>Math.abs(N-(m-1)*K))
        min=Math.abs(N-(m-1)*K);
        else if(min>Math.abs(N-(m+1)*K))
        min=Math.abs(N-(m+1)*K);
        System.out.println(min);
    }
} Main public static  void main(String[]args){
        Scanner sc = new Scanner(System.in);
        long N=0;
        long K=0;
        N=sc.nextLong();
        K=sc.nextLong();
        long m=0;
        m=N/K;
        long min=0;
        min=Math.abs(N-m*K);
        if(min>Math.abs(N-(m-1)*K))
        min=Math.abs(N-(m-1)*K);
        else if(min>Math.abs(N-(m+1)*K))
        min=Math.abs(N-(m+1)*K);
        System.out.println(min);
    } main {
        Scanner sc = new Scanner(System.in);
        long N=0;
        long K=0;
        N=sc.nextLong();
        K=sc.nextLong();
        long m=0;
        m=N/K;
        long min=0;
        min=Math.abs(N-m*K);
        if(min>Math.abs(N-(m-1)*K))
        min=Math.abs(N-(m-1)*K);
        else if(min>Math.abs(N-(m+1)*K))
        min=Math.abs(N-(m+1)*K);
        System.out.println(min);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long N=0; N 0 long K=0; K 0 N=sc.nextLong() N sc.nextLong() sc.nextLong sc K=sc.nextLong() K sc.nextLong() sc.nextLong sc long m=0; m 0 m=N/K m N/K N K long min=0; min 0 min=Math.abs(N-m*K) min Math.abs(N-m*K) Math.abs Math N-m*K N m*K m K if(min>Math.abs(N-(m-1)*K))
        min=Math.abs(N-(m-1)*K);
        else if(min>Math.abs(N-(m+1)*K))
        min=Math.abs(N-(m+1)*K); min>Math.abs(N-(m-1)*K) min Math.abs(N-(m-1)*K) Math.abs Math N-(m-1)*K N (m-1)*K (m-1) m 1 K min=Math.abs(N-(m-1)*K) min Math.abs(N-(m-1)*K) Math.abs Math N-(m-1)*K N (m-1)*K (m-1) m 1 K if(min>Math.abs(N-(m+1)*K))
        min=Math.abs(N-(m+1)*K); min>Math.abs(N-(m+1)*K) min Math.abs(N-(m+1)*K) Math.abs Math N-(m+1)*K N (m+1)*K (m+1) m 1 K min=Math.abs(N-(m+1)*K) min Math.abs(N-(m+1)*K) Math.abs Math N-(m+1)*K N (m+1)*K (m+1) m 1 K System.out.println(min) System.out.println System.out System System.out min String[]args args public class Main{
    public static  void main(String[]args){
        Scanner sc = new Scanner(System.in);
        long N=0;
        long K=0;
        N=sc.nextLong();
        K=sc.nextLong();
        long m=0;
        m=N/K;
        long min=0;
        min=Math.abs(N-m*K);
        if(min>Math.abs(N-(m-1)*K))
        min=Math.abs(N-(m-1)*K);
        else if(min>Math.abs(N-(m+1)*K))
        min=Math.abs(N-(m+1)*K);
        System.out.println(min);
    }
} public class Main{
    public static  void main(String[]args){
        Scanner sc = new Scanner(System.in);
        long N=0;
        long K=0;
        N=sc.nextLong();
        K=sc.nextLong();
        long m=0;
        m=N/K;
        long min=0;
        min=Math.abs(N-m*K);
        if(min>Math.abs(N-(m-1)*K))
        min=Math.abs(N-(m-1)*K);
        else if(min>Math.abs(N-(m+1)*K))
        min=Math.abs(N-(m+1)*K);
        System.out.println(min);
    }
} Main
import java.util.*;
 
public class Main{
    public static  void main(String[]args){
        Scanner sc = new Scanner(System.in);
        long N=0;
        long K=0;
        N=sc.nextLong();
        K=sc.nextLong();
        long m=0;
        m=N/K;
        long min=0;
        min=Math.abs(N-m*K);
        if(min>Math.abs(N-(m-1)*K))
        min=Math.abs(N-(m-1)*K);
        else if(min>Math.abs(N-(m+1)*K))
        min=Math.abs(N-(m+1)*K);
        System.out.println(min);
    }
}