import java.util.*;
 
	public class Main {
		public static void main(String[] args) {
      	Scanner sc = new Scanner(System.in);
      
      	long N  =sc.nextLong();
       	long K  =sc.nextLong();
          
        if(N>K && N%K!=0){
           System.out.println(K-(N%K));
        }
          else if(N<K && K-N<N){
            System.out.println(K-N);
          }
          else if(N<K && K-N>N){
           System.out.println(N);
          }
          else{
          System.out.println(0);
          }
    }
} import java.util.*; util.* java public class Main {
		public static void main(String[] args) {
      	Scanner sc = new Scanner(System.in);
      
      	long N  =sc.nextLong();
       	long K  =sc.nextLong();
          
        if(N>K && N%K!=0){
           System.out.println(K-(N%K));
        }
          else if(N<K && K-N<N){
            System.out.println(K-N);
          }
          else if(N<K && K-N>N){
           System.out.println(N);
          }
          else{
          System.out.println(0);
          }
    }
} Main public static void main(String[] args) {
      	Scanner sc = new Scanner(System.in);
      
      	long N  =sc.nextLong();
       	long K  =sc.nextLong();
          
        if(N>K && N%K!=0){
           System.out.println(K-(N%K));
        }
          else if(N<K && K-N<N){
            System.out.println(K-N);
          }
          else if(N<K && K-N>N){
           System.out.println(N);
          }
          else{
          System.out.println(0);
          }
    } main {
      	Scanner sc = new Scanner(System.in);
      
      	long N  =sc.nextLong();
       	long K  =sc.nextLong();
          
        if(N>K && N%K!=0){
           System.out.println(K-(N%K));
        }
          else if(N<K && K-N<N){
            System.out.println(K-N);
          }
          else if(N<K && K-N>N){
           System.out.println(N);
          }
          else{
          System.out.println(0);
          }
    } 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 if(N>K && N%K!=0){
           System.out.println(K-(N%K));
        }
          else if(N<K && K-N<N){
            System.out.println(K-N);
          }
          else if(N<K && K-N>N){
           System.out.println(N);
          }
          else{
          System.out.println(0);
          } N>K && N%K!=0 N>K N K N%K!=0 N%K N K 0 {
           System.out.println(K-(N%K));
        } System.out.println(K-(N%K)) System.out.println System.out System System.out K-(N%K) K (N%K) N K if(N<K && K-N<N){
            System.out.println(K-N);
          }
          else if(N<K && K-N>N){
           System.out.println(N);
          }
          else{
          System.out.println(0);
          } N<K && K-N<N N<K N K K-N<N K-N K N N {
            System.out.println(K-N);
          } System.out.println(K-N) System.out.println System.out System System.out K-N K N if(N<K && K-N>N){
           System.out.println(N);
          }
          else{
          System.out.println(0);
          } N<K && K-N>N N<K N K K-N>N K-N K N N {
           System.out.println(N);
          } System.out.println(N) System.out.println System.out System System.out N {
          System.out.println(0);
          } System.out.println(0) System.out.println System.out System System.out 0 String[] args args public class Main {
		public static void main(String[] args) {
      	Scanner sc = new Scanner(System.in);
      
      	long N  =sc.nextLong();
       	long K  =sc.nextLong();
          
        if(N>K && N%K!=0){
           System.out.println(K-(N%K));
        }
          else if(N<K && K-N<N){
            System.out.println(K-N);
          }
          else if(N<K && K-N>N){
           System.out.println(N);
          }
          else{
          System.out.println(0);
          }
    }
} public class Main {
		public static void main(String[] args) {
      	Scanner sc = new Scanner(System.in);
      
      	long N  =sc.nextLong();
       	long K  =sc.nextLong();
          
        if(N>K && N%K!=0){
           System.out.println(K-(N%K));
        }
          else if(N<K && K-N<N){
            System.out.println(K-N);
          }
          else if(N<K && K-N>N){
           System.out.println(N);
          }
          else{
          System.out.println(0);
          }
    }
} Main
import java.util.*;
 
	public class Main {
		public static void main(String[] args) {
      	Scanner sc = new Scanner(System.in);
      
      	long N  =sc.nextLong();
       	long K  =sc.nextLong();
          
        if(N>K && N%K!=0){
           System.out.println(K-(N%K));
        }
          else if(N<K && K-N<N){
            System.out.println(K-N);
          }
          else if(N<K && K-N>N){
           System.out.println(N);
          }
          else{
          System.out.println(0);
          }
    }
}