import java.io.*;

public class Main
{
	public static void main(String[] args)throws IOException
	{
		BufferedReader br=
		new BufferedReader(new InputStreamReader(System.in));
		
		String str1=br.readLine();
		String[] strs=str1.split(" ",2);
		long[] nums =new long[2];
		for(int i=0;i<2;i++){
			nums[i]=Long.parseLong(strs[i]);
		}
		if(nums[0]>nums[1]){
			nums[0]=nums[0]%nums[1];}
		
		System.out.println(Math.min(nums[0],(nums[1]-nums[0])));
	}
} import java.io.*; io.* java public class Main
{
	public static void main(String[] args)throws IOException
	{
		BufferedReader br=
		new BufferedReader(new InputStreamReader(System.in));
		
		String str1=br.readLine();
		String[] strs=str1.split(" ",2);
		long[] nums =new long[2];
		for(int i=0;i<2;i++){
			nums[i]=Long.parseLong(strs[i]);
		}
		if(nums[0]>nums[1]){
			nums[0]=nums[0]%nums[1];}
		
		System.out.println(Math.min(nums[0],(nums[1]-nums[0])));
	}
} Main public static void main(String[] args)throws IOException
	{
		BufferedReader br=
		new BufferedReader(new InputStreamReader(System.in));
		
		String str1=br.readLine();
		String[] strs=str1.split(" ",2);
		long[] nums =new long[2];
		for(int i=0;i<2;i++){
			nums[i]=Long.parseLong(strs[i]);
		}
		if(nums[0]>nums[1]){
			nums[0]=nums[0]%nums[1];}
		
		System.out.println(Math.min(nums[0],(nums[1]-nums[0])));
	} main {
		BufferedReader br=
		new BufferedReader(new InputStreamReader(System.in));
		
		String str1=br.readLine();
		String[] strs=str1.split(" ",2);
		long[] nums =new long[2];
		for(int i=0;i<2;i++){
			nums[i]=Long.parseLong(strs[i]);
		}
		if(nums[0]>nums[1]){
			nums[0]=nums[0]%nums[1];}
		
		System.out.println(Math.min(nums[0],(nums[1]-nums[0])));
	} BufferedReader br=
		new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String str1=br.readLine(); str1 br.readLine() br.readLine br String[] strs=str1.split(" ",2); strs str1.split(" ",2) str1.split str1 " " 2 long[] nums =new long[2]; nums new long[2] 2 for(int i=0;i<2;i++){
			nums[i]=Long.parseLong(strs[i]);
		} int i=0; int i=0; i 0 i<2 i 2 i++ i++ i {
			nums[i]=Long.parseLong(strs[i]);
		} {
			nums[i]=Long.parseLong(strs[i]);
		} nums[i]=Long.parseLong(strs[i]) nums[i] nums i Long.parseLong(strs[i]) Long.parseLong Long strs[i] strs i if(nums[0]>nums[1]){
			nums[0]=nums[0]%nums[1];} nums[0]>nums[1] nums[0] nums 0 nums[1] nums 1 {
			nums[0]=nums[0]%nums[1];} nums[0]=nums[0]%nums[1] nums[0] nums 0 nums[0]%nums[1] nums[0] nums 0 nums[1] nums 1 System.out.println(Math.min(nums[0],(nums[1]-nums[0]))) System.out.println System.out System System.out Math.min(nums[0],(nums[1]-nums[0])) Math.min Math nums[0] nums 0 (nums[1]-nums[0]) nums[1] nums 1 nums[0] nums 0 String[] args args public class Main
{
	public static void main(String[] args)throws IOException
	{
		BufferedReader br=
		new BufferedReader(new InputStreamReader(System.in));
		
		String str1=br.readLine();
		String[] strs=str1.split(" ",2);
		long[] nums =new long[2];
		for(int i=0;i<2;i++){
			nums[i]=Long.parseLong(strs[i]);
		}
		if(nums[0]>nums[1]){
			nums[0]=nums[0]%nums[1];}
		
		System.out.println(Math.min(nums[0],(nums[1]-nums[0])));
	}
} public class Main
{
	public static void main(String[] args)throws IOException
	{
		BufferedReader br=
		new BufferedReader(new InputStreamReader(System.in));
		
		String str1=br.readLine();
		String[] strs=str1.split(" ",2);
		long[] nums =new long[2];
		for(int i=0;i<2;i++){
			nums[i]=Long.parseLong(strs[i]);
		}
		if(nums[0]>nums[1]){
			nums[0]=nums[0]%nums[1];}
		
		System.out.println(Math.min(nums[0],(nums[1]-nums[0])));
	}
} Main
import java.io.*;

public class Main
{
	public static void main(String[] args)throws IOException
	{
		BufferedReader br=
		new BufferedReader(new InputStreamReader(System.in));
		
		String str1=br.readLine();
		String[] strs=str1.split(" ",2);
		long[] nums =new long[2];
		for(int i=0;i<2;i++){
			nums[i]=Long.parseLong(strs[i]);
		}
		if(nums[0]>nums[1]){
			nums[0]=nums[0]%nums[1];}
		
		System.out.println(Math.min(nums[0],(nums[1]-nums[0])));
	}
}
