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

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    long n = Long.parseLong(str[0]);
    long k = Long.parseLong(str[1]);
    System.out.println(n%k<=k-n%k?n%k:k-n%k);
  }
}
 import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.IOException; IOException java.io public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    long n = Long.parseLong(str[0]);
    long k = Long.parseLong(str[1]);
    System.out.println(n%k<=k-n%k?n%k:k-n%k);
  }
} Main public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    long n = Long.parseLong(str[0]);
    long k = Long.parseLong(str[1]);
    System.out.println(n%k<=k-n%k?n%k:k-n%k);
  } main {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    long n = Long.parseLong(str[0]);
    long k = Long.parseLong(str[1]);
    System.out.println(n%k<=k-n%k?n%k:k-n%k);
  } BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in new BufferedReader(new InputStreamReader(System.in)) String[] str = in.readLine().split(" "); str in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " long n = Long.parseLong(str[0]); n Long.parseLong(str[0]) Long.parseLong Long str[0] str 0 long k = Long.parseLong(str[1]); k Long.parseLong(str[1]) Long.parseLong Long str[1] str 1 System.out.println(n%k<=k-n%k?n%k:k-n%k) System.out.println System.out System System.out n%k<=k-n%k?n%k:k-n%k n%k<=k-n%k n%k n k k-n%k k n%k n k n%k n k k-n%k k n%k n k String[] args args public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    long n = Long.parseLong(str[0]);
    long k = Long.parseLong(str[1]);
    System.out.println(n%k<=k-n%k?n%k:k-n%k);
  }
} public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    long n = Long.parseLong(str[0]);
    long k = Long.parseLong(str[1]);
    System.out.println(n%k<=k-n%k?n%k:k-n%k);
  }
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] str = in.readLine().split(" ");
    long n = Long.parseLong(str[0]);
    long k = Long.parseLong(str[1]);
    System.out.println(n%k<=k-n%k?n%k:k-n%k);
  }
}

