import java.util.*;

public class Main {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        if( N < K ){
            long mi = Math.min(N, K-N);
            System.out.println(mi);
            return;
        }
        if(N % K == 0){
            System.out.println(0);
            return;
        }
        N = N % K;
        System.out.println(Math.min(N, K-N));
        // while(K != 0){
        //     N = N % K;
        //     long temp = N;
        //     N = K;
        //     K = temp;
        // }
        // System.out.println(N);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        if( N < K ){
            long mi = Math.min(N, K-N);
            System.out.println(mi);
            return;
        }
        if(N % K == 0){
            System.out.println(0);
            return;
        }
        N = N % K;
        System.out.println(Math.min(N, K-N));
        // while(K != 0){
        //     N = N % K;
        //     long temp = N;
        //     N = K;
        //     K = temp;
        // }
        // System.out.println(N);
    }
} Main public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        if( N < K ){
            long mi = Math.min(N, K-N);
            System.out.println(mi);
            return;
        }
        if(N % K == 0){
            System.out.println(0);
            return;
        }
        N = N % K;
        System.out.println(Math.min(N, K-N));
        // while(K != 0){
        //     N = N % K;
        //     long temp = N;
        //     N = K;
        //     K = temp;
        // }
        // System.out.println(N);
    } main {
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        if( N < K ){
            long mi = Math.min(N, K-N);
            System.out.println(mi);
            return;
        }
        if(N % K == 0){
            System.out.println(0);
            return;
        }
        N = N % K;
        System.out.println(Math.min(N, K-N));
        // while(K != 0){
        //     N = N % K;
        //     long temp = N;
        //     N = K;
        //     K = temp;
        // }
        // System.out.println(N);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) long N = scanner.nextLong(); N scanner.nextLong() scanner.nextLong scanner long K = scanner.nextLong(); K scanner.nextLong() scanner.nextLong scanner if( N < K ){
            long mi = Math.min(N, K-N);
            System.out.println(mi);
            return;
        } N < K N K {
            long mi = Math.min(N, K-N);
            System.out.println(mi);
            return;
        } long mi = Math.min(N, K-N); mi Math.min(N, K-N) Math.min Math N K-N K N System.out.println(mi) System.out.println System.out System System.out mi return; if(N % K == 0){
            System.out.println(0);
            return;
        } N % K == 0 N % K N K 0 {
            System.out.println(0);
            return;
        } System.out.println(0) System.out.println System.out System System.out 0 return; N = N % K N N % K N K System.out.println(Math.min(N, K-N)) System.out.println System.out System System.out Math.min(N, K-N) Math.min Math N K-N K N String args[] args public class Main {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        if( N < K ){
            long mi = Math.min(N, K-N);
            System.out.println(mi);
            return;
        }
        if(N % K == 0){
            System.out.println(0);
            return;
        }
        N = N % K;
        System.out.println(Math.min(N, K-N));
        // while(K != 0){
        //     N = N % K;
        //     long temp = N;
        //     N = K;
        //     K = temp;
        // }
        // System.out.println(N);
    }
} public class Main {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        if( N < K ){
            long mi = Math.min(N, K-N);
            System.out.println(mi);
            return;
        }
        if(N % K == 0){
            System.out.println(0);
            return;
        }
        N = N % K;
        System.out.println(Math.min(N, K-N));
        // while(K != 0){
        //     N = N % K;
        //     long temp = N;
        //     N = K;
        //     K = temp;
        // }
        // System.out.println(N);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        long N = scanner.nextLong();
        long K = scanner.nextLong();
        if( N < K ){
            long mi = Math.min(N, K-N);
            System.out.println(mi);
            return;
        }
        if(N % K == 0){
            System.out.println(0);
            return;
        }
        N = N % K;
        System.out.println(Math.min(N, K-N));
        // while(K != 0){
        //     N = N % K;
        //     long temp = N;
        //     N = K;
        //     K = temp;
        // }
        // System.out.println(N);
    }
}