import java.util.*;
import java.io.*;
public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        long n=Long.parseLong(str[0]);
        long k=Long.parseLong(str[1]);
        if(n%k==0)
        pw.println(0);
        else
        {
            n=n%k;
            pw.println(Math.min((long)Math.abs(n-k),(long)Math.abs(n)));
        }
        pw.flush();
        pw.close();
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        long n=Long.parseLong(str[0]);
        long k=Long.parseLong(str[1]);
        if(n%k==0)
        pw.println(0);
        else
        {
            n=n%k;
            pw.println(Math.min((long)Math.abs(n-k),(long)Math.abs(n)));
        }
        pw.flush();
        pw.close();
    }
} Main public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        long n=Long.parseLong(str[0]);
        long k=Long.parseLong(str[1]);
        if(n%k==0)
        pw.println(0);
        else
        {
            n=n%k;
            pw.println(Math.min((long)Math.abs(n-k),(long)Math.abs(n)));
        }
        pw.flush();
        pw.close();
    } main {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        long n=Long.parseLong(str[0]);
        long k=Long.parseLong(str[1]);
        if(n%k==0)
        pw.println(0);
        else
        {
            n=n%k;
            pw.println(Math.min((long)Math.abs(n-k),(long)Math.abs(n)));
        }
        pw.flush();
        pw.close();
    } BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) PrintWriter pw=new PrintWriter(System.out); pw new PrintWriter(System.out) String str[]=br.readLine().split(" "); str br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long n=Long.parseLong(str[0]); n Long.parseLong(str[0]) Long.parseLong Long str[0] str 0 long k=Long.parseLong(str[1]); k Long.parseLong(str[1]) Long.parseLong Long str[1] str 1 if(n%k==0)
        pw.println(0);
        else
        {
            n=n%k;
            pw.println(Math.min((long)Math.abs(n-k),(long)Math.abs(n)));
        } n%k==0 n%k n k 0 pw.println(0) pw.println pw 0 {
            n=n%k;
            pw.println(Math.min((long)Math.abs(n-k),(long)Math.abs(n)));
        } n=n%k n n%k n k pw.println(Math.min((long)Math.abs(n-k),(long)Math.abs(n))) pw.println pw Math.min((long)Math.abs(n-k),(long)Math.abs(n)) Math.min Math (long)Math.abs(n-k) Math.abs Math n-k n k (long)Math.abs(n) Math.abs Math n pw.flush() pw.flush pw pw.close() pw.close pw String args[] args public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        long n=Long.parseLong(str[0]);
        long k=Long.parseLong(str[1]);
        if(n%k==0)
        pw.println(0);
        else
        {
            n=n%k;
            pw.println(Math.min((long)Math.abs(n-k),(long)Math.abs(n)));
        }
        pw.flush();
        pw.close();
    }
} public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        long n=Long.parseLong(str[0]);
        long k=Long.parseLong(str[1]);
        if(n%k==0)
        pw.println(0);
        else
        {
            n=n%k;
            pw.println(Math.min((long)Math.abs(n-k),(long)Math.abs(n)));
        }
        pw.flush();
        pw.close();
    }
} Main
import java.util.*;
import java.io.*;
public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        long n=Long.parseLong(str[0]);
        long k=Long.parseLong(str[1]);
        if(n%k==0)
        pw.println(0);
        else
        {
            n=n%k;
            pw.println(Math.min((long)Math.abs(n-k),(long)Math.abs(n)));
        }
        pw.flush();
        pw.close();
    }
}