import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		// 入力値の取得
		long N = sc.nextLong();
		long K = sc.nextLong();
		
		long rem = N % K;
		
		// 結果の出力
		System.out.println(K - rem > rem? rem : K - rem);

		sc.close();
	}

} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		// 入力値の取得
		long N = sc.nextLong();
		long K = sc.nextLong();
		
		long rem = N % K;
		
		// 結果の出力
		System.out.println(K - rem > rem? rem : K - rem);

		sc.close();
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		// 入力値の取得
		long N = sc.nextLong();
		long K = sc.nextLong();
		
		long rem = N % K;
		
		// 結果の出力
		System.out.println(K - rem > rem? rem : K - rem);

		sc.close();
	} main {
		Scanner sc = new Scanner(System.in);

		// 入力値の取得
		long N = sc.nextLong();
		long K = sc.nextLong();
		
		long rem = N % K;
		
		// 結果の出力
		System.out.println(K - rem > rem? rem : K - rem);

		sc.close();
	} 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 long rem = N % K; rem N % K N K System.out.println(K - rem > rem? rem : K - rem) System.out.println System.out System System.out K - rem > rem? rem : K - rem K - rem > rem K - rem K rem rem rem K - rem K rem sc.close() sc.close sc 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();
		
		long rem = N % K;
		
		// 結果の出力
		System.out.println(K - rem > rem? rem : K - rem);

		sc.close();
	}

} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		// 入力値の取得
		long N = sc.nextLong();
		long K = sc.nextLong();
		
		long rem = N % K;
		
		// 結果の出力
		System.out.println(K - rem > rem? rem : K - rem);

		sc.close();
	}

} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		// 入力値の取得
		long N = sc.nextLong();
		long K = sc.nextLong();
		
		long rem = N % K;
		
		// 結果の出力
		System.out.println(K - rem > rem? rem : K - rem);

		sc.close();
	}

}
