import java.util.*;

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

public class Main {
	void solve() {
		Scanner scan = new Scanner(System.in);
		long n = scan.nextLong();
		long k = scan.nextLong();
		if(n % k == 0) {
			System.out.println(0);
			return;
		}
		n %= k;
		System.out.println(Math.min(n, k - n));
		scan.close();
	}
	
	public static void main(String[] args) {
		new Main().solve();
	}
}
