import java.util.*;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        if(n%k == 0){
            System.out.println(0);
        }
        else{
            n = n % k;
            if(n > Math.abs(n-k)){
                System.out.println(Math.abs(n-k));
            }
            else{
                System.out.println(n);
            }
        }
    }
} import java.util.*; util.* java import java.math.BigDecimal; BigDecimal java.math public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        if(n%k == 0){
            System.out.println(0);
        }
        else{
            n = n % k;
            if(n > Math.abs(n-k)){
                System.out.println(Math.abs(n-k));
            }
            else{
                System.out.println(n);
            }
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        if(n%k == 0){
            System.out.println(0);
        }
        else{
            n = n % k;
            if(n > Math.abs(n-k)){
                System.out.println(Math.abs(n-k));
            }
            else{
                System.out.println(n);
            }
        }
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        if(n%k == 0){
            System.out.println(0);
        }
        else{
            n = n % k;
            if(n > Math.abs(n-k)){
                System.out.println(Math.abs(n-k));
            }
            else{
                System.out.println(n);
            }
        }
    } 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 if(n%k == 0){
            System.out.println(0);
        }
        else{
            n = n % k;
            if(n > Math.abs(n-k)){
                System.out.println(Math.abs(n-k));
            }
            else{
                System.out.println(n);
            }
        } n%k == 0 n%k n k 0 {
            System.out.println(0);
        } System.out.println(0) System.out.println System.out System System.out 0 {
            n = n % k;
            if(n > Math.abs(n-k)){
                System.out.println(Math.abs(n-k));
            }
            else{
                System.out.println(n);
            }
        } n = n % k n n % k n k if(n > Math.abs(n-k)){
                System.out.println(Math.abs(n-k));
            }
            else{
                System.out.println(n);
            } n > Math.abs(n-k) n Math.abs(n-k) Math.abs Math n-k n k {
                System.out.println(Math.abs(n-k));
            } System.out.println(Math.abs(n-k)) System.out.println System.out System System.out Math.abs(n-k) Math.abs Math n-k n k {
                System.out.println(n);
            } System.out.println(n) System.out.println System.out System System.out n 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();
        
        if(n%k == 0){
            System.out.println(0);
        }
        else{
            n = n % k;
            if(n > Math.abs(n-k)){
                System.out.println(Math.abs(n-k));
            }
            else{
                System.out.println(n);
            }
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        if(n%k == 0){
            System.out.println(0);
        }
        else{
            n = n % k;
            if(n > Math.abs(n-k)){
                System.out.println(Math.abs(n-k));
            }
            else{
                System.out.println(n);
            }
        }
    }
} Main
import java.util.*;
import java.math.BigDecimal;

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