import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();
    long g = gcd(n, k);
    n = n / g;
    k = k / g;
    n = (n % k);
    System.out.println(Math.min(g * n, g * (k - n)));
  }

  public static long gcd(long x, long y) {
    if(y == 0) return x;
    return gcd(y, (x % y));
  }
} import java.util.*; util.* java public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();
    long g = gcd(n, k);
    n = n / g;
    k = k / g;
    n = (n % k);
    System.out.println(Math.min(g * n, g * (k - n)));
  }

  public static long gcd(long x, long y) {
    if(y == 0) return x;
    return gcd(y, (x % y));
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();
    long g = gcd(n, k);
    n = n / g;
    k = k / g;
    n = (n % k);
    System.out.println(Math.min(g * n, g * (k - n)));
  } main {
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();
    long g = gcd(n, k);
    n = n / g;
    k = k / g;
    n = (n % k);
    System.out.println(Math.min(g * n, g * (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 long g = gcd(n, k); g gcd(n, k) gcd n k n = n / g n n / g n g k = k / g k k / g k g n = (n % k) n (n % k) n k System.out.println(Math.min(g * n, g * (k - n))) System.out.println System.out System System.out Math.min(g * n, g * (k - n)) Math.min Math g * n g n g * (k - n) g (k - n) k n String[] args args public static long gcd(long x, long y) {
    if(y == 0) return x;
    return gcd(y, (x % y));
  } gcd {
    if(y == 0) return x;
    return gcd(y, (x % y));
  } if(y == 0) return x; y == 0 y 0 return x; x return gcd(y, (x % y)); gcd(y, (x % y)) gcd y (x % y) x y long x x long y y public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();
    long g = gcd(n, k);
    n = n / g;
    k = k / g;
    n = (n % k);
    System.out.println(Math.min(g * n, g * (k - n)));
  }

  public static long gcd(long x, long y) {
    if(y == 0) return x;
    return gcd(y, (x % y));
  }
} public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();
    long g = gcd(n, k);
    n = n / g;
    k = k / g;
    n = (n % k);
    System.out.println(Math.min(g * n, g * (k - n)));
  }

  public static long gcd(long x, long y) {
    if(y == 0) return x;
    return gcd(y, (x % y));
  }
} Main
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long k = sc.nextLong();
    long g = gcd(n, k);
    n = n / g;
    k = k / g;
    n = (n % k);
    System.out.println(Math.min(g * n, g * (k - n)));
  }

  public static long gcd(long x, long y) {
    if(y == 0) return x;
    return gcd(y, (x % y));
  }
}