import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        
        long ab = N;
        ab = N % K;
       // System.out.print(ab);
        
        long min = Math.abs(ab - K);
        if(min > ab){
            min = ab;
        }
        
        System.out.print(min);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        
        long ab = N;
        ab = N % K;
       // System.out.print(ab);
        
        long min = Math.abs(ab - K);
        if(min > ab){
            min = ab;
        }
        
        System.out.print(min);
    }
} Main public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        
        long ab = N;
        ab = N % K;
       // System.out.print(ab);
        
        long min = Math.abs(ab - K);
        if(min > ab){
            min = ab;
        }
        
        System.out.print(min);
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        
        long ab = N;
        ab = N % K;
       // System.out.print(ab);
        
        long min = Math.abs(ab - K);
        if(min > ab){
            min = ab;
        }
        
        System.out.print(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 ab = N; ab N ab = N % K ab N % K N K long min = Math.abs(ab - K); min Math.abs(ab - K) Math.abs Math ab - K ab K if(min > ab){
            min = ab;
        } min > ab min ab {
            min = ab;
        } min = ab min ab System.out.print(min) System.out.print System.out System System.out min String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        
        long ab = N;
        ab = N % K;
       // System.out.print(ab);
        
        long min = Math.abs(ab - K);
        if(min > ab){
            min = ab;
        }
        
        System.out.print(min);
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        
        long ab = N;
        ab = N % K;
       // System.out.print(ab);
        
        long min = Math.abs(ab - K);
        if(min > ab){
            min = ab;
        }
        
        System.out.print(min);
    }
} Main
import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        
        long ab = N;
        ab = N % K;
       // System.out.print(ab);
        
        long min = Math.abs(ab - K);
        if(min > ab){
            min = ab;
        }
        
        System.out.print(min);
    }
}