import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        Long N = sc.nextLong();
        Long K = sc.nextLong();
        
        Long result1=N%K;
        Long result2=Math.abs(K-result1);

        System.out.println(Math.min(result1, result2));
    }
} import java.util.Scanner; Scanner 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 result1=N%K;
        Long result2=Math.abs(K-result1);

        System.out.println(Math.min(result1, result2));
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        Long N = sc.nextLong();
        Long K = sc.nextLong();
        
        Long result1=N%K;
        Long result2=Math.abs(K-result1);

        System.out.println(Math.min(result1, result2));
    } main {
        Scanner sc = new Scanner(System.in);

        Long N = sc.nextLong();
        Long K = sc.nextLong();
        
        Long result1=N%K;
        Long result2=Math.abs(K-result1);

        System.out.println(Math.min(result1, result2));
    } 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 result1=N%K; result1 N%K N K Long result2=Math.abs(K-result1); result2 Math.abs(K-result1) Math.abs Math K-result1 K result1 System.out.println(Math.min(result1, result2)) System.out.println System.out System System.out Math.min(result1, result2) Math.min Math result1 result2 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 result1=N%K;
        Long result2=Math.abs(K-result1);

        System.out.println(Math.min(result1, result2));
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        Long N = sc.nextLong();
        Long K = sc.nextLong();
        
        Long result1=N%K;
        Long result2=Math.abs(K-result1);

        System.out.println(Math.min(result1, result2));
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        Long N = sc.nextLong();
        Long K = sc.nextLong();
        
        Long result1=N%K;
        Long result2=Math.abs(K-result1);

        System.out.println(Math.min(result1, result2));
    }
}