import java.util.Scanner;
import java.lang.Math;

public class 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());
        System.out.println(value(N,K));
    }

    public static long value(long N, long K) {
        if (N%K > K-(N%K)) {
            return K - (N%K);
        }

        else {
            return N%K;
        }
    }
}

 import java.util.Scanner; Scanner java.util import java.lang.Math; Math java.lang public class 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());
        System.out.println(value(N,K));
    }

    public static long value(long N, long K) {
        if (N%K > K-(N%K)) {
            return K - (N%K);
        }

        else {
            return N%K;
        }
    }
} 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());
        System.out.println(value(N,K));
    } main {
        Scanner sc = new Scanner(System.in);
        long N = Long.parseLong(sc.next());
        long K = Long.parseLong(sc.next());
        System.out.println(value(N,K));
    } 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 System.out.println(value(N,K)) System.out.println System.out System System.out value(N,K) value N K String[] args args public static long value(long N, long K) {
        if (N%K > K-(N%K)) {
            return K - (N%K);
        }

        else {
            return N%K;
        }
    } value {
        if (N%K > K-(N%K)) {
            return K - (N%K);
        }

        else {
            return N%K;
        }
    } if (N%K > K-(N%K)) {
            return K - (N%K);
        }

        else {
            return N%K;
        } N%K > K-(N%K) N%K N K K-(N%K) K (N%K) N K {
            return K - (N%K);
        } return K - (N%K); K - (N%K) K (N%K) N K {
            return N%K;
        } return N%K; N%K N K long N N long K K public class 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());
        System.out.println(value(N,K));
    }

    public static long value(long N, long K) {
        if (N%K > K-(N%K)) {
            return K - (N%K);
        }

        else {
            return N%K;
        }
    }
} public class 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());
        System.out.println(value(N,K));
    }

    public static long value(long N, long K) {
        if (N%K > K-(N%K)) {
            return K - (N%K);
        }

        else {
            return N%K;
        }
    }
} Main
import java.util.Scanner;
import java.lang.Math;

public class 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());
        System.out.println(value(N,K));
    }

    public static long value(long N, long K) {
        if (N%K > K-(N%K)) {
            return K - (N%K);
        }

        else {
            return N%K;
        }
    }
}


