import java.util.*;
        public class Main {
        	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long x=n/k;
		if(Math.abs(n-x*k)<Math.abs(n-(x+1)*k)){

			System.out.print(Math.abs(n-x*k));
		}else{
			System.out.print(Math.abs(n-(x+1)*k));
		}
	}
        } import java.util.*; util.* java public class Main {
        	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long x=n/k;
		if(Math.abs(n-x*k)<Math.abs(n-(x+1)*k)){

			System.out.print(Math.abs(n-x*k));
		}else{
			System.out.print(Math.abs(n-(x+1)*k));
		}
	}
        } Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long x=n/k;
		if(Math.abs(n-x*k)<Math.abs(n-(x+1)*k)){

			System.out.print(Math.abs(n-x*k));
		}else{
			System.out.print(Math.abs(n-(x+1)*k));
		}
	} main {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long x=n/k;
		if(Math.abs(n-x*k)<Math.abs(n-(x+1)*k)){

			System.out.print(Math.abs(n-x*k));
		}else{
			System.out.print(Math.abs(n-(x+1)*k));
		}
	} 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 long x=n/k; x n/k n k if(Math.abs(n-x*k)<Math.abs(n-(x+1)*k)){

			System.out.print(Math.abs(n-x*k));
		}else{
			System.out.print(Math.abs(n-(x+1)*k));
		} Math.abs(n-x*k)<Math.abs(n-(x+1)*k) Math.abs(n-x*k) Math.abs Math n-x*k n x*k x k Math.abs(n-(x+1)*k) Math.abs Math n-(x+1)*k n (x+1)*k (x+1) x 1 k {

			System.out.print(Math.abs(n-x*k));
		} System.out.print(Math.abs(n-x*k)) System.out.print System.out System System.out Math.abs(n-x*k) Math.abs Math n-x*k n x*k x k {
			System.out.print(Math.abs(n-(x+1)*k));
		} System.out.print(Math.abs(n-(x+1)*k)) System.out.print System.out System System.out Math.abs(n-(x+1)*k) Math.abs Math n-(x+1)*k n (x+1)*k (x+1) x 1 k String[] args args public class Main {
        	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long x=n/k;
		if(Math.abs(n-x*k)<Math.abs(n-(x+1)*k)){

			System.out.print(Math.abs(n-x*k));
		}else{
			System.out.print(Math.abs(n-(x+1)*k));
		}
	}
        } public class Main {
        	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long x=n/k;
		if(Math.abs(n-x*k)<Math.abs(n-(x+1)*k)){

			System.out.print(Math.abs(n-x*k));
		}else{
			System.out.print(Math.abs(n-(x+1)*k));
		}
	}
        } Main
import java.util.*;
        public class Main {
        	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long k = sc.nextLong();
		long x=n/k;
		if(Math.abs(n-x*k)<Math.abs(n-(x+1)*k)){

			System.out.print(Math.abs(n-x*k));
		}else{
			System.out.print(Math.abs(n-(x+1)*k));
		}
	}
        }