import java.util.Scanner;
import java.util.*;
import java.lang.*;

class Main{
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    long x = scanner.nextLong(); 
    long y = scanner.nextLong();
    if(x<y){
      long d = Math.abs(x-y);
      if(x>d){
      	System.out.print(d);
       }
   	   else{
     	 System.out.print(x);}
    }
    else{
      long a = x%y;
      if(a==0){
        System.out.print(a);
      }
      else{
        long b = Math.abs(a-y);
        if(a>b){
      		System.out.print(b);
        }
        else{
          System.out.print(a);
        }
      }
    }
  }
} import java.util.Scanner; Scanner java.util import java.util.*; util.* java import java.lang.*; lang.* java class Main{
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    long x = scanner.nextLong(); 
    long y = scanner.nextLong();
    if(x<y){
      long d = Math.abs(x-y);
      if(x>d){
      	System.out.print(d);
       }
   	   else{
     	 System.out.print(x);}
    }
    else{
      long a = x%y;
      if(a==0){
        System.out.print(a);
      }
      else{
        long b = Math.abs(a-y);
        if(a>b){
      		System.out.print(b);
        }
        else{
          System.out.print(a);
        }
      }
    }
  }
} Main public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    long x = scanner.nextLong(); 
    long y = scanner.nextLong();
    if(x<y){
      long d = Math.abs(x-y);
      if(x>d){
      	System.out.print(d);
       }
   	   else{
     	 System.out.print(x);}
    }
    else{
      long a = x%y;
      if(a==0){
        System.out.print(a);
      }
      else{
        long b = Math.abs(a-y);
        if(a>b){
      		System.out.print(b);
        }
        else{
          System.out.print(a);
        }
      }
    }
  } main {
    Scanner scanner = new Scanner(System.in);
    long x = scanner.nextLong(); 
    long y = scanner.nextLong();
    if(x<y){
      long d = Math.abs(x-y);
      if(x>d){
      	System.out.print(d);
       }
   	   else{
     	 System.out.print(x);}
    }
    else{
      long a = x%y;
      if(a==0){
        System.out.print(a);
      }
      else{
        long b = Math.abs(a-y);
        if(a>b){
      		System.out.print(b);
        }
        else{
          System.out.print(a);
        }
      }
    }
  } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) long x = scanner.nextLong(); x scanner.nextLong() scanner.nextLong scanner long y = scanner.nextLong(); y scanner.nextLong() scanner.nextLong scanner if(x<y){
      long d = Math.abs(x-y);
      if(x>d){
      	System.out.print(d);
       }
   	   else{
     	 System.out.print(x);}
    }
    else{
      long a = x%y;
      if(a==0){
        System.out.print(a);
      }
      else{
        long b = Math.abs(a-y);
        if(a>b){
      		System.out.print(b);
        }
        else{
          System.out.print(a);
        }
      }
    } x<y x y {
      long d = Math.abs(x-y);
      if(x>d){
      	System.out.print(d);
       }
   	   else{
     	 System.out.print(x);}
    } long d = Math.abs(x-y); d Math.abs(x-y) Math.abs Math x-y x y if(x>d){
      	System.out.print(d);
       }
   	   else{
     	 System.out.print(x);} x>d x d {
      	System.out.print(d);
       } System.out.print(d) System.out.print System.out System System.out d {
     	 System.out.print(x);} System.out.print(x) System.out.print System.out System System.out x {
      long a = x%y;
      if(a==0){
        System.out.print(a);
      }
      else{
        long b = Math.abs(a-y);
        if(a>b){
      		System.out.print(b);
        }
        else{
          System.out.print(a);
        }
      }
    } long a = x%y; a x%y x y if(a==0){
        System.out.print(a);
      }
      else{
        long b = Math.abs(a-y);
        if(a>b){
      		System.out.print(b);
        }
        else{
          System.out.print(a);
        }
      } a==0 a 0 {
        System.out.print(a);
      } System.out.print(a) System.out.print System.out System System.out a {
        long b = Math.abs(a-y);
        if(a>b){
      		System.out.print(b);
        }
        else{
          System.out.print(a);
        }
      } long b = Math.abs(a-y); b Math.abs(a-y) Math.abs Math a-y a y if(a>b){
      		System.out.print(b);
        }
        else{
          System.out.print(a);
        } a>b a b {
      		System.out.print(b);
        } System.out.print(b) System.out.print System.out System System.out b {
          System.out.print(a);
        } System.out.print(a) System.out.print System.out System System.out a String[] args args
import java.util.Scanner;
import java.util.*;
import java.lang.*;

class Main{
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    long x = scanner.nextLong(); 
    long y = scanner.nextLong();
    if(x<y){
      long d = Math.abs(x-y);
      if(x>d){
      	System.out.print(d);
       }
   	   else{
     	 System.out.print(x);}
    }
    else{
      long a = x%y;
      if(a==0){
        System.out.print(a);
      }
      else{
        long b = Math.abs(a-y);
        if(a>b){
      		System.out.print(b);
        }
        else{
          System.out.print(a);
        }
      }
    }
  }
}