import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long N =  sc.nextLong();
        long M =  sc.nextLong();

        if(N>=M)
        N = N%M;
        while(true){
            long temp =  Math.abs(N-M);
            if(temp>=N){
                break;
            }
            N=temp;
        }
        System.out.println( N);
    }

} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long N =  sc.nextLong();
        long M =  sc.nextLong();

        if(N>=M)
        N = N%M;
        while(true){
            long temp =  Math.abs(N-M);
            if(temp>=N){
                break;
            }
            N=temp;
        }
        System.out.println( N);
    }

} Main public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long N =  sc.nextLong();
        long M =  sc.nextLong();

        if(N>=M)
        N = N%M;
        while(true){
            long temp =  Math.abs(N-M);
            if(temp>=N){
                break;
            }
            N=temp;
        }
        System.out.println( N);
    } main {

        Scanner sc = new Scanner(System.in);
        long N =  sc.nextLong();
        long M =  sc.nextLong();

        if(N>=M)
        N = N%M;
        while(true){
            long temp =  Math.abs(N-M);
            if(temp>=N){
                break;
            }
            N=temp;
        }
        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 M =  sc.nextLong(); M sc.nextLong() sc.nextLong sc if(N>=M)
        N = N%M; N>=M N M N = N%M N N%M N M while(true){
            long temp =  Math.abs(N-M);
            if(temp>=N){
                break;
            }
            N=temp;
        } true {
            long temp =  Math.abs(N-M);
            if(temp>=N){
                break;
            }
            N=temp;
        } long temp =  Math.abs(N-M); temp Math.abs(N-M) Math.abs Math N-M N M if(temp>=N){
                break;
            } temp>=N temp N {
                break;
            } break; N=temp N temp 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 M =  sc.nextLong();

        if(N>=M)
        N = N%M;
        while(true){
            long temp =  Math.abs(N-M);
            if(temp>=N){
                break;
            }
            N=temp;
        }
        System.out.println( N);
    }

} public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long N =  sc.nextLong();
        long M =  sc.nextLong();

        if(N>=M)
        N = N%M;
        while(true){
            long temp =  Math.abs(N-M);
            if(temp>=N){
                break;
            }
            N=temp;
        }
        System.out.println( N);
    }

} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long N =  sc.nextLong();
        long M =  sc.nextLong();

        if(N>=M)
        N = N%M;
        while(true){
            long temp =  Math.abs(N-M);
            if(temp>=N){
                break;
            }
            N=temp;
        }
        System.out.println( N);
    }

}
