d = int(input()) H = [int(input()) for _ in range(3)] if d == 0: h1, h2, h3 = H if h1 != h2 and h2 != h3 and h1 != h3: if (h2 > h1 and h2 > h3) or (h2 < h1 and h2 < h3): print(0) else: print(-1) else: print(-1) exit() from itertools import product def get_candidates(Hi, d): if d == 0: return [(0, Hi)] candidates = [] kmax = Hi // d for k in range(0, 6): h = Hi - k * d if h < 0: h = 0 candidates.append((k, h)) for delta in [-2, -1, 0, 1, 2]: k = kmax + delta if k < 0: continue h = Hi - k * d if h < 0: h = 0 candidates.append((k, h)) k_zero = (Hi + d - 1) // d if Hi != 0 else 0 candidates.append((k_zero, 0)) h_mod = Hi % d candidates.append((kmax, h_mod)) unique = {} for k, h in candidates: if h not in unique or k < unique[h][0]: unique[h] = (k, h) return sorted(unique.values(), key=lambda x: x[0]) candidates1 = get_candidates(H[0], d) candidates2 = get_candidates(H[1], d) candidates3 = get_candidates(H[2], d) min_total = float('inf') for (k1, h1), (k2, h2), (k3, h3) in product(candidates1, candidates2, candidates3): if h1 == h2 or h2 == h3 or h1 == h3: continue if (h2 > h1 and h2 > h3) or (h2 < h1 and h2 < h3): total = k1 + k2 + k3 if total < min_total: min_total = total print(min_total if min_total != float('inf') else -1)