import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long n,k;
  	n=sc.nextLong();
  	k=sc.nextLong();
  	int f=0;
  	while(f==0){
  		if(n>k){
  			n%=k;
  		}else{
  			if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			}
  		}
  	}
  	System.out.print(n);
  }
} import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long n,k;
  	n=sc.nextLong();
  	k=sc.nextLong();
  	int f=0;
  	while(f==0){
  		if(n>k){
  			n%=k;
  		}else{
  			if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			}
  		}
  	}
  	System.out.print(n);
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long n,k;
  	n=sc.nextLong();
  	k=sc.nextLong();
  	int f=0;
  	while(f==0){
  		if(n>k){
  			n%=k;
  		}else{
  			if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			}
  		}
  	}
  	System.out.print(n);
  } main {
    Scanner sc = new Scanner(System.in);
  	long n,k;
  	n=sc.nextLong();
  	k=sc.nextLong();
  	int f=0;
  	while(f==0){
  		if(n>k){
  			n%=k;
  		}else{
  			if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			}
  		}
  	}
  	System.out.print(n);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n n k; k n=sc.nextLong() n sc.nextLong() sc.nextLong sc k=sc.nextLong() k sc.nextLong() sc.nextLong sc int f=0; f 0 while(f==0){
  		if(n>k){
  			n%=k;
  		}else{
  			if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			}
  		}
  	} f==0 f 0 {
  		if(n>k){
  			n%=k;
  		}else{
  			if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			}
  		}
  	} if(n>k){
  			n%=k;
  		}else{
  			if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			}
  		} n>k n k {
  			n%=k;
  		} n%=k n k {
  			if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			}
  		} if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			} n<=k-n n k-n k n {
  				break;
  			} break; {
  				n=k-n;
  				break;
  			} n=k-n n k-n k n break; System.out.print(n) System.out.print System.out System System.out n String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long n,k;
  	n=sc.nextLong();
  	k=sc.nextLong();
  	int f=0;
  	while(f==0){
  		if(n>k){
  			n%=k;
  		}else{
  			if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			}
  		}
  	}
  	System.out.print(n);
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long n,k;
  	n=sc.nextLong();
  	k=sc.nextLong();
  	int f=0;
  	while(f==0){
  		if(n>k){
  			n%=k;
  		}else{
  			if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			}
  		}
  	}
  	System.out.print(n);
  }
} Main
import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	long n,k;
  	n=sc.nextLong();
  	k=sc.nextLong();
  	int f=0;
  	while(f==0){
  		if(n>k){
  			n%=k;
  		}else{
  			if(n<=k-n){
  				break;
  			}else{
  				n=k-n;
  				break;
  			}
  		}
  	}
  	System.out.print(n);
  }
}