import java.util.*;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        n %= k;
        n = Math.min(n,k-n);
        System.out.println(n);
	}
} import java.util.*; util.* java import java.math.*; math.* java public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        n %= k;
        n = Math.min(n,k-n);
        System.out.println(n);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        n %= k;
        n = Math.min(n,k-n);
        System.out.println(n);
	} main {
		Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        n %= k;
        n = Math.min(n,k-n);
        System.out.println(n);
	} 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 n %= k n k n = Math.min(n,k-n) n Math.min(n,k-n) Math.min Math n k-n k n System.out.println(n) System.out.println System.out System System.out n 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();
        n %= k;
        n = Math.min(n,k-n);
        System.out.println(n);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        n %= k;
        n = Math.min(n,k-n);
        System.out.println(n);
	}
} Main
import java.util.*;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        n %= k;
        n = Math.min(n,k-n);
        System.out.println(n);
	}
}