import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		long n = in.nextLong();
		long k = in.nextLong();
		long r = n % k;
		r = Math.min(r, Math.abs(k - r));
		System.out.println(r);
		in.close();
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		long n = in.nextLong();
		long k = in.nextLong();
		long r = n % k;
		r = Math.min(r, Math.abs(k - r));
		System.out.println(r);
		in.close();
	}
} Main public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		long n = in.nextLong();
		long k = in.nextLong();
		long r = n % k;
		r = Math.min(r, Math.abs(k - r));
		System.out.println(r);
		in.close();
	} main {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		long n = in.nextLong();
		long k = in.nextLong();
		long r = n % k;
		r = Math.min(r, Math.abs(k - r));
		System.out.println(r);
		in.close();
	} Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); in new Scanner(new BufferedReader(new InputStreamReader(System.in))) long n = in.nextLong(); n in.nextLong() in.nextLong in long k = in.nextLong(); k in.nextLong() in.nextLong in long r = n % k; r n % k n k r = Math.min(r, Math.abs(k - r)) r Math.min(r, Math.abs(k - r)) Math.min Math r Math.abs(k - r) Math.abs Math k - r k r System.out.println(r) System.out.println System.out System System.out r in.close() in.close in String[] args args public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		long n = in.nextLong();
		long k = in.nextLong();
		long r = n % k;
		r = Math.min(r, Math.abs(k - r));
		System.out.println(r);
		in.close();
	}
} public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		long n = in.nextLong();
		long k = in.nextLong();
		long r = n % k;
		r = Math.min(r, Math.abs(k - r));
		System.out.println(r);
		in.close();
	}
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		long n = in.nextLong();
		long k = in.nextLong();
		long r = n % k;
		r = Math.min(r, Math.abs(k - r));
		System.out.println(r);
		in.close();
	}
}