/* -*- coding: utf-8 -*- * * 281.cc: No.281 門松と魔法(1) - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int INF = 1 << 30; /* typedef */ /* global variables */ /* subroutines */ inline bool is_kadomatsu(int h0, int h1, int h2) { return (h0 != h2 && ((h0 < h1 && h1 > h2) || (h0 > h1 && h1 < h2))); } void finish(int ans) { printf("%d\n", ans); exit(0); } /* main */ int main() { int d, h0, h1, h2; cin >> d >> h0 >> h1 >> h2; if (d == 0) finish(is_kadomatsu(h0, h1, h2) ? 0 : -1); int cnt = 0; if (h0 == h2) { h0 = max(0, h0 - d); cnt++; if (h0 == h2) finish(-1); } if (is_kadomatsu(h0, h1, h2)) finish(cnt); // keep h0 < h2 if (h0 > h2) swap(h0, h2); // h0 < h1 > h2 int c0 = INF; if (h1 > 0) { c0 = cnt; int ch0 = h0, ch2 = h2; if (ch0 >= h1) { int e = (ch0 - h1) / d + 1; c0 += e; ch0 = max(ch0 - e * d, 0); } if (ch2 >= h1) { int e = (ch2 - h1) / d + 1; c0 += e; ch2 = max(ch2 - e * d, 0); } if (ch0 == ch2) { if (ch0 == 0) c0 = INF; else c0++; } } // h0 > h1 < h2 int c1 = INF; if (h0 > 0) { c1 = cnt; int ch1 = h1; if (h0 <= ch1) { int e = (ch1 - h0) / d + 1; c1 += e; ch1 = max(ch1 - e * d, 0); } } int c = min(c0, c1); finish((c >= INF) ? -1 : c); return 0; }