import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

	    long result = execute(sc.nextLong(),sc.nextLong());
        System.out.println(result);
    }

    public static long execute(long n, long k){
        long plusTreated = n % k;
        long minusTreated =Math.abs( plusTreated - k);
        long treated = Math.min(plusTreated,minusTreated);
        if(n > treated) return execute(treated,k);
        return treated;
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

	    long result = execute(sc.nextLong(),sc.nextLong());
        System.out.println(result);
    }

    public static long execute(long n, long k){
        long plusTreated = n % k;
        long minusTreated =Math.abs( plusTreated - k);
        long treated = Math.min(plusTreated,minusTreated);
        if(n > treated) return execute(treated,k);
        return treated;
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

	    long result = execute(sc.nextLong(),sc.nextLong());
        System.out.println(result);
    } main {
        Scanner sc = new Scanner(System.in);

	    long result = execute(sc.nextLong(),sc.nextLong());
        System.out.println(result);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long result = execute(sc.nextLong(),sc.nextLong()); result execute(sc.nextLong(),sc.nextLong()) execute sc.nextLong() sc.nextLong sc sc.nextLong() sc.nextLong sc System.out.println(result) System.out.println System.out System System.out result String[] args args public static long execute(long n, long k){
        long plusTreated = n % k;
        long minusTreated =Math.abs( plusTreated - k);
        long treated = Math.min(plusTreated,minusTreated);
        if(n > treated) return execute(treated,k);
        return treated;
    } execute {
        long plusTreated = n % k;
        long minusTreated =Math.abs( plusTreated - k);
        long treated = Math.min(plusTreated,minusTreated);
        if(n > treated) return execute(treated,k);
        return treated;
    } long plusTreated = n % k; plusTreated n % k n k long minusTreated =Math.abs( plusTreated - k); minusTreated Math.abs( plusTreated - k) Math.abs Math plusTreated - k plusTreated k long treated = Math.min(plusTreated,minusTreated); treated Math.min(plusTreated,minusTreated) Math.min Math plusTreated minusTreated if(n > treated) return execute(treated,k); n > treated n treated return execute(treated,k); execute(treated,k) execute treated k return treated; treated long n n long k k public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

	    long result = execute(sc.nextLong(),sc.nextLong());
        System.out.println(result);
    }

    public static long execute(long n, long k){
        long plusTreated = n % k;
        long minusTreated =Math.abs( plusTreated - k);
        long treated = Math.min(plusTreated,minusTreated);
        if(n > treated) return execute(treated,k);
        return treated;
    }
} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

	    long result = execute(sc.nextLong(),sc.nextLong());
        System.out.println(result);
    }

    public static long execute(long n, long k){
        long plusTreated = n % k;
        long minusTreated =Math.abs( plusTreated - k);
        long treated = Math.min(plusTreated,minusTreated);
        if(n > treated) return execute(treated,k);
        return treated;
    }
} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

	    long result = execute(sc.nextLong(),sc.nextLong());
        System.out.println(result);
    }

    public static long execute(long n, long k){
        long plusTreated = n % k;
        long minusTreated =Math.abs( plusTreated - k);
        long treated = Math.min(plusTreated,minusTreated);
        if(n > treated) return execute(treated,k);
        return treated;
    }
}
