
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long N = scan.nextLong();
        long K = scan.nextLong();

        long ans = 0;

        if(K < N) {
            ans = Math.min(Math.abs(K*(N/K) - N), Math.abs(K*(N/K+1) - N));
            os.println(ans);
        } else if (N < K) {
            ans = Math.min(N, Math.abs(K-N));
            os.println(ans);
        } else if(N == K) {
            os.println(0);
        }


    }
} import java.io.InputStream; InputStream java.io import java.io.PrintStream; PrintStream java.io import java.util.ArrayList; ArrayList java.util import java.util.Arrays; Arrays java.util import java.util.HashMap; HashMap java.util import java.util.List; List java.util import java.util.Map; Map java.util import java.util.PriorityQueue; PriorityQueue java.util import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long N = scan.nextLong();
        long K = scan.nextLong();

        long ans = 0;

        if(K < N) {
            ans = Math.min(Math.abs(K*(N/K) - N), Math.abs(K*(N/K+1) - N));
            os.println(ans);
        } else if (N < K) {
            ans = Math.min(N, Math.abs(K-N));
            os.println(ans);
        } else if(N == K) {
            os.println(0);
        }


    }
} Main public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    } main {
        solve(System.in, System.out);
    } solve(System.in, System.out) solve System.in System System.in System.out System System.out String[] args args static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long N = scan.nextLong();
        long K = scan.nextLong();

        long ans = 0;

        if(K < N) {
            ans = Math.min(Math.abs(K*(N/K) - N), Math.abs(K*(N/K+1) - N));
            os.println(ans);
        } else if (N < K) {
            ans = Math.min(N, Math.abs(K-N));
            os.println(ans);
        } else if(N == K) {
            os.println(0);
        }


    } solve {
        // Your code here!
        Scanner scan = new Scanner(is);
        long N = scan.nextLong();
        long K = scan.nextLong();

        long ans = 0;

        if(K < N) {
            ans = Math.min(Math.abs(K*(N/K) - N), Math.abs(K*(N/K+1) - N));
            os.println(ans);
        } else if (N < K) {
            ans = Math.min(N, Math.abs(K-N));
            os.println(ans);
        } else if(N == K) {
            os.println(0);
        }


    } Scanner scan = new Scanner(is); scan new Scanner(is) long N = scan.nextLong(); N scan.nextLong() scan.nextLong scan long K = scan.nextLong(); K scan.nextLong() scan.nextLong scan long ans = 0; ans 0 if(K < N) {
            ans = Math.min(Math.abs(K*(N/K) - N), Math.abs(K*(N/K+1) - N));
            os.println(ans);
        } else if (N < K) {
            ans = Math.min(N, Math.abs(K-N));
            os.println(ans);
        } else if(N == K) {
            os.println(0);
        } K < N K N {
            ans = Math.min(Math.abs(K*(N/K) - N), Math.abs(K*(N/K+1) - N));
            os.println(ans);
        } ans = Math.min(Math.abs(K*(N/K) - N), Math.abs(K*(N/K+1) - N)) ans Math.min(Math.abs(K*(N/K) - N), Math.abs(K*(N/K+1) - N)) Math.min Math Math.abs(K*(N/K) - N) Math.abs Math K*(N/K) - N K*(N/K) K (N/K) N K N Math.abs(K*(N/K+1) - N) Math.abs Math K*(N/K+1) - N K*(N/K+1) K (N/K+1) N/K N K 1 N os.println(ans) os.println os ans if (N < K) {
            ans = Math.min(N, Math.abs(K-N));
            os.println(ans);
        } else if(N == K) {
            os.println(0);
        } N < K N K {
            ans = Math.min(N, Math.abs(K-N));
            os.println(ans);
        } ans = Math.min(N, Math.abs(K-N)) ans Math.min(N, Math.abs(K-N)) Math.min Math N Math.abs(K-N) Math.abs Math K-N K N os.println(ans) os.println os ans if(N == K) {
            os.println(0);
        } N == K N K {
            os.println(0);
        } os.println(0) os.println os 0 InputStream is is PrintStream os os public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long N = scan.nextLong();
        long K = scan.nextLong();

        long ans = 0;

        if(K < N) {
            ans = Math.min(Math.abs(K*(N/K) - N), Math.abs(K*(N/K+1) - N));
            os.println(ans);
        } else if (N < K) {
            ans = Math.min(N, Math.abs(K-N));
            os.println(ans);
        } else if(N == K) {
            os.println(0);
        }


    }
} public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long N = scan.nextLong();
        long K = scan.nextLong();

        long ans = 0;

        if(K < N) {
            ans = Math.min(Math.abs(K*(N/K) - N), Math.abs(K*(N/K+1) - N));
            os.println(ans);
        } else if (N < K) {
            ans = Math.min(N, Math.abs(K-N));
            os.println(ans);
        } else if(N == K) {
            os.println(0);
        }


    }
} Main

import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        solve(System.in, System.out);
    }

    static void solve(InputStream is, PrintStream os) {
        // Your code here!
        Scanner scan = new Scanner(is);
        long N = scan.nextLong();
        long K = scan.nextLong();

        long ans = 0;

        if(K < N) {
            ans = Math.min(Math.abs(K*(N/K) - N), Math.abs(K*(N/K+1) - N));
            os.println(ans);
        } else if (N < K) {
            ans = Math.min(N, Math.abs(K-N));
            os.println(ans);
        } else if(N == K) {
            os.println(0);
        }


    }
}