
import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        long n = sc.nextLong();
        long k = sc.nextLong();
        n = n % k;
        if (n == 0) {
            System.out.println(0);
            return;
        }
        long next = Math.abs(n - k);
        long min = n;
        while (n != next) {
            n = Math.abs(n - k);
            next = Math.abs(Math.abs(next - k) - k);
            min = Math.min(min, n);
        }
        System.out.println(min);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        long n = sc.nextLong();
        long k = sc.nextLong();
        n = n % k;
        if (n == 0) {
            System.out.println(0);
            return;
        }
        long next = Math.abs(n - k);
        long min = n;
        while (n != next) {
            n = Math.abs(n - k);
            next = Math.abs(Math.abs(next - k) - k);
            min = Math.min(min, n);
        }
        System.out.println(min);
    }
} Main static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) {
        long n = sc.nextLong();
        long k = sc.nextLong();
        n = n % k;
        if (n == 0) {
            System.out.println(0);
            return;
        }
        long next = Math.abs(n - k);
        long min = n;
        while (n != next) {
            n = Math.abs(n - k);
            next = Math.abs(Math.abs(next - k) - k);
            min = Math.min(min, n);
        }
        System.out.println(min);
    } main {
        long n = sc.nextLong();
        long k = sc.nextLong();
        n = n % k;
        if (n == 0) {
            System.out.println(0);
            return;
        }
        long next = Math.abs(n - k);
        long min = n;
        while (n != next) {
            n = Math.abs(n - k);
            next = Math.abs(Math.abs(next - k) - k);
            min = Math.min(min, n);
        }
        System.out.println(min);
    } long n = sc.nextLong(); n sc.nextLong() sc.nextLong sc long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc n = n % k n n % k n k if (n == 0) {
            System.out.println(0);
            return;
        } n == 0 n 0 {
            System.out.println(0);
            return;
        } System.out.println(0) System.out.println System.out System System.out 0 return; long next = Math.abs(n - k); next Math.abs(n - k) Math.abs Math n - k n k long min = n; min n while (n != next) {
            n = Math.abs(n - k);
            next = Math.abs(Math.abs(next - k) - k);
            min = Math.min(min, n);
        } n != next n next {
            n = Math.abs(n - k);
            next = Math.abs(Math.abs(next - k) - k);
            min = Math.min(min, n);
        } n = Math.abs(n - k) n Math.abs(n - k) Math.abs Math n - k n k next = Math.abs(Math.abs(next - k) - k) next Math.abs(Math.abs(next - k) - k) Math.abs Math Math.abs(next - k) - k Math.abs(next - k) Math.abs Math next - k next k k min = Math.min(min, n) min Math.min(min, n) Math.min Math min n System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        long n = sc.nextLong();
        long k = sc.nextLong();
        n = n % k;
        if (n == 0) {
            System.out.println(0);
            return;
        }
        long next = Math.abs(n - k);
        long min = n;
        while (n != next) {
            n = Math.abs(n - k);
            next = Math.abs(Math.abs(next - k) - k);
            min = Math.min(min, n);
        }
        System.out.println(min);
    }
} public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        long n = sc.nextLong();
        long k = sc.nextLong();
        n = n % k;
        if (n == 0) {
            System.out.println(0);
            return;
        }
        long next = Math.abs(n - k);
        long min = n;
        while (n != next) {
            n = Math.abs(n - k);
            next = Math.abs(Math.abs(next - k) - k);
            min = Math.min(min, n);
        }
        System.out.println(min);
    }
} Main

import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);


    public static void main(String[] args) {
        long n = sc.nextLong();
        long k = sc.nextLong();
        n = n % k;
        if (n == 0) {
            System.out.println(0);
            return;
        }
        long next = Math.abs(n - k);
        long min = n;
        while (n != next) {
            n = Math.abs(n - k);
            next = Math.abs(Math.abs(next - k) - k);
            min = Math.min(min, n);
        }
        System.out.println(min);
    }
}
