import java.util.*; 
import java.lang.Math;
class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        System.out.println(Math.min(N%K, Math.abs(N%K-K)));
    }
} import java.util.*; util.* java import java.lang.Math; Math java.lang class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        System.out.println(Math.min(N%K, Math.abs(N%K-K)));
    }
} Main public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        System.out.println(Math.min(N%K, Math.abs(N%K-K)));
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        System.out.println(Math.min(N%K, Math.abs(N%K-K)));
    } 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 System.out.println(Math.min(N%K, Math.abs(N%K-K))) System.out.println System.out System System.out Math.min(N%K, Math.abs(N%K-K)) Math.min Math N%K N K Math.abs(N%K-K) Math.abs Math N%K-K N%K N K K String args[] args
import java.util.*; 
import java.lang.Math;
class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        System.out.println(Math.min(N%K, Math.abs(N%K-K)));
    }
}