import java.*;
import java.util.*;
 
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
		long number = Long.parseLong(sc.next());
      	long constNum = Long.parseLong(sc.next());
      	long answer=number%constNum;
      	while(true){
        	long afterNum=Math.abs(answer-constNum);
          	if(answer<=afterNum){
              	break;
            }
          	answer=afterNum;
        }
      System.out.println(answer);
    }
} import java.*; java.* import java.*; import java.util.*; util.* java public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
		long number = Long.parseLong(sc.next());
      	long constNum = Long.parseLong(sc.next());
      	long answer=number%constNum;
      	while(true){
        	long afterNum=Math.abs(answer-constNum);
          	if(answer<=afterNum){
              	break;
            }
          	answer=afterNum;
        }
      System.out.println(answer);
    }
} Main public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
		long number = Long.parseLong(sc.next());
      	long constNum = Long.parseLong(sc.next());
      	long answer=number%constNum;
      	while(true){
        	long afterNum=Math.abs(answer-constNum);
          	if(answer<=afterNum){
              	break;
            }
          	answer=afterNum;
        }
      System.out.println(answer);
    } main {
        Scanner sc = new Scanner(System.in);
		long number = Long.parseLong(sc.next());
      	long constNum = Long.parseLong(sc.next());
      	long answer=number%constNum;
      	while(true){
        	long afterNum=Math.abs(answer-constNum);
          	if(answer<=afterNum){
              	break;
            }
          	answer=afterNum;
        }
      System.out.println(answer);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long number = Long.parseLong(sc.next()); number Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long constNum = Long.parseLong(sc.next()); constNum Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long answer=number%constNum; answer number%constNum number constNum while(true){
        	long afterNum=Math.abs(answer-constNum);
          	if(answer<=afterNum){
              	break;
            }
          	answer=afterNum;
        } true {
        	long afterNum=Math.abs(answer-constNum);
          	if(answer<=afterNum){
              	break;
            }
          	answer=afterNum;
        } long afterNum=Math.abs(answer-constNum); afterNum Math.abs(answer-constNum) Math.abs Math answer-constNum answer constNum if(answer<=afterNum){
              	break;
            } answer<=afterNum answer afterNum {
              	break;
            } break; answer=afterNum answer afterNum System.out.println(answer) System.out.println System.out System System.out answer String args[] args public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
		long number = Long.parseLong(sc.next());
      	long constNum = Long.parseLong(sc.next());
      	long answer=number%constNum;
      	while(true){
        	long afterNum=Math.abs(answer-constNum);
          	if(answer<=afterNum){
              	break;
            }
          	answer=afterNum;
        }
      System.out.println(answer);
    }
} public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
		long number = Long.parseLong(sc.next());
      	long constNum = Long.parseLong(sc.next());
      	long answer=number%constNum;
      	while(true){
        	long afterNum=Math.abs(answer-constNum);
          	if(answer<=afterNum){
              	break;
            }
          	answer=afterNum;
        }
      System.out.println(answer);
    }
} Main
import java.*;
import java.util.*;
 
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
		long number = Long.parseLong(sc.next());
      	long constNum = Long.parseLong(sc.next());
      	long answer=number%constNum;
      	while(true){
        	long afterNum=Math.abs(answer-constNum);
          	if(answer<=afterNum){
              	break;
            }
          	answer=afterNum;
        }
      System.out.println(answer);
    }
}