import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int l = Integer.parseInt(sa[0]); int r = Integer.parseInt(sa[1]); int c = Integer.parseInt(sa[2]); br.close(); boolean[] b = new boolean[1000]; for (int i = l; i <= r; i++) { int a = (int) ((long) c * i % 1000); if (b[a]) { break; } b[a] = true; } if (b[0]) { System.out.println(0); return; } for (int i = 999; i > 0; i--) { if (b[i]) { System.out.println(1000 - i); return; } } } }