import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N=sc.nextLong(),K = sc.nextLong();
		long amari = N%K;
		if(amari!=0) {
			long ans = Long.min(amari, K-amari);
			System.out.println(ans);
		}else {
			System.out.println(0);
		}
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N=sc.nextLong(),K = sc.nextLong();
		long amari = N%K;
		if(amari!=0) {
			long ans = Long.min(amari, K-amari);
			System.out.println(ans);
		}else {
			System.out.println(0);
		}
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N=sc.nextLong(),K = sc.nextLong();
		long amari = N%K;
		if(amari!=0) {
			long ans = Long.min(amari, K-amari);
			System.out.println(ans);
		}else {
			System.out.println(0);
		}
	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N=sc.nextLong(),K = sc.nextLong();
		long amari = N%K;
		if(amari!=0) {
			long ans = Long.min(amari, K-amari);
			System.out.println(ans);
		}else {
			System.out.println(0);
		}
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long N=sc.nextLong() N sc.nextLong() sc.nextLong sc K = sc.nextLong(); K sc.nextLong() sc.nextLong sc long amari = N%K; amari N%K N K if(amari!=0) {
			long ans = Long.min(amari, K-amari);
			System.out.println(ans);
		}else {
			System.out.println(0);
		} amari!=0 amari 0 {
			long ans = Long.min(amari, K-amari);
			System.out.println(ans);
		} long ans = Long.min(amari, K-amari); ans Long.min(amari, K-amari) Long.min Long amari K-amari K amari System.out.println(ans) System.out.println System.out System System.out ans {
			System.out.println(0);
		} System.out.println(0) System.out.println System.out System System.out 0 String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N=sc.nextLong(),K = sc.nextLong();
		long amari = N%K;
		if(amari!=0) {
			long ans = Long.min(amari, K-amari);
			System.out.println(ans);
		}else {
			System.out.println(0);
		}
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N=sc.nextLong(),K = sc.nextLong();
		long amari = N%K;
		if(amari!=0) {
			long ans = Long.min(amari, K-amari);
			System.out.println(ans);
		}else {
			System.out.println(0);
		}
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long N=sc.nextLong(),K = sc.nextLong();
		long amari = N%K;
		if(amari!=0) {
			long ans = Long.min(amari, K-amari);
			System.out.println(ans);
		}else {
			System.out.println(0);
		}
	}

}