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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo = reader.readLine().split(" ");
        long N = Long.parseLong(lo[0]);
        long K = Long.parseLong(lo[1]);
        if(N<K){
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else if(N>K){
            long q = N/K -1;
            N = N - q*K;
            N = Math.abs(N-K);
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else System.out.println(0);
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo = reader.readLine().split(" ");
        long N = Long.parseLong(lo[0]);
        long K = Long.parseLong(lo[1]);
        if(N<K){
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else if(N>K){
            long q = N/K -1;
            N = N - q*K;
            N = Math.abs(N-K);
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else System.out.println(0);
    }
} Main public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo = reader.readLine().split(" ");
        long N = Long.parseLong(lo[0]);
        long K = Long.parseLong(lo[1]);
        if(N<K){
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else if(N>K){
            long q = N/K -1;
            N = N - q*K;
            N = Math.abs(N-K);
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else System.out.println(0);
    } main {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo = reader.readLine().split(" ");
        long N = Long.parseLong(lo[0]);
        long K = Long.parseLong(lo[1]);
        if(N<K){
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else if(N>K){
            long q = N/K -1;
            N = N - q*K;
            N = Math.abs(N-K);
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else System.out.println(0);
    } BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); reader new BufferedReader(new InputStreamReader(System.in)) String[] lo = reader.readLine().split(" "); lo reader.readLine().split(" ") reader.readLine().split reader.readLine() reader.readLine reader " " long N = Long.parseLong(lo[0]); N Long.parseLong(lo[0]) Long.parseLong Long lo[0] lo 0 long K = Long.parseLong(lo[1]); K Long.parseLong(lo[1]) Long.parseLong Long lo[1] lo 1 if(N<K){
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else if(N>K){
            long q = N/K -1;
            N = N - q*K;
            N = Math.abs(N-K);
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else System.out.println(0); N<K N K {
            System.out.println(Math.min(N,Math.abs(N-K)));
        } System.out.println(Math.min(N,Math.abs(N-K))) System.out.println System.out System System.out Math.min(N,Math.abs(N-K)) Math.min Math N Math.abs(N-K) Math.abs Math N-K N K if(N>K){
            long q = N/K -1;
            N = N - q*K;
            N = Math.abs(N-K);
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else System.out.println(0); N>K N K {
            long q = N/K -1;
            N = N - q*K;
            N = Math.abs(N-K);
            System.out.println(Math.min(N,Math.abs(N-K)));
        } long q = N/K -1; q N/K -1 N/K N K 1 N = N - q*K N N - q*K N q*K q K N = Math.abs(N-K) N Math.abs(N-K) Math.abs Math N-K N K System.out.println(Math.min(N,Math.abs(N-K))) System.out.println System.out System System.out Math.min(N,Math.abs(N-K)) Math.min Math N Math.abs(N-K) Math.abs Math N-K N K System.out.println(0) System.out.println System.out System System.out 0 String[] args args public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo = reader.readLine().split(" ");
        long N = Long.parseLong(lo[0]);
        long K = Long.parseLong(lo[1]);
        if(N<K){
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else if(N>K){
            long q = N/K -1;
            N = N - q*K;
            N = Math.abs(N-K);
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else System.out.println(0);
    }
} public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo = reader.readLine().split(" ");
        long N = Long.parseLong(lo[0]);
        long K = Long.parseLong(lo[1]);
        if(N<K){
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else if(N>K){
            long q = N/K -1;
            N = N - q*K;
            N = Math.abs(N-K);
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else System.out.println(0);
    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo = reader.readLine().split(" ");
        long N = Long.parseLong(lo[0]);
        long K = Long.parseLong(lo[1]);
        if(N<K){
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else if(N>K){
            long q = N/K -1;
            N = N - q*K;
            N = Math.abs(N-K);
            System.out.println(Math.min(N,Math.abs(N-K)));
        }
        else System.out.println(0);
    }
}
