/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		long n=sc.nextLong(),k=sc.nextLong();
		long ans=n%k;
		System.out.println(Math.min(ans,Math.abs(ans-k)));
	}
} import java.util.*; util.* java import java.lang.*; lang.* java import java.io.*; io.* java public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		long n=sc.nextLong(),k=sc.nextLong();
		long ans=n%k;
		System.out.println(Math.min(ans,Math.abs(ans-k)));
	}
} Main public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		long n=sc.nextLong(),k=sc.nextLong();
		long ans=n%k;
		System.out.println(Math.min(ans,Math.abs(ans-k)));
	} main {
		// your code goes here
		Scanner sc=new Scanner(System.in);
		long n=sc.nextLong(),k=sc.nextLong();
		long ans=n%k;
		System.out.println(Math.min(ans,Math.abs(ans-k)));
	} Scanner sc=new Scanner(System.in); sc new Scanner(System.in) long n=sc.nextLong() n sc.nextLong() sc.nextLong sc k=sc.nextLong(); k sc.nextLong() sc.nextLong sc long ans=n%k; ans n%k n k System.out.println(Math.min(ans,Math.abs(ans-k))) System.out.println System.out System System.out Math.min(ans,Math.abs(ans-k)) Math.min Math ans Math.abs(ans-k) Math.abs Math ans-k ans k String[] args args public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		long n=sc.nextLong(),k=sc.nextLong();
		long ans=n%k;
		System.out.println(Math.min(ans,Math.abs(ans-k)));
	}
} public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		long n=sc.nextLong(),k=sc.nextLong();
		long ans=n%k;
		System.out.println(Math.min(ans,Math.abs(ans-k)));
	}
} Main
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		long n=sc.nextLong(),k=sc.nextLong();
		long ans=n%k;
		System.out.println(Math.min(ans,Math.abs(ans-k)));
	}
}