import java.util.*;
import java.lang.Math;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		long N = in.nextLong();
		long K = in.nextLong();
		System.out.println(absolutediff(N,K));
	}
	
	public static long absolutediff(long N, long K) {
		if(N < K) {
			return Math.min(Math.abs(N-K),Math.abs(Math.abs(N-K)-K));
		}
		else {
			return absolutediff(N%K, K);
		}
	}
	
} import java.util.*; util.* java import java.lang.Math; Math java.lang public class Main {

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

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

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		long N = in.nextLong();
		long K = in.nextLong();
		System.out.println(absolutediff(N,K));
	}
	
	public static long absolutediff(long N, long K) {
		if(N < K) {
			return Math.min(Math.abs(N-K),Math.abs(Math.abs(N-K)-K));
		}
		else {
			return absolutediff(N%K, K);
		}
	}
	
} Main
import java.util.*;
import java.lang.Math;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		long N = in.nextLong();
		long K = in.nextLong();
		System.out.println(absolutediff(N,K));
	}
	
	public static long absolutediff(long N, long K) {
		if(N < K) {
			return Math.min(Math.abs(N-K),Math.abs(Math.abs(N-K)-K));
		}
		else {
			return absolutediff(N%K, K);
		}
	}
	
}
