import java.io.*;
import java.util.*;

public class Main {
    public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        n=n%k;
        System.out.println(Math.min(n,Math.abs(n-k)));
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {
    public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        n=n%k;
        System.out.println(Math.min(n,Math.abs(n-k)));
    }
} Main public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        n=n%k;
        System.out.println(Math.min(n,Math.abs(n-k)));
    } main {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        n=n%k;
        System.out.println(Math.min(n,Math.abs(n-k)));
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) long n = scanner.nextLong(); n scanner.nextLong() scanner.nextLong scanner long k = scanner.nextLong(); k scanner.nextLong() scanner.nextLong scanner n=n%k n n%k n k System.out.println(Math.min(n,Math.abs(n-k))) System.out.println System.out System System.out Math.min(n,Math.abs(n-k)) Math.min Math n Math.abs(n-k) Math.abs Math n-k n k String[]args args public class Main {
    public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        n=n%k;
        System.out.println(Math.min(n,Math.abs(n-k)));
    }
} public class Main {
    public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        n=n%k;
        System.out.println(Math.min(n,Math.abs(n-k)));
    }
} Main
import java.io.*;
import java.util.*;

public class Main {
    public static void main (String[]args)throws IOException {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextLong();
        long k = scanner.nextLong();
        n=n%k;
        System.out.println(Math.min(n,Math.abs(n-k)));
    }
}
