import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        long p = scan.nextLong();
        long m = scan.nextLong();
        long ans = p;
        while (p > m){
            p = p%m;
            ans = Math.min(ans,p);
        }
        System.out.println(Math.min(ans,Math.abs(p-m)));
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        long p = scan.nextLong();
        long m = scan.nextLong();
        long ans = p;
        while (p > m){
            p = p%m;
            ans = Math.min(ans,p);
        }
        System.out.println(Math.min(ans,Math.abs(p-m)));
    }
} Main public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        long p = scan.nextLong();
        long m = scan.nextLong();
        long ans = p;
        while (p > m){
            p = p%m;
            ans = Math.min(ans,p);
        }
        System.out.println(Math.min(ans,Math.abs(p-m)));
    } main {
        Scanner scan = new Scanner(System.in);
        long p = scan.nextLong();
        long m = scan.nextLong();
        long ans = p;
        while (p > m){
            p = p%m;
            ans = Math.min(ans,p);
        }
        System.out.println(Math.min(ans,Math.abs(p-m)));
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) long p = scan.nextLong(); p scan.nextLong() scan.nextLong scan long m = scan.nextLong(); m scan.nextLong() scan.nextLong scan long ans = p; ans p while (p > m){
            p = p%m;
            ans = Math.min(ans,p);
        } p > m p m {
            p = p%m;
            ans = Math.min(ans,p);
        } p = p%m p p%m p m ans = Math.min(ans,p) ans Math.min(ans,p) Math.min Math ans p System.out.println(Math.min(ans,Math.abs(p-m))) System.out.println System.out System System.out Math.min(ans,Math.abs(p-m)) Math.min Math ans Math.abs(p-m) Math.abs Math p-m p m String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        long p = scan.nextLong();
        long m = scan.nextLong();
        long ans = p;
        while (p > m){
            p = p%m;
            ans = Math.min(ans,p);
        }
        System.out.println(Math.min(ans,Math.abs(p-m)));
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        long p = scan.nextLong();
        long m = scan.nextLong();
        long ans = p;
        while (p > m){
            p = p%m;
            ans = Math.min(ans,p);
        }
        System.out.println(Math.min(ans,Math.abs(p-m)));
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        long p = scan.nextLong();
        long m = scan.nextLong();
        long ans = p;
        while (p > m){
            p = p%m;
            ans = Math.min(ans,p);
        }
        System.out.println(Math.min(ans,Math.abs(p-m)));
    }
}
