import java.util.Scanner;

class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		Long N = Long.parseLong(sc.next());
		Long K = Long.parseLong(sc.next());
		Long J = 0L;

		while(true) {
			if(N/K > 10) {
				J = N;
				N -= (N/K)*K;
			} else {
				J = N;
				N -= K;
			}
			if(Math.abs(J) <= Math.abs(N)) {
				break;
			}
		}
		System.out.println(Math.min(Math.abs(J), Math.abs(N)));
	}
} import java.util.Scanner; Scanner java.util class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		Long N = Long.parseLong(sc.next());
		Long K = Long.parseLong(sc.next());
		Long J = 0L;

		while(true) {
			if(N/K > 10) {
				J = N;
				N -= (N/K)*K;
			} else {
				J = N;
				N -= K;
			}
			if(Math.abs(J) <= Math.abs(N)) {
				break;
			}
		}
		System.out.println(Math.min(Math.abs(J), Math.abs(N)));
	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		Long N = Long.parseLong(sc.next());
		Long K = Long.parseLong(sc.next());
		Long J = 0L;

		while(true) {
			if(N/K > 10) {
				J = N;
				N -= (N/K)*K;
			} else {
				J = N;
				N -= K;
			}
			if(Math.abs(J) <= Math.abs(N)) {
				break;
			}
		}
		System.out.println(Math.min(Math.abs(J), Math.abs(N)));
	} main {

		Scanner sc = new Scanner(System.in);
		Long N = Long.parseLong(sc.next());
		Long K = Long.parseLong(sc.next());
		Long J = 0L;

		while(true) {
			if(N/K > 10) {
				J = N;
				N -= (N/K)*K;
			} else {
				J = N;
				N -= K;
			}
			if(Math.abs(J) <= Math.abs(N)) {
				break;
			}
		}
		System.out.println(Math.min(Math.abs(J), Math.abs(N)));
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) Long N = Long.parseLong(sc.next()); N Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc Long K = Long.parseLong(sc.next()); K Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc Long J = 0L; J 0L while(true) {
			if(N/K > 10) {
				J = N;
				N -= (N/K)*K;
			} else {
				J = N;
				N -= K;
			}
			if(Math.abs(J) <= Math.abs(N)) {
				break;
			}
		} true {
			if(N/K > 10) {
				J = N;
				N -= (N/K)*K;
			} else {
				J = N;
				N -= K;
			}
			if(Math.abs(J) <= Math.abs(N)) {
				break;
			}
		} if(N/K > 10) {
				J = N;
				N -= (N/K)*K;
			} else {
				J = N;
				N -= K;
			} N/K > 10 N/K N K 10 {
				J = N;
				N -= (N/K)*K;
			} J = N J N N -= (N/K)*K N (N/K)*K (N/K) N K K {
				J = N;
				N -= K;
			} J = N J N N -= K N K if(Math.abs(J) <= Math.abs(N)) {
				break;
			} Math.abs(J) <= Math.abs(N) Math.abs(J) Math.abs Math J Math.abs(N) Math.abs Math N {
				break;
			} break; System.out.println(Math.min(Math.abs(J), Math.abs(N))) System.out.println System.out System System.out Math.min(Math.abs(J), Math.abs(N)) Math.min Math Math.abs(J) Math.abs Math J Math.abs(N) Math.abs Math N String[] args args
import java.util.Scanner;

class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		Long N = Long.parseLong(sc.next());
		Long K = Long.parseLong(sc.next());
		Long J = 0L;

		while(true) {
			if(N/K > 10) {
				J = N;
				N -= (N/K)*K;
			} else {
				J = N;
				N -= K;
			}
			if(Math.abs(J) <= Math.abs(N)) {
				break;
			}
		}
		System.out.println(Math.min(Math.abs(J), Math.abs(N)));
	}
}
