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 a = N % K;
        long b = -(N % K - K);
        
        if(a < b) {
            System.out.println(a);
        }else {
            System.out.println(b);
        }
    }
} 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 a = N % K;
        long b = -(N % K - K);
        
        if(a < b) {
            System.out.println(a);
        }else {
            System.out.println(b);
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        long a = N % K;
        long b = -(N % K - K);
        
        if(a < b) {
            System.out.println(a);
        }else {
            System.out.println(b);
        }
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        long a = N % K;
        long b = -(N % K - K);
        
        if(a < b) {
            System.out.println(a);
        }else {
            System.out.println(b);
        }
    } 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 a = N % K; a N % K N K long b = -(N % K - K); b -(N % K - K) (N % K - K) N % K N K K if(a < b) {
            System.out.println(a);
        }else {
            System.out.println(b);
        } a < b a b {
            System.out.println(a);
        } System.out.println(a) System.out.println System.out System System.out a {
            System.out.println(b);
        } System.out.println(b) System.out.println System.out System System.out b 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 a = N % K;
        long b = -(N % K - K);
        
        if(a < b) {
            System.out.println(a);
        }else {
            System.out.println(b);
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        long a = N % K;
        long b = -(N % K - K);
        
        if(a < b) {
            System.out.println(a);
        }else {
            System.out.println(b);
        }
    }
} 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 a = N % K;
        long b = -(N % K - K);
        
        if(a < b) {
            System.out.println(a);
        }else {
            System.out.println(b);
        }
    }
}
