import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		long N=scan.nextLong();
		long K=scan.nextLong();
		
		
		
	
		if(N==0||N%K==0) {
			System.out.println("0");
		}
		else {
			List<Long> list=new ArrayList<Long>();
			boolean flag=false,flagb=false;
			if(N>K)N=N%K;
			for(;;) {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			}
			if(flagb) {
				System.out.println("1");
			}
			else {
				long min=list.get(0);
				for(Long i:list) {
					if(min>i)min=i;
				}
				System.out.println(min);
			}
		}

	}

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

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		long N=scan.nextLong();
		long K=scan.nextLong();
		
		
		
	
		if(N==0||N%K==0) {
			System.out.println("0");
		}
		else {
			List<Long> list=new ArrayList<Long>();
			boolean flag=false,flagb=false;
			if(N>K)N=N%K;
			for(;;) {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			}
			if(flagb) {
				System.out.println("1");
			}
			else {
				long min=list.get(0);
				for(Long i:list) {
					if(min>i)min=i;
				}
				System.out.println(min);
			}
		}

	}

} Main public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		long N=scan.nextLong();
		long K=scan.nextLong();
		
		
		
	
		if(N==0||N%K==0) {
			System.out.println("0");
		}
		else {
			List<Long> list=new ArrayList<Long>();
			boolean flag=false,flagb=false;
			if(N>K)N=N%K;
			for(;;) {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			}
			if(flagb) {
				System.out.println("1");
			}
			else {
				long min=list.get(0);
				for(Long i:list) {
					if(min>i)min=i;
				}
				System.out.println(min);
			}
		}

	} main {
		Scanner scan=new Scanner(System.in);
		long N=scan.nextLong();
		long K=scan.nextLong();
		
		
		
	
		if(N==0||N%K==0) {
			System.out.println("0");
		}
		else {
			List<Long> list=new ArrayList<Long>();
			boolean flag=false,flagb=false;
			if(N>K)N=N%K;
			for(;;) {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			}
			if(flagb) {
				System.out.println("1");
			}
			else {
				long min=list.get(0);
				for(Long i:list) {
					if(min>i)min=i;
				}
				System.out.println(min);
			}
		}

	} Scanner scan=new Scanner(System.in); scan new Scanner(System.in) long N=scan.nextLong(); N scan.nextLong() scan.nextLong scan long K=scan.nextLong(); K scan.nextLong() scan.nextLong scan if(N==0||N%K==0) {
			System.out.println("0");
		}
		else {
			List<Long> list=new ArrayList<Long>();
			boolean flag=false,flagb=false;
			if(N>K)N=N%K;
			for(;;) {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			}
			if(flagb) {
				System.out.println("1");
			}
			else {
				long min=list.get(0);
				for(Long i:list) {
					if(min>i)min=i;
				}
				System.out.println(min);
			}
		} N==0||N%K==0 N==0 N 0 N%K==0 N%K N K 0 {
			System.out.println("0");
		} System.out.println("0") System.out.println System.out System System.out "0" {
			List<Long> list=new ArrayList<Long>();
			boolean flag=false,flagb=false;
			if(N>K)N=N%K;
			for(;;) {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			}
			if(flagb) {
				System.out.println("1");
			}
			else {
				long min=list.get(0);
				for(Long i:list) {
					if(min>i)min=i;
				}
				System.out.println(min);
			}
		} List<Long> list=new ArrayList<Long>(); list new ArrayList<Long>() boolean flag=false flag false flagb=false; flagb false if(N>K)N=N%K; N>K N K N=N%K N N%K N K for(;;) {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			} ; {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			} {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			} long A=Math.abs(N-K); A Math.abs(N-K) Math.abs Math N-K N K N=A N A if(A==1) {
					flag=true;
					flagb=true;
				} A==1 A 1 {
					flag=true;
					flagb=true;
				} flag=true flag true flagb=true flagb true for(Long i:list) {
					if(i==A)flag=true;
				} Long i list {
					if(i==A)flag=true;
				} {
					if(i==A)flag=true;
				} if(i==A)flag=true; i==A i A flag=true flag true if(flag)break; flag break; list.add(A) list.add list A if(flagb) {
				System.out.println("1");
			}
			else {
				long min=list.get(0);
				for(Long i:list) {
					if(min>i)min=i;
				}
				System.out.println(min);
			} flagb {
				System.out.println("1");
			} System.out.println("1") System.out.println System.out System System.out "1" {
				long min=list.get(0);
				for(Long i:list) {
					if(min>i)min=i;
				}
				System.out.println(min);
			} long min=list.get(0); min list.get(0) list.get list 0 for(Long i:list) {
					if(min>i)min=i;
				} Long i list {
					if(min>i)min=i;
				} {
					if(min>i)min=i;
				} if(min>i)min=i; min>i min i min=i min i 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) {
		Scanner scan=new Scanner(System.in);
		long N=scan.nextLong();
		long K=scan.nextLong();
		
		
		
	
		if(N==0||N%K==0) {
			System.out.println("0");
		}
		else {
			List<Long> list=new ArrayList<Long>();
			boolean flag=false,flagb=false;
			if(N>K)N=N%K;
			for(;;) {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			}
			if(flagb) {
				System.out.println("1");
			}
			else {
				long min=list.get(0);
				for(Long i:list) {
					if(min>i)min=i;
				}
				System.out.println(min);
			}
		}

	}

} public class Main {

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		long N=scan.nextLong();
		long K=scan.nextLong();
		
		
		
	
		if(N==0||N%K==0) {
			System.out.println("0");
		}
		else {
			List<Long> list=new ArrayList<Long>();
			boolean flag=false,flagb=false;
			if(N>K)N=N%K;
			for(;;) {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			}
			if(flagb) {
				System.out.println("1");
			}
			else {
				long min=list.get(0);
				for(Long i:list) {
					if(min>i)min=i;
				}
				System.out.println(min);
			}
		}

	}

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

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		long N=scan.nextLong();
		long K=scan.nextLong();
		
		
		
	
		if(N==0||N%K==0) {
			System.out.println("0");
		}
		else {
			List<Long> list=new ArrayList<Long>();
			boolean flag=false,flagb=false;
			if(N>K)N=N%K;
			for(;;) {
				long A=Math.abs(N-K);
				
				N=A;
				if(A==1) {
					flag=true;
					flagb=true;
				}
				for(Long i:list) {
					if(i==A)flag=true;
				}
				if(flag)break;
				list.add(A);
			}
			if(flagb) {
				System.out.println("1");
			}
			else {
				long min=list.get(0);
				for(Long i:list) {
					if(min>i)min=i;
				}
				System.out.println(min);
			}
		}

	}

}
