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 now = 0;
        long min = N;

        now = N % K;
        do{
            now = Math.abs(now - K);
            if(min > now) {
               min = now;
            }
        }while(N % K != now);
        System.out.println(min);
    }
} 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 now = 0;
        long min = N;

        now = N % K;
        do{
            now = Math.abs(now - K);
            if(min > now) {
               min = now;
            }
        }while(N % K != now);
        System.out.println(min);
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();
        long now = 0;
        long min = N;

        now = N % K;
        do{
            now = Math.abs(now - K);
            if(min > now) {
               min = now;
            }
        }while(N % K != now);
        System.out.println(min);
    } main {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();
        long now = 0;
        long min = N;

        now = N % K;
        do{
            now = Math.abs(now - K);
            if(min > now) {
               min = now;
            }
        }while(N % K != now);
        System.out.println(min);
    } 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 now = 0; now 0 long min = N; min N now = N % K now N % K N K do{
            now = Math.abs(now - K);
            if(min > now) {
               min = now;
            }
        }while(N % K != now); N % K != now N % K N K now {
            now = Math.abs(now - K);
            if(min > now) {
               min = now;
            }
        } now = Math.abs(now - K) now Math.abs(now - K) Math.abs Math now - K now K if(min > now) {
               min = now;
            } min > now min now {
               min = now;
            } min = now min now System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();
        long now = 0;
        long min = N;

        now = N % K;
        do{
            now = Math.abs(now - K);
            if(min > now) {
               min = now;
            }
        }while(N % K != now);
        System.out.println(min);
    }
} public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long K = sc.nextLong();
        long now = 0;
        long min = N;

        now = N % K;
        do{
            now = Math.abs(now - K);
            if(min > now) {
               min = now;
            }
        }while(N % K != now);
        System.out.println(min);
    }
} 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 now = 0;
        long min = N;

        now = N % K;
        do{
            now = Math.abs(now - K);
            if(min > now) {
               min = now;
            }
        }while(N % K != now);
        System.out.println(min);
    }
}