import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        long remainder = N % K;
        long residual = K - remainder;
        if (remainder < residual) {
            System.out.println(remainder);
        } else {
            System.out.println(residual);
        }
    }
} import java.util.Scanner; Scanner 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 remainder = N % K;
        long residual = K - remainder;
        if (remainder < residual) {
            System.out.println(remainder);
        } else {
            System.out.println(residual);
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        long remainder = N % K;
        long residual = K - remainder;
        if (remainder < residual) {
            System.out.println(remainder);
        } else {
            System.out.println(residual);
        }
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        long remainder = N % K;
        long residual = K - remainder;
        if (remainder < residual) {
            System.out.println(remainder);
        } else {
            System.out.println(residual);
        }
    } 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 remainder = N % K; remainder N % K N K long residual = K - remainder; residual K - remainder K remainder if (remainder < residual) {
            System.out.println(remainder);
        } else {
            System.out.println(residual);
        } remainder < residual remainder residual {
            System.out.println(remainder);
        } System.out.println(remainder) System.out.println System.out System System.out remainder {
            System.out.println(residual);
        } System.out.println(residual) System.out.println System.out System System.out residual 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 remainder = N % K;
        long residual = K - remainder;
        if (remainder < residual) {
            System.out.println(remainder);
        } else {
            System.out.println(residual);
        }
    }
} public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        long remainder = N % K;
        long residual = K - remainder;
        if (remainder < residual) {
            System.out.println(remainder);
        } else {
            System.out.println(residual);
        }
    }
} Main
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        long remainder = N % K;
        long residual = K - remainder;
        if (remainder < residual) {
            System.out.println(remainder);
        } else {
            System.out.println(residual);
        }
    }
}