import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
  
        long n;
        long m;

         n = sc.nextLong();
      m = sc.nextLong();
       
      if(m>n){
        long k= Math.abs(m-n);
        if(k>=n){
          System.out.println(n);
        }
        else{
          System.out.println(k);
        }
      }else if(m==n){
         System.out.println(0);
      } else {
    	  if(m==0) {
    		  System.out.println(n);
    	  }else {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  }
      }
          
          
          
       
       
        
  }
}
         import java.util.*; util.* java import java.io.*; io.* java public class Main {
    public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
  
        long n;
        long m;

         n = sc.nextLong();
      m = sc.nextLong();
       
      if(m>n){
        long k= Math.abs(m-n);
        if(k>=n){
          System.out.println(n);
        }
        else{
          System.out.println(k);
        }
      }else if(m==n){
         System.out.println(0);
      } else {
    	  if(m==0) {
    		  System.out.println(n);
    	  }else {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  }
      }
          
          
          
       
       
        
  }
} Main public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
  
        long n;
        long m;

         n = sc.nextLong();
      m = sc.nextLong();
       
      if(m>n){
        long k= Math.abs(m-n);
        if(k>=n){
          System.out.println(n);
        }
        else{
          System.out.println(k);
        }
      }else if(m==n){
         System.out.println(0);
      } else {
    	  if(m==0) {
    		  System.out.println(n);
    	  }else {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  }
      }
          
          
          
       
       
        
  } main {
        
        Scanner sc = new Scanner(System.in);
  
        long n;
        long m;

         n = sc.nextLong();
      m = sc.nextLong();
       
      if(m>n){
        long k= Math.abs(m-n);
        if(k>=n){
          System.out.println(n);
        }
        else{
          System.out.println(k);
        }
      }else if(m==n){
         System.out.println(0);
      } else {
    	  if(m==0) {
    		  System.out.println(n);
    	  }else {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  }
      }
          
          
          
       
       
        
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n; n long m; m n = sc.nextLong() n sc.nextLong() sc.nextLong sc m = sc.nextLong() m sc.nextLong() sc.nextLong sc if(m>n){
        long k= Math.abs(m-n);
        if(k>=n){
          System.out.println(n);
        }
        else{
          System.out.println(k);
        }
      }else if(m==n){
         System.out.println(0);
      } else {
    	  if(m==0) {
    		  System.out.println(n);
    	  }else {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  }
      } m>n m n {
        long k= Math.abs(m-n);
        if(k>=n){
          System.out.println(n);
        }
        else{
          System.out.println(k);
        }
      } long k= Math.abs(m-n); k Math.abs(m-n) Math.abs Math m-n m n if(k>=n){
          System.out.println(n);
        }
        else{
          System.out.println(k);
        } k>=n k n {
          System.out.println(n);
        } System.out.println(n) System.out.println System.out System System.out n {
          System.out.println(k);
        } System.out.println(k) System.out.println System.out System System.out k if(m==n){
         System.out.println(0);
      } else {
    	  if(m==0) {
    		  System.out.println(n);
    	  }else {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  }
      } m==n m n {
         System.out.println(0);
      } System.out.println(0) System.out.println System.out System System.out 0 {
    	  if(m==0) {
    		  System.out.println(n);
    	  }else {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  }
      } if(m==0) {
    		  System.out.println(n);
    	  }else {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  } m==0 m 0 {
    		  System.out.println(n);
    	  } System.out.println(n) System.out.println System.out System System.out n {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  } long l = n/m; l n/m n m long rem = n%m; rem n%m n m long x = Math.abs(rem-m); x Math.abs(rem-m) Math.abs Math rem-m rem m if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        } x>=rem x rem {
          System.out.println(rem);
        } System.out.println(rem) System.out.println System.out System System.out rem {
          System.out.println(x);
        } System.out.println(x) System.out.println System.out System System.out x String[] args args public class Main {
    public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
  
        long n;
        long m;

         n = sc.nextLong();
      m = sc.nextLong();
       
      if(m>n){
        long k= Math.abs(m-n);
        if(k>=n){
          System.out.println(n);
        }
        else{
          System.out.println(k);
        }
      }else if(m==n){
         System.out.println(0);
      } else {
    	  if(m==0) {
    		  System.out.println(n);
    	  }else {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  }
      }
          
          
          
       
       
        
  }
} public class Main {
    public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
  
        long n;
        long m;

         n = sc.nextLong();
      m = sc.nextLong();
       
      if(m>n){
        long k= Math.abs(m-n);
        if(k>=n){
          System.out.println(n);
        }
        else{
          System.out.println(k);
        }
      }else if(m==n){
         System.out.println(0);
      } else {
    	  if(m==0) {
    		  System.out.println(n);
    	  }else {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  }
      }
          
          
          
       
       
        
  }
} Main
import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception{
        
        Scanner sc = new Scanner(System.in);
  
        long n;
        long m;

         n = sc.nextLong();
      m = sc.nextLong();
       
      if(m>n){
        long k= Math.abs(m-n);
        if(k>=n){
          System.out.println(n);
        }
        else{
          System.out.println(k);
        }
      }else if(m==n){
         System.out.println(0);
      } else {
    	  if(m==0) {
    		  System.out.println(n);
    	  }else {
        long l = n/m;
        long rem = n%m;
        long x = Math.abs(rem-m);
         if(x>=rem){
          System.out.println(rem);
        }
        else{
          System.out.println(x);
        }
    	  }
      }
          
          
          
       
       
        
  }
}
        
