import java.util.Scanner; public class Main { static long MOD = 1000000000; public static void main(String[] args) { Scanner sc = new Scanner(System.in); long N = sc.nextLong(); int M = sc.nextInt(); long val = (N - N%1000)/1000; val %= M; if (val > M/2) { val = M - val; } long ans = 1; for (int i=0; i