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 res = N % K;
        System.out.println(Math.min(res, K - res));     }
} 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 res = N % K;
        System.out.println(Math.min(res, K - res));     }
} Main public static void main(String...args){
        Scanner sc = new Scanner(System.in); long N = sc.nextLong(); long K = sc.nextLong(); long res = N % K;
        System.out.println(Math.min(res, K - res));     } main {
        Scanner sc = new Scanner(System.in); long N = sc.nextLong(); long K = sc.nextLong(); long res = N % K;
        System.out.println(Math.min(res, K - res));     } 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 res = N % K; res N % K N K System.out.println(Math.min(res, K - res)) System.out.println System.out System System.out Math.min(res, K - res) Math.min Math res K - res K res 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 res = N % K;
        System.out.println(Math.min(res, K - res));     }
} public class Main{ public static void main(String...args){
        Scanner sc = new Scanner(System.in); long N = sc.nextLong(); long K = sc.nextLong(); long res = N % K;
        System.out.println(Math.min(res, K - res));     }
} 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 res = N % K;
        System.out.println(Math.min(res, K - res));     }
}