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 a = n%k;
		long ans = 0;
		if( n%k==0 ){
			ans = 0;
		}else{
			ans = Math.min(a, k-a);
		}
		System.out.println(ans);
    }
} 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 a = n%k;
		long ans = 0;
		if( n%k==0 ){
			ans = 0;
		}else{
			ans = Math.min(a, k-a);
		}
		System.out.println(ans);
    }
} Main public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();

		long a = n%k;
		long ans = 0;
		if( n%k==0 ){
			ans = 0;
		}else{
			ans = Math.min(a, k-a);
		}
		System.out.println(ans);
    } main {

        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();

		long a = n%k;
		long ans = 0;
		if( n%k==0 ){
			ans = 0;
		}else{
			ans = Math.min(a, k-a);
		}
		System.out.println(ans);
    } 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 a = n%k; a n%k n k long ans = 0; ans 0 if( n%k==0 ){
			ans = 0;
		}else{
			ans = Math.min(a, k-a);
		} n%k==0 n%k n k 0 {
			ans = 0;
		} ans = 0 ans 0 {
			ans = Math.min(a, k-a);
		} ans = Math.min(a, k-a) ans Math.min(a, k-a) Math.min Math a k-a k a System.out.println(ans) System.out.println System.out System System.out ans 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 a = n%k;
		long ans = 0;
		if( n%k==0 ){
			ans = 0;
		}else{
			ans = Math.min(a, k-a);
		}
		System.out.println(ans);
    }
} public class Main {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();

		long a = n%k;
		long ans = 0;
		if( n%k==0 ){
			ans = 0;
		}else{
			ans = Math.min(a, k-a);
		}
		System.out.println(ans);
    }
} 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 a = n%k;
		long ans = 0;
		if( n%k==0 ){
			ans = 0;
		}else{
			ans = Math.min(a, k-a);
		}
		System.out.println(ans);
    }
}