// "static void main" must be defined in a public class.
import java.util.*;
public class Main {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
        long x = sc.nextLong();
        long y = sc.nextLong();
        long a = x%y;
        long p = Math.min(a,y-a);
        
        
        
        System.out.println(p);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
        long x = sc.nextLong();
        long y = sc.nextLong();
        long a = x%y;
        long p = Math.min(a,y-a);
        
        
        
        System.out.println(p);
    }
} Main public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
        long x = sc.nextLong();
        long y = sc.nextLong();
        long a = x%y;
        long p = Math.min(a,y-a);
        
        
        
        System.out.println(p);
    } main {
       Scanner sc = new Scanner(System.in);
        long x = sc.nextLong();
        long y = sc.nextLong();
        long a = x%y;
        long p = Math.min(a,y-a);
        
        
        
        System.out.println(p);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long x = sc.nextLong(); x sc.nextLong() sc.nextLong sc long y = sc.nextLong(); y sc.nextLong() sc.nextLong sc long a = x%y; a x%y x y long p = Math.min(a,y-a); p Math.min(a,y-a) Math.min Math a y-a y a System.out.println(p) System.out.println System.out System System.out p String[] args args public class Main {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
        long x = sc.nextLong();
        long y = sc.nextLong();
        long a = x%y;
        long p = Math.min(a,y-a);
        
        
        
        System.out.println(p);
    }
} public class Main {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
        long x = sc.nextLong();
        long y = sc.nextLong();
        long a = x%y;
        long p = Math.min(a,y-a);
        
        
        
        System.out.println(p);
    }
} Main
// "static void main" must be defined in a public class.
import java.util.*;
public class Main {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
        long x = sc.nextLong();
        long y = sc.nextLong();
        long a = x%y;
        long p = Math.min(a,y-a);
        
        
        
        System.out.println(p);
    }
}
