import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        ArrayList<Long> already = new ArrayList<>();
        long minAnswer = N;
        minAnswer = Math.min(N%K, Math.abs(N%K-K));
        System.out.println(minAnswer);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        ArrayList<Long> already = new ArrayList<>();
        long minAnswer = N;
        minAnswer = Math.min(N%K, Math.abs(N%K-K));
        System.out.println(minAnswer);
    }
} Main public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        ArrayList<Long> already = new ArrayList<>();
        long minAnswer = N;
        minAnswer = Math.min(N%K, Math.abs(N%K-K));
        System.out.println(minAnswer);
    } main {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        ArrayList<Long> already = new ArrayList<>();
        long minAnswer = N;
        minAnswer = Math.min(N%K, Math.abs(N%K-K));
        System.out.println(minAnswer);
    } 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 ArrayList<Long> already = new ArrayList<>(); already new ArrayList<>() long minAnswer = N; minAnswer N minAnswer = Math.min(N%K, Math.abs(N%K-K)) minAnswer Math.min(N%K, Math.abs(N%K-K)) Math.min Math N%K N K Math.abs(N%K-K) Math.abs Math N%K-K N%K N K K System.out.println(minAnswer) System.out.println System.out System System.out minAnswer String[] args args public class Main {
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        ArrayList<Long> already = new ArrayList<>();
        long minAnswer = N;
        minAnswer = Math.min(N%K, Math.abs(N%K-K));
        System.out.println(minAnswer);
    }
} public class Main {
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        ArrayList<Long> already = new ArrayList<>();
        long minAnswer = N;
        minAnswer = Math.min(N%K, Math.abs(N%K-K));
        System.out.println(minAnswer);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        ArrayList<Long> already = new ArrayList<>();
        long minAnswer = N;
        minAnswer = Math.min(N%K, Math.abs(N%K-K));
        System.out.println(minAnswer);
    }
}
