import java.util.*;

import javax.swing.plaf.basic.BasicSliderUI.ScrollListener;

public class Main {
    static public void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String temp = sc.next();
        long n = Long.parseLong(temp);
        temp = sc.next();
        long k = Long.parseLong(temp);

        long temp2 = n % k;
        if (temp2 > k / 2) {
            temp2 = k - temp2;
        }
        System.out.println(temp2);
    }
} import java.util.*; util.* java import javax.swing.plaf.basic.BasicSliderUI.ScrollListener; ScrollListener javax.swing.plaf.basic.BasicSliderUI public class Main {
    static public void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String temp = sc.next();
        long n = Long.parseLong(temp);
        temp = sc.next();
        long k = Long.parseLong(temp);

        long temp2 = n % k;
        if (temp2 > k / 2) {
            temp2 = k - temp2;
        }
        System.out.println(temp2);
    }
} Main static public void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String temp = sc.next();
        long n = Long.parseLong(temp);
        temp = sc.next();
        long k = Long.parseLong(temp);

        long temp2 = n % k;
        if (temp2 > k / 2) {
            temp2 = k - temp2;
        }
        System.out.println(temp2);
    } main {

        Scanner sc = new Scanner(System.in);
        String temp = sc.next();
        long n = Long.parseLong(temp);
        temp = sc.next();
        long k = Long.parseLong(temp);

        long temp2 = n % k;
        if (temp2 > k / 2) {
            temp2 = k - temp2;
        }
        System.out.println(temp2);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) String temp = sc.next(); temp sc.next() sc.next sc long n = Long.parseLong(temp); n Long.parseLong(temp) Long.parseLong Long temp temp = sc.next() temp sc.next() sc.next sc long k = Long.parseLong(temp); k Long.parseLong(temp) Long.parseLong Long temp long temp2 = n % k; temp2 n % k n k if (temp2 > k / 2) {
            temp2 = k - temp2;
        } temp2 > k / 2 temp2 k / 2 k 2 {
            temp2 = k - temp2;
        } temp2 = k - temp2 temp2 k - temp2 k temp2 System.out.println(temp2) System.out.println System.out System System.out temp2 String args[] args public class Main {
    static public void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String temp = sc.next();
        long n = Long.parseLong(temp);
        temp = sc.next();
        long k = Long.parseLong(temp);

        long temp2 = n % k;
        if (temp2 > k / 2) {
            temp2 = k - temp2;
        }
        System.out.println(temp2);
    }
} public class Main {
    static public void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String temp = sc.next();
        long n = Long.parseLong(temp);
        temp = sc.next();
        long k = Long.parseLong(temp);

        long temp2 = n % k;
        if (temp2 > k / 2) {
            temp2 = k - temp2;
        }
        System.out.println(temp2);
    }
} Main
import java.util.*;

import javax.swing.plaf.basic.BasicSliderUI.ScrollListener;

public class Main {
    static public void main(String args[]) {

        Scanner sc = new Scanner(System.in);
        String temp = sc.next();
        long n = Long.parseLong(temp);
        temp = sc.next();
        long k = Long.parseLong(temp);

        long temp2 = n % k;
        if (temp2 > k / 2) {
            temp2 = k - temp2;
        }
        System.out.println(temp2);
    }
}