D = gets.to_i H = 3.times.map { gets.to_i } if D == 0 if H[0] < H[1] && H[1] > H[2] && H.size == H.uniq.size puts 0 elsif H[0] > H[1] && H[1] < H[2] && H.size == H.uniq.size puts 0 else puts -1 end exit end def f(h) cnt = 0 if h[0] == h[2] h[0] = [0, h[0] - D].max cnt += 1 end if h[0] == h[2] return Float::INFINITY end if h[0] <= h[1] diff = h[1] - h[0] + 1 c = Rational(diff, D).ceil cnt += c h[1] = [0, h[1] - c * D].max end if h[2] <= h[1] diff = h[2] - h[0] + 1 c = Rational(diff, D).ceil cnt += c h[2] = [0, h[2] - c * D].max end if h[0] == h[1] return Float::INFINITY end if h[1] == h[2] return Float::INFINITY end cnt end def g(h) cnt = 0 if h[0] >= h[1] diff = h[0] - h[1] + 1 c = Rational(diff, D).ceil cnt += c h[0] = [0, h[1] - c * D].max end if h[2] >= h[1] diff = h[2] - h[1] + 1 c = Rational(diff, D).ceil cnt += c h[2] = [0, h[2] - c * D].max end if h[0] == h[2] h[0] = [0, h[0] - D].max cnt += 1 end if h[0] == h[2] return Float::INFINITY end cnt end a1 = f(H.dup) a2 = g(H.dup) ans = [a1, a2].min if ans == Float::INFINITY puts -1 else puts ans end