import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws Exception {
        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        List<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {

            mylist.add(strinput);
            strinput = reader.readLine();

        }

        String arr[] = mylist.get(0).split(" ");
        long n = Long.valueOf(arr[0]);
        long k = Long.valueOf(arr[1]);
        long amari = n % k;
        long temp = Math.abs(amari - k);

//        System.out.println(temp + " " + amari);

        System.out.println(Math.min(amari, temp));

    }

} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.ArrayList; ArrayList java.util import java.util.List; List java.util public class Main {

    public static void main(String[] args) throws Exception {
        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        List<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {

            mylist.add(strinput);
            strinput = reader.readLine();

        }

        String arr[] = mylist.get(0).split(" ");
        long n = Long.valueOf(arr[0]);
        long k = Long.valueOf(arr[1]);
        long amari = n % k;
        long temp = Math.abs(amari - k);

//        System.out.println(temp + " " + amari);

        System.out.println(Math.min(amari, temp));

    }

} Main public static void main(String[] args) throws Exception {
        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        List<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {

            mylist.add(strinput);
            strinput = reader.readLine();

        }

        String arr[] = mylist.get(0).split(" ");
        long n = Long.valueOf(arr[0]);
        long k = Long.valueOf(arr[1]);
        long amari = n % k;
        long temp = Math.abs(amari - k);

//        System.out.println(temp + " " + amari);

        System.out.println(Math.min(amari, temp));

    } main {
        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        List<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {

            mylist.add(strinput);
            strinput = reader.readLine();

        }

        String arr[] = mylist.get(0).split(" ");
        long n = Long.valueOf(arr[0]);
        long k = Long.valueOf(arr[1]);
        long amari = n % k;
        long temp = Math.abs(amari - k);

//        System.out.println(temp + " " + amari);

        System.out.println(Math.min(amari, temp));

    } InputStreamReader is = new InputStreamReader(System.in); is new InputStreamReader(System.in) BufferedReader reader = new BufferedReader(is); reader new BufferedReader(is) List<String> mylist = new ArrayList<>(); mylist new ArrayList<>() String strinput = reader.readLine(); strinput reader.readLine() reader.readLine reader while (strinput != null) {

            mylist.add(strinput);
            strinput = reader.readLine();

        } strinput != null strinput null {

            mylist.add(strinput);
            strinput = reader.readLine();

        } mylist.add(strinput) mylist.add mylist strinput strinput = reader.readLine() strinput reader.readLine() reader.readLine reader String arr[] = mylist.get(0).split(" "); arr mylist.get(0).split(" ") mylist.get(0).split mylist.get(0) mylist.get mylist 0 " " long n = Long.valueOf(arr[0]); n Long.valueOf(arr[0]) Long.valueOf Long arr[0] arr 0 long k = Long.valueOf(arr[1]); k Long.valueOf(arr[1]) Long.valueOf Long arr[1] arr 1 long amari = n % k; amari n % k n k long temp = Math.abs(amari - k); temp Math.abs(amari - k) Math.abs Math amari - k amari k System.out.println(Math.min(amari, temp)) System.out.println System.out System System.out Math.min(amari, temp) Math.min Math amari temp String[] args args public class Main {

    public static void main(String[] args) throws Exception {
        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        List<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {

            mylist.add(strinput);
            strinput = reader.readLine();

        }

        String arr[] = mylist.get(0).split(" ");
        long n = Long.valueOf(arr[0]);
        long k = Long.valueOf(arr[1]);
        long amari = n % k;
        long temp = Math.abs(amari - k);

//        System.out.println(temp + " " + amari);

        System.out.println(Math.min(amari, temp));

    }

} public class Main {

    public static void main(String[] args) throws Exception {
        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        List<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {

            mylist.add(strinput);
            strinput = reader.readLine();

        }

        String arr[] = mylist.get(0).split(" ");
        long n = Long.valueOf(arr[0]);
        long k = Long.valueOf(arr[1]);
        long amari = n % k;
        long temp = Math.abs(amari - k);

//        System.out.println(temp + " " + amari);

        System.out.println(Math.min(amari, temp));

    }

} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws Exception {
        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        List<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {

            mylist.add(strinput);
            strinput = reader.readLine();

        }

        String arr[] = mylist.get(0).split(" ");
        long n = Long.valueOf(arr[0]);
        long k = Long.valueOf(arr[1]);
        long amari = n % k;
        long temp = Math.abs(amari - k);

//        System.out.println(temp + " " + amari);

        System.out.println(Math.min(amari, temp));

    }

}
