d = int(input()) h1 = int(input()) h2 = int(input()) h3 = int(input()) if d == 0: a, b, c = h1, h2, h3 if a != b and b != c and a != c and ((b > a and b > c) or (b < a and b < c)): print(0) else: print(-1) exit() def generate_candidates(h, d_val): if d_val == 0: return [(h, 0)] candidates = [] seen = set() max_k = (h + d_val - 1) // d_val # Ceiling division for k in [0, 1, 2, max_k]: new_h = max(0, h - k * d_val) if new_h not in seen: seen.add(new_h) candidates.append((new_h, k)) return candidates c1 = generate_candidates(h1, d) c2 = generate_candidates(h2, d) c3 = generate_candidates(h3, d) min_operations = float('inf') for (a, ka) in c1: for (b, kb) in c2: for (c, kc) in c3: if a == b or b == c or a == c: continue if (b > a and b > c) or (b < a and b < c): total = ka + kb + kc if total < min_operations: min_operations = total print(min_operations if min_operations != float('inf') else -1)