import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        long a = in.nextLong(), b = in.nextLong();
        if (a >= b) {
            a = a % b;
        }
        if (a > b - a)
            System.out.println(b - a);
        else {
            System.out.println(a);
        }
    }
} import java.util.Scanner; Scanner java.util public class Main{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        long a = in.nextLong(), b = in.nextLong();
        if (a >= b) {
            a = a % b;
        }
        if (a > b - a)
            System.out.println(b - a);
        else {
            System.out.println(a);
        }
    }
} Main public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        long a = in.nextLong(), b = in.nextLong();
        if (a >= b) {
            a = a % b;
        }
        if (a > b - a)
            System.out.println(b - a);
        else {
            System.out.println(a);
        }
    } main {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        long a = in.nextLong(), b = in.nextLong();
        if (a >= b) {
            a = a % b;
        }
        if (a > b - a)
            System.out.println(b - a);
        else {
            System.out.println(a);
        }
    } Scanner in = new Scanner(System.in); in new Scanner(System.in) long a = in.nextLong() a in.nextLong() in.nextLong in b = in.nextLong(); b in.nextLong() in.nextLong in if (a >= b) {
            a = a % b;
        } a >= b a b {
            a = a % b;
        } a = a % b a a % b a b if (a > b - a)
            System.out.println(b - a);
        else {
            System.out.println(a);
        } a > b - a a b - a b a System.out.println(b - a) System.out.println System.out System System.out b - a b a {
            System.out.println(a);
        } System.out.println(a) System.out.println System.out System System.out a String[] args args public class Main{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        long a = in.nextLong(), b = in.nextLong();
        if (a >= b) {
            a = a % b;
        }
        if (a > b - a)
            System.out.println(b - a);
        else {
            System.out.println(a);
        }
    }
} public class Main{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        long a = in.nextLong(), b = in.nextLong();
        if (a >= b) {
            a = a % b;
        }
        if (a > b - a)
            System.out.println(b - a);
        else {
            System.out.println(a);
        }
    }
} Main
import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        long a = in.nextLong(), b = in.nextLong();
        if (a >= b) {
            a = a % b;
        }
        if (a > b - a)
            System.out.println(b - a);
        else {
            System.out.println(a);
        }
    }
}
