import java.util.*; public class Main { static int[] dy = {0,1,0,1}; static int[] dx = {1,0,1,0}; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int l = sc.nextInt(); int r = sc.nextInt(); int c = sc.nextInt(); int ans = 999; for(int i = l ; i <= r ; i++){ int x = ((i % 1000) * (c % 1000)) % 1000; if(x == 0){ ans = 0; break; } ans = Math.min(ans , 1000 - x); } System.out.println(ans); } }