#!/usr/bin/env python3 T = 12 def solve(n, m, p, q): per_cycle = (T - q) * m + q * 2 * m num_cycles, rest_leaves = divmod(n, per_cycle) per_months = [m] * (p - 1) + [2 * m] * q + [m] * (T - p - q + 1) for idx_month, per_month in enumerate(per_months): if rest_leaves <= 0: return num_cycles * T + idx_month else: rest_leaves -= per_month raise Exception("Error") def main(): n, m, p, q = (int(z) for z in input().split()) print(solve(n, m, p, q)) if __name__ == '__main__': main()