import java.util.*;

class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans = n;
        boolean con = true;

        if(n%k==0){
            System.out.println(0);
            return;
        }

        if(n>=k && n%k != 0){
            n %= k;
        }

        while(con){
            long hikaku = Math.abs(n-k);
            n = hikaku;
            if(ans <=  hikaku){
                break;
            }
            ans = hikaku;
        }
        
        System.out.println(ans);
    }
} import java.util.*; util.* java class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans = n;
        boolean con = true;

        if(n%k==0){
            System.out.println(0);
            return;
        }

        if(n>=k && n%k != 0){
            n %= k;
        }

        while(con){
            long hikaku = Math.abs(n-k);
            n = hikaku;
            if(ans <=  hikaku){
                break;
            }
            ans = hikaku;
        }
        
        System.out.println(ans);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans = n;
        boolean con = true;

        if(n%k==0){
            System.out.println(0);
            return;
        }

        if(n>=k && n%k != 0){
            n %= k;
        }

        while(con){
            long hikaku = Math.abs(n-k);
            n = hikaku;
            if(ans <=  hikaku){
                break;
            }
            ans = hikaku;
        }
        
        System.out.println(ans);
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans = n;
        boolean con = true;

        if(n%k==0){
            System.out.println(0);
            return;
        }

        if(n>=k && n%k != 0){
            n %= k;
        }

        while(con){
            long hikaku = Math.abs(n-k);
            n = hikaku;
            if(ans <=  hikaku){
                break;
            }
            ans = hikaku;
        }
        
        System.out.println(ans);
    } 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 long ans = n; ans n boolean con = true; con true 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; if(n>=k && n%k != 0){
            n %= k;
        } n>=k && n%k != 0 n>=k n k n%k != 0 n%k n k 0 {
            n %= k;
        } n %= k n k while(con){
            long hikaku = Math.abs(n-k);
            n = hikaku;
            if(ans <=  hikaku){
                break;
            }
            ans = hikaku;
        } con {
            long hikaku = Math.abs(n-k);
            n = hikaku;
            if(ans <=  hikaku){
                break;
            }
            ans = hikaku;
        } long hikaku = Math.abs(n-k); hikaku Math.abs(n-k) Math.abs Math n-k n k n = hikaku n hikaku if(ans <=  hikaku){
                break;
            } ans <=  hikaku ans hikaku {
                break;
            } break; ans = hikaku ans hikaku System.out.println(ans) System.out.println System.out System System.out ans String[] args args
import java.util.*;

class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans = n;
        boolean con = true;

        if(n%k==0){
            System.out.println(0);
            return;
        }

        if(n>=k && n%k != 0){
            n %= k;
        }

        while(con){
            long hikaku = Math.abs(n-k);
            n = hikaku;
            if(ans <=  hikaku){
                break;
            }
            ans = hikaku;
        }
        
        System.out.println(ans);
    }
}