import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {

	static final boolean debugMode = false;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long k = sc.nextLong();
		n = n - k * (n / k);

		System.out.println(Math.min(n, k - n));

	}

	static int readInt(Scanner sc) {
		return Integer.parseInt(sc.next());
	}

	static void log(String s) {
		if (debugMode) {
			System.out.println(s);
		}
		return;
	}
}

class Pair {
	private int x;
	private int y;

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Pair() {

	}

	public Pair(int x, int y) {
		this.x = x;
		this.y = y;
	}

} import java.math.BigDecimal; BigDecimal java.math import java.math.RoundingMode; RoundingMode java.math import java.util.ArrayList; ArrayList java.util import java.util.Collections; Collections java.util import java.util.Comparator; Comparator java.util import java.util.List; List java.util import java.util.Scanner; Scanner java.util public class Main {

	static final boolean debugMode = false;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long k = sc.nextLong();
		n = n - k * (n / k);

		System.out.println(Math.min(n, k - n));

	}

	static int readInt(Scanner sc) {
		return Integer.parseInt(sc.next());
	}

	static void log(String s) {
		if (debugMode) {
			System.out.println(s);
		}
		return;
	}
} Main static final boolean debugMode = false; debugMode false public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long k = sc.nextLong();
		n = n - k * (n / k);

		System.out.println(Math.min(n, k - n));

	} main {
		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long k = sc.nextLong();
		n = n - k * (n / k);

		System.out.println(Math.min(n, k - n));

	} 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 n = n - k * (n / k) n n - k * (n / k) n k * (n / k) k (n / k) n k System.out.println(Math.min(n, k - n)) System.out.println System.out System System.out Math.min(n, k - n) Math.min Math n k - n k n String[] args args static int readInt(Scanner sc) {
		return Integer.parseInt(sc.next());
	} readInt {
		return Integer.parseInt(sc.next());
	} return Integer.parseInt(sc.next()); Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc Scanner sc sc static void log(String s) {
		if (debugMode) {
			System.out.println(s);
		}
		return;
	} log {
		if (debugMode) {
			System.out.println(s);
		}
		return;
	} if (debugMode) {
			System.out.println(s);
		} debugMode {
			System.out.println(s);
		} System.out.println(s) System.out.println System.out System System.out s return; String s s class Pair {
	private int x;
	private int y;

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Pair() {

	}

	public Pair(int x, int y) {
		this.x = x;
		this.y = y;
	}

} Pair private int x; x private int y; y public int getX() {
		return x;
	} getX {
		return x;
	} return x; x public void setX(int x) {
		this.x = x;
	} setX {
		this.x = x;
	} this.x = x this.x this this.x x int x x public int getY() {
		return y;
	} getY {
		return y;
	} return y; y public void setY(int y) {
		this.y = y;
	} setY {
		this.y = y;
	} this.y = y this.y this this.y y int y y public Pair() {

	} Pair {

	} public Pair(int x, int y) {
		this.x = x;
		this.y = y;
	} Pair {
		this.x = x;
		this.y = y;
	} this.x = x this.x this this.x x this.y = y this.y this this.y y int x x int y y public class Main {

	static final boolean debugMode = false;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long k = sc.nextLong();
		n = n - k * (n / k);

		System.out.println(Math.min(n, k - n));

	}

	static int readInt(Scanner sc) {
		return Integer.parseInt(sc.next());
	}

	static void log(String s) {
		if (debugMode) {
			System.out.println(s);
		}
		return;
	}
} public class Main {

	static final boolean debugMode = false;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long k = sc.nextLong();
		n = n - k * (n / k);

		System.out.println(Math.min(n, k - n));

	}

	static int readInt(Scanner sc) {
		return Integer.parseInt(sc.next());
	}

	static void log(String s) {
		if (debugMode) {
			System.out.println(s);
		}
		return;
	}
} Main
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {

	static final boolean debugMode = false;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		long n = sc.nextLong();
		long k = sc.nextLong();
		n = n - k * (n / k);

		System.out.println(Math.min(n, k - n));

	}

	static int readInt(Scanner sc) {
		return Integer.parseInt(sc.next());
	}

	static void log(String s) {
		if (debugMode) {
			System.out.println(s);
		}
		return;
	}
}

class Pair {
	private int x;
	private int y;

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Pair() {

	}

	public Pair(int x, int y) {
		this.x = x;
		this.y = y;
	}

}