L, R, C = gets.split.map(&:to_i) memo = Hash.new(false) x_list = (L..[L + 999, R].min) puts x_list.map { |x| (x * C) % 1000 == 0 ? 0 : 1000 - (x * C % 1000) }.min