import java.io.*;
import java.util.Scanner;

public class Main{

	static long n,k;
	public static void main(String args[])throws Exception{
		Scanner sc=new Scanner(System.in);
			 n= sc.nextLong();
			 k= sc.nextLong();

			 long x=n%k;
			 long min=x;

			 if(min>Math.abs(x-k)){
			 	min=Math.abs(x-k);
			 }
			 System.out.print(min);
			}
		}
 import java.io.*; io.* java import java.util.Scanner; Scanner java.util public class Main{

	static long n,k;
	public static void main(String args[])throws Exception{
		Scanner sc=new Scanner(System.in);
			 n= sc.nextLong();
			 k= sc.nextLong();

			 long x=n%k;
			 long min=x;

			 if(min>Math.abs(x-k)){
			 	min=Math.abs(x-k);
			 }
			 System.out.print(min);
			}
		} Main static long n n k; k public static void main(String args[])throws Exception{
		Scanner sc=new Scanner(System.in);
			 n= sc.nextLong();
			 k= sc.nextLong();

			 long x=n%k;
			 long min=x;

			 if(min>Math.abs(x-k)){
			 	min=Math.abs(x-k);
			 }
			 System.out.print(min);
			} main {
		Scanner sc=new Scanner(System.in);
			 n= sc.nextLong();
			 k= sc.nextLong();

			 long x=n%k;
			 long min=x;

			 if(min>Math.abs(x-k)){
			 	min=Math.abs(x-k);
			 }
			 System.out.print(min);
			} Scanner sc=new Scanner(System.in); sc new Scanner(System.in) n= sc.nextLong() n sc.nextLong() sc.nextLong sc k= sc.nextLong() k sc.nextLong() sc.nextLong sc long x=n%k; x n%k n k long min=x; min x if(min>Math.abs(x-k)){
			 	min=Math.abs(x-k);
			 } min>Math.abs(x-k) min Math.abs(x-k) Math.abs Math x-k x k {
			 	min=Math.abs(x-k);
			 } min=Math.abs(x-k) min Math.abs(x-k) Math.abs Math x-k x k System.out.print(min) System.out.print System.out System System.out min String args[] args public class Main{

	static long n,k;
	public static void main(String args[])throws Exception{
		Scanner sc=new Scanner(System.in);
			 n= sc.nextLong();
			 k= sc.nextLong();

			 long x=n%k;
			 long min=x;

			 if(min>Math.abs(x-k)){
			 	min=Math.abs(x-k);
			 }
			 System.out.print(min);
			}
		} public class Main{

	static long n,k;
	public static void main(String args[])throws Exception{
		Scanner sc=new Scanner(System.in);
			 n= sc.nextLong();
			 k= sc.nextLong();

			 long x=n%k;
			 long min=x;

			 if(min>Math.abs(x-k)){
			 	min=Math.abs(x-k);
			 }
			 System.out.print(min);
			}
		} Main
import java.io.*;
import java.util.Scanner;

public class Main{

	static long n,k;
	public static void main(String args[])throws Exception{
		Scanner sc=new Scanner(System.in);
			 n= sc.nextLong();
			 k= sc.nextLong();

			 long x=n%k;
			 long min=x;

			 if(min>Math.abs(x-k)){
			 	min=Math.abs(x-k);
			 }
			 System.out.print(min);
			}
		}

