L, R, M = gets.split.map(&:to_i) l = L % M r = R % M limit = R - L + 1 if limit >= M puts M else puts [M, [limit, (M - l + 1) + r].min].min end