import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		//List<String> list=new ArrayList<String>(Arrays.asList(s.split("")));
		//List<Integer> list=new ArrayList<Integer>();
		//int[] array = new int[2000000];
		long n = sc.nextLong();
		long k = sc.nextLong();
		while(true) {
			long x=n/k;
			long y=n%k;
			if(x==0)
				break;
			else
				n=y;
		}
		long z = Math.abs(n-k);
		if(z<n)
			System.out.println(z);
		else
			System.out.println(n);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		//List<String> list=new ArrayList<String>(Arrays.asList(s.split("")));
		//List<Integer> list=new ArrayList<Integer>();
		//int[] array = new int[2000000];
		long n = sc.nextLong();
		long k = sc.nextLong();
		while(true) {
			long x=n/k;
			long y=n%k;
			if(x==0)
				break;
			else
				n=y;
		}
		long z = Math.abs(n-k);
		if(z<n)
			System.out.println(z);
		else
			System.out.println(n);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		//List<String> list=new ArrayList<String>(Arrays.asList(s.split("")));
		//List<Integer> list=new ArrayList<Integer>();
		//int[] array = new int[2000000];
		long n = sc.nextLong();
		long k = sc.nextLong();
		while(true) {
			long x=n/k;
			long y=n%k;
			if(x==0)
				break;
			else
				n=y;
		}
		long z = Math.abs(n-k);
		if(z<n)
			System.out.println(z);
		else
			System.out.println(n);
	} main {
		Scanner sc = new Scanner(System.in);
		//List<String> list=new ArrayList<String>(Arrays.asList(s.split("")));
		//List<Integer> list=new ArrayList<Integer>();
		//int[] array = new int[2000000];
		long n = sc.nextLong();
		long k = sc.nextLong();
		while(true) {
			long x=n/k;
			long y=n%k;
			if(x==0)
				break;
			else
				n=y;
		}
		long z = Math.abs(n-k);
		if(z<n)
			System.out.println(z);
		else
			System.out.println(n);
	} 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 while(true) {
			long x=n/k;
			long y=n%k;
			if(x==0)
				break;
			else
				n=y;
		} true {
			long x=n/k;
			long y=n%k;
			if(x==0)
				break;
			else
				n=y;
		} long x=n/k; x n/k n k long y=n%k; y n%k n k if(x==0)
				break;
			else
				n=y; x==0 x 0 break; n=y n y long z = Math.abs(n-k); z Math.abs(n-k) Math.abs Math n-k n k if(z<n)
			System.out.println(z);
		else
			System.out.println(n); z<n z n System.out.println(z) System.out.println System.out System System.out z System.out.println(n) System.out.println System.out System System.out n String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		//List<String> list=new ArrayList<String>(Arrays.asList(s.split("")));
		//List<Integer> list=new ArrayList<Integer>();
		//int[] array = new int[2000000];
		long n = sc.nextLong();
		long k = sc.nextLong();
		while(true) {
			long x=n/k;
			long y=n%k;
			if(x==0)
				break;
			else
				n=y;
		}
		long z = Math.abs(n-k);
		if(z<n)
			System.out.println(z);
		else
			System.out.println(n);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		//List<String> list=new ArrayList<String>(Arrays.asList(s.split("")));
		//List<Integer> list=new ArrayList<Integer>();
		//int[] array = new int[2000000];
		long n = sc.nextLong();
		long k = sc.nextLong();
		while(true) {
			long x=n/k;
			long y=n%k;
			if(x==0)
				break;
			else
				n=y;
		}
		long z = Math.abs(n-k);
		if(z<n)
			System.out.println(z);
		else
			System.out.println(n);
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		//List<String> list=new ArrayList<String>(Arrays.asList(s.split("")));
		//List<Integer> list=new ArrayList<Integer>();
		//int[] array = new int[2000000];
		long n = sc.nextLong();
		long k = sc.nextLong();
		while(true) {
			long x=n/k;
			long y=n%k;
			if(x==0)
				break;
			else
				n=y;
		}
		long z = Math.abs(n-k);
		if(z<n)
			System.out.println(z);
		else
			System.out.println(n);
	}
}