import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        N = N - N/K*K;
        
        while(N > sub(N,K)) {
        N = sub(N,K);
        }
        System.out.println(N);

        sc.close();
    }
    public static long sub(long A, long B) {
        return Math.abs(A-B);
    }
} 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 K = sc.nextLong();
        
        N = N - N/K*K;
        
        while(N > sub(N,K)) {
        N = sub(N,K);
        }
        System.out.println(N);

        sc.close();
    }
    public static long sub(long A, long B) {
        return Math.abs(A-B);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        N = N - N/K*K;
        
        while(N > sub(N,K)) {
        N = sub(N,K);
        }
        System.out.println(N);

        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        N = N - N/K*K;
        
        while(N > sub(N,K)) {
        N = sub(N,K);
        }
        System.out.println(N);

        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long N = sc.nextLong(); N sc.nextLong() sc.nextLong sc long K = sc.nextLong(); K sc.nextLong() sc.nextLong sc N = N - N/K*K N N - N/K*K N N/K*K N/K N K K while(N > sub(N,K)) {
        N = sub(N,K);
        } N > sub(N,K) N sub(N,K) sub N K {
        N = sub(N,K);
        } N = sub(N,K) N sub(N,K) sub N K System.out.println(N) System.out.println System.out System System.out N sc.close() sc.close sc String[] args args public static long sub(long A, long B) {
        return Math.abs(A-B);
    } sub {
        return Math.abs(A-B);
    } return Math.abs(A-B); Math.abs(A-B) Math.abs Math A-B A B long A A long B B public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        N = N - N/K*K;
        
        while(N > sub(N,K)) {
        N = sub(N,K);
        }
        System.out.println(N);

        sc.close();
    }
    public static long sub(long A, long B) {
        return Math.abs(A-B);
    }
} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        
        N = N - N/K*K;
        
        while(N > sub(N,K)) {
        N = sub(N,K);
        }
        System.out.println(N);

        sc.close();
    }
    public static long sub(long A, long B) {
        return Math.abs(A-B);
    }
} 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 K = sc.nextLong();
        
        N = N - N/K*K;
        
        while(N > sub(N,K)) {
        N = sub(N,K);
        }
        System.out.println(N);

        sc.close();
    }
    public static long sub(long A, long B) {
        return Math.abs(A-B);
    }
}
