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 min = Long.MAX_VALUE;
        long amari = 0;

        min = n%k;
        if(min > k-min){
            min = k-min;
        }
        System.out.println(min);
    }
} 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 min = Long.MAX_VALUE;
        long amari = 0;

        min = n%k;
        if(min > k-min){
            min = k-min;
        }
        System.out.println(min);
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long min = Long.MAX_VALUE;
        long amari = 0;

        min = n%k;
        if(min > k-min){
            min = k-min;
        }
        System.out.println(min);
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long min = Long.MAX_VALUE;
        long amari = 0;

        min = n%k;
        if(min > k-min){
            min = k-min;
        }
        System.out.println(min);
    } 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 min = Long.MAX_VALUE; min Long.MAX_VALUE Long Long.MAX_VALUE long amari = 0; amari 0 min = n%k min n%k n k if(min > k-min){
            min = k-min;
        } min > k-min min k-min k min {
            min = k-min;
        } min = k-min min k-min k min System.out.println(min) System.out.println System.out System System.out min 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 min = Long.MAX_VALUE;
        long amari = 0;

        min = n%k;
        if(min > k-min){
            min = k-min;
        }
        System.out.println(min);
    }
} public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long min = Long.MAX_VALUE;
        long amari = 0;

        min = n%k;
        if(min > k-min){
            min = k-min;
        }
        System.out.println(min);
    }
} 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 min = Long.MAX_VALUE;
        long amari = 0;

        min = n%k;
        if(min > k-min){
            min = k-min;
        }
        System.out.println(min);
    }
}