import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		long N = Long.parseLong(st.nextToken());
		long M = Long.parseLong(st.nextToken());
		if (N % M == 0) {
			System.out.println("0");
			return;
		}
		if (N > M) {
			N %= M;
		}
		System.out.println(Math.min(N, M - N));
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		long N = Long.parseLong(st.nextToken());
		long M = Long.parseLong(st.nextToken());
		if (N % M == 0) {
			System.out.println("0");
			return;
		}
		if (N > M) {
			N %= M;
		}
		System.out.println(Math.min(N, M - N));
	}
} Main public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		long N = Long.parseLong(st.nextToken());
		long M = Long.parseLong(st.nextToken());
		if (N % M == 0) {
			System.out.println("0");
			return;
		}
		if (N > M) {
			N %= M;
		}
		System.out.println(Math.min(N, M - N));
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		long N = Long.parseLong(st.nextToken());
		long M = Long.parseLong(st.nextToken());
		if (N % M == 0) {
			System.out.println("0");
			return;
		}
		if (N > M) {
			N %= M;
		}
		System.out.println(Math.min(N, M - N));
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st = new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) long N = Long.parseLong(st.nextToken()); N Long.parseLong(st.nextToken()) Long.parseLong Long st.nextToken() st.nextToken st long M = Long.parseLong(st.nextToken()); M Long.parseLong(st.nextToken()) Long.parseLong Long st.nextToken() st.nextToken st if (N % M == 0) {
			System.out.println("0");
			return;
		} N % M == 0 N % M N M 0 {
			System.out.println("0");
			return;
		} System.out.println("0") System.out.println System.out System System.out "0" return; if (N > M) {
			N %= M;
		} N > M N M {
			N %= M;
		} N %= M N M System.out.println(Math.min(N, M - N)) System.out.println System.out System System.out Math.min(N, M - N) Math.min Math N M - N M N String[] args args public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		long N = Long.parseLong(st.nextToken());
		long M = Long.parseLong(st.nextToken());
		if (N % M == 0) {
			System.out.println("0");
			return;
		}
		if (N > M) {
			N %= M;
		}
		System.out.println(Math.min(N, M - N));
	}
} public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		long N = Long.parseLong(st.nextToken());
		long M = Long.parseLong(st.nextToken());
		if (N % M == 0) {
			System.out.println("0");
			return;
		}
		if (N > M) {
			N %= M;
		}
		System.out.println(Math.min(N, M - N));
	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		long N = Long.parseLong(st.nextToken());
		long M = Long.parseLong(st.nextToken());
		if (N % M == 0) {
			System.out.println("0");
			return;
		}
		if (N > M) {
			N %= M;
		}
		System.out.println(Math.min(N, M - N));
	}
}