import heapq def next_to(mu, mi, h, d): if mu < h: t = mu elif mi < h: t = mi else: return 1 return (h - t) // d + 1 def solve(): d = int(input()) h1 = int(input()) h2 = int(input()) h3 = int(input()) if d == 0: if h1 != h2 and h1 != h3 and h2 != h3: print(0) else: print(-1) return q = [(0, (h1, h2, h3))] while q: n, h = heapq.heappop(q) zs = h.count(0) if zs >= 2: continue ma = max(h) mi = min(h) mu = sum(h) - ma - mi mic = h.count(mi) if h[0] != h[1] and h[0] != h[2] and h[1] != h[2] and h[1] != mu: print(n) return if h[0] != mi or mic != 1: m = next_to(mu, mi, h[0], d) heapq.heappush(q, (n + m, (max(0, h[0] - d * m), h[1], h[2]))) if (h[1] != mi or mic != 1) and h[1] != ma: m = next_to(mu, mi, h[1], d) heapq.heappush(q, (n + m, (h[0], max(0, h[1] - d * m), h[2]))) if h[2] != mi or mic != 1: m = next_to(mu, mi, h[2], d) heapq.heappush(q, (n + m, (h[0], h[1], max(0, h[2] - d * m)))) print(-1) if __name__ == '__main__': solve()