import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner S = new Scanner(System.in);
        long N = S.nextLong();
        long K = S.nextLong();
        long Nx = N % K;
        System.out.println(replace(Nx, K));
        S.close();
    }

    public static long replace(long x, long K) {
        long div = x - K;
        if (div < 0) {
            div = -div;
        }
        if (x <= div) {
            return x;
        }
        return replace(div, K);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner S = new Scanner(System.in);
        long N = S.nextLong();
        long K = S.nextLong();
        long Nx = N % K;
        System.out.println(replace(Nx, K));
        S.close();
    }

    public static long replace(long x, long K) {
        long div = x - K;
        if (div < 0) {
            div = -div;
        }
        if (x <= div) {
            return x;
        }
        return replace(div, K);
    }
} Main public static void main(String[] args) {
        Scanner S = new Scanner(System.in);
        long N = S.nextLong();
        long K = S.nextLong();
        long Nx = N % K;
        System.out.println(replace(Nx, K));
        S.close();
    } main {
        Scanner S = new Scanner(System.in);
        long N = S.nextLong();
        long K = S.nextLong();
        long Nx = N % K;
        System.out.println(replace(Nx, K));
        S.close();
    } Scanner S = new Scanner(System.in); S new Scanner(System.in) long N = S.nextLong(); N S.nextLong() S.nextLong S long K = S.nextLong(); K S.nextLong() S.nextLong S long Nx = N % K; Nx N % K N K System.out.println(replace(Nx, K)) System.out.println System.out System System.out replace(Nx, K) replace Nx K S.close() S.close S String[] args args public static long replace(long x, long K) {
        long div = x - K;
        if (div < 0) {
            div = -div;
        }
        if (x <= div) {
            return x;
        }
        return replace(div, K);
    } replace {
        long div = x - K;
        if (div < 0) {
            div = -div;
        }
        if (x <= div) {
            return x;
        }
        return replace(div, K);
    } long div = x - K; div x - K x K if (div < 0) {
            div = -div;
        } div < 0 div 0 {
            div = -div;
        } div = -div div -div div if (x <= div) {
            return x;
        } x <= div x div {
            return x;
        } return x; x return replace(div, K); replace(div, K) replace div K long x x long K K public class Main {
    public static void main(String[] args) {
        Scanner S = new Scanner(System.in);
        long N = S.nextLong();
        long K = S.nextLong();
        long Nx = N % K;
        System.out.println(replace(Nx, K));
        S.close();
    }

    public static long replace(long x, long K) {
        long div = x - K;
        if (div < 0) {
            div = -div;
        }
        if (x <= div) {
            return x;
        }
        return replace(div, K);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner S = new Scanner(System.in);
        long N = S.nextLong();
        long K = S.nextLong();
        long Nx = N % K;
        System.out.println(replace(Nx, K));
        S.close();
    }

    public static long replace(long x, long K) {
        long div = x - K;
        if (div < 0) {
            div = -div;
        }
        if (x <= div) {
            return x;
        }
        return replace(div, K);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner S = new Scanner(System.in);
        long N = S.nextLong();
        long K = S.nextLong();
        long Nx = N % K;
        System.out.println(replace(Nx, K));
        S.close();
    }

    public static long replace(long x, long K) {
        long div = x - K;
        if (div < 0) {
            div = -div;
        }
        if (x <= div) {
            return x;
        }
        return replace(div, K);
    }
}