

import java.util.*;

public class Main {
	//static boolean f;
	static long min;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		/* 入力の受け取り
		 * String s = sc.next();
		 * int a = sc.nextInt();
		 * long a = sc.nextLong();
		 */
		
		// 文字列(String)を文字(char)に分解
		// char[] s_char = s.toCharArray();
		
		long n = sc.nextLong();
		long k = sc.nextLong();
		
		min = n%k;
		min = Math.min(min, Math.abs(min-k));
		
		System.out.println(min);
		
		/*
		 * if(f)
		 *	System.out.println("Yes");
		 * else
		 * 	System.out.println("No");
		 */
	}
} import java.util.*; util.* java public class Main {
	//static boolean f;
	static long min;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		/* 入力の受け取り
		 * String s = sc.next();
		 * int a = sc.nextInt();
		 * long a = sc.nextLong();
		 */
		
		// 文字列(String)を文字(char)に分解
		// char[] s_char = s.toCharArray();
		
		long n = sc.nextLong();
		long k = sc.nextLong();
		
		min = n%k;
		min = Math.min(min, Math.abs(min-k));
		
		System.out.println(min);
		
		/*
		 * if(f)
		 *	System.out.println("Yes");
		 * else
		 * 	System.out.println("No");
		 */
	}
} Main //static boolean f;
	static long min; min public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		/* 入力の受け取り
		 * String s = sc.next();
		 * int a = sc.nextInt();
		 * long a = sc.nextLong();
		 */
		
		// 文字列(String)を文字(char)に分解
		// char[] s_char = s.toCharArray();
		
		long n = sc.nextLong();
		long k = sc.nextLong();
		
		min = n%k;
		min = Math.min(min, Math.abs(min-k));
		
		System.out.println(min);
		
		/*
		 * if(f)
		 *	System.out.println("Yes");
		 * else
		 * 	System.out.println("No");
		 */
	} main {
		Scanner sc = new Scanner(System.in);
		
		/* 入力の受け取り
		 * String s = sc.next();
		 * int a = sc.nextInt();
		 * long a = sc.nextLong();
		 */
		
		// 文字列(String)を文字(char)に分解
		// char[] s_char = s.toCharArray();
		
		long n = sc.nextLong();
		long k = sc.nextLong();
		
		min = n%k;
		min = Math.min(min, Math.abs(min-k));
		
		System.out.println(min);
		
		/*
		 * if(f)
		 *	System.out.println("Yes");
		 * else
		 * 	System.out.println("No");
		 */
	} 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 min = n%k min n%k n k min = Math.min(min, Math.abs(min-k)) min Math.min(min, Math.abs(min-k)) Math.min Math min Math.abs(min-k) Math.abs Math min-k min k System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main {
	//static boolean f;
	static long min;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		/* 入力の受け取り
		 * String s = sc.next();
		 * int a = sc.nextInt();
		 * long a = sc.nextLong();
		 */
		
		// 文字列(String)を文字(char)に分解
		// char[] s_char = s.toCharArray();
		
		long n = sc.nextLong();
		long k = sc.nextLong();
		
		min = n%k;
		min = Math.min(min, Math.abs(min-k));
		
		System.out.println(min);
		
		/*
		 * if(f)
		 *	System.out.println("Yes");
		 * else
		 * 	System.out.println("No");
		 */
	}
} public class Main {
	//static boolean f;
	static long min;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		/* 入力の受け取り
		 * String s = sc.next();
		 * int a = sc.nextInt();
		 * long a = sc.nextLong();
		 */
		
		// 文字列(String)を文字(char)に分解
		// char[] s_char = s.toCharArray();
		
		long n = sc.nextLong();
		long k = sc.nextLong();
		
		min = n%k;
		min = Math.min(min, Math.abs(min-k));
		
		System.out.println(min);
		
		/*
		 * if(f)
		 *	System.out.println("Yes");
		 * else
		 * 	System.out.println("No");
		 */
	}
} Main


import java.util.*;

public class Main {
	//static boolean f;
	static long min;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		/* 入力の受け取り
		 * String s = sc.next();
		 * int a = sc.nextInt();
		 * long a = sc.nextLong();
		 */
		
		// 文字列(String)を文字(char)に分解
		// char[] s_char = s.toCharArray();
		
		long n = sc.nextLong();
		long k = sc.nextLong();
		
		min = n%k;
		min = Math.min(min, Math.abs(min-k));
		
		System.out.println(min);
		
		/*
		 * if(f)
		 *	System.out.println("Yes");
		 * else
		 * 	System.out.println("No");
		 */
	}
}
