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();
        if (N == K) {
            System.out.println(0);
        } else if (N > K) {
            if (N % K == 0) {
                System.out.println(0);
            } else {
                N -= K * (N / K);
                solve(N, K);
            }
        } else {
            solve(N, K);
        }
    }

    private static void solve(long N, long K) {
        long comp = K - N;
        System.out.println(Math.min(comp, N));
    }
}

// x * 3 <= 16 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();
        if (N == K) {
            System.out.println(0);
        } else if (N > K) {
            if (N % K == 0) {
                System.out.println(0);
            } else {
                N -= K * (N / K);
                solve(N, K);
            }
        } else {
            solve(N, K);
        }
    }

    private static void solve(long N, long K) {
        long comp = K - N;
        System.out.println(Math.min(comp, N));
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        if (N == K) {
            System.out.println(0);
        } else if (N > K) {
            if (N % K == 0) {
                System.out.println(0);
            } else {
                N -= K * (N / K);
                solve(N, K);
            }
        } else {
            solve(N, K);
        }
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        if (N == K) {
            System.out.println(0);
        } else if (N > K) {
            if (N % K == 0) {
                System.out.println(0);
            } else {
                N -= K * (N / K);
                solve(N, K);
            }
        } else {
            solve(N, K);
        }
    } 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 if (N == K) {
            System.out.println(0);
        } else if (N > K) {
            if (N % K == 0) {
                System.out.println(0);
            } else {
                N -= K * (N / K);
                solve(N, K);
            }
        } else {
            solve(N, K);
        } N == K N K {
            System.out.println(0);
        } System.out.println(0) System.out.println System.out System System.out 0 if (N > K) {
            if (N % K == 0) {
                System.out.println(0);
            } else {
                N -= K * (N / K);
                solve(N, K);
            }
        } else {
            solve(N, K);
        } N > K N K {
            if (N % K == 0) {
                System.out.println(0);
            } else {
                N -= K * (N / K);
                solve(N, K);
            }
        } if (N % K == 0) {
                System.out.println(0);
            } else {
                N -= K * (N / K);
                solve(N, K);
            } N % K == 0 N % K N K 0 {
                System.out.println(0);
            } System.out.println(0) System.out.println System.out System System.out 0 {
                N -= K * (N / K);
                solve(N, K);
            } N -= K * (N / K) N K * (N / K) K (N / K) N K solve(N, K) solve N K {
            solve(N, K);
        } solve(N, K) solve N K String[] args args private static void solve(long N, long K) {
        long comp = K - N;
        System.out.println(Math.min(comp, N));
    } solve {
        long comp = K - N;
        System.out.println(Math.min(comp, N));
    } long comp = K - N; comp K - N K N System.out.println(Math.min(comp, N)) System.out.println System.out System System.out Math.min(comp, N) Math.min Math comp N long N N long K K public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        if (N == K) {
            System.out.println(0);
        } else if (N > K) {
            if (N % K == 0) {
                System.out.println(0);
            } else {
                N -= K * (N / K);
                solve(N, K);
            }
        } else {
            solve(N, K);
        }
    }

    private static void solve(long N, long K) {
        long comp = K - N;
        System.out.println(Math.min(comp, N));
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        if (N == K) {
            System.out.println(0);
        } else if (N > K) {
            if (N % K == 0) {
                System.out.println(0);
            } else {
                N -= K * (N / K);
                solve(N, K);
            }
        } else {
            solve(N, K);
        }
    }

    private static void solve(long N, long K) {
        long comp = K - N;
        System.out.println(Math.min(comp, N));
    }
} 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();
        if (N == K) {
            System.out.println(0);
        } else if (N > K) {
            if (N % K == 0) {
                System.out.println(0);
            } else {
                N -= K * (N / K);
                solve(N, K);
            }
        } else {
            solve(N, K);
        }
    }

    private static void solve(long N, long K) {
        long comp = K - N;
        System.out.println(Math.min(comp, N));
    }
}

// x * 3 <= 16