import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long x = 0;
        x = Math.min(n % k, k - n % k);

        System.out.print(x);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long x = 0;
        x = Math.min(n % k, k - n % k);

        System.out.print(x);
    }
} Main public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long x = 0;
        x = Math.min(n % k, k - n % k);

        System.out.print(x);
    } main {

        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long x = 0;
        x = Math.min(n % k, k - n % k);

        System.out.print(x);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n = Long.parseLong(sc.next()); n Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long k = Long.parseLong(sc.next()); k Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long x = 0; x 0 x = Math.min(n % k, k - n % k) x Math.min(n % k, k - n % k) Math.min Math n % k n k k - n % k k n % k n k System.out.print(x) System.out.print System.out System System.out x String[] args args public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long x = 0;
        x = Math.min(n % k, k - n % k);

        System.out.print(x);
    }
} public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long x = 0;
        x = Math.min(n % k, k - n % k);

        System.out.print(x);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long n = Long.parseLong(sc.next());
        long k = Long.parseLong(sc.next());
        long x = 0;
        x = Math.min(n % k, k - n % k);

        System.out.print(x);
    }
}
