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();
		if(N % K == 0) {
			System.out.println(0);
			return;
		}
		System.out.println(Math.min(N, Math.abs(N % K - 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();
		if(N % K == 0) {
			System.out.println(0);
			return;
		}
		System.out.println(Math.min(N, Math.abs(N % K - K)));
	}
} Main public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		if(N % K == 0) {
			System.out.println(0);
			return;
		}
		System.out.println(Math.min(N, Math.abs(N % K - K)));
	} main {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		if(N % K == 0) {
			System.out.println(0);
			return;
		}
		System.out.println(Math.min(N, Math.abs(N % K - 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 if(N % K == 0) {
			System.out.println(0);
			return;
		} N % K == 0 N % K N K 0 {
			System.out.println(0);
			return;
		} System.out.println(0) System.out.println System.out System System.out 0 return; System.out.println(Math.min(N, Math.abs(N % K - K))) System.out.println System.out System System.out Math.min(N, Math.abs(N % K - K)) Math.min Math N Math.abs(N % K - K) Math.abs Math N % K - K N % K N K 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();
		if(N % K == 0) {
			System.out.println(0);
			return;
		}
		System.out.println(Math.min(N, Math.abs(N % K - K)));
	}
} public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();
		if(N % K == 0) {
			System.out.println(0);
			return;
		}
		System.out.println(Math.min(N, Math.abs(N % K - 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();
		if(N % K == 0) {
			System.out.println(0);
			return;
		}
		System.out.println(Math.min(N, Math.abs(N % K - K)));
	}
}
