import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long remainder = n % k;
        long smallest = remainder;
        while (Math.abs(k - remainder) < smallest) {
        	smallest = Math.abs(k - remainder);
        }
        System.out.println(smallest);
        sc.close();
    }
} import java.util.Scanner; Scanner 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();
        
        long remainder = n % k;
        long smallest = remainder;
        while (Math.abs(k - remainder) < smallest) {
        	smallest = Math.abs(k - remainder);
        }
        System.out.println(smallest);
        sc.close();
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long remainder = n % k;
        long smallest = remainder;
        while (Math.abs(k - remainder) < smallest) {
        	smallest = Math.abs(k - remainder);
        }
        System.out.println(smallest);
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long remainder = n % k;
        long smallest = remainder;
        while (Math.abs(k - remainder) < smallest) {
        	smallest = Math.abs(k - remainder);
        }
        System.out.println(smallest);
        sc.close();
    } 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 long remainder = n % k; remainder n % k n k long smallest = remainder; smallest remainder while (Math.abs(k - remainder) < smallest) {
        	smallest = Math.abs(k - remainder);
        } Math.abs(k - remainder) < smallest Math.abs(k - remainder) Math.abs Math k - remainder k remainder smallest {
        	smallest = Math.abs(k - remainder);
        } smallest = Math.abs(k - remainder) smallest Math.abs(k - remainder) Math.abs Math k - remainder k remainder System.out.println(smallest) System.out.println System.out System System.out smallest sc.close() sc.close sc 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();
        
        long remainder = n % k;
        long smallest = remainder;
        while (Math.abs(k - remainder) < smallest) {
        	smallest = Math.abs(k - remainder);
        }
        System.out.println(smallest);
        sc.close();
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long remainder = n % k;
        long smallest = remainder;
        while (Math.abs(k - remainder) < smallest) {
        	smallest = Math.abs(k - remainder);
        }
        System.out.println(smallest);
        sc.close();
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        
        long remainder = n % k;
        long smallest = remainder;
        while (Math.abs(k - remainder) < smallest) {
        	smallest = Math.abs(k - remainder);
        }
        System.out.println(smallest);
        sc.close();
    }
}