import java.util.Scanner;

public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			long N = scan.nextLong();
			long K = scan.nextLong();
			
			int answer = 0;
			if(N==K) {
				answer = 0;
				System.out.println(0);
			}else if(N<K){
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}else if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}
			
			
			
		}
		
		
	}
		

} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			long N = scan.nextLong();
			long K = scan.nextLong();
			
			int answer = 0;
			if(N==K) {
				answer = 0;
				System.out.println(0);
			}else if(N<K){
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}else if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}
			
			
			
		}
		
		
	}
		

} Main public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			long N = scan.nextLong();
			long K = scan.nextLong();
			
			int answer = 0;
			if(N==K) {
				answer = 0;
				System.out.println(0);
			}else if(N<K){
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}else if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}
			
			
			
		}
		
		
	} main {
		try(Scanner scan = new Scanner(System.in)){
			long N = scan.nextLong();
			long K = scan.nextLong();
			
			int answer = 0;
			if(N==K) {
				answer = 0;
				System.out.println(0);
			}else if(N<K){
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}else if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}
			
			
			
		}
		
		
	} try(Scanner scan = new Scanner(System.in)){
			long N = scan.nextLong();
			long K = scan.nextLong();
			
			int answer = 0;
			if(N==K) {
				answer = 0;
				System.out.println(0);
			}else if(N<K){
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}else if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}
			
			
			
		} {
			long N = scan.nextLong();
			long K = scan.nextLong();
			
			int answer = 0;
			if(N==K) {
				answer = 0;
				System.out.println(0);
			}else if(N<K){
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}else if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}
			
			
			
		} Scanner scan = new Scanner(System.in) Scanner scan = new Scanner(System.in) new Scanner(System.in) long N = scan.nextLong(); N scan.nextLong() scan.nextLong scan long K = scan.nextLong(); K scan.nextLong() scan.nextLong scan int answer = 0; answer 0 if(N==K) {
				answer = 0;
				System.out.println(0);
			}else if(N<K){
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}else if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			} N==K N K {
				answer = 0;
				System.out.println(0);
			} answer = 0 answer 0 System.out.println(0) System.out.println System.out System System.out 0 if(N<K){
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}else if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			} N<K N K {
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			} long sa = Math.abs(N-K); sa Math.abs(N-K) Math.abs Math N-K N K long min  = sa; min sa long sb = Math.abs(sa-K); sb Math.abs(sa-K) Math.abs Math sa-K sa K if(sb<min)min = sb; sb<min sb min min = sb min sb System.out.println(min) System.out.println System.out System System.out min if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			} N>K N K {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			} long amari = N%K; amari N%K N K long sa = Math.abs(amari-K); sa Math.abs(amari-K) Math.abs Math amari-K amari K long min  = sa; min sa long sb = Math.abs(sa-K); sb Math.abs(sa-K) Math.abs Math sa-K sa K if(sb<min)min = sb; sb<min sb min min = sb min sb System.out.println(min) System.out.println System.out System System.out min String[]args args public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			long N = scan.nextLong();
			long K = scan.nextLong();
			
			int answer = 0;
			if(N==K) {
				answer = 0;
				System.out.println(0);
			}else if(N<K){
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}else if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}
			
			
			
		}
		
		
	}
		

} public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			long N = scan.nextLong();
			long K = scan.nextLong();
			
			int answer = 0;
			if(N==K) {
				answer = 0;
				System.out.println(0);
			}else if(N<K){
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}else if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}
			
			
			
		}
		
		
	}
		

} Main
import java.util.Scanner;

public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			long N = scan.nextLong();
			long K = scan.nextLong();
			
			int answer = 0;
			if(N==K) {
				answer = 0;
				System.out.println(0);
			}else if(N<K){
				long sa = Math.abs(N-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}else if(N>K) {
				long amari = N%K;
				long sa = Math.abs(amari-K);
				long min  = sa;
				long sb = Math.abs(sa-K);
				if(sb<min)min = sb;
				System.out.println(min);
			}
			
			
			
		}
		
		
	}
		

}
