import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long minsub = Math.abs(n-k)+1;
        long t = n%k;
        long answer = Math.min(t,k-t);
        System.out.print(answer);
    }
} import java.util.*; util.* java public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long minsub = Math.abs(n-k)+1;
        long t = n%k;
        long answer = Math.min(t,k-t);
        System.out.print(answer);
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long minsub = Math.abs(n-k)+1;
        long t = n%k;
        long answer = Math.min(t,k-t);
        System.out.print(answer);
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long minsub = Math.abs(n-k)+1;
        long t = n%k;
        long answer = Math.min(t,k-t);
        System.out.print(answer);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n = sc.nextLong(); n sc.nextLong() sc.nextLong sc long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc long minsub = Math.abs(n-k)+1; minsub Math.abs(n-k)+1 Math.abs(n-k) Math.abs Math n-k n k 1 long t = n%k; t n%k n k long answer = Math.min(t,k-t); answer Math.min(t,k-t) Math.min Math t k-t k t System.out.print(answer) System.out.print System.out System System.out answer String[] args args public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long minsub = Math.abs(n-k)+1;
        long t = n%k;
        long answer = Math.min(t,k-t);
        System.out.print(answer);
    }
} public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long minsub = Math.abs(n-k)+1;
        long t = n%k;
        long answer = Math.min(t,k-t);
        System.out.print(answer);
    }
} Main
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long minsub = Math.abs(n-k)+1;
        long t = n%k;
        long answer = Math.min(t,k-t);
        System.out.print(answer);
    }
}