import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {

//    	File file = new File("test.txt");
//    	Scanner sc = new Scanner(file);
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();
        boolean flg = true;
        if(k == 1){
        	System.out.println(0);
        	return;
        }
        n = n % k;
        while(flg){
        	n = Math.abs(n - k);
        	if(n < Math.abs(n - k)) flg = false;
        }
        System.out.println(n);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) throws Exception {

//    	File file = new File("test.txt");
//    	Scanner sc = new Scanner(file);
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();
        boolean flg = true;
        if(k == 1){
        	System.out.println(0);
        	return;
        }
        n = n % k;
        while(flg){
        	n = Math.abs(n - k);
        	if(n < Math.abs(n - k)) flg = false;
        }
        System.out.println(n);
    }
} Main public static void main(String[] args) throws Exception {

//    	File file = new File("test.txt");
//    	Scanner sc = new Scanner(file);
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();
        boolean flg = true;
        if(k == 1){
        	System.out.println(0);
        	return;
        }
        n = n % k;
        while(flg){
        	n = Math.abs(n - k);
        	if(n < Math.abs(n - k)) flg = false;
        }
        System.out.println(n);
    } main {

//    	File file = new File("test.txt");
//    	Scanner sc = new Scanner(file);
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();
        boolean flg = true;
        if(k == 1){
        	System.out.println(0);
        	return;
        }
        n = n % k;
        while(flg){
        	n = Math.abs(n - k);
        	if(n < Math.abs(n - k)) flg = false;
        }
        System.out.println(n);
    } 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 boolean flg = true; flg true if(k == 1){
        	System.out.println(0);
        	return;
        } k == 1 k 1 {
        	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 while(flg){
        	n = Math.abs(n - k);
        	if(n < Math.abs(n - k)) flg = false;
        } flg {
        	n = Math.abs(n - k);
        	if(n < Math.abs(n - k)) flg = false;
        } n = Math.abs(n - k) n Math.abs(n - k) Math.abs Math n - k n k if(n < Math.abs(n - k)) flg = false; n < Math.abs(n - k) n Math.abs(n - k) Math.abs Math n - k n k flg = false flg false System.out.println(n) System.out.println System.out System System.out n String[] args args public class Main {
    public static void main(String[] args) throws Exception {

//    	File file = new File("test.txt");
//    	Scanner sc = new Scanner(file);
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();
        boolean flg = true;
        if(k == 1){
        	System.out.println(0);
        	return;
        }
        n = n % k;
        while(flg){
        	n = Math.abs(n - k);
        	if(n < Math.abs(n - k)) flg = false;
        }
        System.out.println(n);
    }
} public class Main {
    public static void main(String[] args) throws Exception {

//    	File file = new File("test.txt");
//    	Scanner sc = new Scanner(file);
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();
        boolean flg = true;
        if(k == 1){
        	System.out.println(0);
        	return;
        }
        n = n % k;
        while(flg){
        	n = Math.abs(n - k);
        	if(n < Math.abs(n - k)) flg = false;
        }
        System.out.println(n);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {

//    	File file = new File("test.txt");
//    	Scanner sc = new Scanner(file);
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        sc.close();
        boolean flg = true;
        if(k == 1){
        	System.out.println(0);
        	return;
        }
        n = n % k;
        while(flg){
        	n = Math.abs(n - k);
        	if(n < Math.abs(n - k)) flg = false;
        }
        System.out.println(n);
    }
}