import 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();

        if (n < k)
        {
            long candidate = Math.abs(n-k);
            long candidate2 = Math.abs(candidate-k);
            System.out.println(Math.min(candidate,candidate2));
        }
        else
        {
            if (n%k == 0) System.out.println(0);
            else
            {
                long candidate = n%k;
                long candidate2 = Math.abs(candidate-k);
                System.out.println(Math.min(candidate,candidate2));
            }
        }

        sc.close();
    }
} import java.util.*; util.* java public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();

        if (n < k)
        {
            long candidate = Math.abs(n-k);
            long candidate2 = Math.abs(candidate-k);
            System.out.println(Math.min(candidate,candidate2));
        }
        else
        {
            if (n%k == 0) System.out.println(0);
            else
            {
                long candidate = n%k;
                long candidate2 = Math.abs(candidate-k);
                System.out.println(Math.min(candidate,candidate2));
            }
        }

        sc.close();
    }
} Main public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();

        if (n < k)
        {
            long candidate = Math.abs(n-k);
            long candidate2 = Math.abs(candidate-k);
            System.out.println(Math.min(candidate,candidate2));
        }
        else
        {
            if (n%k == 0) System.out.println(0);
            else
            {
                long candidate = n%k;
                long candidate2 = Math.abs(candidate-k);
                System.out.println(Math.min(candidate,candidate2));
            }
        }

        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();

        if (n < k)
        {
            long candidate = Math.abs(n-k);
            long candidate2 = Math.abs(candidate-k);
            System.out.println(Math.min(candidate,candidate2));
        }
        else
        {
            if (n%k == 0) System.out.println(0);
            else
            {
                long candidate = n%k;
                long candidate2 = Math.abs(candidate-k);
                System.out.println(Math.min(candidate,candidate2));
            }
        }

        sc.close();
    } 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 if (n < k)
        {
            long candidate = Math.abs(n-k);
            long candidate2 = Math.abs(candidate-k);
            System.out.println(Math.min(candidate,candidate2));
        }
        else
        {
            if (n%k == 0) System.out.println(0);
            else
            {
                long candidate = n%k;
                long candidate2 = Math.abs(candidate-k);
                System.out.println(Math.min(candidate,candidate2));
            }
        } n < k n k {
            long candidate = Math.abs(n-k);
            long candidate2 = Math.abs(candidate-k);
            System.out.println(Math.min(candidate,candidate2));
        } long candidate = Math.abs(n-k); candidate Math.abs(n-k) Math.abs Math n-k n k long candidate2 = Math.abs(candidate-k); candidate2 Math.abs(candidate-k) Math.abs Math candidate-k candidate k System.out.println(Math.min(candidate,candidate2)) System.out.println System.out System System.out Math.min(candidate,candidate2) Math.min Math candidate candidate2 {
            if (n%k == 0) System.out.println(0);
            else
            {
                long candidate = n%k;
                long candidate2 = Math.abs(candidate-k);
                System.out.println(Math.min(candidate,candidate2));
            }
        } if (n%k == 0) System.out.println(0);
            else
            {
                long candidate = n%k;
                long candidate2 = Math.abs(candidate-k);
                System.out.println(Math.min(candidate,candidate2));
            } n%k == 0 n%k n k 0 System.out.println(0) System.out.println System.out System System.out 0 {
                long candidate = n%k;
                long candidate2 = Math.abs(candidate-k);
                System.out.println(Math.min(candidate,candidate2));
            } long candidate = n%k; candidate n%k n k long candidate2 = Math.abs(candidate-k); candidate2 Math.abs(candidate-k) Math.abs Math candidate-k candidate k System.out.println(Math.min(candidate,candidate2)) System.out.println System.out System System.out Math.min(candidate,candidate2) Math.min Math candidate candidate2 sc.close() sc.close sc 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();

        if (n < k)
        {
            long candidate = Math.abs(n-k);
            long candidate2 = Math.abs(candidate-k);
            System.out.println(Math.min(candidate,candidate2));
        }
        else
        {
            if (n%k == 0) System.out.println(0);
            else
            {
                long candidate = n%k;
                long candidate2 = Math.abs(candidate-k);
                System.out.println(Math.min(candidate,candidate2));
            }
        }

        sc.close();
    }
} public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();

        if (n < k)
        {
            long candidate = Math.abs(n-k);
            long candidate2 = Math.abs(candidate-k);
            System.out.println(Math.min(candidate,candidate2));
        }
        else
        {
            if (n%k == 0) System.out.println(0);
            else
            {
                long candidate = n%k;
                long candidate2 = Math.abs(candidate-k);
                System.out.println(Math.min(candidate,candidate2));
            }
        }

        sc.close();
    }
} Main
import 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();

        if (n < k)
        {
            long candidate = Math.abs(n-k);
            long candidate2 = Math.abs(candidate-k);
            System.out.println(Math.min(candidate,candidate2));
        }
        else
        {
            if (n%k == 0) System.out.println(0);
            else
            {
                long candidate = n%k;
                long candidate2 = Math.abs(candidate-k);
                System.out.println(Math.min(candidate,candidate2));
            }
        }

        sc.close();
    }
}