import java.util.*;

public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long N = scanner.nextLong();
        Long K = scanner.nextLong();
        Long R1 = N % K;
        Long R2 = Math.abs(R1 - K);

        System.out.println(Math.min(R1,R2));
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long N = scanner.nextLong();
        Long K = scanner.nextLong();
        Long R1 = N % K;
        Long R2 = Math.abs(R1 - K);

        System.out.println(Math.min(R1,R2));
    }
} Main public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long N = scanner.nextLong();
        Long K = scanner.nextLong();
        Long R1 = N % K;
        Long R2 = Math.abs(R1 - K);

        System.out.println(Math.min(R1,R2));
    } main {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long N = scanner.nextLong();
        Long K = scanner.nextLong();
        Long R1 = N % K;
        Long R2 = Math.abs(R1 - K);

        System.out.println(Math.min(R1,R2));
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) Long N = scanner.nextLong(); N scanner.nextLong() scanner.nextLong scanner Long K = scanner.nextLong(); K scanner.nextLong() scanner.nextLong scanner Long R1 = N % K; R1 N % K N K Long R2 = Math.abs(R1 - K); R2 Math.abs(R1 - K) Math.abs Math R1 - K R1 K System.out.println(Math.min(R1,R2)) System.out.println System.out System System.out Math.min(R1,R2) Math.min Math R1 R2 String[] args args public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long N = scanner.nextLong();
        Long K = scanner.nextLong();
        Long R1 = N % K;
        Long R2 = Math.abs(R1 - K);

        System.out.println(Math.min(R1,R2));
    }
} public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long N = scanner.nextLong();
        Long K = scanner.nextLong();
        Long R1 = N % K;
        Long R2 = Math.abs(R1 - K);

        System.out.println(Math.min(R1,R2));
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Long N = scanner.nextLong();
        Long K = scanner.nextLong();
        Long R1 = N % K;
        Long R2 = Math.abs(R1 - K);

        System.out.println(Math.min(R1,R2));
    }
}
