import 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();
        
        System.out.println(Math.min(N % K, K - (N % K)));
    }
} import java.util.*; util.* java 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();
        
        System.out.println(Math.min(N % K, K - (N % K)));
    }
} Main public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Long N = sc.nextLong();
        Long K = sc.nextLong();
        
        System.out.println(Math.min(N % K, K - (N % K)));
    } main {
        Scanner sc = new Scanner(System.in);
        Long N = sc.nextLong();
        Long K = sc.nextLong();
        
        System.out.println(Math.min(N % K, K - (N % K)));
    } 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 System.out.println(Math.min(N % K, K - (N % K))) System.out.println System.out System System.out Math.min(N % K, K - (N % K)) Math.min Math N % K N K K - (N % K) K (N % K) N K 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();
        
        System.out.println(Math.min(N % K, K - (N % K)));
    }
} 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();
        
        System.out.println(Math.min(N % K, K - (N % K)));
    }
} Main
import 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();
        
        System.out.println(Math.min(N % K, K - (N % K)));
    }
}
