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

class Main {
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] ips = br.readLine().split(" ");
        long n = Long.parseLong(ips[0]);
        long k = Long.parseLong(ips[1]);
        if(n >= k)
            n %= k;
        long res = Math.min(n, k-n);
        System.out.println(res);
    }
} import java.io.*; io.* java import java.util.*; util.* java class Main {
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] ips = br.readLine().split(" ");
        long n = Long.parseLong(ips[0]);
        long k = Long.parseLong(ips[1]);
        if(n >= k)
            n %= k;
        long res = Math.min(n, k-n);
        System.out.println(res);
    }
} Main public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] ips = br.readLine().split(" ");
        long n = Long.parseLong(ips[0]);
        long k = Long.parseLong(ips[1]);
        if(n >= k)
            n %= k;
        long res = Math.min(n, k-n);
        System.out.println(res);
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] ips = br.readLine().split(" ");
        long n = Long.parseLong(ips[0]);
        long k = Long.parseLong(ips[1]);
        if(n >= k)
            n %= k;
        long res = Math.min(n, k-n);
        System.out.println(res);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] ips = br.readLine().split(" "); ips br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long n = Long.parseLong(ips[0]); n Long.parseLong(ips[0]) Long.parseLong Long ips[0] ips 0 long k = Long.parseLong(ips[1]); k Long.parseLong(ips[1]) Long.parseLong Long ips[1] ips 1 if(n >= k)
            n %= k; n >= k n k n %= k n k long res = Math.min(n, k-n); res Math.min(n, k-n) Math.min Math n k-n k n System.out.println(res) System.out.println System.out System System.out res String args[] args
import java.io.*;
import java.util.*;

class Main {
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] ips = br.readLine().split(" ");
        long n = Long.parseLong(ips[0]);
        long k = Long.parseLong(ips[1]);
        if(n >= k)
            n %= k;
        long res = Math.min(n, k-n);
        System.out.println(res);
    }
}