import java.util.Scanner;
import static java.lang.Math.min;

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

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