import java.util.stream.DoubleStream;

import java.util.Arrays;
import java.util.Comparator;
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 a = n % k;
        long b = k - a;
        System.out.println(a < b ? a : b);
    }
} import java.util.stream.DoubleStream; DoubleStream java.util.stream import java.util.Arrays; Arrays java.util import java.util.Comparator; Comparator java.util 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 a = n % k;
        long b = k - a;
        System.out.println(a < b ? a : 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 = k - a;
        System.out.println(a < b ? a : b);
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long a = n % k;
        long b = k - a;
        System.out.println(a < b ? a : 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 = k - a; b k - a k a System.out.println(a < b ? a : b) System.out.println System.out System System.out a < b ? a : b a < b a b a 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 = k - a;
        System.out.println(a < b ? a : 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 = k - a;
        System.out.println(a < b ? a : b);
    }
} Main
import java.util.stream.DoubleStream;

import java.util.Arrays;
import java.util.Comparator;
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 a = n % k;
        long b = k - a;
        System.out.println(a < b ? a : b);
    }
}