import java.util.*;
public class Main {//ここが Main になっている
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Long N = Long.parseLong(sc.next());
        Long K = Long.parseLong(sc.next());
        Long next_n;
        Long t = N % K;
        Long k_t = K - t;
        if (t<k_t){
            System.out.println(t);
        }
        if (t>=k_t){
            System.out.println(k_t);
        }
    }
} import java.util.*; util.* java public class Main {//ここが Main になっている
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Long N = Long.parseLong(sc.next());
        Long K = Long.parseLong(sc.next());
        Long next_n;
        Long t = N % K;
        Long k_t = K - t;
        if (t<k_t){
            System.out.println(t);
        }
        if (t>=k_t){
            System.out.println(k_t);
        }
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Long N = Long.parseLong(sc.next());
        Long K = Long.parseLong(sc.next());
        Long next_n;
        Long t = N % K;
        Long k_t = K - t;
        if (t<k_t){
            System.out.println(t);
        }
        if (t>=k_t){
            System.out.println(k_t);
        }
    } main {
        Scanner sc = new Scanner(System.in);
        Long N = Long.parseLong(sc.next());
        Long K = Long.parseLong(sc.next());
        Long next_n;
        Long t = N % K;
        Long k_t = K - t;
        if (t<k_t){
            System.out.println(t);
        }
        if (t>=k_t){
            System.out.println(k_t);
        }
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) Long N = Long.parseLong(sc.next()); N Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc Long K = Long.parseLong(sc.next()); K Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc Long next_n; next_n Long t = N % K; t N % K N K Long k_t = K - t; k_t K - t K t if (t<k_t){
            System.out.println(t);
        } t<k_t t k_t {
            System.out.println(t);
        } System.out.println(t) System.out.println System.out System System.out t if (t>=k_t){
            System.out.println(k_t);
        } t>=k_t t k_t {
            System.out.println(k_t);
        } System.out.println(k_t) System.out.println System.out System System.out k_t String[] args args public class Main {//ここが Main になっている
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Long N = Long.parseLong(sc.next());
        Long K = Long.parseLong(sc.next());
        Long next_n;
        Long t = N % K;
        Long k_t = K - t;
        if (t<k_t){
            System.out.println(t);
        }
        if (t>=k_t){
            System.out.println(k_t);
        }
    }
} public class Main {//ここが Main になっている
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Long N = Long.parseLong(sc.next());
        Long K = Long.parseLong(sc.next());
        Long next_n;
        Long t = N % K;
        Long k_t = K - t;
        if (t<k_t){
            System.out.println(t);
        }
        if (t>=k_t){
            System.out.println(k_t);
        }
    }
} Main
import java.util.*;
public class Main {//ここが Main になっている
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Long N = Long.parseLong(sc.next());
        Long K = Long.parseLong(sc.next());
        Long next_n;
        Long t = N % K;
        Long k_t = K - t;
        if (t<k_t){
            System.out.println(t);
        }
        if (t>=k_t){
            System.out.println(k_t);
        }
    }
}
