import java.io.*;
import java.util.*;

public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int[] size; // size of subtree, including self
    List<Integer>[] graph;
    long[] dp,ans;
    final long mod = 1000000000+7;
    long[] frac,inv; // inv[i] = the inversion of i! mod M
    public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    }
    long solve() throws IOException{
        long n = sc.nextLong(), k = sc.nextLong();
        if(n<=k){
            return Math.min(n,k-n);
        }
        long dif = n-k;
        long time = dif/k;
        n -= time*k;
        if(n>k) n -= k;
        return Math.min(n,k-n);
//        return 0;
    }
}
 import java.io.*; io.* java import java.util.*; util.* java public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int[] size; // size of subtree, including self
    List<Integer>[] graph;
    long[] dp,ans;
    final long mod = 1000000000+7;
    long[] frac,inv; // inv[i] = the inversion of i! mod M
    public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    }
    long solve() throws IOException{
        long n = sc.nextLong(), k = sc.nextLong();
        if(n<=k){
            return Math.min(n,k-n);
        }
        long dif = n-k;
        long time = dif/k;
        n -= time*k;
        if(n>k) n -= k;
        return Math.min(n,k-n);
//        return 0;
    }
} Main static PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); reader new BufferedReader(new InputStreamReader(System.in)) int[] size; // size of subtree, including self size List<Integer>[] graph; graph long[] dp dp ans; ans final long mod = 1000000000+7; mod 1000000000+7 1000000000 7 long[] frac frac inv; // inv[i] = the inversion of i! mod M inv public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    } main {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    } Main main = new Main(); main new Main() System.out.println(main.solve()) System.out.println System.out System System.out main.solve() main.solve main String[] args args long solve() throws IOException{
        long n = sc.nextLong(), k = sc.nextLong();
        if(n<=k){
            return Math.min(n,k-n);
        }
        long dif = n-k;
        long time = dif/k;
        n -= time*k;
        if(n>k) n -= k;
        return Math.min(n,k-n);
//        return 0;
    } solve {
        long n = sc.nextLong(), k = sc.nextLong();
        if(n<=k){
            return Math.min(n,k-n);
        }
        long dif = n-k;
        long time = dif/k;
        n -= time*k;
        if(n>k) n -= k;
        return Math.min(n,k-n);
//        return 0;
    } long n = sc.nextLong() n sc.nextLong() sc.nextLong sc k = sc.nextLong(); k sc.nextLong() sc.nextLong sc if(n<=k){
            return Math.min(n,k-n);
        } n<=k n k {
            return Math.min(n,k-n);
        } return Math.min(n,k-n); Math.min(n,k-n) Math.min Math n k-n k n long dif = n-k; dif n-k n k long time = dif/k; time dif/k dif k n -= time*k n time*k time k if(n>k) n -= k; n>k n k n -= k n k return Math.min(n,k-n); Math.min(n,k-n) Math.min Math n k-n k n public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int[] size; // size of subtree, including self
    List<Integer>[] graph;
    long[] dp,ans;
    final long mod = 1000000000+7;
    long[] frac,inv; // inv[i] = the inversion of i! mod M
    public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    }
    long solve() throws IOException{
        long n = sc.nextLong(), k = sc.nextLong();
        if(n<=k){
            return Math.min(n,k-n);
        }
        long dif = n-k;
        long time = dif/k;
        n -= time*k;
        if(n>k) n -= k;
        return Math.min(n,k-n);
//        return 0;
    }
} public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int[] size; // size of subtree, including self
    List<Integer>[] graph;
    long[] dp,ans;
    final long mod = 1000000000+7;
    long[] frac,inv; // inv[i] = the inversion of i! mod M
    public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    }
    long solve() throws IOException{
        long n = sc.nextLong(), k = sc.nextLong();
        if(n<=k){
            return Math.min(n,k-n);
        }
        long dif = n-k;
        long time = dif/k;
        n -= time*k;
        if(n>k) n -= k;
        return Math.min(n,k-n);
//        return 0;
    }
} Main
import java.io.*;
import java.util.*;

public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int[] size; // size of subtree, including self
    List<Integer>[] graph;
    long[] dp,ans;
    final long mod = 1000000000+7;
    long[] frac,inv; // inv[i] = the inversion of i! mod M
    public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
//        out.flush();
        System.out.println(main.solve());
    }
    long solve() throws IOException{
        long n = sc.nextLong(), k = sc.nextLong();
        if(n<=k){
            return Math.min(n,k-n);
        }
        long dif = n-k;
        long time = dif/k;
        n -= time*k;
        if(n>k) n -= k;
        return Math.min(n,k-n);
//        return 0;
    }
}

