
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        new Main().solve();
    }

    void solve() {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        List<Long> list = new ArrayList<>();
        Map<Long, Boolean> map = new HashMap<>();
        long lnum = -1;
        long newnum = N;
        long min = 1000000000000000000L; 

        lnum = N % K;
        while(lnum > Math.abs(lnum - K)){
            lnum = Math.abs(lnum - K);
        }

        if(lnum <= Math.abs(lnum - K)){
            System.out.println(lnum);       
        }
        

    }
    
}

        import java.util.ArrayList; ArrayList java.util import java.util.HashMap; HashMap java.util import java.util.List; List java.util import java.util.Map; Map java.util import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        new Main().solve();
    }

    void solve() {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        List<Long> list = new ArrayList<>();
        Map<Long, Boolean> map = new HashMap<>();
        long lnum = -1;
        long newnum = N;
        long min = 1000000000000000000L; 

        lnum = N % K;
        while(lnum > Math.abs(lnum - K)){
            lnum = Math.abs(lnum - K);
        }

        if(lnum <= Math.abs(lnum - K)){
            System.out.println(lnum);       
        }
        

    }
    
} Main public static void main(String[] args) {
        new Main().solve();
    } main {
        new Main().solve();
    } new Main().solve() new Main().solve new Main() String[] args args void solve() {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        List<Long> list = new ArrayList<>();
        Map<Long, Boolean> map = new HashMap<>();
        long lnum = -1;
        long newnum = N;
        long min = 1000000000000000000L; 

        lnum = N % K;
        while(lnum > Math.abs(lnum - K)){
            lnum = Math.abs(lnum - K);
        }

        if(lnum <= Math.abs(lnum - K)){
            System.out.println(lnum);       
        }
        

    } solve {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        List<Long> list = new ArrayList<>();
        Map<Long, Boolean> map = new HashMap<>();
        long lnum = -1;
        long newnum = N;
        long min = 1000000000000000000L; 

        lnum = N % K;
        while(lnum > Math.abs(lnum - K)){
            lnum = Math.abs(lnum - K);
        }

        if(lnum <= Math.abs(lnum - K)){
            System.out.println(lnum);       
        }
        

    } 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 sc.close() sc.close sc List<Long> list = new ArrayList<>(); list new ArrayList<>() Map<Long, Boolean> map = new HashMap<>(); map new HashMap<>() long lnum = -1; lnum -1 1 long newnum = N; newnum N long min = 1000000000000000000L; min 1000000000000000000L lnum = N % K lnum N % K N K while(lnum > Math.abs(lnum - K)){
            lnum = Math.abs(lnum - K);
        } lnum > Math.abs(lnum - K) lnum Math.abs(lnum - K) Math.abs Math lnum - K lnum K {
            lnum = Math.abs(lnum - K);
        } lnum = Math.abs(lnum - K) lnum Math.abs(lnum - K) Math.abs Math lnum - K lnum K if(lnum <= Math.abs(lnum - K)){
            System.out.println(lnum);       
        } lnum <= Math.abs(lnum - K) lnum Math.abs(lnum - K) Math.abs Math lnum - K lnum K {
            System.out.println(lnum);       
        } System.out.println(lnum) System.out.println System.out System System.out lnum public class Main {

    public static void main(String[] args) {
        new Main().solve();
    }

    void solve() {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        List<Long> list = new ArrayList<>();
        Map<Long, Boolean> map = new HashMap<>();
        long lnum = -1;
        long newnum = N;
        long min = 1000000000000000000L; 

        lnum = N % K;
        while(lnum > Math.abs(lnum - K)){
            lnum = Math.abs(lnum - K);
        }

        if(lnum <= Math.abs(lnum - K)){
            System.out.println(lnum);       
        }
        

    }
    
} public class Main {

    public static void main(String[] args) {
        new Main().solve();
    }

    void solve() {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        List<Long> list = new ArrayList<>();
        Map<Long, Boolean> map = new HashMap<>();
        long lnum = -1;
        long newnum = N;
        long min = 1000000000000000000L; 

        lnum = N % K;
        while(lnum > Math.abs(lnum - K)){
            lnum = Math.abs(lnum - K);
        }

        if(lnum <= Math.abs(lnum - K)){
            System.out.println(lnum);       
        }
        

    }
    
} Main

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        new Main().solve();
    }

    void solve() {
        Scanner sc = new Scanner(System.in);
        long N = sc.nextLong();
        long K = sc.nextLong();
        sc.close();

        List<Long> list = new ArrayList<>();
        Map<Long, Boolean> map = new HashMap<>();
        long lnum = -1;
        long newnum = N;
        long min = 1000000000000000000L; 

        lnum = N % K;
        while(lnum > Math.abs(lnum - K)){
            lnum = Math.abs(lnum - K);
        }

        if(lnum <= Math.abs(lnum - K)){
            System.out.println(lnum);       
        }
        

    }
    
}

       