import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			long n = sc.nextLong();
			long k = sc.nextLong();
			
			long residual = n % k;
			
			long minus = k - residual;
			
			if ( residual < minus ) {
				System.out.println(residual);
			}
			else {
				System.out.println(minus);
			}
		}
	}

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

} Main public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			long n = sc.nextLong();
			long k = sc.nextLong();
			
			long residual = n % k;
			
			long minus = k - residual;
			
			if ( residual < minus ) {
				System.out.println(residual);
			}
			else {
				System.out.println(minus);
			}
		}
	} main {
		try(Scanner sc = new Scanner(System.in)) {
			
			long n = sc.nextLong();
			long k = sc.nextLong();
			
			long residual = n % k;
			
			long minus = k - residual;
			
			if ( residual < minus ) {
				System.out.println(residual);
			}
			else {
				System.out.println(minus);
			}
		}
	} try(Scanner sc = new Scanner(System.in)) {
			
			long n = sc.nextLong();
			long k = sc.nextLong();
			
			long residual = n % k;
			
			long minus = k - residual;
			
			if ( residual < minus ) {
				System.out.println(residual);
			}
			else {
				System.out.println(minus);
			}
		} {
			
			long n = sc.nextLong();
			long k = sc.nextLong();
			
			long residual = n % k;
			
			long minus = k - residual;
			
			if ( residual < minus ) {
				System.out.println(residual);
			}
			else {
				System.out.println(minus);
			}
		} Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) 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 residual = n % k; residual n % k n k long minus = k - residual; minus k - residual k residual if ( residual < minus ) {
				System.out.println(residual);
			}
			else {
				System.out.println(minus);
			} residual < minus residual minus {
				System.out.println(residual);
			} System.out.println(residual) System.out.println System.out System System.out residual {
				System.out.println(minus);
			} System.out.println(minus) System.out.println System.out System System.out minus String[] args args public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			long n = sc.nextLong();
			long k = sc.nextLong();
			
			long residual = n % k;
			
			long minus = k - residual;
			
			if ( residual < minus ) {
				System.out.println(residual);
			}
			else {
				System.out.println(minus);
			}
		}
	}

} public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			long n = sc.nextLong();
			long k = sc.nextLong();
			
			long residual = n % k;
			
			long minus = k - residual;
			
			if ( residual < minus ) {
				System.out.println(residual);
			}
			else {
				System.out.println(minus);
			}
		}
	}

} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			long n = sc.nextLong();
			long k = sc.nextLong();
			
			long residual = n % k;
			
			long minus = k - residual;
			
			if ( residual < minus ) {
				System.out.println(residual);
			}
			else {
				System.out.println(minus);
			}
		}
	}

}