import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		        Scanner sc = new Scanner(System.in);
		        long n = sc.nextLong();
		        long k = sc.nextLong();
		        long i = n%k;
		        //余り
		        if (i<=k-i){
		            System.out.println(i);
		        }else{
		            System.out.println(k-i);
		        }
	}
} import java.util.Scanner; Scanner 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 i = n%k;
		        //余り
		        if (i<=k-i){
		            System.out.println(i);
		        }else{
		            System.out.println(k-i);
		        }
	}
} Main public static void main(String[] args) {
		        Scanner sc = new Scanner(System.in);
		        long n = sc.nextLong();
		        long k = sc.nextLong();
		        long i = n%k;
		        //余り
		        if (i<=k-i){
		            System.out.println(i);
		        }else{
		            System.out.println(k-i);
		        }
	} main {
		        Scanner sc = new Scanner(System.in);
		        long n = sc.nextLong();
		        long k = sc.nextLong();
		        long i = n%k;
		        //余り
		        if (i<=k-i){
		            System.out.println(i);
		        }else{
		            System.out.println(k-i);
		        }
	} 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 i = n%k; i n%k n k if (i<=k-i){
		            System.out.println(i);
		        }else{
		            System.out.println(k-i);
		        } i<=k-i i k-i k i {
		            System.out.println(i);
		        } System.out.println(i) System.out.println System.out System System.out i {
		            System.out.println(k-i);
		        } System.out.println(k-i) System.out.println System.out System System.out k-i k i 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 i = n%k;
		        //余り
		        if (i<=k-i){
		            System.out.println(i);
		        }else{
		            System.out.println(k-i);
		        }
	}
} public class Main {

	public static void main(String[] args) {
		        Scanner sc = new Scanner(System.in);
		        long n = sc.nextLong();
		        long k = sc.nextLong();
		        long i = n%k;
		        //余り
		        if (i<=k-i){
		            System.out.println(i);
		        }else{
		            System.out.println(k-i);
		        }
	}
} Main
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		        Scanner sc = new Scanner(System.in);
		        long n = sc.nextLong();
		        long k = sc.nextLong();
		        long i = n%k;
		        //余り
		        if (i<=k-i){
		            System.out.println(i);
		        }else{
		            System.out.println(k-i);
		        }
	}
}
