import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        
        Scanner sc = new Scanner(System.in);
        String data = sc.nextLine();
        
        String[] array = data.split(" ");
        
        long n = Long.parseLong(array[0]);
        long k = Long.parseLong(array[1]);
        
        long min = n % k;
        long buf = 0;
        while(true)
        {
            buf = Math.abs(min - k);
            
            if(min < buf)
            {
                break;
            }
            else
            {
                min = buf;
            }
        }
        
        System.out.println(min);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        
        Scanner sc = new Scanner(System.in);
        String data = sc.nextLine();
        
        String[] array = data.split(" ");
        
        long n = Long.parseLong(array[0]);
        long k = Long.parseLong(array[1]);
        
        long min = n % k;
        long buf = 0;
        while(true)
        {
            buf = Math.abs(min - k);
            
            if(min < buf)
            {
                break;
            }
            else
            {
                min = buf;
            }
        }
        
        System.out.println(min);
    }
} Main public static void main(String[] args) throws Exception {
        
        Scanner sc = new Scanner(System.in);
        String data = sc.nextLine();
        
        String[] array = data.split(" ");
        
        long n = Long.parseLong(array[0]);
        long k = Long.parseLong(array[1]);
        
        long min = n % k;
        long buf = 0;
        while(true)
        {
            buf = Math.abs(min - k);
            
            if(min < buf)
            {
                break;
            }
            else
            {
                min = buf;
            }
        }
        
        System.out.println(min);
    } main {
        
        Scanner sc = new Scanner(System.in);
        String data = sc.nextLine();
        
        String[] array = data.split(" ");
        
        long n = Long.parseLong(array[0]);
        long k = Long.parseLong(array[1]);
        
        long min = n % k;
        long buf = 0;
        while(true)
        {
            buf = Math.abs(min - k);
            
            if(min < buf)
            {
                break;
            }
            else
            {
                min = buf;
            }
        }
        
        System.out.println(min);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) String data = sc.nextLine(); data sc.nextLine() sc.nextLine sc String[] array = data.split(" "); array data.split(" ") data.split data " " long n = Long.parseLong(array[0]); n Long.parseLong(array[0]) Long.parseLong Long array[0] array 0 long k = Long.parseLong(array[1]); k Long.parseLong(array[1]) Long.parseLong Long array[1] array 1 long min = n % k; min n % k n k long buf = 0; buf 0 while(true)
        {
            buf = Math.abs(min - k);
            
            if(min < buf)
            {
                break;
            }
            else
            {
                min = buf;
            }
        } true {
            buf = Math.abs(min - k);
            
            if(min < buf)
            {
                break;
            }
            else
            {
                min = buf;
            }
        } buf = Math.abs(min - k) buf Math.abs(min - k) Math.abs Math min - k min k if(min < buf)
            {
                break;
            }
            else
            {
                min = buf;
            } min < buf min buf {
                break;
            } break; {
                min = buf;
            } min = buf min buf System.out.println(min) System.out.println System.out System System.out min String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        
        Scanner sc = new Scanner(System.in);
        String data = sc.nextLine();
        
        String[] array = data.split(" ");
        
        long n = Long.parseLong(array[0]);
        long k = Long.parseLong(array[1]);
        
        long min = n % k;
        long buf = 0;
        while(true)
        {
            buf = Math.abs(min - k);
            
            if(min < buf)
            {
                break;
            }
            else
            {
                min = buf;
            }
        }
        
        System.out.println(min);
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        
        Scanner sc = new Scanner(System.in);
        String data = sc.nextLine();
        
        String[] array = data.split(" ");
        
        long n = Long.parseLong(array[0]);
        long k = Long.parseLong(array[1]);
        
        long min = n % k;
        long buf = 0;
        while(true)
        {
            buf = Math.abs(min - k);
            
            if(min < buf)
            {
                break;
            }
            else
            {
                min = buf;
            }
        }
        
        System.out.println(min);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        
        Scanner sc = new Scanner(System.in);
        String data = sc.nextLine();
        
        String[] array = data.split(" ");
        
        long n = Long.parseLong(array[0]);
        long k = Long.parseLong(array[1]);
        
        long min = n % k;
        long buf = 0;
        while(true)
        {
            buf = Math.abs(min - k);
            
            if(min < buf)
            {
                break;
            }
            else
            {
                min = buf;
            }
        }
        
        System.out.println(min);
    }
}
