import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		long N;
		long K;
		Scanner sc = new Scanner(System.in);
		N = sc.nextLong();
		K = sc.nextLong();
		long mod = N % K;
		System.out.println(Math.min(mod, Math.abs(mod - K)));
		sc.close();
	}
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		long N;
		long K;
		Scanner sc = new Scanner(System.in);
		N = sc.nextLong();
		K = sc.nextLong();
		long mod = N % K;
		System.out.println(Math.min(mod, Math.abs(mod - K)));
		sc.close();
	}
} Main public static void main(String[] args) {
		long N;
		long K;
		Scanner sc = new Scanner(System.in);
		N = sc.nextLong();
		K = sc.nextLong();
		long mod = N % K;
		System.out.println(Math.min(mod, Math.abs(mod - K)));
		sc.close();
	} main {
		long N;
		long K;
		Scanner sc = new Scanner(System.in);
		N = sc.nextLong();
		K = sc.nextLong();
		long mod = N % K;
		System.out.println(Math.min(mod, Math.abs(mod - K)));
		sc.close();
	} long N; N long K; K Scanner sc = new Scanner(System.in); sc new Scanner(System.in) N = sc.nextLong() N sc.nextLong() sc.nextLong sc K = sc.nextLong() K sc.nextLong() sc.nextLong sc long mod = N % K; mod N % K N K System.out.println(Math.min(mod, Math.abs(mod - K))) System.out.println System.out System System.out Math.min(mod, Math.abs(mod - K)) Math.min Math mod Math.abs(mod - K) Math.abs Math mod - K mod K sc.close() sc.close sc String[] args args public class Main {

	public static void main(String[] args) {
		long N;
		long K;
		Scanner sc = new Scanner(System.in);
		N = sc.nextLong();
		K = sc.nextLong();
		long mod = N % K;
		System.out.println(Math.min(mod, Math.abs(mod - K)));
		sc.close();
	}
} public class Main {

	public static void main(String[] args) {
		long N;
		long K;
		Scanner sc = new Scanner(System.in);
		N = sc.nextLong();
		K = sc.nextLong();
		long mod = N % K;
		System.out.println(Math.min(mod, Math.abs(mod - K)));
		sc.close();
	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		long N;
		long K;
		Scanner sc = new Scanner(System.in);
		N = sc.nextLong();
		K = sc.nextLong();
		long mod = N % K;
		System.out.println(Math.min(mod, Math.abs(mod - K)));
		sc.close();
	}
}