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